Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/evp/bio_b64.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 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
148k
#define B64_BLOCK_SIZE  1024
25
#define B64_BLOCK_SIZE2 768
26
6.36k
#define B64_NONE        0
27
2.98k
#define B64_ENCODE      1
28
3.41M
#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
6.09k
{
63
6.09k
    return &methods_b64;
64
6.09k
}
65
66
static int b64_new(BIO *bi)
67
6.09k
{
68
6.09k
    BIO_B64_CTX *ctx;
69
70
6.09k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
71
0
        return 0;
72
73
6.09k
    ctx->cont = 1;
74
6.09k
    ctx->start = 1;
75
6.09k
    ctx->base64 = EVP_ENCODE_CTX_new();
76
6.09k
    if (ctx->base64 == NULL) {
77
0
        OPENSSL_free(ctx);
78
0
        return 0;
79
0
    }
80
81
6.09k
    BIO_set_data(bi, ctx);
82
6.09k
    BIO_set_init(bi, 1);
83
84
6.09k
    return 1;
85
6.09k
}
86
87
static int b64_free(BIO *a)
88
6.09k
{
89
6.09k
    BIO_B64_CTX *ctx;
90
91
6.09k
    if (a == NULL)
92
0
        return 0;
93
94
6.09k
    ctx = BIO_get_data(a);
95
6.09k
    if (ctx == NULL)
96
0
        return 0;
97
98
6.09k
    EVP_ENCODE_CTX_free(ctx->base64);
99
6.09k
    OPENSSL_free(ctx);
100
6.09k
    BIO_set_data(a, NULL);
101
6.09k
    BIO_set_init(a, 0);
102
103
6.09k
    return 1;
104
6.09k
}
105
106
static int b64_read(BIO *b, char *out, int outl)
107
3.41M
{
108
3.41M
    int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
109
3.41M
    BIO_B64_CTX *ctx;
110
3.41M
    unsigned char *p, *q;
111
3.41M
    BIO *next;
112
113
3.41M
    if (out == NULL)
114
0
        return 0;
115
3.41M
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
116
117
3.41M
    next = BIO_next(b);
118
3.41M
    if (ctx == NULL || next == NULL)
119
0
        return 0;
120
121
3.41M
    BIO_clear_retry_flags(b);
122
123
3.41M
    if (ctx->encode != B64_DECODE) {
124
2.99k
        ctx->encode = B64_DECODE;
125
2.99k
        ctx->buf_len = 0;
126
2.99k
        ctx->buf_off = 0;
127
2.99k
        ctx->tmp_len = 0;
128
2.99k
        EVP_DecodeInit(ctx->base64);
129
2.99k
    }
130
131
    /* First check if there are bytes decoded/encoded */
132
3.41M
    if (ctx->buf_len > 0) {
133
3.40M
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
134
3.40M
        i = ctx->buf_len - ctx->buf_off;
135
3.40M
        if (i > outl)
136
3.37M
            i = outl;
137
3.40M
        OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
138
3.40M
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
139
3.40M
        ret = i;
140
3.40M
        out += i;
141
3.40M
        outl -= i;
142
3.40M
        ctx->buf_off += i;
143
3.40M
        if (ctx->buf_len == ctx->buf_off) {
144
29.0k
            ctx->buf_len = 0;
145
29.0k
            ctx->buf_off = 0;
146
29.0k
        }
147
3.40M
    }
148
149
    /*
150
     * At this point, we have room of outl bytes and an empty buffer, so we
151
     * should read in some more.
152
     */
153
154
3.41M
    ret_code = 0;
155
3.46M
    while (outl > 0) {
156
53.8k
        if (ctx->cont <= 0)
157
1.34k
            break;
158
159
52.5k
        i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
160
52.5k
                     B64_BLOCK_SIZE - ctx->tmp_len);
161
162
52.5k
        if (i <= 0) {
163
2.25k
            ret_code = i;
164
165
            /* Should we continue next time we are called? */
166
2.25k
            if (!BIO_should_retry(next)) {
167
2.25k
                ctx->cont = i;
168
                /* If buffer empty break */
169
2.25k
                if (ctx->tmp_len == 0)
170
895
                    break;
171
                /* Fall through and process what we have */
172
1.35k
                else
173
1.35k
                    i = 0;
174
2.25k
            }
175
            /* else we retry and add more data to buffer */
176
0
            else
177
0
                break;
178
2.25k
        }
179
51.6k
        i += ctx->tmp_len;
180
51.6k
        ctx->tmp_len = i;
181
182
        /*
183
         * We need to scan, a line at a time until we have a valid line if we
184
         * are starting.
185
         */
186
51.6k
        if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
187
0
            ctx->tmp_len = 0;
188
51.6k
        } else if (ctx->start) {
189
4.14k
            q = p = ctx->tmp;
190
4.14k
            num = 0;
191
1.28M
            for (j = 0; j < i; j++) {
192
1.28M
                if (*(q++) != '\n')
193
1.26M
                    continue;
194
195
                /*
196
                 * due to a previous very long line, we need to keep on
197
                 * scanning for a '\n' before we even start looking for
198
                 * base64 encoded stuff.
199
                 */
200
15.8k
                if (ctx->tmp_nl) {
201
295
                    p = q;
202
295
                    ctx->tmp_nl = 0;
203
295
                    continue;
204
295
                }
205
206
15.5k
                k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
207
15.5k
                if (k <= 0 && num == 0 && ctx->start) {
208
12.7k
                    EVP_DecodeInit(ctx->base64);
209
12.7k
                } else {
210
2.85k
                    if (p != ctx->tmp) {
211
122
                        i -= p - ctx->tmp;
212
77.0k
                        for (x = 0; x < i; x++)
213
76.9k
                            ctx->tmp[x] = p[x];
214
122
                    }
215
2.85k
                    EVP_DecodeInit(ctx->base64);
216
2.85k
                    ctx->start = 0;
217
2.85k
                    break;
218
2.85k
                }
219
12.7k
                p = q;
220
12.7k
            }
221
222
            /* we fell off the end without starting */
223
4.14k
            if (j == i && num == 0) {
224
                /*
225
                 * Is this is one long chunk?, if so, keep on reading until a
226
                 * new line.
227
                 */
228
1.29k
                if (p == ctx->tmp) {
229
                    /* Check buffer full */
230
652
                    if (i == B64_BLOCK_SIZE) {
231
556
                        ctx->tmp_nl = 1;
232
556
                        ctx->tmp_len = 0;
233
556
                    }
234
652
                } else if (p != q) { /* finished on a '\n' */
235
519
                    n = q - p;
236
388k
                    for (ii = 0; ii < n; ii++)
237
387k
                        ctx->tmp[ii] = p[ii];
238
519
                    ctx->tmp_len = n;
239
519
                }
240
                /* else finished on a '\n' */
241
1.29k
                continue;
242
2.85k
            } else {
243
2.85k
                ctx->tmp_len = 0;
244
2.85k
            }
245
47.5k
        } else if (i < B64_BLOCK_SIZE && ctx->cont > 0) {
246
            /*
247
             * If buffer isn't full and we can retry then restart to read in
248
             * more data.
249
             */
250
1.22k
            continue;
251
1.22k
        }
