aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-08-13 11:22:44 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-08-13 11:23:12 +0100
commitebe5dfa9bbd8f2a6241fefe3daa99eb654da6336 (patch)
treedc0b24a6039341272671fc5ee53ca00f88cba802 /src
parenta7b2ee81dfc78d0f9b17e93a3ed9ce9fbe220eba (diff)
downloadrspamd-ebe5dfa9bbd8f2a6241fefe3daa99eb654da6336.tar.gz
rspamd-ebe5dfa9bbd8f2a6241fefe3daa99eb654da6336.zip
[Feature] Pre-calculate blake2 digest for all parts
Diffstat (limited to 'src')
-rw-r--r--src/libmime/images.c1
-rw-r--r--src/libmime/images.h8
-rw-r--r--src/libmime/message.c22
-rw-r--r--src/libmime/message.h5
-rw-r--r--src/plugins/fuzzy_check.c13
5 files changed, 35 insertions, 14 deletions
diff --git a/src/libmime/images.c b/src/libmime/images.c
index 5a556ce9c..f2c27f865 100644
--- a/src/libmime/images.c
+++ b/src/libmime/images.c
@@ -226,6 +226,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
img->width, img->height,
task->message_id);
img->filename = part->filename;
+ img->parent = part;
part->flags |= RSPAMD_MIME_PART_IMAGE;
part->specific_data = img;
diff --git a/src/libmime/images.h b/src/libmime/images.h
index aa63d92e2..73e00aebd 100644
--- a/src/libmime/images.h
+++ b/src/libmime/images.h
@@ -5,6 +5,7 @@
struct html_image;
struct rspamd_task;
+struct rspamd_mime_part;
enum rspamd_image_type {
IMAGE_TYPE_PNG = 0,
@@ -15,12 +16,13 @@ enum rspamd_image_type {
};
struct rspamd_image {
- enum rspamd_image_type type;
+ struct rspamd_mime_part *parent;
GByteArray *data;
- guint32 width;
- guint32 height;
const gchar *filename;
struct html_image *html_image;
+ enum rspamd_image_type type;
+ guint32 width;
+ guint32 height;
};
/*
diff --git a/src/libmime/message.c b/src/libmime/message.c
index a3b673f28..f97de592f 100644
--- a/src/libmime/message.c
+++ b/src/libmime/message.c
@@ -1061,6 +1061,17 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
GMimeStream *part_stream;
GByteArray *part_content;
gchar *hdrs;
+ /* Blake2b applied to string 'rspamd' */
+ static const guchar hash_key[] = {
+ 0x78,0x6a,0x02,0xf7,0x42,0x01,0x59,0x03,
+ 0xc6,0xc6,0xfd,0x85,0x25,0x52,0xd2,0x72,
+ 0x91,0x2f,0x47,0x40,0xe1,0x58,0x47,0x61,
+ 0x8a,0x86,0xe2,0x17,0xf7,0x1f,0x54,0x19,
+ 0xd2,0x5e,0x10,0x31,0xaf,0xee,0x58,0x53,
+ 0x13,0x89,0x64,0x44,0x93,0x4e,0xb0,0x4b,
+ 0x90,0x3a,0x68,0x5b,0x14,0x48,0xb7,0x55,
+ 0xd5,0x6f,0x70,0x1a,0xfe,0x9b,0xe2,0xce
+ };
task = md->task;
/* 'part' points to the current part node that g_mime_message_foreach_part() is iterating over */
@@ -1139,6 +1150,11 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
mime_part->mime = part;
mime_part->boundary = g_mime_multipart_get_boundary (GMIME_MULTIPART (part));
+ if (mime_part->boundary) {
+ rspamd_cryptobox_hash (mime_part->digest, mime_part->boundary,
+ strlen (mime_part->boundary), hash_key, sizeof (hash_key));
+ }
+
debug_task ("found part with content-type: %s/%s",
type->type,
type->subtype);
@@ -1203,6 +1219,12 @@ mime_foreach_callback (GMimeObject * part, gpointer user_data)
part));
mime_part->mime = part;
+ if (mime_part->content->len > 0) {
+ rspamd_cryptobox_hash (mime_part->digest,
+ mime_part->content->data, mime_part->content->len,
+ hash_key, sizeof (hash_key));
+ }
+
debug_task ("found part with content-type: %s/%s",
type->type,
type->subtype);
diff --git a/src/libmime/message.h b/src/libmime/message.h
index 3fe26e685..b6f7062dc 100644
--- a/src/libmime/message.h
+++ b/src/libmime/message.h
@@ -9,6 +9,7 @@
#include "config.h"
#include "email_addr.h"
#include "addr.h"
+#include "cryptobox.h"
#include <gmime/gmime.h>
struct rspamd_task;
@@ -29,11 +30,11 @@ struct rspamd_mime_part {
GMimeObject *mime;
GHashTable *raw_headers;
gchar *raw_headers_str;
- gchar *checksum;
+ guchar digest[rspamd_cryptobox_HASHBYTES];
const gchar *filename;
const gchar *boundary;
- enum rspamd_mime_part_flags flags;
gpointer specific_data;
+ enum rspamd_mime_part_flags flags;
};
#define RSPAMD_MIME_TEXT_PART_FLAG_UTF (1 << 0)
diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c
index 82800e714..429360e1c 100644
--- a/src/plugins/fuzzy_check.c
+++ b/src/plugins/fuzzy_check.c
@@ -1288,13 +1288,11 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule,
gint flag,
guint32 weight,
rspamd_mempool_t *pool,
- const guchar *data,
- gsize datalen)
+ guchar digest[rspamd_cryptobox_HASHBYTES])
{
struct rspamd_fuzzy_cmd *cmd;
struct rspamd_fuzzy_encrypted_cmd *enccmd = NULL;
struct fuzzy_cmd_io *io;
- rspamd_cryptobox_hash_state_t st;
if (rule->peer_key) {
enccmd = rspamd_mempool_alloc0 (pool, sizeof (*enccmd));
@@ -1312,10 +1310,7 @@ fuzzy_cmd_from_data_part (struct fuzzy_rule *rule,
}
cmd->shingles_count = 0;
cmd->tag = ottery_rand_uint32 ();
- /* Use blake2b for digest */
- rspamd_cryptobox_hash_init (&st, rule->hash_key->str, rule->hash_key->len);
- rspamd_cryptobox_hash_update (&st, data, datalen);
- rspamd_cryptobox_hash_final (&st, cmd->digest);
+ memcpy (cmd->digest, digest, sizeof (cmd->digest));
io = rspamd_mempool_alloc (pool, sizeof (*io));
io->flags = 0;
@@ -2059,7 +2054,7 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule,
fuzzy_module_ctx->min_width) {
io = fuzzy_cmd_from_data_part (rule, c, flag, value,
task->task_pool,
- image->data->data, image->data->len);
+ image->parent->digest);
if (io) {
g_ptr_array_add (res, io);
}
@@ -2074,7 +2069,7 @@ fuzzy_generate_commands (struct rspamd_task *task, struct fuzzy_rule *rule,
fuzzy_module_ctx->min_bytes) {
io = fuzzy_cmd_from_data_part (rule, c, flag, value,
task->task_pool,
- mime_part->content->data, mime_part->content->len);
+ mime_part->digest);
if (io) {
g_ptr_array_add (res, io);
}