• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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   switch (padding_pattern) {
311     case 0:
312       // The common case of no padding.
313       *out_num_bytes = 3;
314       out[0] = v >> 16;
315       out[1] = v >> 8;
316       out[2] = v;
317       break;
318 
319     case 1:  // xxx=
320       *out_num_bytes = 2;
321       out[0] = v >> 16;
322       out[1] = v >> 8;
323       break;
324 
325     case 3:  // xx==
326       *out_num_bytes = 1;
327       out[0] = v >> 16;
328       break;
329 
330     default:
331       return 0;
332   }
333 
334   return 1;
335 }
336 
EVP_DecodeUpdate(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len,const uint8_t * in,size_t in_len)337 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
338                      const uint8_t *in, size_t in_len) {
339   *out_len = 0;
340 
341   if (ctx->error_encountered) {
342     return -1;
343   }
344 
345   size_t bytes_out = 0, i;
346   for (i = 0; i < in_len; i++) {
347     const char c = in[i];
348     switch (c) {
349       case ' ':
350       case '\t':
351       case '\r':
352       case '\n':
353         continue;
354     }
355 
356     if (ctx->eof_seen) {
357       ctx->error_encountered = 1;
358       return -1;
359     }
360 
361     ctx->data[ctx->data_used++] = c;
362     if (ctx->data_used == 4) {
363       size_t num_bytes_resulting;
364       if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
365         ctx->error_encountered = 1;
366         return -1;
367       }
368 
369       ctx->data_used = 0;
370       bytes_out += num_bytes_resulting;
371       out += num_bytes_resulting;
372 
373       if (num_bytes_resulting < 3) {
374         ctx->eof_seen = 1;
375       }
376     }
377   }
378 
379   if (bytes_out > INT_MAX) {
380     ctx->error_encountered = 1;
381     *out_len = 0;
382     return -1;
383   }
384   *out_len = (int)bytes_out;
385 
386   if (ctx->eof_seen) {
387     return 0;
388   }
389 
390   return 1;
391 }
392 
EVP_DecodeFinal(EVP_ENCODE_CTX * ctx,uint8_t * out,int * out_len)393 int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
394   *out_len = 0;
395   if (ctx->error_encountered || ctx->data_used != 0) {
396     return -1;
397   }
398 
399   return 1;
400 }
401 
EVP_DecodeBase64(uint8_t * out,size_t * out_len,size_t max_out,const uint8_t * in,size_t in_len)402 int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
403                      const uint8_t *in, size_t in_len) {
404   *out_len = 0;
405 
406   if (in_len % 4 != 0) {
407     return 0;
408   }
409 
410   size_t max_len;
411   if (!EVP_DecodedLength(&max_len, in_len) ||
412       max_out < max_len) {
413     return 0;
414   }
415 
416   size_t i, bytes_out = 0;
417   for (i = 0; i < in_len; i += 4) {
418     size_t num_bytes_resulting;
419 
420     if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
421       return 0;
422     }
423 
424     bytes_out += num_bytes_resulting;
425     out += num_bytes_resulting;
426     if (num_bytes_resulting != 3 && i != in_len - 4) {
427       return 0;
428     }
429   }
430 
431   *out_len = bytes_out;
432   return 1;
433 }
434 
EVP_DecodeBlock(uint8_t * dst,const uint8_t * src,size_t src_len)435 int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
436   // Trim spaces and tabs from the beginning of the input.
437   while (src_len > 0) {
438     if (src[0] != ' ' && src[0] != '\t') {
439       break;
440     }
441 
442     src++;
443     src_len--;
444   }
445 
446   // Trim newlines, spaces and tabs from the end of the line.
447   while (src_len > 0) {
448     switch (src[src_len-1]) {
449       case ' ':
450       case '\t':
451       case '\r':
452       case '\n':
453         src_len--;
454         continue;
455     }
456 
457     break;
458   }
459 
460   size_t dst_len;
461   if (!EVP_DecodedLength(&dst_len, src_len) ||
462       dst_len > INT_MAX ||
463       !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
464     return -1;
465   }
466 
467   // EVP_DecodeBlock does not take padding into account, so put the
468   // NULs back in... so the caller can strip them back out.
469   while (dst_len % 3 != 0) {
470     dst[dst_len++] = '\0';
471   }
472   assert(dst_len <= INT_MAX);
473 
474   return (int)dst_len;
475 }
476