]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
base64: use the Rust base64 encode implementation
authorJason Ish <jason.ish@oisf.net>
Fri, 3 Sep 2021 21:04:58 +0000 (15:04 -0600)
committerVictor Julien <vjulien@oisf.net>
Tue, 11 Jan 2022 06:30:48 +0000 (07:30 +0100)
Replace our internal base64 implementation with a ffi wrapper
around the Rust implementation provided by an external crate.

25 files changed:
rust/src/ffi/base64.rs [new file with mode: 0644]
rust/src/ffi/mod.rs
scripts/dnp3-gen/dnp3-gen.py
src/Makefile.am
src/app-layer-ssl.c
src/datasets-md5.c
src/datasets-sha256.c
src/datasets-string.c
src/datasets.c
src/log-tlslog.c
src/log-tlsstore.c
src/output-json-alert.c
src/output-json-anomaly.c
src/output-json-dnp3-objects.c
src/output-json-dnp3.c
src/output-json-email-common.c
src/output-json-http.c
src/output-json-http2.c
src/output-json-metadata.c
src/output-json-ssh.c
src/output-json-stats.c
src/output-json-tls.c
src/output-json.c
src/util-crypt.c [deleted file]
src/util-crypt.h [deleted file]

diff --git a/rust/src/ffi/base64.rs b/rust/src/ffi/base64.rs
new file mode 100644 (file)
index 0000000..0019a6f
--- /dev/null
@@ -0,0 +1,62 @@
+/* Copyright (C) 2021 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+use std::os::raw::c_uchar;
+use libc::c_ulong;
+
+#[repr(C)]
+#[allow(non_camel_case_types)]
+pub enum Base64ReturnCode {
+    SC_BASE64_OK = 0,
+    SC_BASE64_INVALID_ARG,
+    SC_BASE64_OVERFLOW,
+}
+
+/// Base64 encode a buffer.
+///
+/// This method exposes the Rust base64 encoder to C and should not be called from
+/// Rust code.
+///
+/// The output parameter must be an allocated buffer of at least the size returned
+/// from Base64EncodeBufferSize for the input_len, and this length must be provided
+/// in the output_len variable.
+#[no_mangle]
+pub unsafe extern "C" fn Base64Encode(
+    input: *const u8, input_len: c_ulong, output: *mut c_uchar, output_len: *mut c_ulong,
+) -> Base64ReturnCode {
+    if input.is_null() || output.is_null() || output_len.is_null() {
+        return Base64ReturnCode::SC_BASE64_INVALID_ARG;
+    }
+    let input = std::slice::from_raw_parts(input, input_len as usize);
+    let encoded = base64::encode(input);
+    if encoded.len() + 1 > *output_len as usize {
+        return Base64ReturnCode::SC_BASE64_OVERFLOW;
+    }
+    let output = std::slice::from_raw_parts_mut(&mut *(output as *mut u8), *output_len as usize);
+    output[0..encoded.len()].copy_from_slice(encoded.as_bytes());
+    output[encoded.len()] = 0;
+    *output_len = encoded.len() as c_ulong;
+    Base64ReturnCode::SC_BASE64_OK
+}
+
+/// Ratio of output bytes to input bytes for Base64 Encoding is 4:3, hence the
+/// required output bytes are 4 * ceil(input_len / 3) and an additional byte for
+/// storing the NULL pointer.
+#[no_mangle]
+pub extern "C" fn Base64EncodeBufferSize(len: c_ulong) -> c_ulong {
+    (4 * ((len) + 2) / 3) + 1
+}
index e7b349213401b165ae89b36aecf995823b862b87..c6ba7183036a1423ccb2d86c79269deab0b253a7 100644 (file)
@@ -16,3 +16,4 @@
  */
 
 pub mod hashing;
