2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
17 static int b64_write(BIO *h, const char *buf, int num);
18 static int b64_read(BIO *h, char *buf, int size);
19 static int b64_puts(BIO *h, const char *str);
20 static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
21 static int b64_new(BIO *h);
22 static int b64_free(BIO *data);
23 static long b64_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
24 #define B64_BLOCK_SIZE 1024
25 #define B64_BLOCK_SIZE2 768
30 typedef struct b64_struct {
32 * BIO *bio; moved to the BIO structure
36 int tmp_len; /* used to find the start when decoding */
37 int tmp_nl; /* If true, scan until '\n' */
39 int start; /* have we started decoding yet? */
40 int cont; /* <= 0 when finished */
41 EVP_ENCODE_CTX *base64;
42 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
43 char tmp[B64_BLOCK_SIZE];
46 static const BIO_METHOD methods_b64 = {
49 /* TODO: Convert to new style write function */
52 /* TODO: Convert to new style read function */
64 const BIO_METHOD *BIO_f_base64(void)
69 static int b64_new(BIO *bi)
73 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
74 EVPerr(EVP_F_B64_NEW, ERR_R_MALLOC_FAILURE);
80 ctx->base64 = EVP_ENCODE_CTX_new();
81 if (ctx->base64 == NULL) {
86 BIO_set_data(bi, ctx);
92 static int b64_free(BIO *a)
98 ctx = BIO_get_data(a);
102 EVP_ENCODE_CTX_free(ctx->base64);
104 BIO_set_data(a, NULL);
110 static int b64_read(BIO *b, char *out, int outl)
112 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
114 unsigned char *p, *q;
119 ctx = (BIO_B64_CTX *)BIO_get_data(b);
122 if ((ctx == NULL) || (next == NULL))
125 BIO_clear_retry_flags(b);
127 if (ctx->encode != B64_DECODE) {
128 ctx->encode = B64_DECODE;
132 EVP_DecodeInit(ctx->base64);
135 /* First check if there are bytes decoded/encoded */
136 if (ctx->buf_len > 0) {
137 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
138 i = ctx->buf_len - ctx->buf_off;
141 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
142 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
147 if (ctx->buf_len == ctx->buf_off) {
154 * At this point, we have room of outl bytes and an empty buffer, so we
155 * should read in some more.
163 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
164 B64_BLOCK_SIZE - ctx->tmp_len);
169 /* Should we continue next time we are called? */
170 if (!BIO_should_retry(next)) {
172 /* If buffer empty break */
173 if (ctx->tmp_len == 0)
175 /* Fall through and process what we have */
179 /* else we retry and add more data to buffer */
187 * We need to scan, a line at a time until we have a valid line if we
190 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
193 } else if (ctx->start) {
194 q = p = (unsigned char *)ctx->tmp;
196 for (j = 0; j < i; j++) {
201 * due to a previous very long line, we need to keep on
202 * scanning for a '\n' before we even start looking for
203 * base64 encoded stuff.
211 k = EVP_DecodeUpdate(ctx->base64,
212 (unsigned char *)ctx->buf,
214 if ((k <= 0) && (num == 0) && (ctx->start))
215 EVP_DecodeInit(ctx->base64);
217 if (p != (unsigned char *)
219 i -= (p - (unsigned char *)
221 for (x = 0; x < i; x++)
224 EVP_DecodeInit(ctx->base64);
231 /* we fell off the end without starting */
232 if ((j == i) && (num == 0)) {
234 * Is this is one long chunk?, if so, keep on reading until a
237 if (p == (unsigned char *)&(ctx->tmp[0])) {
238 /* Check buffer full */
239 if (i == B64_BLOCK_SIZE) {
243 } else if (p != q) { /* finished on a '\n' */
245 for (ii = 0; ii < n; ii++)
246 ctx->tmp[ii] = p[ii];
249 /* else finished on a '\n' */
254 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
256 * If buffer isn't full and we can retry then restart to read in
262 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
265 jj = i & ~3; /* process per 4 */
266 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
267 (unsigned char *)ctx->tmp, jj);
269 if (ctx->tmp[jj - 1] == '=') {
271 if (ctx->tmp[jj - 2] == '=')
276 * z is now number of output bytes and jj is the number consumed
279 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
280 ctx->tmp_len = i - jj;
288 i = EVP_DecodeUpdate(ctx->base64,
289 (unsigned char *)ctx->buf, &ctx->buf_len,
290 (unsigned char *)ctx->tmp, i);
294 * If eof or an error was signalled, then the condition
295 * 'ctx->cont <= 0' will prevent b64_read() from reading
296 * more data on subsequent calls. This assignment was
297 * deleted accidentally in commit 5562cfaca4f3.
308 if (ctx->buf_len <= outl)
313 memcpy(out, ctx->buf, i);
316 if (ctx->buf_off == ctx->buf_len) {
323 /* BIO_clear_retry_flags(b); */
324 BIO_copy_next_retry(b);
325 return ((ret == 0) ? ret_code : ret);
328 static int b64_write(BIO *b, const char *in, int inl)
336 ctx = (BIO_B64_CTX *)BIO_get_data(b);
338 if ((ctx == NULL) || (next == NULL))
341 BIO_clear_retry_flags(b);
343 if (ctx->encode != B64_ENCODE) {
344 ctx->encode = B64_ENCODE;
348 EVP_EncodeInit(ctx->base64);
351 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
352 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
353 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
354 n = ctx->buf_len - ctx->buf_off;
356 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
358 BIO_copy_next_retry(b);
361 OPENSSL_assert(i <= n);
363 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
364 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
367 /* at this point all pending data has been written */
371 if ((in == NULL) || (inl <= 0))
375 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
377 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
378 if (ctx->tmp_len > 0) {
379 OPENSSL_assert(ctx->tmp_len <= 3);
380 n = 3 - ctx->tmp_len;
382 * There's a theoretical possibility for this
386 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
389 if (ctx->tmp_len < 3)
392 EVP_EncodeBlock((unsigned char *)ctx->buf,
393 (unsigned char *)ctx->tmp, ctx->tmp_len);
394 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
395 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
397 * Since we're now done using the temporary buffer, the
398 * length should be 0'd
403 memcpy(ctx->tmp, in, n);
410 EVP_EncodeBlock((unsigned char *)ctx->buf,
411 (const unsigned char *)in, n);
412 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
413 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
417 if (!EVP_EncodeUpdate(ctx->base64,
418 (unsigned char *)ctx->buf, &ctx->buf_len,
419 (unsigned char *)in, n))
420 return ((ret == 0) ? -1 : ret);
421 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
422 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
431 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
433 BIO_copy_next_retry(b);
434 return ((ret == 0) ? i : ret);
436 OPENSSL_assert(i <= n);
439 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
440 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
448 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
455 ctx = (BIO_B64_CTX *)BIO_get_data(b);
457 if ((ctx == NULL) || (next == NULL))
464 ctx->encode = B64_NONE;
465 ret = BIO_ctrl(next, cmd, num, ptr);
467 case BIO_CTRL_EOF: /* More to read */
471 ret = BIO_ctrl(next, cmd, num, ptr);
473 case BIO_CTRL_WPENDING: /* More to write in buffer */
474 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
475 ret = ctx->buf_len - ctx->buf_off;
476 if ((ret == 0) && (ctx->encode != B64_NONE)
477 && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
480 ret = BIO_ctrl(next, cmd, num, ptr);
482 case BIO_CTRL_PENDING: /* More to read in buffer */
483 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
484 ret = ctx->buf_len - ctx->buf_off;
486 ret = BIO_ctrl(next, cmd, num, ptr);
489 /* do a final write */
491 while (ctx->buf_len != ctx->buf_off) {
492 i = b64_write(b, NULL, 0);
496 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
497 if (ctx->tmp_len != 0) {
498 ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
499 (unsigned char *)ctx->tmp,
505 } else if (ctx->encode != B64_NONE
506 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
508 EVP_EncodeFinal(ctx->base64,
509 (unsigned char *)ctx->buf, &(ctx->buf_len));
510 /* push out the bytes */
513 /* Finally flush the underlying BIO */
514 ret = BIO_ctrl(next, cmd, num, ptr);
517 case BIO_C_DO_STATE_MACHINE:
518 BIO_clear_retry_flags(b);
519 ret = BIO_ctrl(next, cmd, num, ptr);
520 BIO_copy_next_retry(b);
529 ret = BIO_ctrl(next, cmd, num, ptr);
535 static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
538 BIO *next = BIO_next(b);
544 ret = BIO_callback_ctrl(next, cmd, fp);
550 static int b64_puts(BIO *b, const char *str)
552 return b64_write(b, str, strlen(str));