Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/bio_b64.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
    char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
43
    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
    /* TODO: Convert to new style write function */
50
    bwrite_conv,
51
    b64_write,
52
    /* TODO: Convert to new style read function */
53
    bread_conv,
54
    b64_read,
55
    b64_puts,
56
    NULL,                       /* b64_gets, */
57
    b64_ctrl,
58
    b64_new,
59
    b64_free,
60
    b64_callback_ctrl,
61
};
62
63
64
const BIO_METHOD *BIO_f_base64(void)
65
0
{
66
0
    return &methods_b64;
67
0
}
68
69
static int b64_new(BIO *bi)
70
0
{
71
0
    BIO_B64_CTX *ctx;
72
0
73
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
74
0
        EVPerr(EVP_F_B64_NEW, ERR_R_MALLOC_FAILURE);
75
0
        return 0;
76
0
    }
77
0
78
0
    ctx->cont = 1;
79
0
    ctx->start = 1;
80
0
    ctx->base64 = EVP_ENCODE_CTX_new();
81
0
    if (ctx->base64 == NULL) {
82
0
        OPENSSL_free(ctx);
83
0
        return 0;
84
0
    }
85
0
86
0
    BIO_set_data(bi, ctx);
87
0
    BIO_set_init(bi, 1);
88
0
89
0
    return 1;
90
0
}
91
92
static int b64_free(BIO *a)
93
0
{
94
0
    BIO_B64_CTX *ctx;
95
0
    if (a == NULL)
96
0
        return 0;
97
0
98
0
    ctx = BIO_get_data(a);
99
0
    if (ctx == NULL)
100
0
        return 0;
101
0
102
0
    EVP_ENCODE_CTX_free(ctx->base64);
103
0
    OPENSSL_free(ctx);
104
0
    BIO_set_data(a, NULL);
105
0
    BIO_set_init(a, 0);
106
0
107
0
    return 1;
108
0
}
109
110
static int b64_read(BIO *b, char *out, int outl)
111
0
{
112
0
    int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
113
0
    BIO_B64_CTX *ctx;
114
0
    unsigned char *p, *q;
115
0
    BIO *next;
116
0
117
0
    if (out == NULL)
118
0
        return 0;
119
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
120
0
121
0
    next = BIO_next(b);
122
0
    if ((ctx == NULL) || (next == NULL))
123
0
        return 0;
124
0
125
0
    BIO_clear_retry_flags(b);
126
0
127
0
    if (ctx->encode != B64_DECODE) {
128
0
        ctx->encode = B64_DECODE;
129
0
        ctx->buf_len = 0;
130
0
        ctx->buf_off = 0;
131
0
        ctx->tmp_len = 0;
132
0
        EVP_DecodeInit(ctx->base64);
133
0
    }
134
0
135
0
    /* First check if there are bytes decoded/encoded */
136
0
    if (ctx->buf_len > 0) {
137
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
138
0
        i = ctx->buf_len - ctx->buf_off;
139
0
        if (i > outl)
140
0
            i = outl;
141
0
        OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
142
0
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
143
0
        ret = i;
144
0
        out += i;
145
0
        outl -= i;
146
0
        ctx->buf_off += i;
147
0
        if (ctx->buf_len == ctx->buf_off) {
148
0
            ctx->buf_len = 0;
149
0
            ctx->buf_off = 0;
150
0
        }
151
0
    }
152
0
153
0
    /*
154
0
     * At this point, we have room of outl bytes and an empty buffer, so we
155
0
     * should read in some more.
156
0
     */
157
0
158
0
    ret_code = 0;
159
0
    while (outl > 0) {
160
0
        if (ctx->cont <= 0)
161
0
            break;
162
0
163
0
        i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
164
0
                     B64_BLOCK_SIZE - ctx->tmp_len);
165
0
166
0
        if (i <= 0) {
167
0
            ret_code = i;
168
0
169
0
            /* Should we continue next time we are called? */
170
0
            if (!BIO_should_retry(next)) {
171
0
                ctx->cont = i;
172
0
                /* If buffer empty break */
173
0
                if (ctx->tmp_len == 0)
174
0
                    break;
175
0
                /* Fall through and process what we have */
176
0
                else
177
0
                    i = 0;
178
0
            }
179
0
            /* else we retry and add more data to buffer */
180
0
            else
181
0
                break;
182
0
        }
183
0
        i += ctx->tmp_len;
184
0
        ctx->tmp_len = i;
185
0
186
0
        /*
187
0
         * We need to scan, a line at a time until we have a valid line if we
188
0
         * are starting.
189
0
         */
190
0
        if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
191
0
            /* ctx->start=1; */
192
0
            ctx->tmp_len = 0;
193
0
        } else if (ctx->start) {
194
0
            q = p = (unsigned char *)ctx->tmp;
195
0
            num = 0;
196
0
            for (j = 0; j < i; j++) {
197
0
                if (*(q++) != '\n')
198
0
                    continue;
199
0
200
0
                /*
201
0
                 * due to a previous very long line, we need to keep on
202
0
                 * scanning for a '\n' before we even start looking for
203
0
                 * base64 encoded stuff.
204
0
                 */
205
0
                if (ctx->tmp_nl) {
206
0
                    p = q;
207
0
                    ctx->tmp_nl = 0;
208
0
                    continue;
209
0
                }
210
0
211
0
                k = EVP_DecodeUpdate(ctx->base64,
212
0
                                     (unsigned char *)ctx->buf,
213
0
                                     &num, p, q - p);
214
0
                if ((k <= 0) && (num == 0) && (ctx->start))
215
0
                    EVP_DecodeInit(ctx->base64);
216
0
                else {
217
0
                    if (p != (unsigned char *)
218
0
                        &(ctx->tmp[0])) {
219
0
                        i -= (p - (unsigned char *)
220
0
                              &(ctx->tmp[0]));
221
0
                        for (x = 0; x < i; x++)
222
0
                            ctx->tmp[x] = p[x];
223
0
                    }
224
0
                    EVP_DecodeInit(ctx->base64);
225
0
                    ctx->start = 0;
226
0
                    break;
227
0
                }
228
0
                p = q;
229
0
            }
230
0
231
0
            /* we fell off the end without starting */
232
0
            if ((j == i) && (num == 0)) {
233
0
                /*
234
0
                 * Is this is one long chunk?, if so, keep on reading until a
235
0
                 * new line.
236
0
                 */
237
0
                if (p == (unsigned char *)&(ctx->tmp[0])) {
238
0
                    /* Check buffer full */
239
0
                    if (i == B64_BLOCK_SIZE) {
240
0
                        ctx->tmp_nl = 1;
241
0
                        ctx->tmp_len = 0;
242
0
                    }
243
0
                } else if (p != q) { /* finished on a '\n' */
244
0
                    n = q - p;
245
0
                    for (ii = 0; ii < n; ii++)
246
0
                        ctx->tmp[ii] = p[ii];
247
0
                    ctx->tmp_len = n;
248
0
                }
249
0
                /* else finished on a '\n' */
250
0
                continue;
251
0
            } else {
252
0
                ctx->tmp_len = 0;
253
0
            }
254
0
        } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
