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