2 * Copyright 1995-2016 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);
21 * static int b64_gets(BIO *h, char *str, int size);
23 static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
24 static int b64_new(BIO *h);
25 static int b64_free(BIO *data);
26 static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
27 #define B64_BLOCK_SIZE 1024
28 #define B64_BLOCK_SIZE2 768
33 typedef struct b64_struct {
35 * BIO *bio; moved to the BIO structure
39 int tmp_len; /* used to find the start when decoding */
40 int tmp_nl; /* If true, scan until '\n' */
42 int start; /* have we started decoding yet? */
43 int cont; /* <= 0 when finished */
44 EVP_ENCODE_CTX *base64;
45 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
46 char tmp[B64_BLOCK_SIZE];
49 static const BIO_METHOD methods_b64 = {
50 BIO_TYPE_BASE64, "base64 encoding",
51 /* TODO: Convert to new style write function */
54 /* TODO: Convert to new style read function */
66 const BIO_METHOD *BIO_f_base64(void)
71 static int b64_new(BIO *bi)
75 ctx = OPENSSL_zalloc(sizeof(*ctx));
81 ctx->base64 = EVP_ENCODE_CTX_new();
82 if (ctx->base64 == NULL) {
87 BIO_set_data(bi, ctx);
93 static int b64_free(BIO *a)
99 ctx = BIO_get_data(a);
103 EVP_ENCODE_CTX_free(ctx->base64);
105 BIO_set_data(a, NULL);
111 static int b64_read(BIO *b, char *out, int outl)
113 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
115 unsigned char *p, *q;
120 ctx = (BIO_B64_CTX *)BIO_get_data(b);
123 if ((ctx == NULL) || (next == NULL))
126 BIO_clear_retry_flags(b);
128 if (ctx->encode != B64_DECODE) {
129 ctx->encode = B64_DECODE;
133 EVP_DecodeInit(ctx->base64);
136 /* First check if there are bytes decoded/encoded */
137 if (ctx->buf_len > 0) {
138 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
139 i = ctx->buf_len - ctx->buf_off;
142 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
143 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
148 if (ctx->buf_len == ctx->buf_off) {
155 * At this point, we have room of outl bytes and an empty buffer, so we
156 * should read in some more.
164 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
165 B64_BLOCK_SIZE - ctx->tmp_len);
170 /* Should we continue next time we are called? */
171 if (!BIO_should_retry(next)) {
173 /* If buffer empty break */
174 if (ctx->tmp_len == 0)
176 /* Fall through and process what we have */
180 /* else we retry and add more data to buffer */
188 * We need to scan, a line at a time until we have a valid line if we
191 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
194 } else if (ctx->start) {
195 q = p = (unsigned char *)ctx->tmp;
197 for (j = 0; j < i; j++) {
202 * due to a previous very long line, we need to keep on
203 * scanning for a '\n' before we even start looking for
204 * base64 encoded stuff.
212 k = EVP_DecodeUpdate(ctx->base64,
213 (unsigned char *)ctx->buf,
215 if ((k <= 0) && (num == 0) && (ctx->start))
216 EVP_DecodeInit(ctx->base64);
218 if (p != (unsigned char *)
220 i -= (p - (unsigned char *)
222 for (x = 0; x < i; x++)
225 EVP_DecodeInit(ctx->base64);
232 /* we fell off the end without starting */
233 if ((j == i) && (num == 0)) {
235 * Is this is one long chunk?, if so, keep on reading until a
238 if (p == (unsigned char *)&(ctx->tmp[0])) {
239 /* Check buffer full */
240 if (i == B64_BLOCK_SIZE) {
244 } else if (p != q) { /* finished on a '\n' */
246 for (ii = 0; ii < n; ii++)
247 ctx->tmp[ii] = p[ii];
250 /* else finished on a '\n' */
255 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
257 * If buffer isn't full and we can retry then restart to read in
263 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
266 jj = i & ~3; /* process per 4 */
267 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
268 (unsigned char *)ctx->tmp, jj);
270 if (ctx->tmp[jj - 1] == '=') {
272 if (ctx->tmp[jj - 2] == '=')
277 * z is now number of output bytes and jj is the number consumed
280 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
281 ctx->tmp_len = i - jj;
289 i = EVP_DecodeUpdate(ctx->base64,
290 (unsigned char *)ctx->buf, &ctx->buf_len,
291 (unsigned char *)ctx->tmp, i);
301 if (ctx->buf_len <= outl)
306 memcpy(out, ctx->buf, i);
309 if (ctx->buf_off == ctx->buf_len) {
316 /* BIO_clear_retry_flags(b); */
317 BIO_copy_next_retry(b);
318 return ((ret == 0) ? ret_code : ret);
321 static int b64_write(BIO *b, const char *in, int inl)
329 ctx = (BIO_B64_CTX *)BIO_get_data(b);
331 if ((ctx == NULL) || (next == NULL))
334 BIO_clear_retry_flags(b);
336 if (ctx->encode != B64_ENCODE) {
337 ctx->encode = B64_ENCODE;
341 EVP_EncodeInit(ctx->base64);
344 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
345 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
346 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
347 n = ctx->buf_len - ctx->buf_off;
349 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
351 BIO_copy_next_retry(b);
354 OPENSSL_assert(i <= n);
356 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
357 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
360 /* at this point all pending data has been written */
364 if ((in == NULL) || (inl <= 0))
368 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
370 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
371 if (ctx->tmp_len > 0) {
372 OPENSSL_assert(ctx->tmp_len <= 3);
373 n = 3 - ctx->tmp_len;
375 * There's a theoretical possibility for this
379 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
382 if (ctx->tmp_len < 3)
385 EVP_EncodeBlock((unsigned char *)ctx->buf,
386 (unsigned char *)ctx->tmp, ctx->tmp_len);
387 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
388 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
390 * Since we're now done using the temporary buffer, the
391 * length should be 0'd
396 memcpy(ctx->tmp, in, n);
403 EVP_EncodeBlock((unsigned char *)ctx->buf,
404 (const unsigned char *)in, n);
405 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
406 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
410 if (!EVP_EncodeUpdate(ctx->base64,
411 (unsigned char *)ctx->buf, &ctx->buf_len,
412 (unsigned char *)in, n))
413 return ((ret == 0) ? -1 : ret);
414 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
415 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
424 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
426 BIO_copy_next_retry(b);
427 return ((ret == 0) ? i : ret);
429 OPENSSL_assert(i <= n);
432 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
433 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
441 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
448 ctx = (BIO_B64_CTX *)BIO_get_data(b);
450 if ((ctx == NULL) || (next == NULL))
457 ctx->encode = B64_NONE;
458 ret = BIO_ctrl(next, cmd, num, ptr);
460 case BIO_CTRL_EOF: /* More to read */
464 ret = BIO_ctrl(next, cmd, num, ptr);
466 case BIO_CTRL_WPENDING: /* More to write in buffer */
467 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
468 ret = ctx->buf_len - ctx->buf_off;
469 if ((ret == 0) && (ctx->encode != B64_NONE)
470 && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
473 ret = BIO_ctrl(next, cmd, num, ptr);
475 case BIO_CTRL_PENDING: /* More to read in buffer */
476 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
477 ret = ctx->buf_len - ctx->buf_off;
479 ret = BIO_ctrl(next, cmd, num, ptr);
482 /* do a final write */
484 while (ctx->buf_len != ctx->buf_off) {
485 i = b64_write(b, NULL, 0);
489 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
490 if (ctx->tmp_len != 0) {
491 ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
492 (unsigned char *)ctx->tmp,
498 } else if (ctx->encode != B64_NONE
499 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
501 EVP_EncodeFinal(ctx->base64,
502 (unsigned char *)ctx->buf, &(ctx->buf_len));
503 /* push out the bytes */
506 /* Finally flush the underlying BIO */
507 ret = BIO_ctrl(next, cmd, num, ptr);
510 case BIO_C_DO_STATE_MACHINE:
511 BIO_clear_retry_flags(b);
512 ret = BIO_ctrl(next, cmd, num, ptr);
513 BIO_copy_next_retry(b);
522 ret = BIO_ctrl(next, cmd, num, ptr);
528 static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
531 BIO *next = BIO_next(b);
537 ret = BIO_callback_ctrl(next, cmd, fp);
543 static int b64_puts(BIO *b, const char *str)
545 return b64_write(b, str, strlen(str));