Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/evp/bio_enc.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 enc_write(BIO *h, const char *buf, int num);
18
static int enc_read(BIO *h, char *buf, int size);
19
static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20
static int enc_new(BIO *h);
21
static int enc_free(BIO *data);
22
static long enc_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fps);
23
0
#define ENC_BLOCK_SIZE  (1024*4)
24
0
#define ENC_MIN_CHUNK   (256)
25
0
#define BUF_OFFSET      (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH)
26
27
typedef struct enc_struct {
28
    int buf_len;
29
    int buf_off;
30
    int cont;                   /* <= 0 when finished */
31
    int finished;
32
    int ok;                     /* bad decrypt */
33
    EVP_CIPHER_CTX *cipher;
34
    unsigned char *read_start, *read_end;
35
    /*
36
     * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
37
     * up to a block more data than is presented to it
38
     */
39
    unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
40
} BIO_ENC_CTX;
41
42
static const BIO_METHOD methods_enc = {
43
    BIO_TYPE_CIPHER,
44
    "cipher",
45
    /* TODO: Convert to new style write function */
46
    bwrite_conv,
47
    enc_write,
48
    /* TODO: Convert to new style read function */
49
    bread_conv,
50
    enc_read,
51
    NULL,                       /* enc_puts, */
52
    NULL,                       /* enc_gets, */
53
    enc_ctrl,
54
    enc_new,
55
    enc_free,
56
    enc_callback_ctrl,
57
};
58
59
const BIO_METHOD *BIO_f_cipher(void)
60
0
{
61
0
    return &methods_enc;
62
0
}
63
64
static int enc_new(BIO *bi)
65
0
{
66
0
    BIO_ENC_CTX *ctx;
67
0
68
0
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
69
0
        EVPerr(EVP_F_ENC_NEW, ERR_R_MALLOC_FAILURE);
70
0
        return 0;
71
0
    }
72
0
73
0
    ctx->cipher = EVP_CIPHER_CTX_new();
74
0
    if (ctx->cipher == NULL) {
75
0
        OPENSSL_free(ctx);
76
0
        return 0;
77
0
    }
78
0
    ctx->cont = 1;
79
0
    ctx->ok = 1;
80
0
    ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
81
0
    BIO_set_data(bi, ctx);
82
0
    BIO_set_init(bi, 1);
83
0
84
0
    return 1;
