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 ctx = OPENSSL_zalloc(sizeof(*ctx));
79 ctx->base64 = EVP_ENCODE_CTX_new();
80 if (ctx->base64 == NULL) {
85 BIO_set_data(bi, ctx);
91 static int b64_free(BIO *a)
97 ctx = BIO_get_data(a);
101 EVP_ENCODE_CTX_free(ctx->base64);
103 BIO_set_data(a, NULL);
109 static int b64_read(BIO *b, char *out, int outl)
111 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
113 unsigned char *p, *q;
118 ctx = (BIO_B64_CTX *)BIO_get_data(b);
121 if ((ctx == NULL) || (next == NULL))
124 BIO_clear_retry_flags(b);
126 if (ctx->encode != B64_DECODE) {
127 ctx->encode = B64_DECODE;
131 EVP_DecodeInit(ctx->base64);
134 /* First check if there are bytes decoded/encoded */
135 if (ctx->buf_len > 0) {
136 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
137 i = ctx->buf_len - ctx->buf_off;
140 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
141 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
146 if (ctx->buf_len == ctx->buf_off) {
153 * At this point, we have room of outl bytes and an empty buffer, so we
154 * should read in some more.
162 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
163 B64_BLOCK_SIZE - ctx->tmp_len);
168 /* Should we continue next time we are called? */
169 if (!BIO_should_retry(next)) {
171 /* If buffer empty break */
172 if (ctx->tmp_len == 0)
174 /* Fall through and process what we have */
178 /* else we retry and add more data to buffer */
186 * We need to scan, a line at a time until we have a valid line if we
189 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
192 } else if (ctx->start) {
193 q = p = (unsigned char *)ctx->tmp;
195 for (j = 0; j < i; j++) {
200 * due to a previous very long line, we need to keep on
201 * scanning for a '\n' before we even start looking for
202 * base64 encoded stuff.
210 k = EVP_DecodeUpdate(ctx->base64,
211 (unsigned char *)ctx->buf,
213 if ((k <= 0) && (num == 0) && (ctx->start))
214 EVP_DecodeInit(ctx->base64);
216 if (p != (unsigned char *)
218 i -= (p - (unsigned char *)
220 for (x = 0; x < i; x++)
223 EVP_DecodeInit(ctx->base64);
230 /* we fell off the end without starting */
231 if ((j == i) && (num == 0)) {
233 * Is this is one long chunk?, if so, keep on reading until a
236 if (p == (unsigned char *)&(ctx->tmp[0])) {
237 /* Check buffer full */
238 if (i == B64_BLOCK_SIZE) {
242 } else if (p != q) { /* finished on a '\n' */
244 for (ii = 0; ii < n; ii++)
245 ctx->tmp[ii] = p[ii];
248 /* else finished on a '\n' */
253 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
255 * If buffer isn't full and we can retry then restart to read in
261 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
264 jj = i & ~3; /* process per 4 */
265 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
266 (unsigned char *)ctx->tmp, jj);
268 if (ctx->tmp[jj - 1] == '=') {
270 if (ctx->tmp[jj - 2] == '=')
275 * z is now number of output bytes and jj is the number consumed
278 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
279 ctx->tmp_len = i - jj;
287 i = EVP_DecodeUpdate(ctx->base64,
288 (unsigned char *)ctx->buf, &ctx->buf_len,
289 (unsigned char *)ctx->tmp, i);
293 * If eof or an error was signalled, then the condition
294 * 'ctx->cont <= 0' will prevent b64_read() from reading
295 * more data on subsequent calls. This assignment was
296 * deleted accidentally in commit 5562cfaca4f3.
307 if (ctx->buf_len <= outl)
312 memcpy(out, ctx->buf, i);
315 if (ctx->buf_off == ctx->buf_len) {
322 /* BIO_clear_retry_flags(b); */
323 BIO_copy_next_retry(b);
324 return ((ret == 0) ? ret_code : ret);
327 static int b64_write(BIO *b, const char *in, int inl)
335 ctx = (BIO_B64_CTX *)BIO_get_data(b);
337 if ((ctx == NULL) || (next == NULL))
340 BIO_clear_retry_flags(b);
342 if (ctx->encode != B64_ENCODE) {
343 ctx->encode = B64_ENCODE;
347 EVP_EncodeInit(ctx->base64);
350 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
351 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
352 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
353 n = ctx->buf_len - ctx->buf_off;
355 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
357 BIO_copy_next_retry(b);
360 OPENSSL_assert(i <= n);
362 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
363 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
366 /* at this point all pending data has been written */
370 if ((in == NULL) || (inl <= 0))
374 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
376 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
377 if (ctx->tmp_len > 0) {
378 OPENSSL_assert(ctx->tmp_len <= 3);
379 n = 3 - ctx->tmp_len;
381 * There's a theoretical possibility for this
385 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
388 if (ctx->tmp_len < 3)
391 EVP_EncodeBlock((unsigned char *)ctx->buf,
392 (unsigned char *)ctx->tmp, ctx->tmp_len);
393 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
394 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
396 * Since we're now done using the temporary buffer, the
397 * length should be 0'd
402 memcpy(ctx->tmp, in, n);
409 EVP_EncodeBlock((unsigned char *)ctx->buf,
410 (const unsigned char *)in, n);
411 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
412 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
416 if (!EVP_EncodeUpdate(ctx->base64,
417 (unsigned char *)ctx->buf, &ctx->buf_len,
418 (unsigned char *)in, n))
419 return ((ret == 0) ? -1 : ret);
420 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
421 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
430 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
432 BIO_copy_next_retry(b);
433 return ((ret == 0) ? i : ret);
435 OPENSSL_assert(i <= n);
438 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
439 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
447 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
454 ctx = (BIO_B64_CTX *)BIO_get_data(b);
456 if ((ctx == NULL) || (next == NULL))
463 ctx->encode = B64_NONE;
464 ret = BIO_ctrl(next, cmd, num, ptr);
466 case BIO_CTRL_EOF: /* More to read */
470 ret = BIO_ctrl(next, cmd, num, ptr);
472 case BIO_CTRL_WPENDING: /* More to write in buffer */
473 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
474 ret = ctx->buf_len - ctx->buf_off;
475 if ((ret == 0) && (ctx->encode != B64_NONE)
476 && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
479 ret = BIO_ctrl(next, cmd, num, ptr);
481 case BIO_CTRL_PENDING: /* More to read in buffer */
482 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
483 ret = ctx->buf_len - ctx->buf_off;
485 ret = BIO_ctrl(next, cmd, num, ptr);
488 /* do a final write */
490 while (ctx->buf_len != ctx->buf_off) {
491 i = b64_write(b, NULL, 0);
495 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
496 if (ctx->tmp_len != 0) {
497 ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
498 (unsigned char *)ctx->tmp,
504 } else if (ctx->encode != B64_NONE
505 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
507 EVP_EncodeFinal(ctx->base64,
508 (unsigned char *)ctx->buf, &(ctx->buf_len));
509 /* push out the bytes */
512 /* Finally flush the underlying BIO */
513 ret = BIO_ctrl(next, cmd, num, ptr);
516 case BIO_C_DO_STATE_MACHINE:
517 BIO_clear_retry_flags(b);
518 ret = BIO_ctrl(next, cmd, num, ptr);
519 BIO_copy_next_retry(b);
528 ret = BIO_ctrl(next, cmd, num, ptr);
534 static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
537 BIO *next = BIO_next(b);
543 ret = BIO_callback_ctrl(next, cmd, fp);
549 static int b64_puts(BIO *b, const char *str)
551 return b64_write(b, str, strlen(str));