Skip to content

Commit a3ae686

Browse files
committed
Adds base64 support via mbedtls
This is inspered in the work done at: #1123
1 parent 4b9cff3 commit a3ae686

File tree

8 files changed

+3753
-1
lines changed

8 files changed

+3753
-1
lines changed

src/Makefile.am

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ BUILT_SOURCES= \
33
parser/seclang-parser.cc
44

55
lib_LTLIBRARIES = libmodsecurity.la
6+
noinst_LTLIBRARIES = libmbedtls.la
67
libmodsecurity_ladir = $(prefix)/include
78
libmodsecurity_includesubdir = $(pkgincludedir)/collection/
89

10+
11+
12+
libmbedtls_la_SOURCES = \
13+
utils/mbedtls/base64.c
14+
15+
16+
libmbedtls_la_CFLAGS = -D MBEDTLS_CONFIG_FILE=\"mbed-tls-config.h\" -Iutils
17+
libmbedtls_la_CPPFLAGS =
18+
libmbedtls_la_LIBADD =
19+
20+
921
CLEANFILES = \
1022
location.hh \
1123
position.hh \
@@ -170,6 +182,7 @@ OPERATORS = \
170182

171183
UTILS = \
172184
utils/acmp.cc \
185+
utils/base64.cc \
173186
utils/geo_lookup.cc \
174187
utils/https_client.cc \
175188
utils/ip_tree.cc \
@@ -241,7 +254,8 @@ libmodsecurity_la_LIBADD = \
241254
$(PCRE_LDADD) \
242255
$(YAJL_LDADD) \
243256
$(LIBXML2_LDADD) \
244-
../others/libinjection.la
257+
../others/libinjection.la \
258+
libmbedtls.la
245259

246260

247261
libmodsecurity_la_LDFLAGS = \

src/utils/base64.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include <string>
17+
#include <fstream>
18+
#include <iostream>
19+
20+
#include "utils/base64.h"
21+
#include "mbedtls/base64.h"
22+
#include <string.h>
23+
24+
namespace modsecurity {
25+
namespace Utils {
26+
27+
28+
std::string Base64::encode(std::string& data) {
29+
size_t encoded_len = 0;
30+
unsigned char *d = NULL;
31+
std::string ret;
32+
33+
mbedtls_base64_encode(NULL, 0, &encoded_len,
34+
reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
35+
36+
d = reinterpret_cast<unsigned char*>(malloc(sizeof(char) * encoded_len));
37+
if (d == NULL) {
38+
return data;
39+
}
40+
41+
memset(d, '\0', encoded_len);
42+
43+
mbedtls_base64_encode(d, encoded_len, &encoded_len,
44+
(unsigned char*) data.c_str(), data.size());
45+
46+
ret.assign(reinterpret_cast<const char*>(d));
47+
free(d);
48+
49+
50+
return std::string(reinterpret_cast<const char*>(d));
51+
}
52+
53+
54+
std::string Base64::decode(std::string& data) {
55+
size_t decoded_len = 0;
56+
unsigned char *d = NULL;
57+
std::string ret;
58+
59+
mbedtls_base64_decode(NULL, 0, &decoded_len,
60+
reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
61+
62+
d = reinterpret_cast<unsigned char*>(malloc(sizeof(char) * decoded_len));
63+
if (d == NULL) {
64+
return data;
65+
}
66+
67+
memset(d, '\0', decoded_len);
68+
69+
mbedtls_base64_decode(d, decoded_len, &decoded_len,
70+
reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
71+
72+
ret.assign(reinterpret_cast<const char*>(d));
73+
free(d);
74+
75+
return ret;
76+
}
77+
78+
79+
} // namespace Utils
80+
} // namespace modsecurity

src/utils/base64.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
namespace modsecurity {
4+
namespace Utils {
5+
6+
class Base64 {
7+
public:
8+
Base64() { }
9+
10+
static std::string encode(std::string& data);
11+
static std::string decode(std::string& data);
12+
};
13+
14+
15+
} // namespace Utils
16+
} // namespace modsecurity

0 commit comments

Comments
 (0)