255
0
            /*
256
0
             * If buffer isn't full and we can retry then restart to read in
257
0
             * more data.
258
0
             */
259
0
            continue;
260
0
        }
261
0
262
0
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
263
0
            int z, jj;
264
0
265
0
            jj = i & ~3;        /* process per 4 */
266
0
            z = EVP_DecodeBlock((unsigned char *)ctx->buf,
267
0
                                (unsigned char *)ctx->tmp, jj);
268
0
            if (jj > 2) {
269
0
                if (ctx->tmp[jj - 1] == '=') {
270
0
                    z--;
271
0
                    if (ctx->tmp[jj - 2] == '=')
272
0
                        z--;
273
0
                }
274
0
            }
275
0
            /*
276
0
             * z is now number of output bytes and jj is the number consumed
277
0
             */
278
0
            if (jj != i) {
279
0
                memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
280
0
                ctx->tmp_len = i - jj;
281
0
            }
282
0
            ctx->buf_len = 0;
283
0
            if (z > 0) {
284
0
                ctx->buf_len = z;
285
0
            }
286
0
            i = z;
287
0
        } else {
288
0
            i = EVP_DecodeUpdate(ctx->base64,
289
0
                                 (unsigned char *)ctx->buf, &ctx->buf_len,
290
0
                                 (unsigned char *)ctx->tmp, i);
291
0
            ctx->tmp_len = 0;
292
0
        }
293
0
        /*
294
0
         * If eof or an error was signalled, then the condition
295
0
         * 'ctx->cont <= 0' will prevent b64_read() from reading
296
0
         * more data on subsequent calls. This assignment was
297
0
         * deleted accidentally in commit 5562cfaca4f3.
298
0
         */
299
0
        ctx->cont = i;
300
0
301
0
        ctx->buf_off = 0;