+pub mod base64;
index 5604237008df2b64aa711ae084a595c26abbf738..6b0f14e3fccbfcf88bfbff7a7a531ba9c377dfaf 100755 (executable)
@@ -150,8 +150,6 @@ output_json_dnp3_objects_template = """/* Copyright (C) 2015 Open Information Se
 
 #include "suricata-common.h"
 
-#include "util-crypt.h"
-
 #include "app-layer-dnp3.h"
 #include "app-layer-dnp3-objects.h"
 #include "output-json-dnp3-objects.h"
index e186b5a8548277eaf008fd07e336640e9315cc59..f7edaaa518a65ab1723855141f72ab717d7987a5 100755 (executable)
@@ -500,7 +500,6 @@ noinst_HEADERS = \
        util-config.h \
        util-coredump-config.h \
        util-cpu.h \
-       util-crypt.h \
        util-daemon.h \
        util-debug-filters.h \
        util-debug.h \
@@ -1068,7 +1067,6 @@ libsuricata_c_a_SOURCES = \
        util-conf.c \
        util-coredump-config.c \
        util-cpu.c \
-       util-crypt.c \
        util-daemon.c \
        util-debug.c \
        util-debug-filters.c \
index fbab51ab3458c7cfb785075f81db3f63725deb95..265756ad7e5cb128295f00a7d94e3fce24ecfb03 100644 (file)
@@ -42,7 +42,6 @@
 #include "decode-events.h"
 #include "conf.h"
 
-#include "util-crypt.h"
 #include "util-spm.h"
 #include "util-unittest.h"
 #include "util-debug.h"
index 58bdd90b7c1cce5ae47370786c67e1149918f1c6..3b1d8f3fc02ceb0f0fdf3e300bbbea9627f69069 100644 (file)
@@ -27,7 +27,6 @@
 #include "datasets-md5.h"
 #include "util-thash.h"
 #include "util-print.h"
-#include "util-crypt.h"     // encode base64
 #include "util-base64.h"    // decode base64
 
 int Md5StrSet(void *dst, void *src)
index 06673bd1dba0395a0e80cbda910e962b994e16eb..346397d6d6fa967e593e31e8029b2e1655e9e67f 100644 (file)
@@ -27,7 +27,6 @@
 #include "datasets-sha256.h"
 #include "util-thash.h"
 #include "util-print.h"
-#include "util-crypt.h"     // encode base64
 #include "util-base64.h"    // decode base64
 
 int Sha256StrSet(void *dst, void *src)
index 66e5a8713a98f09506c7205e2628f2b251a11672..4a572898ceb3b0d9e1492c2c86b6fcacc530e5fd 100644 (file)
@@ -27,8 +27,8 @@
 #include "datasets-string.h"
 #include "util-thash.h"
 #include "util-print.h"
-#include "util-crypt.h"     // encode base64
 #include "util-base64.h"    // decode base64
+#include "rust.h"
 
 #if 0
 static int StringAsAscii(const void *s, char *out, size_t out_size)
@@ -47,7 +47,7 @@ int StringAsBase64(const void *s, char *out, size_t out_size)
 {
     const StringType *str = s;
 
-    unsigned long len = BASE64_BUFFER_SIZE(str->len);
+    unsigned long len = Base64EncodeBufferSize(str->len);
     uint8_t encoded_data[len];
     if (Base64Encode((unsigned char *)str->ptr, str->len,
         encoded_data, &len) != SC_BASE64_OK)
index 448e0b20cb66ea3c9aaf7f613e7433b7611761d4..3964622d51a2c42542535c6b7b2ceb2775347490 100644 (file)
@@ -30,7 +30,6 @@
 #include "datasets-reputation.h"
 #include "util-thash.h"
 #include "util-print.h"
-#include "util-crypt.h"     // encode base64
 #include "util-base64.h"    // decode base64
 #include "util-byte.h"
 #include "util-misc.h"
index fde1fdbc81adf95e078f5411c74ab31c1c501777..4b41ffbeea68f137e1a81aabeee94c823aaf757f 100644 (file)
@@ -50,7 +50,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 #include "util-time.h"
 #include "log-cf-common.h"
 
index 3d50f5ca2d71390b1fff8a82236ebc40b81eef49..481c7dd7de78a70e940649f0e1a446eb82754f38 100644 (file)
@@ -51,7 +51,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 #include "util-time.h"
 
 #define MODULE_NAME "LogTlsStoreLog"
@@ -123,7 +122,7 @@ static void LogTlsLogPem(LogTlsStoreLogThread *aft, const Packet *p, SSLState *s
     }
 
     TAILQ_FOREACH(cert, &state->server_connp.certs, next) {
-        pemlen = BASE64_BUFFER_SIZE(cert->cert_len);
+        pemlen = Base64EncodeBufferSize(cert->cert_len);
         if (pemlen > aft->enc_buf_len) {
             ptmp = (uint8_t*) SCRealloc(aft->enc_buf, sizeof(uint8_t) * pemlen);
             if (ptmp == NULL) {
index b79128deac8a1a9c97aac8d85ceb6db09e259996..d2373ed0e41647caac094ec20e3489c9fd9cad41 100644 (file)
@@ -80,7 +80,6 @@
 #include "util-proto-name.h"
 #include "util-optimize.h"
 #include "util-buffer.h"
-#include "util-crypt.h"
 #include "util-validate.h"
 
 #define MODULE_NAME "JsonAlertLog"
index 172e2eb80e1d6c5f92f81ac3b836b662f2f87c4d..7864f8fe5681df033fb5f882a00b578835aa8dfb 100644 (file)
@@ -55,7 +55,6 @@
 #include "util-proto-name.h"
 #include "util-optimize.h"
 #include "util-buffer.h"
-#include "util-crypt.h"
 #include "util-validate.h"
 
 #define MODULE_NAME "JsonAnomalyLog"
index 377dffe417e5d115e73668107bfab42bc20092ec..65a1a396ed58fe4a6e4400efe147fe8d66390157 100644 (file)
@@ -24,8 +24,6 @@
 
 #include "suricata-common.h"
 
-#include "util-crypt.h"
-
 #include "app-layer-dnp3.h"
 #include "app-layer-dnp3-objects.h"
 #include "output-json-dnp3-objects.h"
index aead12cfd3a55e1aa3c67b895ce5afe9163f2b3d..082a37fcbc022b7d5e8f5809c2f7ded78825dce4 100644 (file)
@@ -28,7 +28,6 @@
 #include "util-print.h"
 #include "util-unittest.h"
 #include "util-buffer.h"
-#include "util-crypt.h"
 #include "util-debug.h"
 
 #include "app-layer.h"
index 250d7d17d743fb5d1fa92453dded3a9c16409afe..613421eb805cc5aeeb49b70c0047cafe97e7a030 100644 (file)
@@ -48,7 +48,6 @@
 #include "util-byte.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 
 #include "output-json.h"
 #include "output-json-email-common.h"
index 88c0c8967cb07b4fe75b5e54dcf1e184dac77b78..bb6bc0643408dee2ecec922d8359b09cddd700b0 100644 (file)
@@ -49,7 +49,6 @@
 #include "util-proto-name.h"
 #include "util-logopenfile.h"
 #include "util-time.h"
-#include "util-crypt.h"
 #include "output-json.h"
 #include "output-json-alert.h"
 #include "output-json-http.h"
index 7e57c719360401e42dac6665057353496a941eb2..609ac87879a5af3adfeea985fcdcaac3b8a4dda5 100644 (file)
@@ -45,7 +45,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 
 #include "output-json.h"
 #include "output-json-http2.h"
index a2a8a5f85aae7e260a6945d933bb3caadf37b62e..62583dfcdfd3419a676aa472c52ded79366edc2f 100644 (file)
@@ -61,7 +61,6 @@
 #include "util-proto-name.h"
 #include "util-optimize.h"
 #include "util-buffer.h"
-#include "util-crypt.h"
 
 #define MODULE_NAME "JsonMetadataLog"
 
index ec649abf4f9c7ecd8ced8b15e2b30a2fe7771a25..e82e4994e4dbc7a31b2d1db131191d4bb5cb6e9a 100644 (file)
@@ -45,7 +45,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 
 #include "output-json.h"
 #include "output-json-ssh.h"
index 6e82745eb7588d168d3808a114675f485d1dbd54..07c4dc1c78a182507e7eae45781b299cc556a3f6 100644 (file)
@@ -43,7 +43,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 
 #include "output-json.h"
 #include "output-json-stats.h"
index 5280483a406ef1854b25fe51abdd9e0223661d6d..4cc13b4cac2ba5b1c38171eef25c63fc0616e319 100644 (file)
@@ -45,7 +45,6 @@
 #include "util-buffer.h"
 
 #include "util-logopenfile.h"
-#include "util-crypt.h"
 #include "util-ja3.h"
 
 #include "output-json.h"
index e9eccf4a79286e8105814faaf658962eb7b313a2..04bd6ed159953d7cacb5c86b536c0a9e68ddd456 100644 (file)
@@ -59,7 +59,6 @@
 #include "util-log-redis.h"
 #include "util-device.h"
 #include "util-validate.h"
-#include "util-crypt.h"
 #include "util-plugin.h"
 
 #include "flow-var.h"
diff --git a/src/util-crypt.c b/src/util-crypt.c
deleted file mode 100644 (file)
index eacdf48..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/* Copyright (C) 2007-2012 Open Information Security Foundation
- *
- * You can copy, redistribute or modify this Program under the terms of
- * the GNU General Public License version 2 as published by the Free
- * Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- */
-
-/**
- * \file
- *
- * \author Roliers Jean-Paul <popof.fpn@gmail.co>
- *
- * Implements cryptographic functions.
- * Based on the libtomcrypt library ( http://libtom.org/?page=features&newsitems=5&whatfile=crypt )
- * 
- * Implementation of function using NSS is not linked with libtomcrypt.
- */
-
-#include "suricata-common.h"
-#include "suricata.h"
-#include "util-crypt.h"
-
-static const char *b64codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-int Base64Encode(const unsigned char *in,  unsigned long inlen,
-                        unsigned char *out, unsigned long *outlen)
-{
-   unsigned long i, len2, leven;
-   unsigned char *p;
-   if(in == NULL || out == NULL || outlen == NULL)
-   {
-       return SC_BASE64_INVALID_ARG;
-   }
-   /* valid output size ? */
-   len2 = 4 * ((inlen + 2) / 3);
-   if (*outlen < len2 + 1) {
-      *outlen = len2 + 1;
-      return SC_BASE64_OVERFLOW;
-   }
-   p = out;
-   leven = 3*(inlen / 3);
-   for (i = 0; i < leven; i += 3) {
-       *p++ = b64codes[(in[0] >> 2) & 0x3F];
-       *p++ = b64codes[(((in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
-       *p++ = b64codes[(((in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
-       *p++ = b64codes[in[2] & 0x3F];
-       in += 3;
-   }
-   /* Pad it if necessary...  */
-   if (i < inlen) {
-       unsigned a = in[0];
-       unsigned b = (i+1 < inlen) ? in[1] : 0;
-
-       *p++ = b64codes[(a >> 2) & 0x3F];
-       *p++ = b64codes[(((a & 3) << 4) + (b >> 4)) & 0x3F];
-       *p++ = (i+1 < inlen) ? b64codes[(((b & 0xf) << 2)) & 0x3F] : '=';
-       *p++ = '=';
-   }
-   /* append a NULL byte */
-   *p = '\0';
-   /* return ok */
-   *outlen = p - out;
-   return SC_BASE64_OK;
-}
diff --git a/src/util-crypt.h b/src/util-crypt.h
deleted file mode 100644 (file)
index f36238f..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/* Copyright (C) 2007-2012 Open Information Security Foundation
- *
- * You can copy, redistribute or modify this Program under the terms of
- * the GNU General Public License version 2 as published by the Free
- * Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- */
-
-/**
- * \file
- *
- * \author Roliers Jean-Paul <popof.fpn@gmail.co>
- *
- * Implements cryptographic functions.
- * Based on the libtomcrypt library ( http://libtom.org/?page=features&newsitems=5&whatfile=crypt )
- */
-
-#ifndef UTIL_CRYPT_H_
-#define UTIL_CRYPT_H_
-
-#include "suricata-common.h"
-
-/* Ratio of output bytes to input bytes for Base64 Encoding is 4:3, hence the
- * required output bytes are 4 * ceil(input_len / 3) and an additional byte
- * for storing the NULL pointer.
- * */
-#define BASE64_BUFFER_SIZE(x)  ((4 * ((x) + 2) / 3) + 1)
-
-typedef enum {
-    SC_BASE64_OK,
-    SC_BASE64_INVALID_ARG,
-    SC_BASE64_OVERFLOW,
-
-} CryptId;
-
-int Base64Encode(const unsigned char *in,  unsigned long inlen, unsigned char *out, unsigned long *outlen);
-
-#endif /* UTIL_CRYPT_H_ */