Coverage Report

Created: 2023-09-25 06:41

/src/openssl/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
0
#define B64_BLOCK_SIZE  1024
25
#define B64_BLOCK_SIZE2 768
26
0
#define B64_NONE        0
27
0
#define B64_ENCODE      1
28
0
#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
0
{
63
0
    return &methods_b64;
64
0
}
65
66
static int b64_new(BIO *bi)
67
0
{
68
0
    BIO_B64_CTX *ctx;
69
70
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
71
0
        return 0;
72
73
0
    ctx->cont = 1;
74
0
    ctx->start = 1;
75
0
    ctx->base64 = EVP_ENCODE_CTX_new();
76
0
    if (ctx->base64 == NULL) {
77
0
        OPENSSL_free(ctx);
78
0
        return 0;
79
0
    }
80
81
0
    BIO_set_data(bi, ctx);
82
0
    BIO_set_init(bi, 1);
83
84
0
    return 1;
85
0
}
86
87
static int b64_free(BIO *a)
88
0
{
89
0
    BIO_B64_CTX *ctx;
90
91
0
    if (a == NULL)
92
0
        return 0;
93
94
0
    ctx = BIO_get_data(a);
95
0
    if (ctx == NULL)
96
0
        return 0;
97
98
0
    EVP_ENCODE_CTX_free(ctx->base64);
99
0
    OPENSSL_free(ctx);
100
0
    BIO_set_data(a, NULL);
101
0
    BIO_set_init(a, 0);
102
103
0
    return 1;
104
0
}
105
106
static int b64_read(BIO *b, char *out, int outl)
107
0
{
108
0
    int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
109
0
    BIO_B64_CTX *ctx;
110
0
    unsigned char *p, *q;
111
0
    BIO *next;
112
113
0
    if (out == NULL)
114
0
        return 0;
115
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
116
117
0
    next = BIO_next(b);
118
0
    if (ctx == NULL || next == NULL)
119
0
        return 0;
120
121
0
    BIO_clear_retry_flags(b);
122
123
0
    if (ctx->encode != B64_DECODE) {
124
0
        ctx->encode = B64_DECODE;
125
0
        ctx->buf_len = 0;
126
0
        ctx->buf_off = 0;
127
0
        ctx->tmp_len = 0;
128
0
        EVP_DecodeInit(ctx->base64);
129
0
    }
130
131
    /* First check if there are bytes decoded/encoded */
132
0
    if (ctx->buf_len > 0) {
133
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
134
0
        i = ctx->buf_len - ctx->buf_off;
135
0
        if (i > outl)
136
0
            i = outl;
137
0
        OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
138
0
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
139
0
        ret = i;
140
0
        out += i;
141
0
        outl -= i;
142
0
        ctx->buf_off += i;
143
0
        if (ctx->buf_len == ctx->buf_off) {
144
0
            ctx->buf_len = 0;
145
0
            ctx->buf_off = 0;
146
0
        }
147
0
    }
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
0
    ret_code = 0;
155
0
    while (outl > 0) {
156
0
        if (ctx->cont <= 0)
157
0
            break;
158
159
0
        i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
160
0
                     B64_BLOCK_SIZE - ctx->tmp_len);
161
162
0
        if (i <= 0) {
163
0
            ret_code = i;
164
165
            /* Should we continue next time we are called? */
166
0
            if (!BIO_should_retry(next)) {
167
0
                ctx->cont = i;
168
                /* If buffer empty break */
169
0
                if (ctx->tmp_len == 0)
170
0
                    break;
171
                /* Fall through and process what we have */
172
0
                else
173
0
                    i = 0;
174
0
            }
175
            /* else we retry and add more data to buffer */
176
0
            else
177
0
                break;
178
0
        }
179
0
        i += ctx->tmp_len;
180
0
        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
0
        if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) != 0) {
187
0
            ctx->tmp_len = 0;
188
0
        } else if (ctx->start) {
189
0
            q = p = ctx->tmp;
190
0
            num = 0;
191
0
            for (j = 0; j < i; j++) {
192
0
                if (*(q++) != '\n')
193
0
                    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
0
                if (ctx->tmp_nl) {
201
0
                    p = q;
202
0
                    ctx->tmp_nl = 0;
203
0
                    continue;
204
0
                }
205
206
0
                k = EVP_DecodeUpdate(ctx->base64, ctx->buf, &num, p, q - p);
207
0
                if (k <= 0 && num == 0 && ctx->start) {
208
0
                    EVP_DecodeInit(ctx->base64);
209
0
                } else {
210
0
                    if (p != ctx->tmp) {
211
0
                        i -= p - ctx->tmp;
212
0
                        for (x = 0; x < i; x++)
213
0
                            ctx->tmp[x] = p[x];
214
0
                    }
215
0
                    EVP_DecodeInit(ctx->base64);
216
0
                    ctx->start = 0;
217
0
                    break;
218
0
                }
219
0
                p = q;
220
0
            }
221
222
            /* we fell off the end without starting */
223
0
            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
0
                if (p == ctx->tmp) {
229
                    /* Check buffer full */
230
0
                    if (i == B64_BLOCK_SIZE) {
231
0
                        ctx->tmp_nl = 1;
232
0
                        ctx->tmp_len = 0;
233
0
                    }
234
0
                } else if (p != q) { /* finished on a '\n' */
235
0
                    n = q - p;
236
0
                    for (ii = 0; ii < n; ii++)
237
0
                        ctx->tmp[ii] = p[ii];
238
0
                    ctx->tmp_len = n;
239
0
                }
240
                /* else finished on a '\n' */
241
0
                continue;
242
0
            } else {
243
0
                ctx->tmp_len = 0;
244
0
            }