252
253
49.1k
        if ((BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
254
0
            int z, jj;
255
256
0
            jj = i & ~3;        /* process per 4 */
257
0
            z = EVP_DecodeBlock(ctx->buf, ctx->tmp, jj);
258
0
            if (jj > 2) {
259
0
                if (ctx->tmp[jj - 1] == '=') {
260
0
                    z--;
261
0
                    if (ctx->tmp[jj - 2] == '=')
262
0
                        z--;
263
0
                }
264
0
            }
265
            /*
266
             * z is now number of output bytes and jj is the number consumed
267
             */
268
0
            if (jj != i) {
269
0
                memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
270
0
                ctx->tmp_len = i - jj;
271
0
            }
272
0
            ctx->buf_len = 0;
273
0
            if (z > 0) {
274
0
                ctx->buf_len = z;
275
0
            }
276
0
            i = z;
277
49.1k
        } else {
278
49.1k
            i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
279
49.1k
                                 ctx->tmp, i);
280
49.1k
            ctx->tmp_len = 0;
281
49.1k
        }
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
49.1k
        ctx->cont = i;
289
290
49.1k
        ctx->buf_off = 0;
291
49.1k
        if (i < 0) {
292
57
            ret_code = 0;
293
57
            ctx->buf_len = 0;
294
57
            break;
295
57
        }
