1 /*
2 * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
16
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
26 #define B64_NONE 0
27 #define B64_ENCODE 1
28 #define B64_DECODE 2
29
30 typedef 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 */
41 EVP_ENCODE_CTX *base64;
42 unsigned char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
43 unsigned char tmp[B64_BLOCK_SIZE];
44 } BIO_B64_CTX;
45
46 static const BIO_METHOD methods_b64 = {
47 BIO_TYPE_BASE64,
48 "base64 encoding",
49 bwrite_conv,
50 b64_write,
51 bread_conv,
52 b64_read,
53 b64_puts,
54 NULL, /* b64_gets, */
55 b64_ctrl,
56 b64_new,
57 b64_free,
58 b64_callback_ctrl,
59 };
60
BIO_f_base64(void)61 const BIO_METHOD *BIO_f_base64(void)
62 {
63 return &methods_b64;
64 }
65
b64_new(BIO * bi)66 static int b64_new(BIO *bi)
67 {
68 BIO_B64_CTX *ctx;
69
70 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
71 return 0;
72
73 ctx->cont = 1;
74 ctx->start = 1;
75 ctx->base64 = EVP_ENCODE_CTX_new();
76 if (ctx->base64 == NULL) {
77 OPENSSL_free(ctx);
78 return 0;
79 }
80
81 BIO_set_data(bi, ctx);
82 BIO_set_init(bi, 1);
83
84 return 1;
85 }
86
b64_free(BIO * a)87 static int b64_free(BIO *a)
88 {
89 BIO_B64_CTX *ctx;
90
91 if (a == NULL)
92 return 0;
93
94 ctx = BIO_get_data(a);
95 if (ctx == NULL)
96 return 0;
97
98 EVP_ENCODE_CTX_free(ctx->base64);
99 OPENSSL_free(ctx);
100 BIO_set_data(a, NULL);
101 BIO_set_init(a, 0);
102
103 return 1;
104 }
105
106 /*
107 * Unless `BIO_FLAGS_BASE64_NO_NL` is set, this BIO ignores leading lines that
108 * aren't exclusively composed of valid Base64 characters (followed by <CRLF>
109 * or <LF>). Once a valid Base64 line is found, `ctx->start` is set to 0 and
110 * lines are processed until EOF or the first line that contains invalid Base64
111 * characters. In a nod to PEM, lines that start with a '-' (hyphen) are
112 * treated as a soft EOF, rather than an error.
113 */
b64_read(BIO * b,char * out,int outl)114 static int b64_read(BIO *b, char *out, int outl)
115 {
116 int ret = 0, i, ii, j, k, x, n, num, ret_code;
117 BIO_B64_CTX *ctx;
118 unsigned char *p, *q;
119 BIO *next;
120
121 if (out == NULL)
122 return 0;
123 ctx = (BIO_B64_CTX *)BIO_get_data(b);
124
125 next = BIO_next(b);
126 if (ctx == NULL || next == NULL)
127 return 0;
128
129 BIO_clear_retry_flags(b);
130
131 if (ctx->encode != B64_DECODE) {
132 ctx->encode = B64_DECODE;
133 ctx->buf_len = 0;
134 ctx->buf_off = 0;
135 ctx->tmp_len = 0;
136 EVP_DecodeInit(ctx->base64);
137 }
138
139 /* First check if there are buffered bytes already decoded */
140 if (ctx->buf_len > 0) {
141 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
142 i = ctx->buf_len - ctx->buf_off;
143 if (i > outl)
144 i = outl;
145 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
146 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
147 ret = i;
148 out += i;
149 outl -= i;
150 ctx->buf_off += i;
151 if (ctx->buf_len == ctx->buf_off) {
152 ctx->buf_len = 0;
153 ctx->buf_off = 0;
154 }
155 }
156
157 /* Restore any non-retriable error condition (ctx->cont < 0) */
158 ret_code = ctx->cont < 0 ? ctx->cont : 0;
159
160 /*
161 * At this point, we have room of outl bytes and an either an empty buffer,
162 * or outl == 0, so we'll attempt to read in some more.
163 */
164 while (outl > 0) {
165 int again = ctx->cont;
166
167 if (again <= 0)
168 break;
169
170 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
171 B64_BLOCK_SIZE - ctx->tmp_len);
172
173 if (i <= 0) {
174 ret_code = i;
175
176 /* Should we continue next time we are called? */
177 if (!BIO_should_retry(next)) {
178 /* Incomplete final Base64 chunk in the decoder is an error */
179 if (ctx->tmp_len == 0) {
180 if (EVP_DecodeFinal(ctx->base64, NULL, &num) < 0)
181 ret_code = -1;
182 EVP_DecodeInit(ctx->base64);
183 }
184 ctx->cont = ret_code;
185 }
186 if (ctx->tmp_len == 0)
187 break;
188 /* Fall through and process what we have */
189 i = 0;
190 /* But don't loop to top-up even if the buffer is not full! */
191 again = 0;
192 }
193
194 i += ctx->tmp_len;
195 ctx->tmp_len = i;
196
197 /*
198 * We need to scan, a line at a time until we have a valid line if we
199 * are starting.
200 */
201 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
202 ctx->tmp_len = 0;
203 } else if (ctx->start) {
204 q = p = ctx->tmp;
205 num = 0;
206 for (j = 0; j < i; j++) {
207 if (*(q++) != '\n')
208 continue;
209
210 /*
211 * due to a previous very long line, we need to keep on
212 * scanning for a '\n' before we even start looking for
213 * base64 encoded stuff.
214 */
215 if (ctx->tmp_nl) {
216 p = q;
217 ctx->tmp_nl = 0;
218 continue;
219 }
220
221 k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
222 EVP_DecodeInit(ctx->base64);
223 if (k <= 0 && num == 0) {
224 p = q;
225 continue;
226 }
227
228 ctx->start = 0;
229 if (p != ctx->tmp) {
230 i -= p - ctx->tmp;
231 for (x = 0; x < i; x++)
232 ctx->tmp[x] = p[x];
233 }
234 break;
235 }
236
237 /* we fell off the end without starting */
238 if (ctx->start) {
239 /*
240 * Is this is one long chunk?, if so, keep on reading until a
241 * new line.
242 */
243 if (p == ctx->tmp) {
244 /* Check buffer full */
245 if (i == B64_BLOCK_SIZE) {
246 ctx->tmp_nl = 1;
247 ctx->tmp_len = 0;
248 }
249 } else if (p != q) {
250 /* Retain partial line at end of buffer */
251 n = q - p;
252 for (ii = 0; ii < n; ii++)
253 ctx->tmp[ii] = p[ii];
254 ctx->tmp_len = n;
255 } else {
256 /* All we have is newline terminated non-start data */
257 ctx->tmp_len = 0;
258 }
259 /*
260 * Try to read more if possible, otherwise we can't make
261 * progress unless the underlying BIO is retriable and may
262 * produce more data next time we're called.
263 */
264 if (again > 0)
265 continue;
266 else
267 break;
268 } else {
269 ctx->tmp_len = 0;
270 }
271 } else if (i < B64_BLOCK_SIZE && again > 0) {
272 /*
273 * If buffer isn't full and we can retry then restart to read in
274 * more data.
275 */
276 continue;
277 }
278
279 i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
280 ctx->tmp, i);
281 ctx->tmp_len = 0;
282 /*
283 * If eof or an error was signalled, then the condition
284 * 'ctx->cont <= 0' will prevent b64_read() from reading
285 * more data on subsequent calls. This assignment was
286 * deleted accidentally in commit 5562cfaca4f3.
287 */
288 ctx->cont = i;
289
290 ctx->buf_off = 0;
291 if (i < 0) {
292 ret_code = ctx->start ? 0 : i;
293 ctx->buf_len = 0;
294 break;
295 }
296
297 if (ctx->buf_len <= outl)
298 i = ctx->buf_len;
299 else
300 i = outl;
301
302 memcpy(out, ctx->buf, i);
303 ret += i;
304 ctx->buf_off = i;
305 if (ctx->buf_off == ctx->buf_len) {
306 ctx->buf_len = 0;
307 ctx->buf_off = 0;
308 }
309 outl -= i;
310 out += i;
311 }
312 /* BIO_clear_retry_flags(b); */
313 BIO_copy_next_retry(b);
314 return ret == 0 ? ret_code : ret;
315 }
316
b64_write(BIO * b,const char * in,int inl)317 static int b64_write(BIO *b, const char *in, int inl)
318 {
319 int ret = 0;
320 int n;
321 int i;
322 BIO_B64_CTX *ctx;
323 BIO *next;
324
325 ctx = (BIO_B64_CTX *)BIO_get_data(b);
326 next = BIO_next(b);
327 if (ctx == NULL || next == NULL)
328 return 0;
329
330 BIO_clear_retry_flags(b);
331
332 if (ctx->encode != B64_ENCODE) {
333 ctx->encode = B64_ENCODE;
334 ctx->buf_len = 0;
335 ctx->buf_off = 0;
336 ctx->tmp_len = 0;
337 EVP_EncodeInit(ctx->base64);
338 }
339
340 OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
341 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
342 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
343 n = ctx->buf_len - ctx->buf_off;
344 while (n > 0) {
345 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
346 if (i <= 0) {
347 BIO_copy_next_retry(b);
348 return i;
349 }
350 OPENSSL_assert(i <= n);
351 ctx->buf_off += i;
352 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
353 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
354 n -= i;
355 }
356 /* at this point all pending data has been written */
357 ctx->buf_off = 0;
358 ctx->buf_len = 0;
359
360 if (in == NULL || inl <= 0)
361 return 0;
362
363 while (inl > 0) {
364 n = inl > B64_BLOCK_SIZE ? B64_BLOCK_SIZE : inl;
365
366 if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
367 if (ctx->tmp_len > 0) {
368 OPENSSL_assert(ctx->tmp_len <= 3);
369 n = 3 - ctx->tmp_len;
370 /*
371 * There's a theoretical possibility for this
372 */
373 if (n > inl)
374 n = inl;
375 memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
376 ctx->tmp_len += n;
377 ret += n;
378 if (ctx->tmp_len < 3)
379 break;
380 ctx->buf_len = EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
381 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
382 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
383 /*
384 * Since we're now done using the temporary buffer, the
385 * length should be 0'd
386 */
387 ctx->tmp_len = 0;
388 } else {
389 if (n < 3) {
390 memcpy(ctx->tmp, in, n);
391 ctx->tmp_len = n;
392 ret += n;
393 break;
394 }
395 n -= n % 3;
396 ctx->buf_len = EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
397 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
398 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
399 ret += n;
400 }
401 } else {
402 if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
403 (unsigned char *)in, n))
404 return ret == 0 ? -1 : ret;
405 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
406 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
407 ret += n;
408 }
409 inl -= n;
410 in += n;
411
412 ctx->buf_off = 0;
413 n = ctx->buf_len;
414 while (n > 0) {
415 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
416 if (i <= 0) {
417 BIO_copy_next_retry(b);
418 return ret == 0 ? i : ret;
419 }
420 OPENSSL_assert(i <= n);
421 n -= i;
422 ctx->buf_off += i;
423 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
424 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
425 }
426 ctx->buf_len = 0;
427 ctx->buf_off = 0;
428 }
429 return ret;
430 }
431
b64_ctrl(BIO * b,int cmd,long num,void * ptr)432 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
433 {
434 BIO_B64_CTX *ctx;
435 long ret = 1;
436 int i;
437 BIO *next;
438
439 ctx = (BIO_B64_CTX *)BIO_get_data(b);
440 next = BIO_next(b);
441 if (ctx == NULL || next == NULL)
442 return 0;
443
444 switch (cmd) {
445 case BIO_CTRL_RESET:
446 ctx->cont = 1;
447 ctx->start = 1;
448 ctx->encode = B64_NONE;
449 ret = BIO_ctrl(next, cmd, num, ptr);
450 break;
451 case BIO_CTRL_EOF: /* More to read */
452 if (ctx->cont <= 0)
453 ret = 1;
454 else
455 ret = BIO_ctrl(next, cmd, num, ptr);
456 break;
457 case BIO_CTRL_WPENDING: /* More to write in buffer */
458 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
459 ret = ctx->buf_len - ctx->buf_off;
460 if (ret == 0 && ctx->encode != B64_NONE
461 && EVP_ENCODE_CTX_num(ctx->base64) != 0)
462 ret = 1;
463 else if (ret <= 0)
464 ret = BIO_ctrl(next, cmd, num, ptr);
465 break;
466 case BIO_CTRL_PENDING: /* More to read in buffer */
467 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
468 ret = ctx->buf_len - ctx->buf_off;
469 if (ret <= 0)
470 ret = BIO_ctrl(next, cmd, num, ptr);
471 break;
472 case BIO_CTRL_FLUSH:
473 /* do a final write */
474 again:
475 while (ctx->buf_len != ctx->buf_off) {
476 i = b64_write(b, NULL, 0);
477 if (i < 0)
478 return i;
479 }
480 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
481 if (ctx->tmp_len != 0) {
482 ctx->buf_len = EVP_EncodeBlock(ctx->buf,
483 ctx->tmp, ctx->tmp_len);
484 ctx->buf_off = 0;
485 ctx->tmp_len = 0;
486 goto again;
487 }
488 } else if (ctx->encode != B64_NONE
489 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
490 ctx->buf_off = 0;
491 EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
492 /* push out the bytes */
493 goto again;
494 }
495 /* Finally flush the underlying BIO */
496 ret = BIO_ctrl(next, cmd, num, ptr);
497 BIO_copy_next_retry(b);
498 break;
499
500 case BIO_C_DO_STATE_MACHINE:
501 BIO_clear_retry_flags(b);
502 ret = BIO_ctrl(next, cmd, num, ptr);
503 BIO_copy_next_retry(b);
504 break;
505
506 case BIO_CTRL_DUP:
507 break;
508 case BIO_CTRL_INFO:
509 case BIO_CTRL_GET:
510 case BIO_CTRL_SET:
511 default:
512 ret = BIO_ctrl(next, cmd, num, ptr);
513 break;
514 }
515 return ret;
516 }
517
b64_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)518 static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
519 {
520 BIO *next = BIO_next(b);
521
522 if (next == NULL)
523 return 0;
524
525 return BIO_callback_ctrl(next, cmd, fp);
526 }
527
b64_puts(BIO * b,const char * str)528 static int b64_puts(BIO *b, const char *str)
529 {
530 return b64_write(b, str, strlen(str));
531 }
532