85
0
}
86
87
static int enc_free(BIO *a)
88
0
{
89
0
    BIO_ENC_CTX *b;
90
0
91
0
    if (a == NULL)
92
0
        return 0;
93
0
94
0
    b = BIO_get_data(a);
95
0
    if (b == NULL)
96
0
        return 0;
97
0
98
0
    EVP_CIPHER_CTX_free(b->cipher);
99
0
    OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
100
0
    BIO_set_data(a, NULL);
101
0
    BIO_set_init(a, 0);
102
0
103
0
    return 1;
104
0
}
105
106
static int enc_read(BIO *b, char *out, int outl)
107
0
{
108
0
    int ret = 0, i, blocksize;
109
0
    BIO_ENC_CTX *ctx;
110
0
    BIO *next;
111
0
112
0
    if (out == NULL)
113
0
        return 0;
114
0
    ctx = BIO_get_data(b);
115
0
116
0
    next = BIO_next(b);
117
0
    if ((ctx == NULL) || (next == NULL))
118
0
        return 0;
119
0
120
0
    /* First check if there are bytes decoded/encoded */
121
0
    if (ctx->buf_len > 0) {
122
0
        i = ctx->buf_len - ctx->buf_off;
123
0
        if (i > outl)
124
0
            i = outl;
125
0
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
126
0
        ret = i;
127
0
        out += i;
128
0
        outl -= i;
129
0
        ctx->buf_off += i;
130
0
        if (ctx->buf_len == ctx->buf_off) {
131
0
            ctx->buf_len = 0;
132
0
            ctx->buf_off = 0;
133
0
        }
134
0
    }
135
0
136
0
    blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
137
0
    if (blocksize == 1)
138
0
        blocksize = 0;
139
0
140
0
    /*
141
0
     * At this point, we have room of outl bytes and an empty buffer, so we
142
0
     * should read in some more.
143
0
     */
144
0
145
0
    while (outl > 0) {
146
0
        if (ctx->cont <= 0)
147
0
            break;
148
0
149
0
        if (ctx->read_start == ctx->read_end) { /* time to read more data */
150
0
            ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
151
0
            i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
152
0
            if (i > 0)
153
0
                ctx->read_end += i;
154
0
        } else {
155
0
            i = ctx->read_end - ctx->read_start;
156
0
        }
157
0
158
0
        if (i <= 0) {
159
0
            /* Should be continue next time we are called? */
160
0
            if (!BIO_should_retry(next)) {
161
0
                ctx->cont = i;
162
0
                i = EVP_CipherFinal_ex(ctx->cipher,
163
0
                                       ctx->buf, &(ctx->buf_len));
164
0
                ctx->ok = i;
165
0
                ctx->buf_off = 0;
166
0
            } else {
167
0
                ret = (ret == 0) ? i : ret;
168
0
                break;
169
0
            }
170
0
        } else {
171
0
            if (outl > ENC_MIN_CHUNK) {
172
0
                /*
173
0
                 * Depending on flags block cipher decrypt can write
174
0
                 * one extra block and then back off, i.e. output buffer
175
0
                 * has to accommodate extra block...
176
0
                 */
177
0
                int j = outl - blocksize, buf_len;
178
0
179
0
                if (!EVP_CipherUpdate(ctx->cipher,
180
0
                                      (unsigned char *)out, &buf_len,
181
0
                                      ctx->read_start, i > j ? j : i)) {
182
0
                    BIO_clear_retry_flags(b);
183
0
                    return 0;
184
0
                }
185
0
                ret += buf_len;
186
0
                out += buf_len;
187
0
                outl -= buf_len;
188
0
189
0
                if ((i -= j) <= 0) {
190
0
                    ctx->read_start = ctx->read_end;
191
0
                    continue;
192
0
                }
193
0
                ctx->read_start += j;
194
0
            }
195
0
            if (i > ENC_MIN_CHUNK)
196
0
                i = ENC_MIN_CHUNK;
197
0
            if (!EVP_CipherUpdate(ctx->cipher,
198
0
                                  ctx->buf, &ctx->buf_len,
199
0
                                  ctx->read_start, i)) {
200
0
                BIO_clear_retry_flags(b);
201
0
                ctx->ok = 0;
202
0
                return 0;
203
0
            }
204
0
            ctx->read_start += i;
205
0
            ctx->cont = 1;
206
0
            /*
207
0
             * Note: it is possible for EVP_CipherUpdate to decrypt zero
208
0
             * bytes because this is or looks like the final block: if this
209
0
             * happens we should retry and either read more data or decrypt
210
0
             * the final block
211
0
             */
212
0
            if (ctx->buf_len == 0)
213
0
                continue;
214
0
        }
215
0
216
0
        if (ctx->buf_len <= outl)
217
0
            i = ctx->buf_len;
218
0
        else
219
0
            i = outl;
220
0
        if (i <= 0)
221
0
            break;
222
0
        memcpy(out, ctx->buf, i);
223
0
        ret += i;
224
0
        ctx->buf_off = i;
225
0
        outl -= i;
226
0
        out += i;
227
0
    }
228
0
229
0
    BIO_clear_retry_flags(b);
230
0
    BIO_copy_next_retry(b);
231
0
    return ((ret == 0) ? ctx->cont : ret);
232
0
}
233
234
static int enc_write(BIO *b, const char *in, int inl)
235
0
{
236
0
    int ret = 0, n, i;
237
0
    BIO_ENC_CTX *ctx;
238
0
    BIO *next;
239
0
240
0
    ctx = BIO_get_data(b);
241
0
    next = BIO_next(b);
242
0
    if ((ctx == NULL) || (next == NULL))
243
0
        return 0;
244
0
245
0
    ret = inl;
246
0
247
0
    BIO_clear_retry_flags(b);
248
0
    n = ctx->buf_len - ctx->buf_off;
249
0
    while (n > 0) {
250
0
        i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
251
0
        if (i <= 0) {
252
0
            BIO_copy_next_retry(b);
253
0
            return i;
254
0
        }
255
0
        ctx->buf_off += i;
256
0
        n -= i;
257
0
    }
258
0
    /* at this point all pending data has been written */
259
0
260
0
    if ((in == NULL) || (inl <= 0))
261
0
        return 0;
262
0
263
0
    ctx->buf_off = 0;
264
0
    while (inl > 0) {
265
0
        n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
266
0
        if (!EVP_CipherUpdate(ctx->cipher,
267
0
                              ctx->buf, &ctx->buf_len,
268
0
                              (const unsigned char *)in, n)) {
269
0
            BIO_clear_retry_flags(b);
270
0
            ctx->ok = 0;
271
0
            return 0;
272
0
        }
273
0
        inl -= n;
274
0
        in += n;
275
0
276
0
        ctx->buf_off = 0;
277
0
        n = ctx->buf_len;
278
0
        while (n > 0) {
279
0
            i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
280
0
            if (i <= 0) {
281
0
                BIO_copy_next_retry(b);
282
0
                return (ret == inl) ? i : ret - inl;
283
0
            }
284
0
            n -= i;
285
0
            ctx->buf_off += i;
286
0
        }
287
0
        ctx->buf_len = 0;
288
0
        ctx->buf_off = 0;
289
0
    }
290
0
    BIO_copy_next_retry(b);
291
0
    return ret;
292
0
}
293
294
static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
295
0
{
296
0
    BIO *dbio;
297
0
    BIO_ENC_CTX *ctx, *dctx;
298
0
    long ret = 1;
299
0
    int i;
300
0
    EVP_CIPHER_CTX **c_ctx;
301
0
    BIO *next;
302
0
303
0
    ctx = BIO_get_data(b);
304
0
    next = BIO_next(b);
305
0
    if (ctx == NULL)
306
0
        return 0;
307
0
308
0
    switch (cmd) {
309
0
    case BIO_CTRL_RESET:
310
0
        ctx->ok = 1;
311
0
        ctx->finished = 0;
312
0
        if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
313
0
                               EVP_CIPHER_CTX_encrypting(ctx->cipher)))
