xref: /openssl/crypto/evp/bio_b64.c (revision 7ed6de99)
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 =
381                     EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
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 =
398                     EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
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 {
404             if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
405                                   (unsigned char *)in, n))
406                 return ret == 0 ? -1 : ret;
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) {
417             i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
418             if (i <= 0) {
419                 BIO_copy_next_retry(b);
420                 return ret == 0 ? i : ret;
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     }
431     return ret;
432 }
433 
b64_ctrl(BIO * b,int cmd,long num,void * ptr)434 static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
435 {
436     BIO_B64_CTX *ctx;
437     long ret = 1;
438     int i;
439     BIO *next;
440 
441     ctx = (BIO_B64_CTX *)BIO_get_data(b);
442     next = BIO_next(b);
443     if (ctx == NULL || next == NULL)
444         return 0;
445 
446     switch (cmd) {
447     case BIO_CTRL_RESET:
448         ctx->cont = 1;
449         ctx->start = 1;
450         ctx->encode = B64_NONE;
451         ret = BIO_ctrl(next, cmd, num, ptr);
452         break;
453     case BIO_CTRL_EOF:         /* More to read */
454         if (ctx->cont <= 0)
455             ret = 1;
456         else
457             ret = BIO_ctrl(next, cmd, num, ptr);
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;
462         if (ret == 0 && ctx->encode != B64_NONE
463             && EVP_ENCODE_CTX_num(ctx->base64) != 0)
464             ret = 1;
465         else if (ret <= 0)
466             ret = BIO_ctrl(next, cmd, num, ptr);
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)
472             ret = BIO_ctrl(next, cmd, num, ptr);
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) {
484                 ctx->buf_len = EVP_EncodeBlock(ctx->buf,
485                                                ctx->tmp, ctx->tmp_len);
486                 ctx->buf_off = 0;
487                 ctx->tmp_len = 0;
488                 goto again;
489             }
490         } else if (ctx->encode != B64_NONE
491                    && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
492             ctx->buf_off = 0;
493             EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
494             /* push out the bytes */
495             goto again;
496         }
497         /* Finally flush the underlying BIO */
498         ret = BIO_ctrl(next, cmd, num, ptr);
499         BIO_copy_next_retry(b);
500         break;
501 
502     case BIO_C_DO_STATE_MACHINE:
503         BIO_clear_retry_flags(b);
504         ret = BIO_ctrl(next, cmd, num, ptr);
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:
514         ret = BIO_ctrl(next, cmd, num, ptr);
515         break;
516     }
517     return ret;
518 }
519 
b64_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)520 static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
521 {
522     BIO *next = BIO_next(b);
523 
524     if (next == NULL)
525         return 0;
526 
527     return BIO_callback_ctrl(next, cmd, fp);
528 }
529 
b64_puts(BIO * b,const char * str)530 static int b64_puts(BIO *b, const char *str)
531 {
532     return b64_write(b, str, strlen(str));
533 }
534