/*** * @method cryptobox_hash:base64() * Finalizes hash and return it as base64 string * @return {string} base64 value of hash */ static gint lua_cryptobox_hash_base64 (lua_State *L) { struct rspamd_lua_cryptobox_hash *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES], *b64; gsize len; guint dlen; if (h && !h->is_finished) { if (h->is_ssl) { dlen = sizeof (out); EVP_DigestFinal_ex (h->c, out, &dlen); } else { dlen = sizeof (out); rspamd_cryptobox_hash_final (h->h, out); } b64 = rspamd_encode_base64 (out, dlen, 0, &len); lua_pushlstring (L, b64, len); g_free (b64); h->is_finished = TRUE; } else { return luaL_error (L, "invalid arguments"); } return 1; }
static gint lua_util_encode_base64 (lua_State *L) { struct rspamd_lua_text *t; const gchar *s = NULL; gchar *out; gsize inlen, outlen; guint str_lim = 0; if (lua_type (L, 1) == LUA_TSTRING) { s = luaL_checklstring (L, 1, &inlen); } else if (lua_type (L, 1) == LUA_TUSERDATA) { t = lua_check_text (L, 1); if (t != NULL) { s = t->start; inlen = t->len; } } if (lua_gettop (L) > 1) { str_lim = luaL_checknumber (L, 2); } if (s == NULL) { lua_pushnil (L); } else { out = rspamd_encode_base64 (s, inlen, str_lim, &outlen); if (out != NULL) { t = lua_newuserdata (L, sizeof (*t)); rspamd_lua_setclass (L, "rspamd{text}", -1); t->start = out; t->len = outlen; /* Need destruction */ t->own = TRUE; } else { lua_pushnil (L); } } return 1; }
/*** * @method cryptobox_hash:base64() * Finalizes hash and return it as base64 string * @return {string} base64 value of hash */ static gint lua_cryptobox_hash_base64 (lua_State *L) { rspamd_cryptobox_hash_state_t *h = lua_check_cryptobox_hash (L, 1); guchar out[rspamd_cryptobox_HASHBYTES], *b64; gsize len; if (h) { rspamd_cryptobox_hash_final (h, out); b64 = rspamd_encode_base64 (out, sizeof (out), 0, &len); lua_pushlstring (L, b64, len); g_free (b64); } else { return luaL_error (L, "invalid arguments"); } return 1; }
static void rspamd_process_file (const gchar *fname, gint decode) { gint fd; gpointer map; struct stat st; guint8 *dest; gsize destlen; fd = open (fname, O_RDONLY); if (fd == -1) { rspamd_fprintf (stderr, "cannot open %s: %s", fname, strerror (errno)); exit (EXIT_FAILURE); } if (fstat (fd, &st) == -1) { rspamd_fprintf (stderr, "cannot stat %s: %s", fname, strerror (errno)); exit (EXIT_FAILURE); } map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); close (fd); if (map == MAP_FAILED) { rspamd_fprintf (stderr, "cannot mmap %s: %s", fname, strerror (errno)); exit (EXIT_FAILURE); } if (decode) { destlen = st.st_size / 4 * 3 + 10; dest = g_malloc (destlen); rspamd_cryptobox_base64_decode (map, st.st_size, dest, &destlen); } else { dest = rspamd_encode_base64 (map, st.st_size, 80, &destlen); } rspamd_printf ("%*s", (gint)destlen, dest); g_free (dest); munmap (map, st.st_size); }
static gchar * rspamd_log_encrypt_message (const gchar *begin, const gchar *end, rspamd_logger_t *rspamd_log) { guchar *out; gchar *b64; guchar *p, *nonce, *mac; const guchar *comp; guint len, inlen; g_assert (end > begin); /* base64 (pubkey | nonce | message) */ inlen = rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + rspamd_cryptobox_pk_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + (end - begin); out = g_malloc (inlen); p = out; comp = rspamd_pubkey_get_pk (rspamd_log->pk, &len); memcpy (p, comp, len); p += len; ottery_rand_bytes (p, rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); nonce = p; p += rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519); mac = p; p += rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519); memcpy (p, begin, end - begin); comp = rspamd_pubkey_get_nm (rspamd_log->pk); g_assert (comp != NULL); rspamd_cryptobox_encrypt_nm_inplace (p, end - begin, nonce, comp, mac, RSPAMD_CRYPTOBOX_MODE_25519); b64 = rspamd_encode_base64 (out, inlen, 0, NULL); g_free (out); return b64; }