Coverage Report

Created: 2026-02-14 07:20

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