suricata
util-base64.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author David Abarbanel <david.abarbanel@baesystems.com>
22  *
23  */
24 
25 #include "util-base64.h"
26 #include "util-debug.h"
27 #include "util-unittest.h"
28 /* Constants */
29 #define BASE64_TABLE_MAX 122
30 
31 /* Base64 character to index conversion table */
32 /* Characters are mapped as "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */
33 static const int b64table[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37  -1, -1, -1, 62, -1, -1, -1, 63, 52, 53,
38  54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
39  -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
40  5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
41  15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
42  25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
43  29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
44  39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
45  49, 50, 51 };
46 
47 /**
48  * \brief Gets a base64-decoded value from an encoded character
49  *
50  * \param c The encoded character
51  *
52  * \return The decoded value (0 or above), or -1 if the parameter is invalid
53  */
54 static inline int GetBase64Value(uint8_t c)
55 {
56  int val = -1;
57 
58  /* Pull from conversion table */
59  if (c <= BASE64_TABLE_MAX) {
60  val = b64table[(int) c];
61  }
62 
63  return val;
64 }
65 
66 /**
67  * \brief Decodes a 4-byte base64-encoded block into a 3-byte ascii-encoded block
68  *
69  * \param ascii the 3-byte ascii output block
70  * \param b64 the 4-byte base64 input block
71  *
72  * \return none
73  */
74 static inline void DecodeBase64Block(uint8_t ascii[ASCII_BLOCK], uint8_t b64[B64_BLOCK])
75 {
76  ascii[0] = (uint8_t) (b64[0] << 2) | (b64[1] >> 4);
77  ascii[1] = (uint8_t) (b64[1] << 4) | (b64[2] >> 2);
78  ascii[2] = (uint8_t) (b64[2] << 6) | (b64[3]);
79 }
80 
81 /**
82  * \brief Decodes a base64-encoded string buffer into an ascii-encoded byte buffer
83  *
84  * \param dest The destination byte buffer
85  * \param dest_size The destination byte buffer size
86  * \param src The source string
87  * \param len The length of the source string
88  * \param consumed_bytes The bytes that were actually processed/consumed
89  * \param decoded_bytes The bytes that were decoded
90  * \param mode The mode in which decoding should happen
91  *
92  * \return Error code indicating success or failures with parsing
93  */
94 Base64Ecode DecodeBase64(uint8_t *dest, uint32_t dest_size, const uint8_t *src, uint32_t len,
95  uint32_t *consumed_bytes, uint32_t *decoded_bytes, Base64Mode mode)
96 {
97  int val;
98  uint32_t padding = 0, bbidx = 0, sp = 0, leading_sp = 0;
99  uint8_t *dptr = dest;
100  uint8_t b64[B64_BLOCK] = { 0,0,0,0 };
101  bool valid = true;
103  *decoded_bytes = 0;
104 
105  /* Traverse through each alpha-numeric letter in the source array */
106  for (uint32_t i = 0; i < len; i++) {
107  /* Get decimal representation */
108  val = GetBase64Value(src[i]);
109  if (val < 0) {
110  if ((mode == BASE64_MODE_RFC2045) && (src[i] == ' ')) {
111  if (bbidx == 0) {
112  /* Special case where last block of data has a leading space */
113  leading_sp++;
114  }
115  sp++;
116  continue;
117  }
118  /* Invalid character found, so decoding fails */
119  if (src[i] != '=') {
120  valid = false;
121  if (mode != BASE64_MODE_RELAX) {
122  *decoded_bytes = 0;
123  }
124  ecode = BASE64_ECODE_ERR;
125  break;
126  }
127  padding++;
128  }
129 
130  /* For each alpha-numeric letter in the source array, find the numeric
131  * value */
132  b64[bbidx++] = (val > 0 ? (uint8_t)val : 0);
133 
134  /* Decode every 4 base64 bytes into 3 ascii bytes */
135  if (bbidx == B64_BLOCK) {
136 
137  /* For every 4 bytes, add 3 bytes but deduct the '=' padded blocks */
138  uint32_t numDecoded_blk = ASCII_BLOCK - (padding < B64_BLOCK ? padding : ASCII_BLOCK);
139  if (dest_size < *decoded_bytes + numDecoded_blk) {
140  SCLogDebug("Destination buffer full");
141  ecode = BASE64_ECODE_BUF;
142  break;
143  }
144 
145  /* Decode base-64 block into ascii block and move pointer */
146  DecodeBase64Block(dptr, b64);
147  dptr += numDecoded_blk;
148  *decoded_bytes += numDecoded_blk;
149  /* Reset base-64 block and index */
150  bbidx = 0;
151  padding = 0;
152  *consumed_bytes += B64_BLOCK + sp;
153  sp = 0;
154  leading_sp = 0;
155  memset(&b64, 0, sizeof(b64));
156  }
157  }
158 
159  if (!valid && mode == BASE64_MODE_RFC4648) {
160  padding = B64_BLOCK - bbidx;
161  *decoded_bytes += ASCII_BLOCK - padding;
162  DecodeBase64Block(dptr, b64);
163  *consumed_bytes += bbidx;
164  }
165 
166  /* Finish remaining b64 bytes by padding */
167  if (valid && bbidx > 0 && (mode != BASE64_MODE_RFC2045)) {
168  /* Decode remaining */
169  *decoded_bytes += ASCII_BLOCK - (B64_BLOCK - bbidx);
170  DecodeBase64Block(dptr, b64);
171  }
172 
173  if (*decoded_bytes == 0) {
174  SCLogDebug("base64 decoding failed");
175  }
176 
177  *consumed_bytes += leading_sp;
178  return ecode;
179 }
180 
181 #ifdef UNITTESTS
182 
183 #define TEST_RFC2045(src, fin_str, dest_size, exp_decoded, exp_consumed, ecode) \
184  { \
185  uint32_t consumed_bytes = 0, num_decoded = 0; \
186  uint8_t dst[dest_size]; \
187  Base64Ecode code = DecodeBase64(dst, strlen(fin_str), (const uint8_t *)src, strlen(src), \
188  &consumed_bytes, &num_decoded, BASE64_MODE_RFC2045); \
189  FAIL_IF(code != ecode); \
190  FAIL_IF(memcmp(dst, fin_str, strlen(fin_str)) != 0); \
191  FAIL_IF(num_decoded != exp_decoded); \
192  FAIL_IF(consumed_bytes != exp_consumed); \
193  }
194 
195 #define TEST_RFC4648(src, fin_str, dest_size, exp_decoded, exp_consumed, ecode) \
196  { \
197  uint32_t consumed_bytes = 0, num_decoded = 0; \
198  uint8_t dst[dest_size]; \
199  Base64Ecode code = DecodeBase64(dst, strlen(fin_str), (const uint8_t *)src, strlen(src), \
200  &consumed_bytes, &num_decoded, BASE64_MODE_RFC4648); \
201  FAIL_IF(code != ecode); \
202  FAIL_IF(memcmp(dst, fin_str, strlen(fin_str)) != 0); \
203  FAIL_IF(num_decoded != exp_decoded); \
204  FAIL_IF(consumed_bytes != exp_consumed); \
205  }
206 
207 static int B64DecodeCompleteString(void)
208 {
209  /*
210  * SGVsbG8gV29ybGR6 : Hello Worldz
211  * */
212  const char *src = "SGVsbG8gV29ybGR6";
213  const char *fin_str = "Hello Worldz";
214  TEST_RFC2045(src, fin_str, strlen(fin_str), strlen(fin_str), strlen(src), BASE64_ECODE_OK);
215  PASS;
216 }
217 
218 static int B64DecodeInCompleteString(void)
219 {
220  /*
221  * SGVsbG8gV29ybGR6 : Hello Worldz
222  * */
223  const char *src = "SGVsbG8gV29ybGR";
224  const char *fin_str = "Hello Wor"; // bc it'll error out on last 3 bytes
225  TEST_RFC2045(src, fin_str, strlen(fin_str), strlen(fin_str), strlen(src) - 3, BASE64_ECODE_OK);
226  PASS;
227 }
228 
229 static int B64DecodeCompleteStringWSp(void)
230 {
231  /*
232  * SGVsbG8gV29ybGQ= : Hello World
233  * */
234 
235  const char *src = "SGVs bG8 gV29y bGQ=";
236  const char *fin_str = "Hello World";
237  TEST_RFC2045(src, fin_str, strlen(fin_str) + 1, strlen(fin_str), strlen(src), BASE64_ECODE_OK);
238  PASS;
239 }
240 
241 static int B64DecodeInCompleteStringWSp(void)
242 {
243  /*
244  * SGVsbG8gV29ybGQ= : Hello World
245  * Special handling for this case (sp in remainder) done in ProcessBase64Remainder
246  * */
247 
248  const char *src = "SGVs bG8 gV29y bGQ";
249  const char *fin_str = "Hello Wor";
250  TEST_RFC2045(src, fin_str, strlen(fin_str), strlen(fin_str), strlen(src) - 3, BASE64_ECODE_OK);
251  PASS;
252 }
253 
254 static int B64DecodeStringBiggerThanBuffer(void)
255 {
256  /*
257  * SGVsbG8gV29ybGQ= : Hello World
258  * */
259 
260  const char *src = "SGVs bG8 gV29y bGQ=";
261  const char *fin_str = "Hello Wor";
262  TEST_RFC2045(
263  src, fin_str, strlen(fin_str) + 1, strlen(fin_str), strlen(src) - 4, BASE64_ECODE_BUF);
264  PASS;
265 }
266 
267 static int B64DecodeStringEndingSpaces(void)
268 {
269  const char *src = "0YPhA d H";
270  uint32_t consumed_bytes = 0, num_decoded = 0;
271  uint8_t dst[10];
272  Base64Ecode code = DecodeBase64(dst, sizeof(dst), (const uint8_t *)src, strlen(src),
273  &consumed_bytes, &num_decoded, BASE64_MODE_RFC2045);
275  FAIL_IF(num_decoded != 3);
276  FAIL_IF(consumed_bytes != 4);
277  PASS;
278 }
279 
280 static int B64TestVectorsRFC2045(void)
281 {
282  const char *src1 = "";
283  const char *fin_str1 = "";
284 
285  const char *src2 = "Zg==";
286  const char *fin_str2 = "f";
287 
288  const char *src3 = "Zm8=";
289  const char *fin_str3 = "fo";
290 
291  const char *src4 = "Zm9v";
292  const char *fin_str4 = "foo";
293 
294  const char *src5 = "Zm9vYg==";
295  const char *fin_str5 = "foob";
296 
297  const char *src6 = "Zm9vYmE=";
298  const char *fin_str6 = "fooba";
299 
300  const char *src7 = "Zm9vYmFy";
301  const char *fin_str7 = "foobar";
302 
303  const char *src8 = "Zm 9v Ym Fy";
304  const char *fin_str8 = "foobar";
305 
306  const char *src9 = "Zm$9vYm.Fy";
307  const char *fin_str9 = "";
308 
309  TEST_RFC2045(src1, fin_str1, strlen(fin_str1), strlen(fin_str1), strlen(src1), BASE64_ECODE_OK);
310  TEST_RFC2045(src2, fin_str2, ASCII_BLOCK * 2, strlen(fin_str2), strlen(src2), BASE64_ECODE_OK);
311  TEST_RFC2045(src3, fin_str3, ASCII_BLOCK * 2, strlen(fin_str3), strlen(src3), BASE64_ECODE_OK);
312  TEST_RFC2045(src4, fin_str4, ASCII_BLOCK * 2, strlen(fin_str4), strlen(src4), BASE64_ECODE_OK);
313  TEST_RFC2045(src5, fin_str5, ASCII_BLOCK * 2, strlen(fin_str5), strlen(src5), BASE64_ECODE_OK);
314  TEST_RFC2045(src6, fin_str6, ASCII_BLOCK * 2, strlen(fin_str6), strlen(src6), BASE64_ECODE_OK);
315  TEST_RFC2045(src7, fin_str7, ASCII_BLOCK * 2, strlen(fin_str7), strlen(src7), BASE64_ECODE_OK);
316  TEST_RFC2045(src8, fin_str8, ASCII_BLOCK * 2, strlen(fin_str8), strlen(src8), BASE64_ECODE_OK);
317  TEST_RFC2045(src9, fin_str9, ASCII_BLOCK * 2, 0, 0,
318  BASE64_ECODE_ERR); // TODO this should be accepted just like the previous string
319  PASS;
320 }
321 
322 static int B64TestVectorsRFC4648(void)
323 {
324  const char *src1 = "";
325  const char *fin_str1 = "";
326 
327  const char *src2 = "Zg==";
328  const char *fin_str2 = "f";
329 
330  const char *src3 = "Zm8=";
331  const char *fin_str3 = "fo";
332 
333  const char *src4 = "Zm9v";
334  const char *fin_str4 = "foo";
335 
336  const char *src5 = "Zm9vYg==";
337  const char *fin_str5 = "foob";
338 
339  const char *src6 = "Zm9vYmE=";
340  const char *fin_str6 = "fooba";
341 
342  const char *src7 = "Zm9vYmFy";
343  const char *fin_str7 = "foobar";
344 
345  const char *src8 = "Zm 9v Ym Fy";
346  const char *fin_str8 = "f";
347 
348  const char *src9 = "Zm$9vYm.Fy";
349  const char *fin_str9 = "f";
350 
351  TEST_RFC4648(src1, fin_str1, ASCII_BLOCK * 2, strlen(fin_str1), strlen(src1), BASE64_ECODE_OK);
352  TEST_RFC4648(src2, fin_str2, ASCII_BLOCK * 2, strlen(fin_str2), strlen(src2), BASE64_ECODE_OK);
353  TEST_RFC4648(src3, fin_str3, ASCII_BLOCK * 2, strlen(fin_str3), strlen(src3), BASE64_ECODE_OK);
354  TEST_RFC4648(src4, fin_str4, ASCII_BLOCK * 2, strlen(fin_str4), strlen(src4), BASE64_ECODE_OK);
355  TEST_RFC4648(src5, fin_str5, ASCII_BLOCK * 2, strlen(fin_str5), strlen(src5), BASE64_ECODE_OK);
356  TEST_RFC4648(src6, fin_str6, ASCII_BLOCK * 2, strlen(fin_str6), strlen(src6), BASE64_ECODE_OK);
357  TEST_RFC4648(src7, fin_str7, ASCII_BLOCK * 2, strlen(fin_str7), strlen(src7), BASE64_ECODE_OK);
358  TEST_RFC4648(src8, fin_str8, ASCII_BLOCK * 2, 1 /* f */, 2 /* Zm */, BASE64_ECODE_ERR);
359  TEST_RFC4648(src9, fin_str9, ASCII_BLOCK * 2, 1 /* f */, 2 /* Zm */, BASE64_ECODE_ERR);
360  PASS;
361 }
362 
364 {
365  UtRegisterTest("B64DecodeCompleteStringWSp", B64DecodeCompleteStringWSp);
366  UtRegisterTest("B64DecodeInCompleteStringWSp", B64DecodeInCompleteStringWSp);
367  UtRegisterTest("B64DecodeCompleteString", B64DecodeCompleteString);
368  UtRegisterTest("B64DecodeInCompleteString", B64DecodeInCompleteString);
369  UtRegisterTest("B64DecodeStringBiggerThanBuffer", B64DecodeStringBiggerThanBuffer);
370  UtRegisterTest("B64DecodeStringEndingSpaces", B64DecodeStringEndingSpaces);
371  UtRegisterTest("B64TestVectorsRFC2045", B64TestVectorsRFC2045);
372  UtRegisterTest("B64TestVectorsRFC4648", B64TestVectorsRFC4648);
373 }
374 #endif
BASE64_ECODE_BUF
@ BASE64_ECODE_BUF
Definition: util-base64.h:75
len
uint8_t len
Definition: app-layer-dnp3.h:2
TEST_RFC4648
#define TEST_RFC4648(src, fin_str, dest_size, exp_decoded, exp_consumed, ecode)
Definition: util-base64.c:195
BASE64_TABLE_MAX
#define BASE64_TABLE_MAX
Definition: util-base64.c:29
BASE64_ECODE_ERR
@ BASE64_ECODE_ERR
Definition: util-base64.h:73
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
util-base64.h
BASE64_MODE_RELAX
@ BASE64_MODE_RELAX
Definition: util-base64.h:35
util-unittest.h
BASE64_MODE_RFC2045
@ BASE64_MODE_RFC2045
Definition: util-base64.h:51
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
BASE64_MODE_RFC4648
@ BASE64_MODE_RFC4648
Definition: util-base64.h:68
Base64Mode
Base64Mode
Definition: util-base64.h:34
padding
uint32_t padding
Definition: decode-erspan.h:2
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
Base64RegisterTests
void Base64RegisterTests(void)
Definition: util-base64.c:363
BASE64_ECODE_OK
@ BASE64_ECODE_OK
Definition: util-base64.h:74
TEST_RFC2045
#define TEST_RFC2045(src, fin_str, dest_size, exp_decoded, exp_consumed, ecode)
Definition: util-base64.c:183
src
uint16_t src
Definition: app-layer-dnp3.h:5
B64_BLOCK
#define B64_BLOCK
Definition: util-base64.h:32
ASCII_BLOCK
#define ASCII_BLOCK
Definition: util-base64.h:31
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
code
uint8_t code
Definition: decode-icmpv4.h:1
Base64Ecode
Base64Ecode
Definition: util-base64.h:72
DecodeBase64
Base64Ecode DecodeBase64(uint8_t *dest, uint32_t dest_size, const uint8_t *src, uint32_t len, uint32_t *consumed_bytes, uint32_t *decoded_bytes, Base64Mode mode)
Decodes a base64-encoded string buffer into an ascii-encoded byte buffer.
Definition: util-base64.c:94