Coverage Report

Created: 2025-08-28 07:07

/src/openssl34/crypto/evp/bio_b64.c
Line
Count
Source (jump to first uncovered line)
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
154k
#define B64_BLOCK_SIZE  1024
25
#define B64_BLOCK_SIZE2 768
26
20.9k
#define B64_NONE        0
27
10.1k
#define B64_ENCODE      1
28
2.59M
#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.7k
{
63
12.7k
    return &methods_b64;
64
12.7k
}
65
66
static int b64_new(BIO *bi)
67
12.7k
{
68
12.7k
    BIO_B64_CTX *ctx;
69
70
12.7k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
71
0
        return 0;
72
73
12.7k
    ctx->cont = 1;
74
12.7k
    ctx->start = 1;
75
12.7k
    ctx->base64 = EVP_ENCODE_CTX_new();
76
12.7k
    if (ctx->base64 == NULL) {
77
0
        OPENSSL_free(ctx);
78
0
        return 0;
79
0
    }
80
81
12.7k
    BIO_set_data(bi, ctx);
82
12.7k
    BIO_set_init(bi, 1);
83
84
12.7k
    return 1;
85
12.7k
}
86
87
static int b64_free(BIO *a)
88
12.7k
{
89
12.7k
    BIO_B64_CTX *ctx;
90
91
12.7k
    if (a == NULL)
92
0
        return 0;
93
94
12.7k
    ctx = BIO_get_data(a);
95
12.7k
    if (ctx == NULL)
96
0
        return 0;
97
98
12.7k
    EVP_ENCODE_CTX_free(ctx->base64);
99
12.7k
    OPENSSL_free(ctx);
100
12.7k
    BIO_set_data(a, NULL);
101
12.7k
    BIO_set_init(a, 0);
102
103
12.7k
    return 1;
104
12.7k
}
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
2.58M
{
116
2.58M
    int ret = 0, i, ii, j, k, x, n, num, ret_code;
117
2.58M
    BIO_B64_CTX *ctx;
118
2.58M
    unsigned char *p, *q;
119
2.58M
    BIO *next;
120
121
2.58M
    if (out == NULL)
122
0
        return 0;
123
2.58M
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
124
125
2.58M
    next = BIO_next(b);
126
2.58M
    if (ctx == NULL || next == NULL)
127
0
        return 0;
128
129
2.58M
    BIO_clear_retry_flags(b);
130
131
2.58M
    if (ctx->encode != B64_DECODE) {
132
4.58k
        ctx->encode = B64_DECODE;
133
4.58k
        ctx->buf_len = 0;
134
4.58k
        ctx->buf_off = 0;
135
4.58k
        ctx->tmp_len = 0;
136
4.58k
        EVP_DecodeInit(ctx->base64);
137
4.58k
    }
138
139
    /* First check if there are buffered bytes already decoded */
140
2.58M
    if (ctx->buf_len > 0) {
141
2.57M
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
142
2.57M
        i = ctx->buf_len - ctx->buf_off;
143
2.57M
        if (i > outl)
144
2.54M
            i = outl;
145
2.57M
        OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
146
2.57M
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
147
2.57M
        ret = i;
148
2.57M
        out += i;
149
2.57M
        outl -= i;
150
2.57M
        ctx->buf_off += i;
151
2.57M
        if (ctx->buf_len == ctx->buf_off) {
152
25.5k
            ctx->buf_len = 0;
153
25.5k
            ctx->buf_off = 0;
154
25.5k
        }
155
2.57M
    }
156
157
    /* Restore any non-retriable error condition (ctx->cont < 0) */
158
2.58M
    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
2.64M
    while (outl > 0) {
165
59.2k
        int again = ctx->cont;
166
167
59.2k
        if (again <= 0)
168
1.79k
            break;
169
170
57.4k
        i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
171
57.4k
                     B64_BLOCK_SIZE - ctx->tmp_len);
172
173
57.4k
        if (i <= 0) {
174
3.29k
            ret_code = i;
175
176
            /* Should we continue next time we are called? */
177
3.29k
            if (!BIO_should_retry(next)) {
178
                /* Incomplete final Base64 chunk in the decoder is an error */
179
3.29k
                if (ctx->tmp_len == 0) {
180
1.27k
                    if (EVP_DecodeFinal(ctx->base64, NULL, &num) < 0)
181
273
                        ret_code = -1;
182
1.27k
                    EVP_DecodeInit(ctx->base64);
183
1.27k
                }
184
3.29k
                ctx->cont = ret_code;
185
3.29k
            }
186
3.29k
            if (ctx->tmp_len == 0)
187
1.27k
                break;
188
            /* Fall through and process what we have */
189
2.01k
            i = 0;
190
            /* But don't loop to top-up even if the buffer is not full! */
191
2.01k
            again = 0;
192
2.01k
        }
193
194
56.1k
        i += ctx->tmp_len;
195
56.1k
        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
56.1k
        if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
202
0
            ctx->tmp_len = 0;
203
56.1k
        } else if (ctx->start) {
204
8.31k
            q = p = ctx->tmp;
205
8.31k
            num = 0;
206
3.94M
            for (j = 0; j < i; j++) {
207
3.93M
                if (*(q++) != '\n')
208
3.81M
                    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
123k
                if (ctx->tmp_nl) {
216
796
                    p = q;
217
796
                    ctx->tmp_nl = 0;
218
796
                    continue;
219
796
                }
220
221
122k
                k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
222
122k
                EVP_DecodeInit(ctx->base64);
223
122k
                if (k <= 0 && num == 0) {
224
118k
                    p = q;
225
118k
                    continue;
226
118k
                }
227
228
4.30k
                ctx->start = 0;
229
4.30k
                if (p != ctx->tmp) {
230
618
                    i -= p - ctx->tmp;
231
455k
                    for (x = 0; x < i; x++)
232
454k
                        ctx->tmp[x] = p[x];
233
618
                }
234
4.30k
                break;
235
122k
            }
236
237
            /* we fell off the end without starting */
238
8.31k
            if (ctx->start) {
239
                /*
240
                 * Is this is one long chunk?, if so, keep on reading until a
241
                 * new line.
242
                 */
243
4.00k
                if (p == ctx->tmp) {
244
                    /* Check buffer full */
245
1.50k
                    if (i == B64_BLOCK_SIZE) {
246
1.23k
                        ctx->tmp_nl = 1;
247
1.23k
                        ctx->tmp_len = 0;
248
1.23k
                    }
249
2.50k
                } else if (p != q) {
250
                    /* Retain partial line at end of buffer */
251
2.03k
                    n = q - p;
252
1.24M
                    for (ii = 0; ii < n; ii++)
253
1.24M
                        ctx->tmp[ii] = p[ii];
254
2.03k
                    ctx->tmp_len = n;
255
2.03k
                } else {
256
                    /* All we have is newline terminated non-start data */
257
468
                    ctx->tmp_len = 0;
258
468
                }
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.00k
                if (again > 0)
265
3.83k
                    continue;
266
175
                else
267
175
                    break;
268
4.30k
            } else {
269
4.30k
                ctx->tmp_len = 0;
270
4.30k
            }
271
47.8k
        } 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
