diff --git a/src/defs.h b/src/defs.h index 4160a694..c8b56667 100644 --- a/src/defs.h +++ b/src/defs.h @@ -41,7 +41,8 @@ #define BASE64_RATIO 1.33333333 -#define DIGEST_LENGTH SHA256_DIGEST_LENGTH +#define DIGEST_LENGTH EVP_MAX_MD_SIZE +#define DIGEST_HEX_LENGTH 2*DIGEST_LENGTH+1 #define UNDEF 0 #define READY 1 diff --git a/src/digest.c b/src/digest.c index 50d98cd5..46a8e070 100644 --- a/src/digest.c +++ b/src/digest.c @@ -34,16 +34,30 @@ int search_header_end(char *p, int n){ int make_digests(struct session_data *sdata, struct config *cfg){ - int i=0, n, fd, offset=3, hdr_len=0; + int n, fd, offset=3, hdr_len=0; char *body=NULL; - unsigned char buf[BIGBUFSIZE], md[DIGEST_LENGTH], md2[DIGEST_LENGTH]; - SHA256_CTX context, context2; + unsigned char buf[BIGBUFSIZE]; + + EVP_MD_CTX *ctx, *ctx2; + const EVP_MD *md, *md2; + unsigned int i=0, md_len, md_len2; + unsigned char md_value[EVP_MAX_MD_SIZE], md_value2[EVP_MAX_MD_SIZE]; memset(sdata->bodydigest, 0, 2*DIGEST_LENGTH+1); memset(sdata->digest, 0, 2*DIGEST_LENGTH+1); - SHA256_Init(&context); - SHA256_Init(&context2); + md = EVP_get_digestbyname("sha256"); + md2 = EVP_get_digestbyname("sha256"); + if(md == NULL || md2 == NULL){ + syslog(LOG_PRIORITY, "ERROR: unknown message digest: sha256 in %s:%d", __func__, __LINE__); + return 1; + } + + ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, md, NULL); + + ctx2 = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx2, md2, NULL); fd = open(sdata->filename, O_RDONLY); if(fd == -1) return -1; @@ -51,7 +65,7 @@ int make_digests(struct session_data *sdata, struct config *cfg){ memset(buf, 0, sizeof(buf)); while((n = read(fd, buf, sizeof(buf))) > 0){ - SHA256_Update(&context2, buf, n); + EVP_DigestUpdate(ctx2, buf, n); body = (char *)&buf[0]; @@ -68,7 +82,7 @@ int make_digests(struct session_data *sdata, struct config *cfg){ } - SHA256_Update(&context, body, n); + EVP_DigestUpdate(ctx, body, n); i++; } @@ -77,82 +91,93 @@ int make_digests(struct session_data *sdata, struct config *cfg){ sdata->hdr_len = hdr_len; - SHA256_Final(md, &context); - SHA256_Final(md2, &context2); + EVP_DigestFinal_ex(ctx, md_value, &md_len); + EVP_MD_CTX_free(ctx); + EVP_DigestFinal_ex(ctx2, md_value2, &md_len2); + EVP_MD_CTX_free(ctx2); - for(i=0;ibodydigest + i*2, 3, "%02x", md[i]); - snprintf(sdata->digest + i*2, 3, "%02x", md2[i]); + for(i=0;ibodydigest + i*2, 3, "%02x", md_value[i]); + } + + for(i=0;idigest + i*2, 3, "%02x", md_value2[i]); } return 0; } -void digest_file(char *filename, char *digest){ - int fd, i, n; - unsigned char buf[MAXBUFSIZE], md[DIGEST_LENGTH]; - SHA256_CTX context; +void raw_digest_file(char *digestname, char *filename, unsigned char *md_value){ + int fd, n; + unsigned char buf[MAXBUFSIZE]; + EVP_MD_CTX *ctx; + const EVP_MD *md; + unsigned int md_len; - memset(digest, 0, 2*DIGEST_LENGTH+1); + md = EVP_get_digestbyname(digestname); + if(md == NULL){ + syslog(LOG_PRIORITY, "ERROR: unknown message digest: '%s' in %s:%d", digestname, __func__, __LINE__); + return; + } fd = open(filename, O_RDONLY); if(fd == -1) return; - SHA256_Init(&context); + ctx = EVP_MD_CTX_new(); + EVP_DigestInit_ex(ctx, md, NULL); while((n = read(fd, buf, sizeof(buf))) > 0){ - SHA256_Update(&context, buf, n); + EVP_DigestUpdate(ctx, buf, n); } close(fd); - SHA256_Final(md, &context); - - for(i=0;imessage_id, sdata->now, sdata->sent, sdata->retained, sdata->tot_len, sdata->hdr_len, sdata->direction, state->n_attachments, sdata->ttmpfile, sdata->digest, sdata->bodydigest); - digest_string(s, &vcode[0]); + digest_string("sha256", s, &vcode[0]); memset(ref, 0, sizeof(ref)); if(strlen(state->reference) > 10){ - digest_string(state->reference, &ref[0]); + digest_string("sha256", state->reference, &ref[0]); update_metadata_reference(sdata, state, &ref[0], cfg); } else if(state->reference[0] == 0){ @@ -218,20 +218,20 @@ int store_meta_data(struct session_data *sdata, struct parser_state *state, stru // check if this is a message which is already referenced uint64 count=0; - digest_string(state->message_id, &ref[0]); + digest_string("sha256", state->message_id, &ref[0]); if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_METADATA_REFERENCE) != ERR){ p_bind_init(&sql); sql.sql[sql.pos] = &ref[0]; sql.type[sql.pos] = TYPE_STRING; sql.pos++; if(p_exec_stmt(sdata, &sql) == OK){ - p_bind_init(&sql); + p_bind_init(&sql); - sql.sql[sql.pos] = (char *)&count; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; - p_store_results(&sql); - p_fetch_results(&sql); - p_free_results(&sql); - } + sql.sql[sql.pos] = (char *)&count; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++; + p_store_results(&sql); + p_fetch_results(&sql); + p_free_results(&sql); + } } close_prepared_statement(&sql); diff --git a/src/parser.c b/src/parser.c index 78ab2543..cd9d16d9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -169,7 +169,7 @@ void post_parse(struct session_data *sdata, struct parser_state *state, struct c } - digest_string(state->message_id, &(state->message_id_hash[0])); + digest_string("sha256", state->message_id, &(state->message_id_hash[0])); if(sdata->sent == 0) sdata->sent = sdata->now; } diff --git a/src/piler.h b/src/piler.h index e4d7395e..b9207d10 100644 --- a/src/piler.h +++ b/src/piler.h @@ -37,7 +37,7 @@ int do_av_check(char *filename, struct config *cfg); int make_digests(struct session_data *sdata, struct config *cfg); void digest_file(char *filename, char *digest); -void digest_string(char *s, char *digest); +void digest_string(char *digestname, char *s, char *digest); void create_md5_from_email_address(char *puf, char *md5buf); void remove_stripped_attachments(struct parser_state *state); diff --git a/unit_tests/check_digest.c b/unit_tests/check_digest.c index 9d5bf37d..2c25bf6b 100644 --- a/unit_tests/check_digest.c +++ b/unit_tests/check_digest.c @@ -29,7 +29,7 @@ static void test_digest_string(){ }; for(i=0; i