1 /*
2  * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <openssl/bio.h>
18 #include <openssl/evp.h>
19 
20 #include <err.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #define BUF_SIZE 128
25 
26 struct base64_test {
27 	const unsigned char in[BUF_SIZE];
28 	const ssize_t in_len;
29 	const unsigned char out[BUF_SIZE];
30 	const ssize_t out_len;
31 	const ssize_t valid_len;
32 };
33 
34 /*
35  * Many of these tests are based on those found in Go's encoding/base64 tests.
36  */
37 struct base64_test base64_tests[] = {
38 
39 	/* RFC3548 examples. */
40 	{ "\x14\xfb\x9c\x03\xd9\x7e", 6, "FPucA9l+", 8, 6, },
41 	{ "\x14\xfb\x9c\x03\xd9", 5, "FPucA9k=", 8, 5, },
42 	{ "\x14\xfb\x9c\x03", 4, "FPucAw==", 8, 4, },
43 
44 	/* RFC4648 examples. */
45 	{ "", 0, "", 0, 0, },
46 	{ "f", 1, "Zg==", 4, 1, },
47 	{ "fo", 2, "Zm8=", 4, 2, },
48 	{ "foo", 3, "Zm9v", 4, 3, },
49 	{ "foob", 4, "Zm9vYg==", 8, 4, },
50 	{ "fooba", 5, "Zm9vYmE=", 8, 5, },
51 	{ "foobar", 6, "Zm9vYmFy", 8, 6, },
52 
53 	/* Wikipedia examples. */
54 	{ "sure.", 5, "c3VyZS4=", 8, 5, },
55 	{ "sure", 4, "c3VyZQ==", 8, 4, },
56 	{ "sur", 3, "c3Vy", 4, 3, },
57 	{ "su", 2, "c3U=", 4, 2, },
58 	{ "leasure.", 8, "bGVhc3VyZS4=", 12, 8, },
59 	{ "easure.", 7, "ZWFzdXJlLg==", 12, 7, },
60 	{ "asure.", 6, "YXN1cmUu", 8, 6, },
61 
62 	{ "abcd", 4, "YWJjZA==", 8, 4, },
63 
64 	{
65 		"Twas brillig, and the slithy toves",
66 		34,
67 		"VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==",
68 		48,
69 		34,
70 	},
71 };
72 
73 #define N_TESTS (sizeof(base64_tests) / sizeof(*base64_tests))
74 
75 struct base64_test base64_nl_tests[] = {
76 
77 	/* Corrupt/invalid encodings. */
78 	{ "", -1, "", 0, 0, },
79 	{ "", -1, "!!!!", 4, 0, },
80 	{ "", -1, "====", 4, 0, },
81 	{ "", -1, "x===", 4, 0, },
82 	{ "", -1, "=AAA", 4, 0, },
83 	{ "", -1, "A=AA", 4, 0, },
84 	{ "", -1, "AA=A", 4, 0, },
85 	{ "", -1, "AA==A", 5, 0, },
86 	{ "", -1, "AAA=AAAA", 8, 0, },
87 	{ "", -1, "AAAAA", 5, 0, },
88 	{ "", -1, "AAAAAA", 6, 0, },
89 	{ "", -1, "A=", 2, 0, },
90 	{ "", -1, "A==", 3, 0, },
91 	{ "", -1, "AA=", 3, 0, },
92 	{ "", -1, "AA==", 4, 1, },		/* XXX - output ix 0x0. */
93 	{ "", -1, "AAA=", 4, 2, },		/* XXX - output ix 2x 0x0. */
94 	{ "", -1, "AAAA", 4, 3, },		/* XXX - output ix 3x 0x0. */
95 	{ "", -1, "AAAAAA=", 7, 0, },
96 	{ "", -1, "YWJjZA=====", 11, 0, },
97 
98 
99 	/* Encodings with embedded CR/LF. */
100 	{ "sure", 4, "c3VyZQ==", 8, 4, },
101 	{ "sure", 4, "c3VyZQ==\r", 9, 4, },
102 	{ "sure", 4, "c3VyZQ==\n", 9, 4, },
103 	{ "sure", 4, "c3VyZQ==\r\n", 10, 4, },
104 	{ "sure", 4, "c3VyZ\r\nQ==", 10, 4, },
105 	{ "sure", 4, "c3V\ryZ\nQ==", 10, 4, },
106 	{ "sure", 4, "c3V\nyZ\rQ==", 10, 4, },
107 	{ "sure", 4, "c3VyZ\nQ==", 9, 4, },
108 	{ "sure", 4, "c3VyZQ\n==", 9, 4, },
109 	{ "sure", 4, "c3VyZQ=\n=", 9, 4, },
110 	{ "sure", 4, "c3VyZQ=\r\n\r\n=", 12, 4, },
111 
112 	{
113 		"",
114 		-1,
115 		"YWJjZA======================================================"
116 		"============",
117 		74,
118 		0,
119 	},
120 };
121 
122 #define N_NL_TESTS (sizeof(base64_nl_tests) / sizeof(*base64_nl_tests))
123 
124 struct base64_test base64_no_nl_tests[] = {
125 
126 	/*
127 	 * In non-newline mode, the output resulting from corrupt/invalid
128 	 * encodings is completely crazy. A number of zero bytes is returned
129 	 * rather than nothing.
130 	 */
131 
132 	/* Corrupt/invalid encodings. */
133 	{ "", -1, "", 0, 0, },
134 	{ "", -1, "!!!!", 4, 0, },
135 	{ "", -1, "====", 4, 1, },
136 	{ "", -1, "x===", 4, 1, },
137 	{ "", -1, "=AAA", 4, 3, },
138 	{ "", -1, "A=AA", 4, 3, },
139 	{ "", -1, "AA=A", 4, 3, },
140 	{ "", -1, "AA==A", 5, 1, },
141 	{ "", -1, "AAA=AAAA", 8, 6, },
142 	{ "", -1, "AAAAA", 5, 3, },
143 	{ "", -1, "AAAAAA", 6, 3, },
144 	{ "", -1, "A=", 2, 0, },
145 	{ "", -1, "A==", 3, 0, },
146 	{ "", -1, "AA=", 3, 0, },
147 	{ "", -1, "AA==", 4, 1, },
148 	{ "", -1, "AAA=", 4, 2, },
149 	{ "", -1, "AAAA", 4, 3, },
150 	{ "", -1, "AAAAAA=", 7, 3, },
151 	{ "", -1, "YWJjZA=====", 11, 4, },
152 
153 	/* Encodings with embedded CR/LF. */
154 	{ "sure", 4, "c3VyZQ==", 8, 4, },
155 	{ "sure", 4, "c3VyZQ==\r", 9, 4, },
156 	{ "sure", 4, "c3VyZQ==\n", 9, 4, },
157 	{ "sure", 4, "c3VyZQ==\r\n", 10, 4, },
158 	{ "sure", -1, "c3VyZ\r\nQ==", 10, 0, },
159 	{ "sure", -1, "c3V\ryZ\nQ==", 10, 0, },
160 	{ "sure", -1, "c3V\nyZ\rQ==", 10, 0, },
161 	{ "sure", -1, "c3VyZ\nQ==", 9, 0, },
162 	{ "sure", -1, "c3VyZQ\n==", 9, 0, },
163 	{ "sure", -1, "c3VyZQ=\n=", 9, 0, },
164 	{ "sure", -1, "c3VyZQ=\r\n\r\n=", 12, 0, },
165 
166 	/*
167 	 * This is invalid, yet results in 'abcd' followed by a stream of
168 	 * zero value bytes.
169 	 */
170 	{
171 		"",
172 		-1,
173 		"YWJjZA======================================================"
174 		"============",
175 		74,
176 		52,
177 	},
178 };
179 
180 #define N_NO_NL_TESTS (sizeof(base64_no_nl_tests) / sizeof(*base64_no_nl_tests))
181 
182 static int
183 base64_encoding_test(int test_no, struct base64_test *bt, int test_nl)
184 {
185 	BIO *bio_b64, *bio_mem;
186 	unsigned char *buf, *out;
187 	ssize_t i, len, b64len;
188 	int failure = 0;
189 
190 	buf = malloc(BUF_SIZE);
191 	if (buf == NULL)
192 		errx(1, "malloc");
193 
194 	bio_b64 = BIO_new(BIO_f_base64());
195 	if (bio_b64 == NULL)
196 		errx(1, "BIO_new failed for BIO_f_base64");
197 
198 	bio_mem = BIO_new(BIO_s_mem());
199 	if (bio_mem == NULL)
200 		errx(1, "BIO_new failed for BIO_s_mem");
201 
202 	bio_mem = BIO_push(bio_b64, bio_mem);
203 
204 	if (!test_nl)
205 		BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
206 
207 	len = BIO_write(bio_mem, bt->in, bt->in_len);
208 	if (len != bt->in_len) {
209 		fprintf(stderr, "FAIL: test %i - only wrote %zi out of %zi "
210 		    "characters\n", test_no, len, bt->in_len);
211 		failure = 1;
212 		goto done;
213 	}
214 	if (BIO_flush(bio_mem) < 0) {
215 		fprintf(stderr, "FAIL: test %i - flush failed\n", test_no);
216 		failure = 1;
217 		goto done;
218 	}
219 
220 	b64len = 0;
221 	for (i = 0; i < bt->out_len; i++) {
222 		if (bt->out[i] == '\r' || bt->out[i] == '\n')
223 			continue;
224 		buf[b64len++] = bt->out[i];
225 	}
226 	if (test_nl)
227 		buf[b64len++] = '\n';
228 
229 	len = BIO_get_mem_data(bio_mem, &out);
230 
231 	/* An empty string with NL results in no output, rather than '\n'. */
232 	if (test_nl && b64len == 1 && len == 0)
233 		goto done;
234 
235 	if (len != b64len) {
236 		fprintf(stderr, "FAIL: test %i - encoding resulted in %zi "
237 		    "characters instead of %zi\n", test_no, len, b64len);
238 		failure = 1;
239 		goto done;
240 	}
241 
242 	if (memcmp(buf, out, b64len) != 0) {
243 		fprintf(stderr, "FAIL: test %i - encoding differs:\n", test_no);
244 		fprintf(stderr, "  encoding: ");
245 		for (i = 0; i < len; i++)
246 			fprintf(stderr, "%c", out[i]);
247 		fprintf(stderr, "\n");
248 		fprintf(stderr, " test data: ");
249 		for (i = 0; i < bt->out_len; i++)
250 			fprintf(stderr, "%c", buf[i]);
251 		fprintf(stderr, "\n");
252 		failure = 1;
253 	}
254 
255 done:
256 	BIO_free_all(bio_mem);
257 	free(buf);
258 
259 	return failure;
260 }
261 
262 static int
263 base64_decoding_test(int test_no, struct base64_test *bt, int test_nl)
264 {
265 	BIO *bio_b64, *bio_mem;
266 	char *buf, *input;
267 	ssize_t i, inlen, len;
268 	int failure = 0;
269 
270 	buf = malloc(BUF_SIZE);
271 	if (buf == NULL)
272 		errx(1, "malloc");
273 
274 	input = (char *)bt->out;
275 	inlen = bt->out_len;
276 
277 	if (test_nl)
278 		inlen = asprintf(&input, "%s\r\n", bt->out);
279 
280 	bio_mem = BIO_new_mem_buf(input, inlen);
281 	if (bio_mem == NULL)
282 		errx(1, "BIO_new_mem_buf failed");
283 
284 	bio_b64 = BIO_new(BIO_f_base64());
285 	if (bio_b64 == NULL)
286 		errx(1, "BIO_new failed for BIO_f_base64");
287 
288 	if (!test_nl)
289 		BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
290 
291 	bio_mem = BIO_push(bio_b64, bio_mem);
292 
293 	/*
294 	 * If we wrote zero characters then a BIO_read will result in a return
295 	 * value of -1, hence we need to handle this case.
296 	 */
297 	len = BIO_read(bio_mem, buf, BUF_SIZE);
298 	if (len != bt->valid_len && (bt->in_len != 0 || len != -1)) {
299 		fprintf(stderr, "FAIL: test %i - decoding resulted in %zi "
300 		    "characters instead of %zi\n", test_no, len, bt->valid_len);
301 		fprintf(stderr, "  input: ");
302 		for (i = 0; i < inlen; i++)
303 			fprintf(stderr, "%c", input[i]);
304 		fprintf(stderr, "\n");
305 		fprintf(stderr, "  decoding: ");
306 		for (i = 0; i < len; i++)
307 			fprintf(stderr, "0x%x ", buf[i]);
308 		fprintf(stderr, "\n");
309 		failure = 1;
310 		goto done;
311 	}
312 
313 	/* See if we expect this to fail decoding. */
314 	if (bt->in_len == -1)
315 		goto done;
316 
317 	if (memcmp(bt->in, buf, bt->in_len) != 0) {
318 		fprintf(stderr, "FAIL: test %i - decoding differs:\n", test_no);
319 		fprintf(stderr, "  decoding: ");
320 		for (i = 0; i < len; i++)
321 			fprintf(stderr, "0x%x ", buf[i]);
322 		fprintf(stderr, "\n");
323 		fprintf(stderr, " test data: ");
324 		for (i = 0; i < inlen; i++)
325 			fprintf(stderr, "0x%x ", input[i]);
326 		fprintf(stderr, "\n");
327 		failure = 1;
328 	}
329 
330 done:
331 	BIO_free_all(bio_mem);
332 	free(buf);
333 	if (test_nl)
334 		free(input);
335 
336 	return failure;
337 }
338 
339 int
340 main(int argc, char **argv)
341 {
342 	struct base64_test *bt;
343 	int failed = 0;
344 	size_t i;
345 
346 	fprintf(stderr, "Starting combined tests...\n");
347 
348 	for (i = 0; i < N_TESTS; i++) {
349 		bt = &base64_tests[i];
350 		if (bt->in_len != -1)
351 			failed += base64_encoding_test(i, bt, 0);
352 		if (bt->out_len != -1)
353 			failed += base64_decoding_test(i, bt, 0);
354 		if (bt->in_len != -1)
355 			failed += base64_encoding_test(i, bt, 1);
356 		if (bt->out_len != -1)
357 			failed += base64_decoding_test(i, bt, 1);
358 	}
359 
360 	fprintf(stderr, "Starting NL tests...\n");
361 
362 	for (i = 0; i < N_NL_TESTS; i++) {
363 		bt = &base64_nl_tests[i];
364 
365 		if (bt->in_len != -1)
366 			failed += base64_encoding_test(i, bt, 1);
367 		if (bt->out_len != -1)
368 			failed += base64_decoding_test(i, bt, 1);
369 	}
370 
371 	fprintf(stderr, "Starting NO NL tests...\n");
372 
373 	for (i = 0; i < N_NO_NL_TESTS; i++) {
374 		bt = &base64_no_nl_tests[i];
375 
376 		if (bt->in_len != -1)
377 			failed += base64_encoding_test(i, bt, 0);
378 		if (bt->out_len != -1)
379 			failed += base64_decoding_test(i, bt, 0);
380 	}
381 
382 	return failed;
383 }
384