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 <assert.h>
58 #include <errno.h>
59 #include <stdio.h>
60 #include <string.h>
61
62 #include <openssl/base64.h>
63 #include <openssl/bio.h>
64 #include <openssl/buffer.h>
65 #include <openssl/evp.h>
66 #include <openssl/mem.h>
67
68 #include "../../crypto/internal.h"
69
70
71 #define B64_BLOCK_SIZE 1024
72 #define B64_BLOCK_SIZE2 768
73 #define B64_NONE 0
74 #define B64_ENCODE 1
75 #define B64_DECODE 2
76 #define EVP_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80)
77
78 typedef struct b64_struct {
79 int buf_len;
80 int buf_off;
81 int tmp_len; // used to find the start when decoding
82 int tmp_nl; // If true, scan until '\n'
83 int encode;
84 int start; // have we started decoding yet?
85 int cont; // <= 0 when finished
86 EVP_ENCODE_CTX base64;
87 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
88 char tmp[B64_BLOCK_SIZE];
89 } BIO_B64_CTX;
90
b64_new(BIO * bio)91 static int b64_new(BIO *bio) {
92 BIO_B64_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
93 if (ctx == NULL) {
94 return 0;
95 }
96
97 ctx->cont = 1;
98 ctx->start = 1;
99
100 bio->init = 1;
101 bio->ptr = (char *)ctx;
102 return 1;
103 }
104
b64_free(BIO * bio)105 static int b64_free(BIO *bio) {
106 if (bio == NULL) {
107 return 0;
108 }
109 OPENSSL_free(bio->ptr);
110 bio->ptr = NULL;
111 bio->init = 0;
112 bio->flags = 0;
113 return 1;
114 }
115
b64_read(BIO * b,char * out,int outl)116 static int b64_read(BIO *b, char *out, int outl) {
117 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
118 BIO_B64_CTX *ctx;
119 uint8_t *p, *q;
120
121 if (out == NULL) {
122 return 0;
123 }
124 ctx = (BIO_B64_CTX *) b->ptr;
125
126 if (ctx == NULL || b->next_bio == NULL) {
127 return 0;
128 }
129
130 BIO_clear_retry_flags(b);
131
132 if (ctx->encode != B64_DECODE) {
133 ctx->encode = B64_DECODE;
134 ctx->buf_len = 0;
135 ctx->buf_off = 0;
136 ctx->tmp_len = 0;
137 EVP_DecodeInit(&ctx->base64);
138 }
139
140 // First check if there are bytes decoded/encoded
141 if (ctx->buf_len > 0) {
142 assert(ctx->buf_len >= ctx->buf_off);
143 i = ctx->buf_len - ctx->buf_off;
144 if (i > outl) {
145 i = outl;
146 }
147 assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
148 OPENSSL_memcpy(out, &ctx->buf[ctx->buf_off], i);
149 ret = i;
150 out += i;
151 outl -= i;
152 ctx->buf_off += i;
153 if (ctx->buf_len == ctx->buf_off) {
154 ctx->buf_len = 0;
155 ctx->buf_off = 0;
156 }
157 }
158
159 // At this point, we have room of outl bytes and an empty buffer, so we
160 // should read in some more.
161
162 ret_code = 0;
163 while (outl > 0) {
164 if (ctx->cont <= 0) {
165 break;
166 }
167
168 i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
169 B64_BLOCK_SIZE - ctx->tmp_len);
170
171 if (i <= 0) {
172 ret_code = i;
173
174 // Should we continue next time we are called?
175 if (!BIO_should_retry(b->next_bio)) {
176 ctx->cont = i;
177 // If buffer empty break
178 if (ctx->tmp_len == 0) {
179 break;
180 } else {
181 // Fall through and process what we have
182 i = 0;
183 }
184 } else {
185 // else we retry and add more data to buffer
186 break;
187 }
188 }
189 i += ctx->tmp_len;
190 ctx->tmp_len = i;
191
192 // We need to scan, a line at a time until we have a valid line if we are
193 // starting.
194 if (ctx->start && (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL))) {
195 // ctx->start = 1;
196 ctx->tmp_len = 0;
197 } else if (ctx->start) {
198 q = p = (uint8_t *)ctx->tmp;
199 num = 0;
200 for (j = 0; j < i; j++) {
201 if (*(q++) != '\n') {
202 continue;
203 }
204
205 // due to a previous very long line, we need to keep on scanning for a
206 // '\n' before we even start looking for base64 encoded stuff.
207 if (ctx->tmp_nl) {
208 p = q;
209 ctx->tmp_nl = 0;
210 continue;
211 }
212
213 k = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &num, p,
214 q - p);
215
216 if (k <= 0 && num == 0 && ctx->start) {
217 EVP_DecodeInit(&ctx->base64);
218 } else {
219 if (p != (uint8_t *)&(ctx->tmp[0])) {
220 i -= (p - (uint8_t *)&(ctx->tmp[0]));
221 for (x = 0; x < i; x++) {
222 ctx->tmp[x] = p[x];
223 }
224 }
225 EVP_DecodeInit(&ctx->base64);
226 ctx->start = 0;
227 break;
228 }
229 p = q;
230 }
231
232 // we fell off the end without starting
233 if (j == i && num == 0) {
234 // Is this is one long chunk?, if so, keep on reading until a new
235 // line.
236 if (p == (uint8_t *)&(ctx->tmp[0])) {
237 // Check buffer full
238 if (i == B64_BLOCK_SIZE) {
239 ctx->tmp_nl = 1;
240 ctx->tmp_len = 0;
241 }
242 } else if (p != q) { // finished on a '\n'
243 n = q - p;
244 for (ii = 0; ii < n; ii++) {
245 ctx->tmp[ii] = p[ii];
246 }
247 ctx->tmp_len = n;
248 }
249 // else finished on a '\n'
250 continue;
251 } else {
252 ctx->tmp_len = 0;
253 }
254 } else if (i < B64_BLOCK_SIZE && ctx->cont > 0) {
255 // If buffer isn't full and we can retry then restart to read in more
256 // data.
257 continue;
258 }
259
260 if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
261 int z, jj;
262
263 jj = i & ~3; // process per 4
264 z = EVP_DecodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp, jj);
265 if (jj > 2) {
266 if (ctx->tmp[jj - 1] == '=') {
267 z--;
268 if (ctx->tmp[jj - 2] == '=') {
269 z--;
270 }
271 }
272 }
273 // z is now number of output bytes and jj is the number consumed.
274 if (jj != i) {
275 OPENSSL_memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
276 ctx->tmp_len = i - jj;
277 }
278 ctx->buf_len = 0;
279 if (z > 0) {
280 ctx->buf_len = z;
281 }
282 i = z;
283 } else {
284 i = EVP_DecodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf,
285 &ctx->buf_len, (uint8_t *)ctx->tmp, i);
286 ctx->tmp_len = 0;
287 }
288 ctx->buf_off = 0;
289 if (i < 0) {
290 ret_code = 0;
291 ctx->buf_len = 0;
292 break;
293 }
294
295 if (ctx->buf_len <= outl) {
296 i = ctx->buf_len;
297 } else {
298 i = outl;
299 }
300
301 OPENSSL_memcpy(out, ctx->buf, i);
302 ret += i;
303 ctx->buf_off = i;
304 if (ctx->buf_off == ctx->buf_len) {
305 ctx->buf_len = 0;
306 ctx->buf_off = 0;
307 }
308 outl -= i;
309 out += i;
310 }
311
312 BIO_copy_next_retry(b);
313 return ret == 0 ? ret_code : ret;
314 }
315
b64_write(BIO * b,const char * in,int inl)316 static int b64_write(BIO *b, const char *in, int inl) {
317 int ret = 0, n, i;
318 BIO_B64_CTX *ctx;
319
320 ctx = (BIO_B64_CTX *)b->ptr;
321 BIO_clear_retry_flags(b);
322
323 if (ctx->encode != B64_ENCODE) {
324 ctx->encode = B64_ENCODE;
325 ctx->buf_len = 0;
326 ctx->buf_off = 0;
327 ctx->tmp_len = 0;
328 EVP_EncodeInit(&(ctx->base64));
329 }
330
331 assert(ctx->buf_off < (int)sizeof(ctx->buf));
332 assert(ctx->buf_len <= (int)sizeof(ctx->buf));
333 assert(ctx->buf_len >= ctx->buf_off);
334
335 n = ctx->buf_len - ctx->buf_off;
336 while (n > 0) {
337 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
338 if (i <= 0) {
339 BIO_copy_next_retry(b);
340 return i;
341 }
342 assert(i <= n);
343 ctx->buf_off += i;
344 assert(ctx->buf_off <= (int)sizeof(ctx->buf));
345 assert(ctx->buf_len >= ctx->buf_off);
346 n -= i;
347 }
348
349 // at this point all pending data has been written.
350 ctx->buf_off = 0;
351 ctx->buf_len = 0;
352
353 if (in == NULL || inl <= 0) {
354 return 0;
355 }
356
357 while (inl > 0) {
358 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
359
360 if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
361 if (ctx->tmp_len > 0) {
362 assert(ctx->tmp_len <= 3);
363 n = 3 - ctx->tmp_len;
364 // There's a theoretical possibility of this.
365 if (n > inl) {
366 n = inl;
367 }
368 OPENSSL_memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
369 ctx->tmp_len += n;
370 ret += n;
371 if (ctx->tmp_len < 3) {
372 break;
373 }
374 ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf, (uint8_t *)ctx->tmp,
375 ctx->tmp_len);
376 assert(ctx->buf_len <= (int)sizeof(ctx->buf));
377 assert(ctx->buf_len >= ctx->buf_off);
378
379 // Since we're now done using the temporary buffer, the length should
380 // be zeroed.
381 ctx->tmp_len = 0;
382 } else {
383 if (n < 3) {
384 OPENSSL_memcpy(ctx->tmp, in, n);
385 ctx->tmp_len = n;
386 ret += n;
387 break;
388 }
389 n -= n % 3;
390 ctx->buf_len =
391 EVP_EncodeBlock((uint8_t *)ctx->buf, (const uint8_t *)in, n);
392 assert(ctx->buf_len <= (int)sizeof(ctx->buf));
393 assert(ctx->buf_len >= ctx->buf_off);
394 ret += n;
395 }
396 } else {
397 EVP_EncodeUpdate(&(ctx->base64), (uint8_t *)ctx->buf, &ctx->buf_len,
398 (uint8_t *)in, n);
399 assert(ctx->buf_len <= (int)sizeof(ctx->buf));
400 assert(ctx->buf_len >= ctx->buf_off);
401 ret += n;
402 }
403 inl -= n;
404 in += n;
405
406 ctx->buf_off = 0;
407 n = ctx->buf_len;
408
409 while (n > 0) {
410 i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
411 if (i <= 0) {
412 BIO_copy_next_retry(b);
413 return ret == 0 ? i : ret;
414 }
415 assert(i <= n);
416 n -= i;
417 ctx->buf_off += i;
418 assert(ctx->buf_off <= (int)sizeof(ctx->buf));
419 assert(ctx->buf_len >= ctx->buf_off);
420 }
421 ctx->buf_len = 0;
422 ctx->buf_off = 0;
423 }
424 return ret;
425 }
426
b64_ctrl(BIO * b,int cmd,long num,void * ptr)427 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) {
428 BIO_B64_CTX *ctx;
429 long ret = 1;
430 int i;
431
432 ctx = (BIO_B64_CTX *)b->ptr;
433
434 switch (cmd) {
435 case BIO_CTRL_RESET:
436 ctx->cont = 1;
437 ctx->start = 1;
438 ctx->encode = B64_NONE;
439 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
440 break;
441
442 case BIO_CTRL_EOF: // More to read
443 if (ctx->cont <= 0) {
444 ret = 1;
445 } else {
446 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
447 }
448 break;
449
450 case BIO_CTRL_WPENDING: // More to write in buffer
451 assert(ctx->buf_len >= ctx->buf_off);
452 ret = ctx->buf_len - ctx->buf_off;
453 if ((ret == 0) && (ctx->encode != B64_NONE) && (ctx->base64.data_used != 0)) {
454 ret = 1;
455 } else if (ret <= 0) {
456 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
457 }
458 break;
459
460 case BIO_CTRL_PENDING: // More to read in buffer
461 assert(ctx->buf_len >= ctx->buf_off);
462 ret = ctx->buf_len - ctx->buf_off;
463 if (ret <= 0) {
464 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
465 }
466 break;
467
468 case BIO_CTRL_FLUSH:
469 // do a final write
470 again:
471 while (ctx->buf_len != ctx->buf_off) {
472 i = b64_write(b, NULL, 0);
473 if (i < 0) {
474 return i;
475 }
476 }
477 if (BIO_test_flags(b, BIO_FLAGS_BASE64_NO_NL)) {
478 if (ctx->tmp_len != 0) {
479 ctx->buf_len = EVP_EncodeBlock((uint8_t *)ctx->buf,
480 (uint8_t *)ctx->tmp, ctx->tmp_len);
481 ctx->buf_off = 0;
482 ctx->tmp_len = 0;
483 goto again;
484 }
485 } else if (ctx->encode != B64_NONE && ctx->base64.data_used != 0) {
486 ctx->buf_off = 0;
487 EVP_EncodeFinal(&(ctx->base64), (uint8_t *)ctx->buf, &(ctx->buf_len));
488 // push out the bytes
489 goto again;
490 }
491 // Finally flush the underlying BIO
492 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
493 break;
494
495 case BIO_C_DO_STATE_MACHINE:
496 BIO_clear_retry_flags(b);
497 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
498 BIO_copy_next_retry(b);
499 break;
500
501 case BIO_CTRL_INFO:
502 case BIO_CTRL_GET:
503 case BIO_CTRL_SET:
504 default:
505 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
506 break;
507 }
508 return ret;
509 }
510
b64_callback_ctrl(BIO * b,int cmd,bio_info_cb fp)511 static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb fp) {
512 if (b->next_bio == NULL) {
513 return 0;
514 }
515 return BIO_callback_ctrl(b->next_bio, cmd, fp);
516 }
517
518 static const BIO_METHOD b64_method = {
519 BIO_TYPE_BASE64, "base64 encoding", b64_write, b64_read, NULL /* puts */,
520 NULL /* gets */, b64_ctrl, b64_new, b64_free, b64_callback_ctrl,
521 };
522
BIO_f_base64(void)523 const BIO_METHOD *BIO_f_base64(void) { return &b64_method; }
524