302
0
        if (i < 0) {
303
0
            ret_code = 0;
304
0
            ctx->buf_len = 0;
305
0
            break;
306
0
        }
307
0
308
0
        if (ctx->buf_len <= outl)
309
0
            i = ctx->buf_len;
310
0
        else
311
0
            i = outl;
312
0
313
0
        memcpy(out, ctx->buf, i);
314
0
        ret += i;
315
0
        ctx->buf_off = i;
316
0
        if (ctx->buf_off == ctx->buf_len) {
317
0
            ctx->buf_len = 0;
318
0
            ctx->buf_off = 0;
319
0
        }
320
0
        outl -= i;
321
0
        out += i;
322
0
    }
323
0
    /* BIO_clear_retry_flags(b); */
324
0
    BIO_copy_next_retry(b);
325
0
    return ((ret == 0) ? ret_code : ret);
326
0
}
327
328
static int b64_write(BIO *b, const char *in, int inl)
329
0
{
330
0
    int ret = 0;
331
0
    int n;
332
0
    int i;
333
0
    BIO_B64_CTX *ctx;
334
0
    BIO *next;
335
0
336
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
337
0
    next = BIO_next(b);
338
0
    if ((ctx == NULL) || (next == NULL))
339
0
        return 0;
340
0
341
0
    BIO_clear_retry_flags(b);
342
0
343
0
    if (ctx->encode != B64_ENCODE) {
344
0
        ctx->encode = B64_ENCODE;
345
0
        ctx->buf_len = 0;
346
0
        ctx->buf_off = 0;
347
0
        ctx->tmp_len = 0;
348
0
        EVP_EncodeInit(ctx->base64);
349
0
    }
350
0
351
0
    OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
352
0
    OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
353
0
    OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
354
0
    n = ctx->buf_len - ctx->buf_off;
355
0
    while (n > 0) {
356
0
        i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
357
0
        if (i <= 0) {
358
0
            BIO_copy_next_retry(b);
359
0
            return i;
360
0
        }
361
0
        OPENSSL_assert(i <= n);
362
0
        ctx->buf_off += i;
363
0
        OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
364
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
365
0
        n -= i;
366
0
    }
367
0
    /* at this point all pending data has been written */
368
0
    ctx->buf_off = 0;
369
0
    ctx->buf_len = 0;
370
0
371
0
    if ((in == NULL) || (inl <= 0))
372
0
        return 0;
373
0
374
0
    while (inl > 0) {
375
0
        n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
376
0
377
0
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
378
0
            if (ctx->tmp_len > 0) {
379
0
                OPENSSL_assert(ctx->tmp_len <= 3);
380
0
                n = 3 - ctx->tmp_len;
381
0
                /*
382
0
                 * There's a theoretical possibility for this
383
0
                 */
384
0
                if (n > inl)
385
0
                    n = inl;
386
0
                memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
387
0
                ctx->tmp_len += n;
388
0
                ret += n;
389
0
                if (ctx->tmp_len < 3)
390
0
                    break;
391
0
                ctx->buf_len =
392
0
                    EVP_EncodeBlock((unsigned char *)ctx->buf,
393
0
                                    (unsigned char *)ctx->tmp, ctx->tmp_len);
394
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
395
0
                OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
396
0
                /*
397
0
                 * Since we're now done using the temporary buffer, the
398
0
                 * length should be 0'd
399
0
                 */
400
0
                ctx->tmp_len = 0;
401
0
            } else {
402
0
                if (n < 3) {
403
0
                    memcpy(ctx->tmp, in, n);
404
0
                    ctx->tmp_len = n;
405
0
                    ret += n;
406
0
                    break;
407
0
                }
408
0
                n -= n % 3;
409
0
                ctx->buf_len =
410
0
                    EVP_EncodeBlock((unsigned char *)ctx->buf,
411
0
                                    (const unsigned char *)in, n);
412
0
                OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
413
0
                OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
414
0
                ret += n;
415
0
            }
416
0
        } else {
417
0
            if (!EVP_EncodeUpdate(ctx->base64,
418
0
                                 (unsigned char *)ctx->buf, &ctx->buf_len,
419
0
                                 (unsigned char *)in, n))
420
0
                return ((ret == 0) ? -1 : ret);
421
0
            OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
422
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
423
0
            ret += n;
424
0
        }
425
0
        inl -= n;
426
0
        in += n;
427
0
428
0
        ctx->buf_off = 0;
429
0
        n = ctx->buf_len;
430
0
        while (n > 0) {
431
0
            i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
432
0
            if (i <= 0) {
433
0
                BIO_copy_next_retry(b);
434
0
                return ((ret == 0) ? i : ret);
435
0
            }
436
0
            OPENSSL_assert(i <= n);
437
0
            n -= i;
438
0
            ctx->buf_off += i;
439
0
            OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
440
0
            OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
441
0
        }
442
0
        ctx->buf_len = 0;
443
0
        ctx->buf_off = 0;
444
0
    }
