Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/evp/bio_b64.c
Line
Count
Source
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
236k
#define B64_BLOCK_SIZE 1024
25
#define B64_BLOCK_SIZE2 768
26
16.1k
#define B64_NONE 0
27
7.27k
#define B64_ENCODE 1
28
4.68M
#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
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
        return 0;
72
73
12.3k
    ctx->cont = 1;
74
12.3k
    ctx->start = 1;
75
12.3k
    ctx->base64 = EVP_ENCODE_CTX_new();
76
12.3k
    if (ctx->base64 == NULL) {
77
0
        OPENSSL_free(ctx);
78
0
        return 0;
79
0
    }
80
81
12.3k
    BIO_set_data(bi, ctx);
82
12.3k
    BIO_set_init(bi, 1);
83
84
12.3k
    return 1;
85
12.3k
}
86
87
static int b64_free(BIO *a)
88
12.3k
{
89
12.3k
    BIO_B64_CTX *ctx;
90
91
12.3k
    if (a == NULL)
92
0
        return 0;
93
94
12.3k
    ctx = BIO_get_data(a);
95
12.3k
    if (ctx == NULL)
96
0
        return 0;
97
98
12.3k
    EVP_ENCODE_CTX_free(ctx->base64);
99
12.3k
    OPENSSL_free(ctx);
100
12.3k
    BIO_set_data(a, NULL);
101
12.3k
    BIO_set_init(a, 0);
102
103
12.3k
    return 1;
104
12.3k
}
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
 */
