]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/bio_b64.c
Update copyright year
[thirdparty/openssl.git] / crypto / evp / bio_b64.c
CommitLineData
62867571 1/*
b0edda11 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <errno.h>
b39fc560 12#include "internal/cryptlib.h"
ec577822
BM
13#include <openssl/buffer.h>
14#include <openssl/evp.h>
a146ae55 15#include "internal/bio.h"
d02b48c6 16
0e1c0612
UM
17static int b64_write(BIO *h, const char *buf, int num);
18static int b64_read(BIO *h, char *buf, int size);
cb877ccb 19static int b64_puts(BIO *h, const char *str);
0e1c0612 20static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
21static int b64_new(BIO *h);
22static int b64_free(BIO *data);
fce78bd4 23static long b64_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
0f113f3e
MC
24#define B64_BLOCK_SIZE 1024
25#define B64_BLOCK_SIZE2 768
26#define B64_NONE 0
27#define B64_ENCODE 1
28#define B64_DECODE 2
29
30typedef struct b64_struct {
31 /*
32 * BIO *bio; moved to the BIO structure
33 */
34 int buf_len;
35 int buf_off;
36 int tmp_len; /* used to find the start when decoding */
37 int tmp_nl; /* If true, scan until '\n' */
38 int encode;
39 int start; /* have we started decoding yet? */
40 int cont; /* <= 0 when finished */
b518d2d5 41 EVP_ENCODE_CTX *base64;
0f113f3e
MC
42 char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
43 char tmp[B64_BLOCK_SIZE];
44} BIO_B64_CTX;
45
04f6b0fd 46static const BIO_METHOD methods_b64 = {
27ab9195
DB
47 BIO_TYPE_BASE64,
48 "base64 encoding",
3befffa3
MC
49 /* TODO: Convert to new style write function */
50 bwrite_conv,
0f113f3e 51 b64_write,
d07aee2c
MC
52 /* TODO: Convert to new style read function */
53 bread_conv,
0f113f3e
MC
54 b64_read,
55 b64_puts,
56 NULL, /* b64_gets, */
57 b64_ctrl,
58 b64_new,
59 b64_free,
60 b64_callback_ctrl,
61};
d02b48c6 62
a146ae55 63
04f6b0fd 64const BIO_METHOD *BIO_f_base64(void)
0f113f3e 65{
a146ae55 66 return &methods_b64;
0f113f3e 67}
d02b48c6 68
6b691a5c 69static int b64_new(BIO *bi)
0f113f3e
MC
70{
71 BIO_B64_CTX *ctx;
72
64b25758 73 ctx = OPENSSL_zalloc(sizeof(*ctx));
0f113f3e 74 if (ctx == NULL)
cb1d435c 75 return 0;
0f113f3e 76
0f113f3e
MC
77 ctx->cont = 1;
78 ctx->start = 1;
e0d32e98 79 ctx->base64 = EVP_ENCODE_CTX_new();
cb1d435c
MRA
80 if (ctx->base64 == NULL) {
81 OPENSSL_free(ctx);
82 return 0;
83 }
84
a146ae55
MC
85 BIO_set_data(bi, ctx);
86 BIO_set_init(bi, 1);
87
88 return 1;
0f113f3e 89}
d02b48c6 90
6b691a5c 91static int b64_free(BIO *a)
0f113f3e 92{
a146ae55 93 BIO_B64_CTX *ctx;
0f113f3e 94 if (a == NULL)
a146ae55
MC
95 return 0;
96
97 ctx = BIO_get_data(a);
98 if (ctx == NULL)
99 return 0;
100
101 EVP_ENCODE_CTX_free(ctx->base64);
102 OPENSSL_free(ctx);
103 BIO_set_data(a, NULL);
104 BIO_set_init(a, 0);
105
106 return 1;
0f113f3e
MC
107}
108
6b691a5c 109static int b64_read(BIO *b, char *out, int outl)
0f113f3e
MC
110{
111 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
112 BIO_B64_CTX *ctx;
113 unsigned char *p, *q;
a146ae55 114 BIO *next;
0f113f3e
MC
115
116 if (out == NULL)
26a7d938 117 return 0;
a146ae55 118 ctx = (BIO_B64_CTX *)BIO_get_data(b);
0f113f3e 119
a146ae55
MC
120 next = BIO_next(b);
121 if ((ctx == NULL) || (next == NULL))
122 return 0;
0f113f3e
MC
123
124 BIO_clear_retry_flags(b);
125
126 if (ctx->encode != B64_DECODE) {
127 ctx->encode = B64_DECODE;
128 ctx->buf_len = 0;
129 ctx->buf_off = 0;
130 ctx->tmp_len = 0;
b518d2d5 131 EVP_DecodeInit(ctx->base64);
0f113f3e
MC
132 }
133
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;
138 if (i > outl)
139 i = outl;
140 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
141 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
142 ret = i;
143 out += i;
144 outl -= i;
145 ctx->buf_off += i;
146 if (ctx->buf_len == ctx->buf_off) {
147 ctx->buf_len = 0;
148 ctx->buf_off = 0;
149 }
150 }
151
152 /*
153 * At this point, we have room of outl bytes and an empty buffer, so we
154 * should read in some more.
155 */
156
157 ret_code = 0;
158 while (outl > 0) {
159 if (ctx->cont <= 0)
160 break;
161
a146ae55 162 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
0f113f3e
MC
163 B64_BLOCK_SIZE - ctx->tmp_len);
164
165 if (i <= 0) {
166 ret_code = i;
167
168 /* Should we continue next time we are called? */
a146ae55 169 if (!BIO_should_retry(next)) {
0f113f3e
MC
170 ctx->cont = i;
171 /* If buffer empty break */
172 if (ctx->tmp_len == 0)
173 break;
174 /* Fall through and process what we have */
175 else
176 i = 0;
177 }
178 /* else we retry and add more data to buffer */
179 else
180 break;
181 }
182 i += ctx->tmp_len;
183 ctx->tmp_len = i;
184
185 /*
186 * We need to scan, a line at a time until we have a valid line if we
187 * are starting.
188 */
189 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
190 /* ctx->start=1; */
191 ctx->tmp_len = 0;
192 } else if (ctx->start) {
193 q = p = (unsigned char *)ctx->tmp;
194 num = 0;
195 for (j = 0; j < i; j++) {
196 if (*(q++) != '\n')
197 continue;
198
199 /*
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.
203 */
204 if (ctx->tmp_nl) {
205 p = q;
206 ctx->tmp_nl = 0;
207 continue;
208 }
209
b518d2d5 210 k = EVP_DecodeUpdate(ctx->base64,
0f113f3e
MC
211 (unsigned char *)ctx->buf,
212 &num, p, q - p);
213 if ((k <= 0) && (num == 0) && (ctx->start))
b518d2d5 214 EVP_DecodeInit(ctx->base64);
0f113f3e
MC
215 else {
216 if (p != (unsigned char *)
217 &(ctx->tmp[0])) {
218 i -= (p - (unsigned char *)
219 &(ctx->tmp[0]));
220 for (x = 0; x < i; x++)
221 ctx->tmp[x] = p[x];
222 }
b518d2d5 223 EVP_DecodeInit(ctx->base64);
0f113f3e
MC
224 ctx->start = 0;
225 break;
226 }
227 p = q;
228 }
229
230 /* we fell off the end without starting */
231 if ((j == i) && (num == 0)) {
232 /*
233 * Is this is one long chunk?, if so, keep on reading until a
234 * new line.
235 */
236 if (p == (unsigned char *)&(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 ctx->tmp_len = n;
247 }
248 /* else finished on a '\n' */
249 continue;
250 } else {
251 ctx->tmp_len = 0;
252 }
253 } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
254 /*
255 * If buffer isn't full and we can retry then restart to read in
256 * more data.
257 */
258 continue;
259 }
260
261 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
262 int z, jj;
58964a49 263
0f113f3e 264 jj = i & ~3; /* process per 4 */
0f113f3e
MC
265 z = EVP_DecodeBlock((unsigned char *)ctx->buf,
266 (unsigned char *)ctx->tmp, jj);
267 if (jj > 2) {
268 if (ctx->tmp[jj - 1] == '=') {
269 z--;
270 if (ctx->tmp[jj - 2] == '=')
271 z--;
272 }
273 }
274 /*
275 * z is now number of output bytes and jj is the number consumed
276 */
277 if (jj != i) {
278 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
279 ctx->tmp_len = i - jj;
280 }
281 ctx->buf_len = 0;
282 if (z > 0) {
283 ctx->buf_len = z;
284 }
285 i = z;
286 } else {
b518d2d5 287 i = EVP_DecodeUpdate(ctx->base64,
0f113f3e
MC
288 (unsigned char *)ctx->buf, &ctx->buf_len,
289 (unsigned char *)ctx->tmp, i);
290 ctx->tmp_len = 0;
291 }
d070b4ae
DMSP
292 /*
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.
297 */
298 ctx->cont = i;
299
0f113f3e
MC
300 ctx->buf_off = 0;
301 if (i < 0) {
302 ret_code = 0;
303 ctx->buf_len = 0;
304 break;
305 }
306
307 if (ctx->buf_len <= outl)
308 i = ctx->buf_len;
309 else
310 i = outl;
311
312 memcpy(out, ctx->buf, i);
313 ret += i;
314 ctx->buf_off = i;
315 if (ctx->buf_off == ctx->buf_len) {
316 ctx->buf_len = 0;
317 ctx->buf_off = 0;
318 }
319 outl -= i;
320 out += i;
321 }
322 /* BIO_clear_retry_flags(b); */
323 BIO_copy_next_retry(b);
324 return ((ret == 0) ? ret_code : ret);
325}
d02b48c6 326
0e1c0612 327static int b64_write(BIO *b, const char *in, int inl)
0f113f3e
MC
328{
329 int ret = 0;
330 int n;
331 int i;
332 BIO_B64_CTX *ctx;
a146ae55
MC
333 BIO *next;
334
335 ctx = (BIO_B64_CTX *)BIO_get_data(b);
336 next = BIO_next(b);
337 if ((ctx == NULL) || (next == NULL))
338 return 0;
0f113f3e 339
0f113f3e
MC
340 BIO_clear_retry_flags(b);
341
342 if (ctx->encode != B64_ENCODE) {
343 ctx->encode = B64_ENCODE;
344 ctx->buf_len = 0;
345 ctx->buf_off = 0;
346 ctx->tmp_len = 0;
b518d2d5 347 EVP_EncodeInit(ctx->base64);
0f113f3e
MC
348 }
349
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;
354 while (n > 0) {
a146ae55 355 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
0f113f3e
MC
356 if (i <= 0) {
357 BIO_copy_next_retry(b);
26a7d938 358 return i;
0f113f3e
MC
359 }
360 OPENSSL_assert(i <= n);
361 ctx->buf_off += i;
362 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
363 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
364 n -= i;
365 }
366 /* at this point all pending data has been written */
367 ctx->buf_off = 0;
368 ctx->buf_len = 0;
369
370 if ((in == NULL) || (inl <= 0))
26a7d938 371 return 0;
0f113f3e
MC
372
373 while (inl > 0) {
374 n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
375
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;
380 /*
381 * There's a theoretical possibility for this
382 */
383 if (n > inl)
384 n = inl;
385 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
386 ctx->tmp_len += n;
387 ret += n;
388 if (ctx->tmp_len < 3)
389 break;
390 ctx->buf_len =
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);
395 /*
396 * Since we're now done using the temporary buffer, the
397 * length should be 0'd
398 */
399 ctx->tmp_len = 0;
400 } else {
401 if (n < 3) {
402 memcpy(ctx->tmp, in, n);
403 ctx->tmp_len = n;
404 ret += n;
405 break;
406 }
407 n -= n % 3;
408 ctx->buf_len =
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);
413 ret += n;
414 }
415 } else {
cf3404fc
MC
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);
0f113f3e
MC
420 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
421 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
422 ret += n;
423 }
424 inl -= n;
425 in += n;
426
427 ctx->buf_off = 0;
428 n = ctx->buf_len;
429 while (n > 0) {
a146ae55 430 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
0f113f3e
MC
431 if (i <= 0) {
432 BIO_copy_next_retry(b);
433 return ((ret == 0) ? i : ret);
434 }
435 OPENSSL_assert(i <= n);
436 n -= i;
437 ctx->buf_off += i;
438 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
439 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
440 }
441 ctx->buf_len = 0;
442 ctx->buf_off = 0;
443 }
26a7d938 444 return ret;
0f113f3e 445}
d02b48c6 446
0e1c0612 447static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
448{
449 BIO_B64_CTX *ctx;
450 long ret = 1;
451 int i;
a146ae55 452 BIO *next;
0f113f3e 453
a146ae55
MC
454 ctx = (BIO_B64_CTX *)BIO_get_data(b);
455 next = BIO_next(b);
456 if ((ctx == NULL) || (next == NULL))
457 return 0;
0f113f3e
MC
458
459 switch (cmd) {
460 case BIO_CTRL_RESET:
461 ctx->cont = 1;
462 ctx->start = 1;
463 ctx->encode = B64_NONE;
a146ae55 464 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
465 break;
466 case BIO_CTRL_EOF: /* More to read */
467 if (ctx->cont <= 0)
468 ret = 1;
469 else
a146ae55 470 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
471 break;
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)
b518d2d5 476 && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
0f113f3e
MC
477 ret = 1;
478 else if (ret <= 0)
a146ae55 479 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
480 break;
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;
484 if (ret <= 0)
a146ae55 485 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
486 break;
487 case BIO_CTRL_FLUSH:
488 /* do a final write */
489 again:
490 while (ctx->buf_len != ctx->buf_off) {
491 i = b64_write(b, NULL, 0);
492 if (i < 0)
493 return i;
494 }
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,
499 ctx->tmp_len);
500 ctx->buf_off = 0;
501 ctx->tmp_len = 0;
502 goto again;
503 }
b518d2d5
RL
504 } else if (ctx->encode != B64_NONE
505 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
0f113f3e 506 ctx->buf_off = 0;
b518d2d5 507 EVP_EncodeFinal(ctx->base64,
0f113f3e
MC
508 (unsigned char *)ctx->buf, &(ctx->buf_len));
509 /* push out the bytes */
510 goto again;
511 }
512 /* Finally flush the underlying BIO */
a146ae55 513 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
514 break;
515
516 case BIO_C_DO_STATE_MACHINE:
517 BIO_clear_retry_flags(b);
a146ae55 518 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
519 BIO_copy_next_retry(b);
520 break;
521
522 case BIO_CTRL_DUP:
523 break;
524 case BIO_CTRL_INFO:
525 case BIO_CTRL_GET:
526 case BIO_CTRL_SET:
527 default:
a146ae55 528 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
529 break;
530 }
a146ae55 531 return ret;
0f113f3e 532}
d02b48c6 533
fce78bd4 534static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
535{
536 long ret = 1;
a146ae55 537 BIO *next = BIO_next(b);
0f113f3e 538
a146ae55
MC
539 if (next == NULL)
540 return 0;
0f113f3e
MC
541 switch (cmd) {
542 default:
a146ae55 543 ret = BIO_callback_ctrl(next, cmd, fp);
0f113f3e
MC
544 break;
545 }
26a7d938 546 return ret;
0f113f3e 547}
d3442bc7 548
cb877ccb 549static int b64_puts(BIO *b, const char *str)
0f113f3e
MC
550{
551 return b64_write(b, str, strlen(str));
552}