245
0
        } 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
0
            continue;
251
0
        }
252
253
0
        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
0
        } else {
278
0
            i = EVP_DecodeUpdate(ctx->base64, ctx->buf, &ctx->buf_len,
279
0
                                 ctx->tmp, i);
280
0
            ctx->tmp_len = 0;
281
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
0
        ctx->cont = i;
289
290
0
        ctx->buf_off = 0;
291
0
        if (i < 0) {
292
0
            ret_code = 0;
293
0
            ctx->buf_len = 0;
294
0
            break;
295
0
        }
296
297
0
        if (ctx->buf_len <= outl)
298
0
            i = ctx->buf_len;
299
0
        else
300
0
            i = outl;
301
302
0
        memcpy(out, ctx->buf, i);
303
0
        ret += i;
304
0
        ctx->buf_off = i;
305
0
        if (ctx->buf_off == ctx->buf_len) {
306
0
            ctx->buf_len = 0;
307
0
            ctx->buf_off = 0;
308
0
        }
309
0
        outl -= i;
310
0
        out += i;
311
0
    }
312
    /* BIO_clear_retry_flags(b); */
313
0
    BIO_copy_next_retry(b);
314
0
    return ret == 0 ? ret_code : ret;
315
0
}
316
317
static int b64_write(BIO *b, const char *in, int inl)
318
0
{
319
0
    int ret = 0;
320
0
    int n;
321
0
    int i;
322
0
    BIO_B64_CTX *ctx;
323
0
    BIO *next;
324
325
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
326
0
    next = BIO_next(b);
327
0
    if (ctx == NULL || next == NULL)
328
0
        return 0;
329
330
0
    BIO_clear_retry_flags(b);
331
332
0
    if (ctx->encode != B64_ENCODE) {
333
0
        ctx->encode = B64_ENCODE;
334
0
        ctx->buf_len = 0;
335
0
        ctx->buf_off = 0;
336
0
        ctx->tmp_len = 0;
337
0
        EVP_EncodeInit(ctx->base64);
338
0
    }
339
340
0
    OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
341
0
    OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
342
0
    OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
343
0
    n = ctx->buf_len - ctx->buf_off;
344
0
    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
0
    ctx->buf_off = 0;
358
0
    ctx->buf_len = 0;
359
360
0
    if (in == NULL || inl <= 0)
361
0
        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
0
{
436
0
    BIO_B64_CTX *ctx;
437
0
    long ret = 1;
438
0
    int i;
439
0
    BIO *next;
440
441
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
442
0
    next = BIO_next(b);
443
0
    if (ctx == NULL || next == NULL)
444
0
        return 0;
445
446
0
    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
0
    case BIO_CTRL_FLUSH:
475
        /* do a final write */
476
0
 again:
477
0
        while (ctx->buf_len != ctx->buf_off) {
478
0
            i = b64_write(b, NULL, 0);
479
0
            if (i < 0)
480
0
                return i;
481
0
        }
482
0
        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
0
        } else if (ctx->encode != B64_NONE
491
0
                   && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
492
0
            ctx->buf_off = 0;
493
0
            EVP_EncodeFinal(ctx->base64, ctx->buf, &(ctx->buf_len));
494
            /* push out the bytes */
495
0
            goto again;
496
0
        }
497
        /* Finally flush the underlying BIO */
498
0
        ret = BIO_ctrl(next, cmd, num, ptr);
499
0
        BIO_copy_next_retry(b);
500
0
        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
0
    default:
514
0
        ret = BIO_ctrl(next, cmd, num, ptr);
515
0
        break;
516
0
    }
517
0
    return ret;
518
0
}
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
}