]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util/mime: use Rust md5 bindings instead of libnss
authorJason Ish <jason.ish@oisf.net>
Wed, 23 Dec 2020 22:35:07 +0000 (16:35 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 13 Jan 2021 08:01:04 +0000 (09:01 +0100)
As the new Md5 hashing consumes its context on finalize, an bool
has_md5 flag has been added to let the logger know there is an
md5 hash available.

src/output-json-email-common.c
src/util-decode-mime.c
src/util-decode-mime.h

index 5bdb495e3fafe47e3c00f7d98d5eb857bc3fe934..63abd447202ba298d6261ab5df5e57580218aec6 100644 (file)
 #include "output-json.h"
 #include "output-json-email-common.h"
 
+#ifdef HAVE_NSS
+#include <sechash.h>
+#endif
+
 #define LOG_EMAIL_DEFAULT       0
 #define LOG_EMAIL_EXTENDED      (1<<0)
 #define LOG_EMAIL_ARRAY         (1<<1) /* require array handling */
@@ -149,7 +153,7 @@ static void EveEmailLogJSONMd5(OutputJsonEmailCtx *email_ctx, JsonBuilder *js, S
 
     if (email_ctx->flags & LOG_EMAIL_BODY_MD5) {
         MimeDecParseState *mime_state = tx->mime_state;
-        if (mime_state && mime_state->md5_ctx && (mime_state->state_flag == PARSE_DONE)) {
+        if (mime_state && mime_state->has_md5 && (mime_state->state_flag == PARSE_DONE)) {
             size_t x;
             int i;
             char s[256];
index cb6ae8d489e056979edbebf24d570dfe8591a239..1bbe0e284fe0c54a90cc05c87d7b0ab9d606bce3 100644 (file)
@@ -32,6 +32,8 @@
 #include "util-memcmp.h"
 #include "util-print.h"
 
+#include "rust.h"
+
 /* Character constants */
 #ifndef CR
 #define CR  13
@@ -2094,13 +2096,6 @@ static int ProcessBodyComplete(MimeDecParseState *state)
         }
     }
 
-#ifdef HAVE_NSS
-    if (state->md5_ctx) {
-        unsigned int len = 0;
-        HASH_End(state->md5_ctx, state->md5, &len, sizeof(state->md5));
-    }
-#endif
-
     /* Invoke pre-processor and callback with remaining data */
     ret = ProcessDecodedDataChunk(state->data_chunk, state->data_chunk_len, state);
     if (ret != MIME_DEC_OK) {
@@ -2269,17 +2264,16 @@ static int ProcessMimeBody(const uint8_t *buf, uint32_t len,
     int body_found = 0;
     uint32_t tlen;
 
-#ifdef HAVE_NSS
-    if (MimeDecGetConfig()->body_md5) {
-        if (state->body_begin == 1) {
-            if (state->md5_ctx == NULL) {
-                state->md5_ctx = HASH_Create(HASH_AlgMD5);
-                HASH_Begin(state->md5_ctx);
+    if (!g_disable_hashing) {
+        if (MimeDecGetConfig()->body_md5) {
+            if (state->body_begin == 1) {
+                if (state->md5_ctx == NULL) {
+                    state->md5_ctx = SCMd5New();
+                }
             }
+            SCMd5Update(state->md5_ctx, buf, len + state->current_line_delimiter_len);
         }
-        HASH_Update(state->md5_ctx, buf, len + state->current_line_delimiter_len);
     }
-#endif
 
     /* Ignore empty lines */
     if (len == 0) {
@@ -2505,10 +2499,8 @@ void MimeDecDeInitParser(MimeDecParseState *state)
     SCFree(state->hname);
     FreeDataValue(state->hvalue);
     FreeMimeDecStack(state->stack);
-#ifdef HAVE_NSS
     if (state->md5_ctx)
-        HASH_Destroy(state->md5_ctx);
-#endif
+        SCMd5Free(state->md5_ctx);
     SCFree(state);
 }
 
@@ -2547,6 +2539,12 @@ int MimeDecParseComplete(MimeDecParseState *state)
         return ret;
     }
 
+    if (state->md5_ctx) {
+        SCMd5Finalize(state->md5_ctx, state->md5, sizeof(state->md5));
+        state->md5_ctx = NULL;
+        state->has_md5 = true;
+    }
+
     if (state->stack->top == NULL) {
         state->msg->anomaly_flags |= ANOM_MALFORMED_MSG;
         SCLogDebug("Error: Message is malformed");
index c070748f5e41b57350a63d2998c499f23d9c819b..0baed68bf1453b231340ae7f80cb54b7f06ddd1d 100644 (file)
 #ifndef MIME_DECODE_H_
 #define MIME_DECODE_H_
 
-#ifdef HAVE_NSS
-#include <sechash.h>
-#endif
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -201,10 +197,9 @@ typedef struct MimeDecParseState {
     uint8_t bvremain[B64_BLOCK];  /**< Remainder from base64-decoded line */
     uint8_t bvr_len;  /**< Length of remainder from base64-decoded line */
     uint8_t data_chunk[DATA_CHUNK_SIZE];  /**< Buffer holding data chunk */
-#ifdef HAVE_NSS
-    HASHContext *md5_ctx;
-    uint8_t md5[MD5_LENGTH];
-#endif
+    SCMd5 *md5_ctx;
+    uint8_t md5[SC_MD5_LEN];
+    bool has_md5;
     uint8_t state_flag;  /**<  Flag representing current state of parser */
     uint32_t data_chunk_len;  /**< Length of data chunk */
     int found_child;  /**< Flag indicating a child entity was found */