296
297
49.0k
        if (ctx->buf_len <= outl)
298
18.7k
            i = ctx->buf_len;
299
30.3k
        else
300
30.3k
            i = outl;
301
302
49.0k
        memcpy(out, ctx->buf, i);
303
49.0k
        ret += i;
304
49.0k
        ctx->buf_off = i;
305
49.0k
        if (ctx->buf_off == ctx->buf_len) {
306
18.7k
            ctx->buf_len = 0;
307
18.7k
            ctx->buf_off = 0;
308
18.7k
        }
309
49.0k
        outl -= i;
310
49.0k
        out += i;
311
49.0k
    }
312
    /* BIO_clear_retry_flags(b); */
313
3.41M
    BIO_copy_next_retry(b);
314
3.41M
    return ret == 0 ? ret_code : ret;
315
3.41M
}
316
317
static int b64_write(BIO *b, const char *in, int inl)
318
1.49k
{
319
1.49k
    int ret = 0;
320
1.49k
    int n;
321
1.49k
    int i;
322
1.49k
    BIO_B64_CTX *ctx;
323
1.49k
    BIO *next;
324
325
1.49k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
326
1.49k
    next = BIO_next(b);
327
1.49k
    if (ctx == NULL || next == NULL)
328
0
        return 0;
329
330
1.49k
    BIO_clear_retry_flags(b);
331
332
1.49k
    if (ctx->encode != B64_ENCODE) {
333
1.49k
        ctx->encode = B64_ENCODE;
334
1.49k
        ctx->buf_len = 0;
335
1.49k
        ctx->buf_off = 0;
336
1.49k
        ctx->tmp_len = 0;
337
1.49k
        EVP_EncodeInit(ctx->base64);
338
1.49k
    }
339
340
1.49k
    OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
341
1.49k
    OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
342
1.49k
    OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
343
1.49k
    n = ctx->buf_len - ctx->buf_off;
344
1.49k
    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
1.49k
    ctx->buf_off = 0;
358
1.49k
    ctx->buf_len = 0;
359
360
1.49k
    if (in == NULL || inl <= 0)
361
1.49k
        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
8.97k
{
436
8.97k
    BIO_B64_CTX *ctx;
437
8.97k
    long ret = 1;
438
8.97k
    int i;
439
8.97k
    BIO *next;
440
441
8.97k
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
442
8.97k
    next = BIO_next(b);
443
8.97k
    if (ctx == NULL || next == NULL)
444
0
        return 0;
445
446
8.97k
    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
2.99k
    case BIO_CTRL_FLUSH:
475
        /* do a final write */
476
3.18k
 again:
477
4.67k
        while (ctx->buf_len != ctx->buf_off) {
478
1.49k
            i = b64_write(b, NULL, 0);
479
1.49k
            if (i < 0)
480
0
                return i;
481
1.49k
        }
482
3.18k
        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
3.18k
        } else if (ctx->encode != B64_NONE
491
3.18k
                   && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
492
193
            ctx->buf_off = 0;
493
193
            EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
494
            /* push out the bytes */
495
193
            goto again;
496
193
        }
497
        /* Finally flush the underlying BIO */
498
2.99k
        ret = BIO_ctrl(next, cmd, num, ptr);
499
2.99k
        BIO_copy_next_retry(b);
500
2.99k
        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
5.98k
    default:
514
5.98k
        ret = BIO_ctrl(next, cmd, num, ptr);
515
5.98k
        break;
516
8.97k
    }
517
8.97k
    return ret;
518
8.97k
}
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
}