314
0
            return 0;
315
0
        ret = BIO_ctrl(next, cmd, num, ptr);
316
0
        break;
317
0
    case BIO_CTRL_EOF:         /* More to read */
318
0
        if (ctx->cont <= 0)
319
0
            ret = 1;
320
0
        else
321
0
            ret = BIO_ctrl(next, cmd, num, ptr);
322
0
        break;
323
0
    case BIO_CTRL_WPENDING:
324
0
        ret = ctx->buf_len - ctx->buf_off;
325
0
        if (ret <= 0)
326
0
            ret = BIO_ctrl(next, cmd, num, ptr);
327
0
        break;
328
0
    case BIO_CTRL_PENDING:     /* More to read in buffer */
329
0
        ret = ctx->buf_len - ctx->buf_off;
330
0
        if (ret <= 0)
331
0
            ret = BIO_ctrl(next, cmd, num, ptr);
332
0
        break;
333
0
    case BIO_CTRL_FLUSH:
334
0
        /* do a final write */
335
0
 again:
336
0
        while (ctx->buf_len != ctx->buf_off) {
337
0
            i = enc_write(b, NULL, 0);
338
0
            if (i < 0)
339
0
                return i;
340
0
        }
341
0
342
0
        if (!ctx->finished) {
343
0
            ctx->finished = 1;
344
0
            ctx->buf_off = 0;
345
0
            ret = EVP_CipherFinal_ex(ctx->cipher,
346
0
                                     (unsigned char *)ctx->buf,
347
0
                                     &(ctx->buf_len));
348
0
            ctx->ok = (int)ret;
349
0
            if (ret <= 0)
350
0
                break;
351
0
352
0
            /* push out the bytes */
353
0
            goto again;
354
0
        }
355
0
356
0
        /* Finally flush the underlying BIO */
357
0
        ret = BIO_ctrl(next, cmd, num, ptr);
358
0
        break;
359
0
    case BIO_C_GET_CIPHER_STATUS:
360
0
        ret = (long)ctx->ok;
361
0
        break;
362
0
    case BIO_C_DO_STATE_MACHINE:
363
0
        BIO_clear_retry_flags(b);
364
0
        ret = BIO_ctrl(next, cmd, num, ptr);
365
0
        BIO_copy_next_retry(b);
366
0
        break;
367
0
    case BIO_C_GET_CIPHER_CTX:
368
0
        c_ctx = (EVP_CIPHER_CTX **)ptr;
369
0
        *c_ctx = ctx->cipher;
370
0
        BIO_set_init(b, 1);
371
0
        break;
372
0
    case BIO_CTRL_DUP:
373
0
        dbio = (BIO *)ptr;
374
0
        dctx = BIO_get_data(dbio);
375
0
        dctx->cipher = EVP_CIPHER_CTX_new();
376
0
        if (dctx->cipher == NULL)
377
0
            return 0;
378
0
        ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
379
0
        if (ret)
380
0
            BIO_set_init(dbio, 1);
381
0
        break;
382
0
    default:
383
0
        ret = BIO_ctrl(next, cmd, num, ptr);
384
0
        break;
385
0
    }
386
0
    return ret;
387
0
}
388
389
static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
390
0
{
391
0
    long ret = 1;
392
0
    BIO *next = BIO_next(b);
393
0
394
0
    if (next == NULL)
395
0
        return 0;
396
0
    switch (cmd) {
397
0
    default:
398
0
        ret = BIO_callback_ctrl(next, cmd, fp);
399
0
        break;
400
0
    }
401
0
    return ret;
402
0
}
403
404
int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
405
                   const unsigned char *i, int e)
406
0
{
407
0
    BIO_ENC_CTX *ctx;
408
0
    long (*callback) (struct bio_st *, int, const char *, int, long, long);
409
0
410
0
    ctx = BIO_get_data(b);
411
0
    if (ctx == NULL)
412
0
        return 0;
413
0
414
0
    callback = BIO_get_callback(b);
415
0
416
0
    if ((callback != NULL) &&
417
0
            (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
418
0
                      0L) <= 0))
419
0
        return 0;
420
0
421
0
    BIO_set_init(b, 1);
422
0
423
0
    if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
424
0
        return 0;
425
0
426
0
    if (callback != NULL)
427
0
        return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
428
0
    return 1;
429
0
}