114
static int b64_read(BIO *b, char *out, int outl)
115
4.68M
{
116
4.68M
    int ret = 0, i, ii, j, k, x, n, num, ret_code;
117
4.68M
    BIO_B64_CTX *ctx;
118
4.68M
    unsigned char *p, *q;
119
4.68M
    BIO *next;
120
121
4.68M
    if (out == NULL)
122
0
        return 0;
123
4.68M
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
124
125
4.68M
    next = BIO_next(b);
126
4.68M
    if (ctx == NULL || next == NULL)
127
0
        return 0;
128
129
4.68M
    BIO_clear_retry_flags(b);
130
131
4.68M
    if (ctx->encode != B64_DECODE) {
132
5.21k
        ctx->encode = B64_DECODE;
133
5.21k
        ctx->buf_len = 0;
134
5.21k
        ctx->buf_off = 0;
135
5.21k
        ctx->tmp_len = 0;
136
5.21k
        EVP_DecodeInit(ctx->base64);
137
5.21k
    }
138
139
    /* First check if there are buffered bytes already decoded */
140
4.68M
    if (ctx->buf_len > 0) {
141
4.66M
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
142
4.66M
        i = ctx->buf_len - ctx->buf_off;
143
4.66M
        if (i > outl)
144
4.61M
            i = outl;
145
4.66M
        OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
146
4.66M
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
147
4.66M
        ret = i;
148
4.66M
        out += i;
149
4.66M
        outl -= i;
150
4.66M
        ctx->buf_off += i;
151
4.66M
        if (ctx->buf_len == ctx->buf_off) {
152
46.8k
            ctx->buf_len = 0;
153
46.8k
            ctx->buf_off = 0;
154
46.8k
        }
155
4.66M
    }
156
157
    /* Restore any non-retriable error condition (ctx->cont < 0) */
158
4.68M
    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
4.76M
    while (outl > 0) {
165
87.1k
        int again = ctx->cont;
166
167
87.1k
        if (again <= 0)
168
1.81k
            break;
169
170
85.3k
        i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
171
85.3k
            B64_BLOCK_SIZE - ctx->tmp_len);
172
173
85.3k
        if (i <= 0) {
174
3.89k
            ret_code = i;
175
176
            /* Should we continue next time we are called? */
177
3.89k
            if (!BIO_should_retry(next)) {
178
                /* Incomplete final Base64 chunk in the decoder is an error */
179
3.89k
                if (ctx->tmp_len == 0) {
180
1.37k
                    if (EVP_DecodeFinal(ctx->base64, NULL, &num) < 0)
181
286
                        ret_code = -1;
182
1.37k
                    EVP_DecodeInit(ctx->base64);
183
1.37k
                }
184
3.89k
                ctx->cont = ret_code;
185
3.89k
            }
186
3.89k
            if (ctx->tmp_len == 0)
187
1.37k
                break;
188
            /* Fall through and process what we have */
189
2.51k
            i = 0;
190
            /* But don't loop to top-up even if the buffer is not full! */
191
2.51k
            again = 0;
192
2.51k
        }
193
194
83.9k
        i += ctx->tmp_len;
195
83.9k
        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
83.9k
        if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
202
0
            ctx->tmp_len = 0;
203
83.9k
        } else if (ctx->start) {
204
8.98k
            q = p = ctx->tmp;
205
8.98k
            num = 0;
206
3.97M
            for (j = 0; j < i; j++) {
207
3.96M
                if (*(q++) != '\n')
208
3.84M
                    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
120k
                if (ctx->tmp_nl) {
216
1.08k
                    p = q;
217
1.08k
                    ctx->tmp_nl = 0;
218
1.08k
                    continue;
219
1.08k
                }
220
221
119k
                k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
222
119k
                EVP_DecodeInit(ctx->base64);
223
119k
                if (k <= 0 && num == 0) {
224
114k
                    p = q;
225
114k
                    continue;
226
114k
                }
227
228
4.94k
                ctx->start = 0;
229
4.94k
                if (p != ctx->tmp) {
230
522
                    i -= p - ctx->tmp;
231
419k
                    for (x = 0; x < i; x++)
232
419k
                        ctx->tmp[x] = p[x];
233
522
                }
234
4.94k
                break;
235
119k
            }
236
237
            /* we fell off the end without starting */
238
8.98k
            if (ctx->start) {
239
                /*
240
                 * Is this is one long chunk?, if so, keep on reading until a
241
                 * new line.
242
                 */
243
4.03k
                if (p == ctx->tmp) {
244
                    /* Check buffer full */
245
1.74k
                    if (i == B64_BLOCK_SIZE) {
246
1.48k
                        ctx->tmp_nl = 1;
247
1.48k
                        ctx->tmp_len = 0;
248
1.48k
                    }
249
2.29k
                } else if (p != q) {
250
                    /* Retain partial line at end of buffer */
251
1.79k
                    n = q - p;
252
1.31M
                    for (ii = 0; ii < n; ii++)
253
1.31M
                        ctx->tmp[ii] = p[ii];
254
1.79k
                    ctx->tmp_len = n;
255
1.79k
                } else {
256
                    /* All we have is newline terminated non-start data */
257
498
                    ctx->tmp_len = 0;
258
498
                }
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
4.03k
                if (again > 0)
265
3.87k
                    continue;
266
166
                else
267
166
                    break;
268
4.94k
            } else {
269
4.94k
                ctx->tmp_len = 0;
270
4.94k
            }
271
74.9k
        } 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
2.35k
            continue;
277
2.35k
        }
278
279
77.5k
        i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
280
77.5k
            ctx->tmp, i);
281
77.5k
        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
77.5k
        ctx->cont = i;
289
290
77.5k
        ctx->buf_off = 0;
291
77.5k
        if (i < 0) {
292
241
            ret_code = ctx->start ? 0 : i;
293
241
            ctx->buf_len = 0;
294
241
            break;
295
241
        }
296
297
77.3k
        if (ctx->buf_len <= outl)
298
28.1k
            i = ctx->buf_len;
299
49.1k
        else
300
49.1k
            i = outl;
301
302
77.3k
        memcpy(out, ctx->buf, i);
303
77.3k
        ret += i;
304
77.3k
        ctx->buf_off = i;
305
77.3k
        if (ctx->buf_off == ctx->buf_len) {
306
28.1k
            ctx->buf_len = 0;
307
28.1k
            ctx->buf_off = 0;
308
28.1k
        }
