Coverage Report

Created: 2025-06-13 06:58

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