Debian Codegraph @

external/boringssl/crypto/base64/base64.c

raw
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young ([email protected]).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson ([email protected]).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young ([email protected])"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson ([email protected])"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/base64.h>
58
59 #include <assert.h>
60 #include <limits.h>
61 #include <string.h>
62
63 #include <openssl/type_check.h>
64
65
66 /* Encoding. */
67
68 static const unsigned char data_bin2ascii[65] =
69 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70
71 #define conv_bin2ascii(a) (data_bin2ascii[(a) & 0x3f])
72
73 OPENSSL_COMPILE_ASSERT(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
74 data_length_must_be_multiple_of_base64_chunk_size);
75
76 int EVP_EncodedLength(size_t *out_len, size_t len) {
77 if (len + 2 < len) {
78 return 0;
79 }
80 len += 2;
81 len /= 3;
82
83 if (((len << 2) >> 2) != len) {
84 return 0;
85 }
86 len <<= 2;
87
88 if (len + 1 < len) {
89 return 0;
90 }
91 len++;
92
93 *out_len = len;
94 return 1;
95 }
96
97 void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
98 memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
99 }
100
101 void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
102 const uint8_t *in, size_t in_len) {
103 size_t total = 0;
104
105 *out_len = 0;
106 if (in_len == 0) {
107 return;
108 }
109
110 assert(ctx->data_used < sizeof(ctx->data));
111
112 if (sizeof(ctx->data) - ctx->data_used > in_len) {
113 memcpy(&ctx->data[ctx->data_used], in, in_len);
114 ctx->data_used += in_len;
115 return;
116 }
117
118 if (ctx->data_used != 0) {
119 const size_t todo = sizeof(ctx->data) - ctx->data_used;
120 memcpy(&ctx->data[ctx->data_used], in, todo);
121 in += todo;
122 in_len -= todo;
123
124 size_t encoded = EVP_EncodeBlock(out, ctx->data, sizeof(ctx->data));
125 ctx->data_used = 0;
126
127 out += encoded;
128 *(out++) = '\n';
129 *out = '\0';
130
131 total = encoded + 1;
132 }
133
134 while (in_len >= sizeof(ctx->data)) {
135 size_t encoded = EVP_EncodeBlock(out, in, sizeof(ctx->data));
136 in += sizeof(ctx->data);
137 in_len -= sizeof(ctx->data);
138
139 out += encoded;
140 *(out++) = '\n';
141 *out = '\0';
142
143 if (total + encoded + 1 < total) {
144 *out_len = 0;
145 return;
146 }
147
148 total += encoded + 1;
149 }
150
151 if (in_len != 0) {
152 memcpy(ctx->data, in, in_len);
153 }
154
155 ctx->data_used = in_len;
156
157 if (total > INT_MAX) {
158 /* We cannot signal an error, but we can at least avoid making *out_len
159 * negative. */
160 total = 0;
161 }
162 *out_len = total;
163 }
164
165 void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
166 if (ctx->data_used == 0) {
167 *out_len = 0;
168 return;
169 }
170
171 size_t encoded = EVP_EncodeBlock(out, ctx->data, ctx->data_used);
172 out[encoded++] = '\n';
173 out[encoded] = '\0';
174 ctx->data_used = 0;
175 *out_len = encoded;
176 }
177
178 size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
179 uint32_t l;
180 size_t remaining = src_len, ret = 0;
181
182 while (remaining) {
183 if (remaining >= 3) {
184 l = (((uint32_t)src[0]) << 16L) | (((uint32_t)src[1]) << 8L) | src[2];
185 *(dst++) = conv_bin2ascii(l >> 18L);
186 *(dst++) = conv_bin2ascii(l >> 12L);
187 *(dst++) = conv_bin2ascii(l >> 6L);
188 *(dst++) = conv_bin2ascii(l);
189 remaining -= 3;
190 } else {
191 l = ((uint32_t)src[0]) << 16L;
192 if (remaining == 2) {
193 l |= ((uint32_t)src[1] << 8L);
194 }
195
196 *(dst++) = conv_bin2ascii(l >> 18L);
197 *(dst++) = conv_bin2ascii(l >> 12L);
198 *(dst++) = (remaining == 1) ? '=' : conv_bin2ascii(l >> 6L);
199 *(dst++) = '=';
200 remaining = 0;
201 }
202 ret += 4;
203 src += 3;
204 }
205
206 *dst = '\0';
207 return ret;
208 }
209
210
211 /* Decoding. */
212
213 int EVP_DecodedLength(size_t *out_len, size_t len) {
214 if (len % 4 != 0) {
215 return 0;
216 }
217
218 *out_len = (len / 4) * 3;
219 return 1;
220 }
221
222 void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) {
223 memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
224 }
225
226 /* kBase64ASCIIToBinData maps characters (c < 128) to their base64 value, or
227 * else 0xff if they are invalid. As a special case, the padding character
228 * ('=') is mapped to zero. */
229 static const uint8_t kBase64ASCIIToBinData[128] = {
230 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff,
231 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
232 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff,
233 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
234 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff,
235 0xff, 0x00, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
236 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
237 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
238 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24,
239 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
240 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
241 };
242
243 static uint8_t base64_ascii_to_bin(uint8_t a) {
244 if (a >= 128) {
245 return 0xFF;
246 }
247
248 return kBase64ASCIIToBinData[a];
249 }
250
251 /* base64_decode_quad decodes a single “quad” (i.e. four characters) of base64
252 * data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the
253 * number of bytes written, which will be less than three if the quad ended
254 * with padding. It returns one on success or zero on error. */
255 static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes,
256 const uint8_t *in) {
257 const uint8_t a = base64_ascii_to_bin(in[0]);
258 const uint8_t b = base64_ascii_to_bin(in[1]);
259 const uint8_t c = base64_ascii_to_bin(in[2]);
260 const uint8_t d = base64_ascii_to_bin(in[3]);
261 if (a == 0xff || b == 0xff || c == 0xff || d == 0xff) {
262 return 0;
263 }
264
265 const uint32_t v = ((uint32_t)a) << 18 | ((uint32_t)b) << 12 |
266 ((uint32_t)c) << 6 | (uint32_t)d;
267
268 const unsigned padding_pattern = (in[0] == '=') << 3 |
269 (in[1] == '=') << 2 |
270 (in[2] == '=') << 1 |
271 (in[3] == '=');
272
273 switch (padding_pattern) {
274 case 0:
275 /* The common case of no padding. */
276 *out_num_bytes = 3;
277 out[0] = v >> 16;
278 out[1] = v >> 8;
279 out[2] = v;
280 break;
281
282 case 1: /* xxx= */
283 *out_num_bytes = 2;
284 out[0] = v >> 16;
285 out[1] = v >> 8;
286 break;
287
288 case 3: /* xx== */
289 *out_num_bytes = 1;
290 out[0] = v >> 16;
291 break;
292
293 default:
294 return 0;
295 }
296
297 return 1;
298 }
299
300 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
301 const uint8_t *in, size_t in_len) {
302 *out_len = 0;
303
304 if (ctx->error_encountered) {
305 return -1;
306 }
307
308 size_t bytes_out = 0, i;
309 for (i = 0; i < in_len; i++) {
310 const char c = in[i];
311 switch (c) {
312 case ' ':
313 case '\t':
314 case '\r':
315 case '\n':
316 continue;
317 }
318
319 if (base64_ascii_to_bin(c) == 0xff || ctx->eof_seen) {
320 ctx->error_encountered = 1;
321 return -1;
322 }
323
324 ctx->data[ctx->data_used++] = c;
325 if (ctx->data_used == 4) {
326 size_t num_bytes_resulting;
327 if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
328 ctx->error_encountered = 1;
329 return -1;
330 }
331
332 ctx->data_used = 0;
333 bytes_out += num_bytes_resulting;
334 out += num_bytes_resulting;
335
336 if (num_bytes_resulting < 3) {
337 ctx->eof_seen = 1;
338 }
339 }
340 }
341
342 if (bytes_out > INT_MAX) {
343 ctx->error_encountered = 1;
344 *out_len = 0;
345 return -1;
346 }
347 *out_len = bytes_out;
348
349 if (ctx->eof_seen) {
350 return 0;
351 }
352
353 return 1;
354 }
355
356 int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
357 *out_len = 0;
358 if (ctx->error_encountered || ctx->data_used != 0) {
359 return -1;
360 }
361
362 return 1;
363 }
364
365 int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
366 const uint8_t *in, size_t in_len) {
367 *out_len = 0;
368
369 if (in_len % 4 != 0) {
370 return 0;
371 }
372
373 size_t max_len;
374 if (!EVP_DecodedLength(&max_len, in_len) ||
375 max_out < max_len) {
376 return 0;
377 }
378
379 size_t i, bytes_out = 0;
380 for (i = 0; i < in_len; i += 4) {
381 size_t num_bytes_resulting;
382
383 if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
384 return 0;
385 }
386
387 bytes_out += num_bytes_resulting;
388 out += num_bytes_resulting;
389 if (num_bytes_resulting != 3 && i != in_len - 4) {
390 return 0;
391 }
392 }
393
394 *out_len = bytes_out;
395 return 1;
396 }
397
398 int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
399 /* Trim spaces and tabs from the beginning of the input. */
400 while (src_len > 0) {
401 if (src[0] != ' ' && src[0] != '\t') {
402 break;
403 }
404
405 src++;
406 src_len--;
407 }
408
409 /* Trim newlines, spaces and tabs from the end of the line. */
410 while (src_len > 0) {
411 switch (src[src_len-1]) {
412 case ' ':
413 case '\t':
414 case '\r':
415 case '\n':
416 src_len--;
417 continue;
418 }
419
420 break;
421 }
422
423 size_t dst_len;
424 if (!EVP_DecodedLength(&dst_len, src_len) ||
425 dst_len > INT_MAX ||
426 !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
427 return -1;
428 }
429
430 /* EVP_DecodeBlock does not take padding into account, so put the
431 * NULs back in... so the caller can strip them back out. */
432 while (dst_len % 3 != 0) {
433 dst[dst_len++] = '\0';
434 }
435 assert(dst_len <= INT_MAX);
436
437 return dst_len;
438 }