309
77.3k
        outl -= i;
310
77.3k
        out += i;
311
77.3k
    }
312
    /* BIO_clear_retry_flags(b); */
313
4.68M
    BIO_copy_next_retry(b);
314
4.68M
    return ret == 0 ? ret_code : ret;
315
4.68M
}
316
317
static int b64_write(BIO *b, const char *in, int inl)
318
3.63k
{
319
3.63k
    int ret = 0;
320
3.63k
    int n;
321
3.63k
    int i;
322
3.63k
    BIO_B64_CTX *ctx;
323
3.63k
    BIO *next;
324
325
3.63k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
326
3.63k
    next = BIO_next(b);
327
3.63k
    if (ctx == NULL || next == NULL)
328
0
        return 0;
329
330
3.63k
    BIO_clear_retry_flags(b);
331
332
3.63k
    if (ctx->encode != B64_ENCODE) {
333
3.63k
        ctx->encode = B64_ENCODE;
334
3.63k
        ctx->buf_len = 0;
335
3.63k
        ctx->buf_off = 0;
336
3.63k
        ctx->tmp_len = 0;
337
3.63k
        EVP_EncodeInit(ctx->base64);
338
3.63k
    }
339
340
3.63k
    OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
341
3.63k
    OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
342
3.63k
    OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
343
3.63k
    n = ctx->buf_len - ctx->buf_off;
344
3.63k
    while (n > 0) {
345
0
        i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
346
0
        if (i <= 0) {
347
0
            BIO_copy_next_retry(b);
348
0
            return i;
349
0
        }
350
0
        OPENSSL_assert(i <= n);
351
0
        ctx->buf_off += i;
352
0
        OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
353
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
354
0
        n -= i;
355
0
    }
356
    /* at this point all pending data has been written */
357
3.63k
    ctx->buf_off = 0;
358
3.63k
    ctx->buf_len = 0;
359
360
3.63k
    if (in == NULL || inl <= 0)
361
3.63k
        return 0;
362
363
0
    while (inl > 0) {
364
0
        n = inl > B64_BLOCK_SIZE ? B64_BLOCK_SIZE : inl;
365
366
0
        if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
367
0
            if (ctx->tmp_len > 0) {
368
0
                OPENSSL_assert(ctx->tmp_len <= 3);
369
0
                n = 3 - ctx->tmp_len;
370
                /*
371
                 * There's a theoretical possibility for this
372
                 */
373
0
                if (n > inl)
374
0
                    n = inl;
375
0
                memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
376
0
                ctx->tmp_len += n;
377
0
                ret += n;
378
0
                if (ctx->tmp_len < 3)
379
0
                    break;
380
0
                ctx->buf_len = EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
381
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
382
0
                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
0
                ctx->tmp_len = 0;
388
0
            } else {
389
0
                if (n < 3) {
390
0
                    memcpy(ctx->tmp, in, n);
391
0
                    ctx->tmp_len = n;
392
0
                    ret += n;
393
0
                    break;
394
0
                }
395
0
                n -= n % 3;
396
0
                ctx->buf_len = EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
397
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
398
0
                OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
399
0
                ret += n;
400
0
            }
401
0
        } else {
402
0
            if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
403
0
                    (unsigned char *)in, n))
404
0
                return ret == 0 ? -1 : ret;
405
0
            OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
406
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
407
0
            ret += n;
408
0
        }
409
0
        inl -= n;
410
0
        in += n;
411
412
0
        ctx->buf_off = 0;
413
0
        n = ctx->buf_len;
414
0
        while (n > 0) {
415
0
            i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
416
0
            if (i <= 0) {
417
0
                BIO_copy_next_retry(b);
418
0
                return ret == 0 ? i : ret;
419
0
            }
420
0
            OPENSSL_assert(i <= n);
421
0
            n -= i;
422
0
            ctx->buf_off += i;
423
0
            OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
424
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
425
0
        }
426
0
        ctx->buf_len = 0;