1.84k
            continue;
277
1.84k
        }
278
279
50.3k
        i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
280
50.3k
                             ctx->tmp, i);
281
50.3k
        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
50.3k
        ctx->cont = i;
289
290
50.3k
        ctx->buf_off = 0;
291
50.3k
        if (i < 0) {
292
265
            ret_code = ctx->start ? 0 : i;
293
265
            ctx->buf_len = 0;
294
265
            break;
295
265
        }
296
297
50.0k
        if (ctx->buf_len <= outl)
298
22.4k
            i = ctx->buf_len;
299
27.5k
        else
300
27.5k
            i = outl;
301
302
50.0k
        memcpy(out, ctx->buf, i);
303
50.0k
        ret += i;
304
50.0k
        ctx->buf_off = i;
305
50.0k
        if (ctx->buf_off == ctx->buf_len) {
306
22.4k
            ctx->buf_len = 0;
307
22.4k
            ctx->buf_off = 0;
308
22.4k
        }
309
50.0k
        outl -= i;
310
50.0k
        out += i;
311
50.0k
    }
312
    /* BIO_clear_retry_flags(b); */
313
2.58M
    BIO_copy_next_retry(b);
314
2.58M
    return ret == 0 ? ret_code : ret;
315
2.58M
}
316
317
static int b64_write(BIO *b, const char *in, int inl)
318
5.05k
{
319
5.05k
    int ret = 0;
320
5.05k
    int n;
321
5.05k
    int i;
322
5.05k
    BIO_B64_CTX *ctx;
323
5.05k
    BIO *next;
324
325
5.05k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
326
5.05k
    next = BIO_next(b);
327
5.05k
    if (ctx == NULL || next == NULL)
328
0
        return 0;
329
330
5.05k
    BIO_clear_retry_flags(b);
331
332
5.05k
    if (ctx->encode != B64_ENCODE) {
333
5.05k
        ctx->encode = B64_ENCODE;
334
5.05k
        ctx->buf_len = 0;
335
5.05k
        ctx->buf_off = 0;
336
5.05k
        ctx->tmp_len = 0;
337
5.05k
        EVP_EncodeInit(ctx->base64);
338
5.05k
    }
339
340
5.05k
    OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
341
5.05k
    OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
342
5.05k
    OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
343
5.05k
    n = ctx->buf_len - ctx->buf_off;
344
5.05k
    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
5.05k
    ctx->buf_off = 0;
358
5.05k
    ctx->buf_len = 0;
359
360
5.05k
    if (in == NULL || inl <= 0)
361
5.05k
        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 =
381
0
                    EVP_EncodeBlock(ctx->buf, ctx->tmp, ctx->tmp_len);
382
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
383
0
                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
0
                ctx->tmp_len = 0;
389
0
            } else {
390
0
                if (n < 3) {
391
0
                    memcpy(ctx->tmp, in, n);
392
0
                    ctx->tmp_len = n;
393
0
                    ret += n;
394
0
                    break;
395
0
                }
396
0
                n -= n % 3;
397
0
                ctx->buf_len =
398
0
                    EVP_EncodeBlock(ctx->buf, (unsigned char *)in, n);
399
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
400
0
                OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
401
0
                ret += n;
402
0
            }
