]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/bio_b64.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / evp / bio_b64.c
CommitLineData
62867571 1/*
da1c088f 2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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;
f95fec29
DDO
42 unsigned char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
43 unsigned char tmp[B64_BLOCK_SIZE];
0f113f3e
MC
44} BIO_B64_CTX;
45
04f6b0fd 46static const BIO_METHOD methods_b64 = {
27ab9195
DB
47 BIO_TYPE_BASE64,
48 "base64 encoding",
3befffa3 49 bwrite_conv,
0f113f3e 50 b64_write,
d07aee2c 51 bread_conv,
0f113f3e
MC
52 b64_read,
53 b64_puts,
54 NULL, /* b64_gets, */
55 b64_ctrl,
56 b64_new,
57 b64_free,
58 b64_callback_ctrl,
59};
d02b48c6 60
04f6b0fd 61const BIO_METHOD *BIO_f_base64(void)
0f113f3e 62{
a146ae55 63 return &methods_b64;
0f113f3e 64}
d02b48c6 65
6b691a5c 66static int b64_new(BIO *bi)
0f113f3e
MC
67{
68 BIO_B64_CTX *ctx;
69
e077455e 70 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
cb1d435c 71 return 0;
0f113f3e 72
0f113f3e
MC
73 ctx->cont = 1;
74 ctx->start = 1;
e0d32e98 75 ctx->base64 = EVP_ENCODE_CTX_new();
cb1d435c
MRA
76 if (ctx->base64 == NULL) {
77 OPENSSL_free(ctx);
78 return 0;
79 }
80
a146ae55
MC
81 BIO_set_data(bi, ctx);
82 BIO_set_init(bi, 1);
83
84 return 1;
0f113f3e 85}
d02b48c6 86
6b691a5c 87static int b64_free(BIO *a)
0f113f3e 88{
a146ae55 89 BIO_B64_CTX *ctx;
f95fec29 90
0f113f3e 91 if (a == NULL)
a146ae55
MC
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;
0f113f3e
MC
104}
105
6b691a5c 106static int b64_read(BIO *b, char *out, int outl)
0f113f3e
MC
107{
108 int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
109 BIO_B64_CTX *ctx;
110 unsigned char *p, *q;
a146ae55 111 BIO *next;
0f113f3e
MC
112
113 if (out == NULL)
26a7d938 114 return 0;
a146ae55 115 ctx = (BIO_B64_CTX *)BIO_get_data(b);
0f113f3e 116
a146ae55 117 next = BIO_next(b);
f95fec29 118 if (ctx == NULL || next == NULL)
a146ae55 119 return 0;
0f113f3e
MC
120
121 BIO_clear_retry_flags(b);
122
123 if (ctx->encode != B64_DECODE) {
124 ctx->encode = B64_DECODE;
125 ctx->buf_len = 0;
126 ctx->buf_off = 0;
127 ctx->tmp_len = 0;
b518d2d5 128 EVP_DecodeInit(ctx->base64);
0f113f3e
MC
129 }
130
131 /* First check if there are bytes decoded/encoded */
132 if (ctx->buf_len > 0) {
133 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
134 i = ctx->buf_len - ctx->buf_off;
135 if (i > outl)
136 i = outl;
137 OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
138 memcpy(out, &(ctx->buf[ctx->buf_off]), i);
139 ret = i;
140 out += i;
141 outl -= i;
142 ctx->buf_off += i;
143 if (ctx->buf_len == ctx->buf_off) {
144 ctx->buf_len = 0;
145 ctx->buf_off = 0;
146 }
147 }
148
149 /*
150 * At this point, we have room of outl bytes and an empty buffer, so we
151 * should read in some more.
152 */
153
154 ret_code = 0;
155 while (outl > 0) {
156 if (ctx->cont <= 0)
157 break;
158
a146ae55 159 i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
0f113f3e
MC
160 B64_BLOCK_SIZE - ctx->tmp_len);
161
162 if (i <= 0) {
163 ret_code = i;
164
165 /* Should we continue next time we are called? */
a146ae55 166 if (!BIO_should_retry(next)) {
0f113f3e
MC
167 ctx->cont = i;
168 /* If buffer empty break */
169 if (ctx->tmp_len == 0)
170 break;
171 /* Fall through and process what we have */
172 else
173 i = 0;
174 }
175 /* else we retry and add more data to buffer */
176 else
177 break;
178 }
179 i += ctx->tmp_len;
180 ctx->tmp_len = i;
181
182 /*
183 * We need to scan, a line at a time until we have a valid line if we
184 * are starting.
185 */
f95fec29 186 if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
0f113f3e
MC
187 ctx->tmp_len = 0;
188 } else if (ctx->start) {
f95fec29 189 q = p = ctx->tmp;
0f113f3e
MC
190 num = 0;
191 for (j = 0; j < i; j++) {
192 if (*(q++) != '\n')
193 continue;
194
195 /*
196 * due to a previous very long line, we need to keep on
197 * scanning for a '\n' before we even start looking for
198 * base64 encoded stuff.
199 */
200 if (ctx->tmp_nl) {
201 p = q;
202 ctx->tmp_nl = 0;
203 continue;
204 }
205
f95fec29
DDO
206 k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
207 if (k <= 0 && num == 0 && ctx->start) {
b518d2d5 208 EVP_DecodeInit(ctx->base64);
f95fec29
DDO
209 } else {
210 if (p != ctx->tmp) {
211 i -= p - ctx->tmp;
0f113f3e
MC
212 for (x = 0; x < i; x++)
213 ctx->tmp[x] = p[x];
214 }
b518d2d5 215 EVP_DecodeInit(ctx->base64);
0f113f3e
MC
216 ctx->start = 0;
217 break;
218 }
219 p = q;
220 }
221
222 /* we fell off the end without starting */
f95fec29 223 if (j == i && num == 0) {
0f113f3e
MC
224 /*
225 * Is this is one long chunk?, if so, keep on reading until a
226 * new line.
227 */
f95fec29 228 if (p == ctx->tmp) {
0f113f3e
MC
229 /* Check buffer full */
230 if (i == B64_BLOCK_SIZE) {
231 ctx->tmp_nl = 1;
232 ctx->tmp_len = 0;
233 }
234 } else if (p != q) { /* finished on a '\n' */
235 n = q - p;
236 for (ii = 0; ii < n; ii++)
237 ctx->tmp[ii] = p[ii];
238 ctx->tmp_len = n;
239 }
240 /* else finished on a '\n' */
241 continue;
242 } else {
243 ctx->tmp_len = 0;
244 }
f95fec29 245 } else if (i < B64_BLOCK_SIZE && ctx->cont > 0) {
0f113f3e
MC
246 /*
247 * If buffer isn't full and we can retry then restart to read in
248 * more data.
249 */
250 continue;
251 }
252
f95fec29 253 if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
0f113f3e 254 int z, jj;
58964a49 255
0f113f3e 256 jj = i & ~3; /* process per 4 */
f95fec29 257 z = EVP_DecodeBlock(ctx->buf, ctx->tmp, jj);
0f113f3e
MC
258 if (jj > 2) {
259 if (ctx->tmp[jj - 1] == '=') {
260 z--;
261 if (ctx->tmp[jj - 2] == '=')
262 z--;
263 }
264 }
265 /*
266 * z is now number of output bytes and jj is the number consumed
267 */
268 if (jj != i) {
269 memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
270 ctx->tmp_len = i - jj;
271 }
272 ctx->buf_len = 0;
273 if (z > 0) {
274 ctx->buf_len = z;
275 }
276 i = z;
277 } else {
f95fec29
DDO
278 i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
279 ctx->tmp, i);
0f113f3e
MC
280 ctx->tmp_len = 0;
281 }
d070b4ae
DMSP
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
0f113f3e
MC
290 ctx->buf_off = 0;
291 if (i < 0) {
292 ret_code = 0;
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);
f95fec29 314 return ret == 0 ? ret_code : ret;
0f113f3e 315}
d02b48c6 316
0e1c0612 317static int b64_write(BIO *b, const char *in, int inl)
0f113f3e
MC
318{
319 int ret = 0;
320 int n;
321 int i;
322 BIO_B64_CTX *ctx;
a146ae55
MC
323 BIO *next;
324
325 ctx = (BIO_B64_CTX *)BIO_get_data(b);
326 next = BIO_next(b);
f95fec29 327 if (ctx == NULL || next == NULL)
a146ae55 328 return 0;
0f113f3e 329
0f113f3e
MC
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;
b518d2d5 337 EVP_EncodeInit(ctx->base64);
0f113f3e
MC
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) {
a146ae55 345 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
0f113f3e
MC
346 if (i <= 0) {
347 BIO_copy_next_retry(b);
26a7d938 348 return i;
0f113f3e
MC
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
f95fec29 360 if (in == NULL || inl <= 0)
26a7d938 361 return 0;
0f113f3e
MC
362
363 while (inl > 0) {
f95fec29 364 n = inl > B64_BLOCK_SIZE ? B64_BLOCK_SIZE : inl;
0f113f3e 365
f95fec29 366 if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
0f113f3e
MC
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 =
f95fec29 381 EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
0f113f3e
MC
382 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
383 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
384 /*
385 * Since we're now done using the temporary buffer, the
386 * length should be 0'd
387 */
388 ctx->tmp_len = 0;
389 } else {
390 if (n < 3) {
391 memcpy(ctx->tmp, in, n);
392 ctx->tmp_len = n;
393 ret += n;
394 break;
395 }
396 n -= n % 3;
397 ctx->buf_len =
f95fec29 398 EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
0f113f3e
MC
399 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
400 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
401 ret += n;
402 }
403 } else {
f95fec29
DDO
404 if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
405 (unsigned char *)in, n))
406 return ret == 0 ? -1 : ret;
0f113f3e
MC
407 OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
408 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
409 ret += n;
410 }
411 inl -= n;
412 in += n;
413
414 ctx->buf_off = 0;
415 n = ctx->buf_len;
416 while (n > 0) {
a146ae55 417 i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
0f113f3e
MC
418 if (i <= 0) {
419 BIO_copy_next_retry(b);
f95fec29 420 return ret == 0 ? i : ret;
0f113f3e
MC
421 }
422 OPENSSL_assert(i <= n);
423 n -= i;
424 ctx->buf_off += i;
425 OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
426 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
427 }
428 ctx->buf_len = 0;
429 ctx->buf_off = 0;
430 }
26a7d938 431 return ret;
0f113f3e 432}
d02b48c6 433
0e1c0612 434static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
435{
436 BIO_B64_CTX *ctx;
437 long ret = 1;
438 int i;
a146ae55 439 BIO *next;
0f113f3e 440
a146ae55
MC
441 ctx = (BIO_B64_CTX *)BIO_get_data(b);
442 next = BIO_next(b);
f95fec29 443 if (ctx == NULL || next == NULL)
a146ae55 444 return 0;
0f113f3e
MC
445
446 switch (cmd) {
447 case BIO_CTRL_RESET:
448 ctx->cont = 1;
449 ctx->start = 1;
450 ctx->encode = B64_NONE;
a146ae55 451 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
452 break;
453 case BIO_CTRL_EOF: /* More to read */
454 if (ctx->cont <= 0)
455 ret = 1;
456 else
a146ae55 457 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
458 break;
459 case BIO_CTRL_WPENDING: /* More to write in buffer */
460 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
461 ret = ctx->buf_len - ctx->buf_off;
f95fec29
DDO
462 if (ret == 0 && ctx->encode != B64_NONE
463 && EVP_ENCODE_CTX_num(ctx->base64) != 0)
0f113f3e
MC
464 ret = 1;
465 else if (ret <= 0)
a146ae55 466 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
467 break;
468 case BIO_CTRL_PENDING: /* More to read in buffer */
469 OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
470 ret = ctx->buf_len - ctx->buf_off;
471 if (ret <= 0)
a146ae55 472 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
473 break;
474 case BIO_CTRL_FLUSH:
475 /* do a final write */
476 again:
477 while (ctx->buf_len != ctx->buf_off) {
478 i = b64_write(b, NULL, 0);
479 if (i < 0)
480 return i;
481 }
482 if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
483 if (ctx->tmp_len != 0) {
f95fec29
DDO
484 ctx->buf_len = EVP_EncodeBlock(ctx->buf,
485 ctx->tmp, ctx->tmp_len);
0f113f3e
MC
486 ctx->buf_off = 0;
487 ctx->tmp_len = 0;
488 goto again;
489 }
b518d2d5
RL
490 } else if (ctx->encode != B64_NONE
491 && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
0f113f3e 492 ctx->buf_off = 0;
f95fec29 493 EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
0f113f3e
MC
494 /* push out the bytes */
495 goto again;
496 }
497 /* Finally flush the underlying BIO */
a146ae55 498 ret = BIO_ctrl(next, cmd, num, ptr);
bcbc7d60 499 BIO_copy_next_retry(b);
0f113f3e
MC
500 break;
501
502 case BIO_C_DO_STATE_MACHINE:
503 BIO_clear_retry_flags(b);
a146ae55 504 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
505 BIO_copy_next_retry(b);
506 break;
507
508 case BIO_CTRL_DUP:
509 break;
510 case BIO_CTRL_INFO:
511 case BIO_CTRL_GET:
512 case BIO_CTRL_SET:
513 default:
a146ae55 514 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
515 break;
516 }
a146ae55 517 return ret;
0f113f3e 518}
d02b48c6 519
fce78bd4 520static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e 521{
a146ae55 522 BIO *next = BIO_next(b);
0f113f3e 523
a146ae55
MC
524 if (next == NULL)
525 return 0;
1f2235ea
D
526
527 return BIO_callback_ctrl(next, cmd, fp);
0f113f3e 528}
d3442bc7 529
cb877ccb 530static int b64_puts(BIO *b, const char *str)
0f113f3e
MC
531{
532 return b64_write(b, str, strlen(str));
533}