427
0
        ctx->buf_off = 0;
428
0
    }
429
0
    return ret;
430
0
}
431
432
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
433
22.9k
{
434
22.9k
    BIO_B64_CTX *ctx;
435
22.9k
    long ret = 1;
436
22.9k
    int i;
437
22.9k
    BIO *next;
438
439
22.9k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
440
22.9k
    next = BIO_next(b);
441
22.9k
    if (ctx == NULL || next == NULL)
442
0
        return 0;
443
444
22.9k
    switch (cmd) {
445
0
    case BIO_CTRL_RESET:
446
0
        ctx->cont = 1;
447
0
        ctx->start = 1;
448
0
        ctx->encode = B64_NONE;
449
0
        ret = BIO_ctrl(next, cmd, num, ptr);
450
0
        break;
451
0
    case BIO_CTRL_EOF: /* More to read */
452
0
        if (ctx->cont <= 0)
453
0
            ret = 1;
454
0
        else
455
0
            ret = BIO_ctrl(next, cmd, num, ptr);
456
0
        break;
457
0
    case BIO_CTRL_WPENDING: /* More to write in buffer */
458
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
459
0
        ret = ctx->buf_len - ctx->buf_off;
460
0
        if (ret == 0 && ctx->encode != B64_NONE
461
0
            && EVP_ENCODE_CTX_num(ctx->base64) != 0)
462
0
            ret = 1;
463
0
        else if (ret <= 0)
464
0
            ret = BIO_ctrl(next, cmd, num, ptr);
465
0
        break;
466
0
    case BIO_CTRL_PENDING: /* More to read 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)
470
0
            ret = BIO_ctrl(next, cmd, num, ptr);
471
0
        break;
472
7.64k
    case BIO_CTRL_FLUSH:
473
        /* do a final write */
474
8.09k
    again:
475
11.7k
        while (ctx->buf_len != ctx->buf_off) {
476
3.63k
            i = b64_write(b, NULL, 0);
477
3.63k
            if (i < 0)
478
0
                return i;
479
3.63k
        }
480
8.09k
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
481
0
            if (ctx->tmp_len != 0) {
482
0
                ctx->buf_len = EVP_EncodeBlock(ctx->buf,
483
0
                    ctx->tmp, ctx->tmp_len);
484
0
                ctx->buf_off = 0;
485
0
                ctx->tmp_len = 0;
486
0
                goto again;
487
0
            }
488
8.09k
        } else if (ctx->encode != B64_NONE
489
8.09k
            && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
490
442
            ctx->buf_off = 0;
491
442
            EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
492
            /* push out the bytes */
493
442
            goto again;
494
442
        }
495
        /* Finally flush the underlying BIO */
496
7.64k
        ret = BIO_ctrl(next, cmd, num, ptr);
497
7.64k
        BIO_copy_next_retry(b);
498
7.64k
        break;
499
500
0
    case BIO_C_DO_STATE_MACHINE:
501
0
        BIO_clear_retry_flags(b);
502
0
        ret = BIO_ctrl(next, cmd, num, ptr);
503
0
        BIO_copy_next_retry(b);
504
0
        break;
505
506
0
    case BIO_CTRL_DUP:
507
0
        break;
508
0
    case BIO_CTRL_INFO:
509
0
    case BIO_CTRL_GET:
510
0
    case BIO_CTRL_SET:
511
15.2k
    default:
512
15.2k
        ret = BIO_ctrl(next, cmd, num, ptr);
513
15.2k
        break;
514
22.9k
    }
515
22.9k
    return ret;
516
22.9k
}
517
518
static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
519
0
{
520
0
    BIO *next = BIO_next(b);
521
522
0
    if (next == NULL)
523
0
        return 0;
524
525
0
    return BIO_callback_ctrl(next, cmd, fp);
526
0
}
527
528
static int b64_puts(BIO *b, const char *str)
529
0
{
530
0
    return b64_write(b, str, strlen(str));
531
0
}