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 "../internal.h"
64
65
66 // constant_time_lt_args_8 behaves like |constant_time_lt_8| but takes |uint8_t|
67 // arguments for a slightly simpler implementation.
constant_time_lt_args_8(uint8_t a,uint8_t b)68 static inline uint8_t constant_time_lt_args_8(uint8_t a, uint8_t b) {
69 crypto_word_t aw = a;
70 crypto_word_t bw = b;
71 // |crypto_word_t| is larger than |uint8_t|, so |aw| and |bw| have the same
72 // MSB. |aw| < |bw| iff MSB(|aw| - |bw|) is 1.
73 return constant_time_msb_w(aw - bw);
74 }
75
76 // constant_time_in_range_8 returns |CONSTTIME_TRUE_8| if |min| <= |a| <= |max|
77 // and |CONSTTIME_FALSE_8| otherwise.
constant_time_in_range_8(uint8_t a,uint8_t min,uint8_t max)78 static inline uint8_t constant_time_in_range_8(uint8_t a, uint8_t min,
79 uint8_t max) {
80 a -= min;
81 return constant_time_lt_args_8(a, max - min + 1);
82 }
83
84 // Encoding.
85
conv_bin2ascii(uint8_t a)86 static uint8_t conv_bin2ascii(uint8_t a) {
87 // Since PEM is sometimes used to carry private keys, we encode base64 data
88 // itself in constant-time.
89 a &= 0x3f;
90 uint8_t ret = constant_time_select_8(constant_time_eq_8(a, 62), '+', '/');
91 ret =
92 constant_time_select_8(constant_time_lt_args_8(a, 62), a - 52 + '0', ret);
93 ret =
94 constant_time_select_8(constant_time_lt_args_8(a, 52), a - 26 + 'a', ret);
95 ret = constant_time_select_8(constant_time_lt_args_8(a, 26), a + 'A', ret);
96 return ret;
97 }
98
99 static_assert(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
100 "data length must be a multiple of base64 chunk size");
101
EVP_EncodedLength(size_t * out_len,size_t len)102 int EVP_EncodedLength(size_t *out_len, size_t len) {
103 if (len + 2 < len) {
104 return 0;
105 }
106 len += 2;
107 len /= 3;
108
109 if (((len << 2) >> 2) != len) {
110 return 0;
111 }
112 len <<= 2;
113
114 if (len + 1 < len) {
115 return 0;
116 }
117 len++;
118
119 *out_len = len;
120 return 1;
121 }
122
EVP_ENCODE_CTX_new(void)123 EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) {
124 return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));
125 }
126
EVP_ENCODE_CTX_free(EVP_ENCODE_CTX * ctx)127 void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) {
128 OPENSSL_free(ctx);
129 }
130
EVP_EncodeInit(EVP_ENCODE_CTX * ctx)131 void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
132 OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
133 }
134
EVP_EncodeUpdate(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len,const uint8_t * in,size_t in_len)135 void EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
136 const uint8_t *in, size_t in_len) {
137 size_t total = 0;
138
139 *out_len = 0;
140 if (in_len == 0) {
141 return;
142 }
143
144 assert(ctx->data_used < sizeof(ctx->data));
145
146 if (sizeof(ctx->data) - ctx->data_used > in_len) {
147 OPENSSL_memcpy(&ctx->data[ctx->data_used], in, in_len);
148 ctx->data_used += (unsigned)in_len;
149 return;
150 }
151
152 if (ctx->data_used != 0) {
153 const size_t todo = sizeof(ctx->data) - ctx->data_used;
154 OPENSSL_memcpy(&ctx->data[ctx->data_used], in, todo);
155 in += todo;
156 in_len -= todo;
157
158 size_t encoded = EVP_EncodeBlock(out, ctx->data, sizeof(ctx->data));
159 ctx->data_used = 0;
160
161 out += encoded;
162 *(out++) = '\n';
163 *out = '\0';
164
165 total = encoded + 1;
166 }
167
168 while (in_len >= sizeof(ctx->data)) {
169 size_t encoded = EVP_EncodeBlock(out, in, sizeof(ctx->data));
170 in += sizeof(ctx->data);
171 in_len -= sizeof(ctx->data);
172
173 out += encoded;
174 *(out++) = '\n';
175 *out = '\0';
176
177 if (total + encoded + 1 < total) {
178 *out_len = 0;
179 return;
180 }
181
182 total += encoded + 1;
183 }
184
185 if (in_len != 0) {
186 OPENSSL_memcpy(ctx->data, in, in_len);
187 }
188
189 ctx->data_used = (unsigned)in_len;
190
191 if (total > INT_MAX) {
192 // We cannot signal an error, but we can at least avoid making *out_len
193 // negative.
194 total = 0;
195 }
196 *out_len = (int)total;
197 }
198
EVP_EncodeFinal(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len)199 void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
200 if (ctx->data_used == 0) {
201 *out_len = 0;
202 return;
203 }
204
205 size_t encoded = EVP_EncodeBlock(out, ctx->data, ctx->data_used);
206 out[encoded++] = '\n';
207 out[encoded] = '\0';
208 ctx->data_used = 0;
209
210 // ctx->data_used is bounded by sizeof(ctx->data), so this does not
211 // overflow.
212 assert(encoded <= INT_MAX);
213 *out_len = (int)encoded;
214 }
215
EVP_EncodeBlock(uint8_t * dst,const uint8_t * src,size_t src_len)216 size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
217 uint32_t l;
218 size_t remaining = src_len, ret = 0;
219
220 while (remaining) {
221 if (remaining >= 3) {
222 l = (((uint32_t)src[0]) << 16L) | (((uint32_t)src[1]) << 8L) | src[2];
223 *(dst++) = conv_bin2ascii(l >> 18L);
224 *(dst++) = conv_bin2ascii(l >> 12L);
225 *(dst++) = conv_bin2ascii(l >> 6L);
226 *(dst++) = conv_bin2ascii(l);
227 remaining -= 3;
228 } else {
229 l = ((uint32_t)src[0]) << 16L;
230 if (remaining == 2) {
231 l |= ((uint32_t)src[1] << 8L);
232 }
233
234 *(dst++) = conv_bin2ascii(l >> 18L);
235 *(dst++) = conv_bin2ascii(l >> 12L);
236 *(dst++) = (remaining == 1) ? '=' : conv_bin2ascii(l >> 6L);
237 *(dst++) = '=';
238 remaining = 0;
239 }
240 ret += 4;
241 src += 3;
242 }
243
244 *dst = '\0';
245 return ret;
246 }
247
248
249 // Decoding.
250
EVP_DecodedLength(size_t * out_len,size_t len)251 int EVP_DecodedLength(size_t *out_len, size_t len) {
252 if (len % 4 != 0) {
253 return 0;
254 }
255
256 *out_len = (len / 4) * 3;
257 return 1;
258 }
259
EVP_DecodeInit(EVP_ENCODE_CTX * ctx)260 void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) {
261 OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
262 }
263
base64_ascii_to_bin(uint8_t a)264 static uint8_t base64_ascii_to_bin(uint8_t a) {
265 // Since PEM is sometimes used to carry private keys, we decode base64 data
266 // itself in constant-time.
267 const uint8_t is_upper = constant_time_in_range_8(a, 'A', 'Z');
268 const uint8_t is_lower = constant_time_in_range_8(a, 'a', 'z');
269 const uint8_t is_digit = constant_time_in_range_8(a, '0', '9');
270 const uint8_t is_plus = constant_time_eq_8(a, '+');
271 const uint8_t is_slash = constant_time_eq_8(a, '/');
272 const uint8_t is_equals = constant_time_eq_8(a, '=');
273
274 uint8_t ret = 0;
275 ret |= is_upper & (a - 'A'); // [0,26)
276 ret |= is_lower & (a - 'a' + 26); // [26,52)
277 ret |= is_digit & (a - '0' + 52); // [52,62)
278 ret |= is_plus & 62;
279 ret |= is_slash & 63;
280 // Invalid inputs, 'A', and '=' have all been mapped to zero. Map invalid
281 // inputs to 0xff. Note '=' is padding and handled separately by the caller.
282 const uint8_t is_valid =
283 is_upper | is_lower | is_digit | is_plus | is_slash | is_equals;
284 ret |= ~is_valid;
285 return ret;
286 }
287
288 // base64_decode_quad decodes a single “quad” (i.e. four characters) of base64
289 // data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the
290 // number of bytes written, which will be less than three if the quad ended
291 // with padding. It returns one on success or zero on error.
base64_decode_quad(uint8_t * out,size_t * out_num_bytes,const uint8_t * in)292 static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes,
293 const uint8_t *in) {
294 const uint8_t a = base64_ascii_to_bin(in[0]);
295 const uint8_t b = base64_ascii_to_bin(in[1]);
296 const uint8_t c = base64_ascii_to_bin(in[2]);
297 const uint8_t d = base64_ascii_to_bin(in[3]);
298 if (a == 0xff || b == 0xff || c == 0xff || d == 0xff) {
299 return 0;
300 }
301
302 const uint32_t v = ((uint32_t)a) << 18 | ((uint32_t)b) << 12 |
303 ((uint32_t)c) << 6 | (uint32_t)d;
304
305 const unsigned padding_pattern = (in[0] == '=') << 3 |
306 (in[1] == '=') << 2 |
307 (in[2] == '=') << 1 |
308 (in[3] == '=');
309
310 // In presence of padding, the lowest bits of v are unused. Canonical encoding
311 // (RFC 4648, section 3.5) requires that these bits all be set to zero. Common
312 // PEM parsers accept noncanonical base64, adding to the malleability of the
313 // format. This decoder follows OpenSSL's and Go's PEM parsers and accepts it.
314 switch (padding_pattern) {
315 case 0:
316 // The common case of no padding.
317 *out_num_bytes = 3;
318 out[0] = v >> 16;
319 out[1] = v >> 8;
320 out[2] = v;
321 break;
322
323 case 1: // xxx=
324 *out_num_bytes = 2;
325 out[0] = v >> 16;
326 out[1] = v >> 8;
327 break;
328
329 case 3: // xx==
330 *out_num_bytes = 1;
331 out[0] = v >> 16;
332 break;
333
334 default:
335 return 0;
336 }
337
338 return 1;
339 }
340
EVP_DecodeUpdate(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len,const uint8_t * in,size_t in_len)341 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
342 const uint8_t *in, size_t in_len) {
343 *out_len = 0;
344
345 if (ctx->error_encountered) {
346 return -1;
347 }
348
349 size_t bytes_out = 0, i;
350 for (i = 0; i < in_len; i++) {
351 const char c = in[i];
352 switch (c) {
353 case ' ':
354 case '\t':
355 case '\r':
356 case '\n':
357 continue;
358 }
359
360 if (ctx->eof_seen) {
361 ctx->error_encountered = 1;
362 return -1;
363 }
364
365 ctx->data[ctx->data_used++] = c;
366 if (ctx->data_used == 4) {
367 size_t num_bytes_resulting;
368 if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
369 ctx->error_encountered = 1;
370 return -1;
371 }
372
373 ctx->data_used = 0;
374 bytes_out += num_bytes_resulting;
375 out += num_bytes_resulting;
376
377 if (num_bytes_resulting < 3) {
378 ctx->eof_seen = 1;
379 }
380 }
381 }
382
383 if (bytes_out > INT_MAX) {
384 ctx->error_encountered = 1;
385 *out_len = 0;
386 return -1;
387 }
388 *out_len = (int)bytes_out;
389
390 if (ctx->eof_seen) {
391 return 0;
392 }
393
394 return 1;
395 }
396
EVP_DecodeFinal(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len)397 int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
398 *out_len = 0;
399 if (ctx->error_encountered || ctx->data_used != 0) {
400 return -1;
401 }
402
403 return 1;
404 }
405
EVP_DecodeBase64(uint8_t * out,size_t * out_len,size_t max_out,const uint8_t * in,size_t in_len)406 int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
407 const uint8_t *in, size_t in_len) {
408 *out_len = 0;
409
410 if (in_len % 4 != 0) {
411 return 0;
412 }
413
414 size_t max_len;
415 if (!EVP_DecodedLength(&max_len, in_len) ||
416 max_out < max_len) {
417 return 0;
418 }
419
420 size_t i, bytes_out = 0;
421 for (i = 0; i < in_len; i += 4) {
422 size_t num_bytes_resulting;
423
424 if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
425 return 0;
426 }
427
428 bytes_out += num_bytes_resulting;
429 out += num_bytes_resulting;
430 if (num_bytes_resulting != 3 && i != in_len - 4) {
431 return 0;
432 }
433 }
434
435 *out_len = bytes_out;
436 return 1;
437 }
438
EVP_DecodeBlock(uint8_t * dst,const uint8_t * src,size_t src_len)439 int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
440 // Trim spaces and tabs from the beginning of the input.
441 while (src_len > 0) {
442 if (src[0] != ' ' && src[0] != '\t') {
443 break;
444 }
445
446 src++;
447 src_len--;
448 }
449
450 // Trim newlines, spaces and tabs from the end of the line.
451 while (src_len > 0) {
452 switch (src[src_len-1]) {
453 case ' ':
454 case '\t':
455 case '\r':
456 case '\n':
457 src_len--;
458 continue;
459 }
460
461 break;
462 }
463
464 size_t dst_len;
465 if (!EVP_DecodedLength(&dst_len, src_len) ||
466 dst_len > INT_MAX ||
467 !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
468 return -1;
469 }
470
471 // EVP_DecodeBlock does not take padding into account, so put the
472 // NULs back in... so the caller can strip them back out.
473 while (dst_len % 3 != 0) {
474 dst[dst_len++] = '\0';
475 }
476 assert(dst_len <= INT_MAX);
477
478 return (int)dst_len;
479 }
480