445
0
    return ret;
446
0
}
447
448
static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
449
0
{
450
0
    BIO_B64_CTX *ctx;
451
0
    long ret = 1;
452
0
    int i;
453
0
    BIO *next;
454
0
455
0
    ctx = (BIO_B64_CTX *)BIO_get_data(b);
456
0
    next = BIO_next(b);
457
0
    if ((ctx == NULL) || (next == NULL))
458
0
        return 0;
459
0
460
0
    switch (cmd) {
461
0
    case BIO_CTRL_RESET:
462
0
        ctx->cont = 1;
463
0
        ctx->start = 1;
464
0
        ctx->encode = B64_NONE;
465
0
        ret = BIO_ctrl(next, cmd, num, ptr);
466
0
        break;
467
0
    case BIO_CTRL_EOF:         /* More to read */
468
0
        if (ctx->cont <= 0)
469
0
            ret = 1;
470
0
        else
471
0
            ret = BIO_ctrl(next, cmd, num, ptr);
472
0
        break;
473
0
    case BIO_CTRL_WPENDING:    /* More to write in buffer */
474
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
475
0
        ret = ctx->buf_len - ctx->buf_off;
476
0
        if ((ret == 0) && (ctx->encode != B64_NONE)
477
0
            && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
478
0
            ret = 1;
479
0
        else if (ret <= 0)
480
0
            ret = BIO_ctrl(next, cmd, num, ptr);
481
0
        break;
482
0
    case BIO_CTRL_PENDING:     /* More to read in buffer */
483
0
        OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
484
0
        ret = ctx->buf_len - ctx->buf_off;
485
0
        if (ret <= 0)
486
0
            ret = BIO_ctrl(next, cmd, num, ptr);
487
0
        break;
488
0
    case BIO_CTRL_FLUSH:
489
0
        /* do a final write */
490
0
 again:
491
0
        while (ctx->buf_len != ctx->buf_off) {
492
0
            i = b64_write(b, NULL, 0);
493
0
            if (i < 0)
494
0
                return i;
495
0
        }
496
0
        if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
497
0
            if (ctx->tmp_len != 0) {
498
0
                ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
499
0
                                               (unsigned char *)ctx->tmp,
500
0
                                               ctx->tmp_len);
501
0
                ctx->buf_off = 0;
502
0
                ctx->tmp_len = 0;
503
0
                goto again;
504
0
            }
505
0
        } else if (ctx->encode != B64_NONE
506
0
                   && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
507
0
            ctx->buf_off = 0;
508
0
            EVP_EncodeFinal(ctx->base64,
509
0
                            (unsigned char *)ctx->buf, &(ctx->buf_len));
510
0
            /* push out the bytes */
511
0
            goto again;
512
0
        }
513
0
        /* Finally flush the underlying BIO */
514
0
        ret = BIO_ctrl(next, cmd, num, ptr);
515
0
        break;
516
0
517
0
    case BIO_C_DO_STATE_MACHINE:
518
0
        BIO_clear_retry_flags(b);
519
0
        ret = BIO_ctrl(next, cmd, num, ptr);
520
0
        BIO_copy_next_retry(b);
521
0
        break;
522
0
523
0
    case BIO_CTRL_DUP:
524
0
        break;
525
0
    case BIO_CTRL_INFO:
526
0
    case BIO_CTRL_GET:
527
0
    case BIO_CTRL_SET:
528
0
    default:
529
0
        ret = BIO_ctrl(next, cmd, num, ptr);
530
0
        break;
531
0
    }
532
0
    return ret;
533
0
}
534
535
static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
536
0
{
537
0
    long ret = 1;
538
0
    BIO *next = BIO_next(b);
539
0
540
0
    if (next == NULL)
541
0
        return 0;
542
0
    switch (cmd) {
543
0
    default:
544
0
        ret = BIO_callback_ctrl(next, cmd, fp);
545
0
        break;
546
0
    }
547
0
    return ret;
548
0
}
549
550
static int b64_puts(BIO *b, const char *str)
551
0
{
552
0
    return b64_write(b, str, strlen(str));
553
0
}