1 /*
2  * Base64 encoding/decoding (RFC1341)
3  * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include <stdint.h>
11 
12 #include "os.h"
13 #include "base64.h"
14 
15 static const char base64_table[65] =
16 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17 static const char base64_url_table[65] =
18 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
19 
20 
base64_gen_encode(const unsigned char * src,size_t len,size_t * out_len,const char * table,int add_pad)21 static char * base64_gen_encode(const unsigned char *src, size_t len,
22 				size_t *out_len, const char *table, int add_pad)
23 {
24 	char *out, *pos;
25 	const unsigned char *end, *in;
26 	size_t olen;
27 	int line_len;
28 
29 	if (len >= SIZE_MAX / 4)
30 		return NULL;
31 	olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
32 	if (add_pad)
33 		olen += olen / 72; /* line feeds */
34 	olen++; /* nul termination */
35 	if (olen < len)
36 		return NULL; /* integer overflow */
37 	out = os_malloc(olen);
38 	if (out == NULL)
39 		return NULL;
40 
41 	end = src + len;
42 	in = src;
43 	pos = out;
44 	line_len = 0;
45 	while (end - in >= 3) {
46 		*pos++ = table[(in[0] >> 2) & 0x3f];
47 		*pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
48 		*pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
49 		*pos++ = table[in[2] & 0x3f];
50 		in += 3;
51 		line_len += 4;
52 		if (add_pad && line_len >= 72) {
53 			*pos++ = '\n';
54 			line_len = 0;
55 		}
56 	}
57 
58 	if (end - in) {
59 		*pos++ = table[(in[0] >> 2) & 0x3f];
60 		if (end - in == 1) {
61 			*pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
62 			if (add_pad)
63 				*pos++ = '=';
64 		} else {
65 			*pos++ = table[(((in[0] & 0x03) << 4) |
66 					(in[1] >> 4)) & 0x3f];
67 			*pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
68 		}
69 		if (add_pad)
70 			*pos++ = '=';
71 		line_len += 4;
72 	}
73 
74 	if (add_pad && line_len)
75 		*pos++ = '\n';
76 
77 	*pos = '\0';
78 	if (out_len)
79 		*out_len = pos - out;
80 	return out;
81 }
82 
83 
base64_gen_decode(const char * src,size_t len,size_t * out_len,const char * table)84 static unsigned char * base64_gen_decode(const char *src, size_t len,
85 					 size_t *out_len, const char *table)
86 {
87 	unsigned char dtable[256], *out, *pos, block[4], tmp;
88 	size_t i, count, olen;
89 	int pad = 0;
90 	size_t extra_pad;
91 
92 	os_memset(dtable, 0x80, 256);
93 	for (i = 0; i < sizeof(base64_table) - 1; i++)
94 		dtable[(unsigned char) table[i]] = (unsigned char) i;
95 	dtable['='] = 0;
96 
97 	count = 0;
98 	for (i = 0; i < len; i++) {
99 		if (dtable[(unsigned char) src[i]] != 0x80)
100 			count++;
101 	}
102 
103 	if (count == 0)
104 		return NULL;
105 	extra_pad = (4 - count % 4) % 4;
106 
107 	olen = (count + extra_pad) / 4 * 3;
108 	pos = out = os_malloc(olen);
109 	if (out == NULL)
110 		return NULL;
111 
112 	count = 0;
113 	for (i = 0; i < len + extra_pad; i++) {
114 		unsigned char val;
115 
116 		if (i >= len)
117 			val = '=';
118 		else
119 			val = src[i];
120 		tmp = dtable[val];
121 		if (tmp == 0x80)
122 			continue;
123 
124 		if (val == '=')
125 			pad++;
126 		block[count] = tmp;
127 		count++;
128 		if (count == 4) {
129 			*pos++ = (block[0] << 2) | (block[1] >> 4);
130 			*pos++ = (block[1] << 4) | (block[2] >> 2);
131 			*pos++ = (block[2] << 6) | block[3];
132 			count = 0;
133 			if (pad) {
134 				if (pad == 1)
135 					pos--;
136 				else if (pad == 2)
137 					pos -= 2;
138 				else {
139 					/* Invalid padding */
140 					os_free(out);
141 					return NULL;
142 				}
143 				break;
144 			}
145 		}
146 	}
147 
148 	*out_len = pos - out;
149 	return out;
150 }
151 
152 
153 /**
154  * base64_encode - Base64 encode
155  * @src: Data to be encoded
156  * @len: Length of the data to be encoded
157  * @out_len: Pointer to output length variable, or %NULL if not used
158  * Returns: Allocated buffer of out_len bytes of encoded data,
159  * or %NULL on failure
160  *
161  * Caller is responsible for freeing the returned buffer. Returned buffer is
162  * nul terminated to make it easier to use as a C string. The nul terminator is
163  * not included in out_len.
164  */
base64_encode(const void * src,size_t len,size_t * out_len)165 char * base64_encode(const void *src, size_t len, size_t *out_len)
166 {
167 	return base64_gen_encode(src, len, out_len, base64_table, 1);
168 }
169 
170 
base64_url_encode(const void * src,size_t len,size_t * out_len)171 char * base64_url_encode(const void *src, size_t len, size_t *out_len)
172 {
173 	return base64_gen_encode(src, len, out_len, base64_url_table, 0);
174 }
175 
176 
177 /**
178  * base64_decode - Base64 decode
179  * @src: Data to be decoded
180  * @len: Length of the data to be decoded
181  * @out_len: Pointer to output length variable
182  * Returns: Allocated buffer of out_len bytes of decoded data,
183  * or %NULL on failure
184  *
185  * Caller is responsible for freeing the returned buffer.
186  */
base64_decode(const char * src,size_t len,size_t * out_len)187 unsigned char * base64_decode(const char *src, size_t len, size_t *out_len)
188 {
189 	return base64_gen_decode(src, len, out_len, base64_table);
190 }
191 
192 
base64_url_decode(const char * src,size_t len,size_t * out_len)193 unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
194 {
195 	return base64_gen_decode(src, len, out_len, base64_url_table);
196 }
197