Coverage Report

Created: 2026-02-22 06:11

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