403
0
        } else {
404
0
            if (!EVP_EncodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
405
0
                                  (unsigned char *)in, n))
406
0
                return ret == 0 ? -1 : ret;
407
0
            OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
408
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
409
0
            ret += n;
410
0
        }
411
0
        inl -= n;
412
0
        in += n;
413
414
0
        ctx->buf_off = 0;
415
0
        n = ctx->buf_len;
416
0
        while (n > 0) {
417
0
            i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
418
0
            if (i <= 0) {
419
0
                BIO_copy_next_retry(b);
420
0
                return ret == 0 ? i : ret;
421
0
            }
422
0
            OPENSSL_assert(i <= n);
423
0
            n -= i;
424
0
            ctx->buf_off += i;
425
0
            OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
426
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
427
0
        }
428
0
        ctx->buf_len = 0;
429
0
        ctx->buf_off = 0;
430
0
    }
431
0
    return ret;
432
0
}
433
434
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
435
29.4k
{
436
29.4k
    BIO_B64_CTX *ctx;
437
29.4k
    long ret = 1;
438
29.4k
    int i;
439
29.4k
    BIO *next;
440
441
29.4k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
442
29.4k
    next = BIO_next(b);
443
29.4k
    if (ctx == NULL || next == NULL)
444
0
        return 0;
445
446
29.4k
    switch (cmd) {
447
0
    case BIO_CTRL_RESET:
448
0
        ctx->cont = 1;
449
0
        ctx->start = 1;
450
0
        ctx->encode = B64_NONE;
451
0
        ret = BIO_ctrl(next, cmd, num, ptr);
452
0
        break;
453
0
    case BIO_CTRL_EOF:         /* More to read */
454
0
        if (ctx->cont <= 0)
455
0
            ret = 1;
456
0
        else
457
0
            ret = BIO_ctrl(next, cmd, num, ptr);
458
0
        break;
459
0
    case BIO_CTRL_WPENDING:    /* More to write in buffer */
460
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
461
0
        ret = ctx->buf_len - ctx->buf_off;
462
0
        if (ret == 0 && ctx->encode != B64_NONE
463
0
            && EVP_ENCODE_CTX_num(ctx->base64) != 0)
464
0
            ret = 1;
465
0
        else if (ret <= 0)
466
0
            ret = BIO_ctrl(next, cmd, num, ptr);
467
0
        break;
468
0
    case BIO_CTRL_PENDING:     /* More to read in buffer */
469
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
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
9.82k
    case BIO_CTRL_FLUSH:
475
        /* do a final write */
476
10.4k
 again:
477
15.5k
        while (ctx->buf_len != ctx->buf_off) {
478
5.05k
            i = b64_write(b, NULL, 0);
479
5.05k
            if (i < 0)
480
0
                return i;
481
5.05k
        }
482
10.4k
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
483
0
            if (ctx->tmp_len != 0) {
484
0
                ctx->buf_len = EVP_EncodeBlock(ctx->buf,
485
0
                                               ctx->tmp, ctx->tmp_len);
486
0
                ctx->buf_off = 0;
487
0
                ctx->tmp_len = 0;
488
0
                goto again;
489
0
            }
490
10.4k
        } else if (ctx->encode != B64_NONE
491
10.4k
                   && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
492
649
            ctx->buf_off = 0;
493
649
            EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
494
            /* push out the bytes */
495
649
            goto again;
496
649
        }
497
        /* Finally flush the underlying BIO */
498
9.82k
        ret = BIO_ctrl(next, cmd, num, ptr);
499
9.82k
        BIO_copy_next_retry(b);
500
9.82k
        break;
501
502
0
    case BIO_C_DO_STATE_MACHINE:
503
0
        BIO_clear_retry_flags(b);
504
0
        ret = BIO_ctrl(next, cmd, num, ptr);
505
0
        BIO_copy_next_retry(b);
506
0
        break;
507
508
0
    case BIO_CTRL_DUP:
509
0
        break;
510
0
    case BIO_CTRL_INFO:
511
0
    case BIO_CTRL_GET:
512
0
    case BIO_CTRL_SET:
513
19.6k
    default:
514
19.6k
        ret = BIO_ctrl(next, cmd, num, ptr);
515
19.6k
        break;
516
29.4k
    }
517
29.4k
    return ret;
518
29.4k
}
519
520
static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
521
0
{
522
0
    BIO *next = BIO_next(b);
523
524
0
    if (next == NULL)
525
0
        return 0;
526
527
0
    return BIO_callback_ctrl(next, cmd, fp);
528
0
}
529
530
static int b64_puts(BIO *b, const char *str)
531
0
{
532
0
    return b64_write(b, str, strlen(str));
533
0
}