Coverage Report

Created: 2025-06-22 06:56

/src/openssl/crypto/bio/bf_buff.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 "bio_local.h"
13
#include "internal/cryptlib.h"
14
15
static int buffer_write(BIO *h, const char *buf, int num);
16
static int buffer_read(BIO *h, char *buf, int size);
17
static int buffer_puts(BIO *h, const char *str);
18
static int buffer_gets(BIO *h, char *str, int size);
19
static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20
static int buffer_new(BIO *h);
21
static int buffer_free(BIO *data);
22
static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
23
0
#define DEFAULT_BUFFER_SIZE     4096
24
25
static const BIO_METHOD methods_buffer = {
26
    BIO_TYPE_BUFFER,
27
    "buffer",
28
    bwrite_conv,
29
    buffer_write,
30
    bread_conv,
31
    buffer_read,
32
    buffer_puts,
33
    buffer_gets,
34
    buffer_ctrl,
35
    buffer_new,
36
    buffer_free,
37
    buffer_callback_ctrl,
38
};
39
40
const BIO_METHOD *BIO_f_buffer(void)
41
0
{
42
0
    return &methods_buffer;
43
0
}
44
45
static int buffer_new(BIO *bi)
46
0
{
47
0
    BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
48
49
0
    if (ctx == NULL)
50
0
        return 0;
51
0
    ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
52
0
    ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
53
0
    if (ctx->ibuf == NULL) {
54
0
        OPENSSL_free(ctx);
55
0
        return 0;
56
0
    }
57
0
    ctx->obuf_size = DEFAULT_BUFFER_SIZE;
58
0
    ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
59
0
    if (ctx->obuf == NULL) {
60
0
        OPENSSL_free(ctx->ibuf);
61
0
        OPENSSL_free(ctx);
62
0
        return 0;
63
0
    }
64
65
0
    bi->init = 1;
66
0
    bi->ptr = (char *)ctx;
67
0
    bi->flags = 0;
68
0
    return 1;
69
0
}
70
71
static int buffer_free(BIO *a)
72
0
{
73
0
    BIO_F_BUFFER_CTX *b;
74
75
0
    if (a == NULL)
76
0
        return 0;
77
0
    b = (BIO_F_BUFFER_CTX *)a->ptr;
78
0
    OPENSSL_free(b->ibuf);
79
0
    OPENSSL_free(b->obuf);
80
0
    OPENSSL_free(a->ptr);
81
0
    a->ptr = NULL;
82
0
    a->init = 0;
83
0
    a->flags = 0;
84
0
    return 1;
85
0
}
86
87
static int buffer_read(BIO *b, char *out, int outl)
88
0
{
89
0
    int i, num = 0;
90
0
    BIO_F_BUFFER_CTX *ctx;
91
92
0
    if (out == NULL)
93
0
        return 0;
94
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
95
96
0
    if ((ctx == NULL) || (b->next_bio == NULL))
97
0
        return 0;
98
0
    num = 0;
99
0
    BIO_clear_retry_flags(b);
100
101
0
 start:
102
0
    i = ctx->ibuf_len;
103
    /* If there is stuff left over, grab it */
104
0
    if (i != 0) {
105
0
        if (i > outl)
106
0
            i = outl;
107
0
        memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
108
0
        ctx->ibuf_off += i;
109
0
        ctx->ibuf_len -= i;
110
0
        num += i;
111
0
        if (outl == i)
112
0
            return num;
113
0
        outl -= i;
114
0
        out += i;
115
0
    }
116
117
    /*
118
     * We may have done a partial read. try to do more. We have nothing in
119
     * the buffer. If we get an error and have read some data, just return it
120
     * and let them retry to get the error again. copy direct to parent
121
     * address space
122
     */
123
0
    if (outl > ctx->ibuf_size) {
124
0
        for (;;) {
125
0
            i = BIO_read(b->next_bio, out, outl);
126
0
            if (i <= 0) {
127
0
                BIO_copy_next_retry(b);
128
0
                if (i < 0)
129
0
                    return ((num > 0) ? num : i);
130
0
                if (i == 0)
131
0
                    return num;
132
0
            }
133
0
            num += i;
134
0
            if (outl == i)
135
0
                return num;
136
0
            out += i;
137
0
            outl -= i;
138
0
        }
139
0
    }
140
    /* else */
141
142
    /* we are going to be doing some buffering */
143
0
    i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
144
0
    if (i <= 0) {
145
0
        BIO_copy_next_retry(b);
146
0
        if (i < 0)
147
0
            return ((num > 0) ? num : i);
148
0
        if (i == 0)
149
0
            return num;
150
0
    }
151
0
    ctx->ibuf_off = 0;
152
0
    ctx->ibuf_len = i;
153
154
    /* Lets re-read using ourselves :-) */
155
0
    goto start;
156
0
}
157
158
static int buffer_write(BIO *b, const char *in, int inl)
159
0
{
160
0
    int i, num = 0;
161
0
    BIO_F_BUFFER_CTX *ctx;
162
163
0
    if ((in == NULL) || (inl <= 0))
164
0
        return 0;
165
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
166
0
    if ((ctx == NULL) || (b->next_bio == NULL))
167
0
        return 0;
168
169
0
    BIO_clear_retry_flags(b);
170
0
 start:
171
0
    i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
172
    /* add to buffer and return */
173
0
    if (i >= inl) {
174
0
        memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
175
0
        ctx->obuf_len += inl;
176
0
        return (num + inl);
177
0
    }
178
    /* else */
179
    /* stuff already in buffer, so add to it first, then flush */
180
0
    if (ctx->obuf_len != 0) {
181
0
        if (i > 0) {            /* lets fill it up if we can */
182
0
            memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
183
0
            in += i;
184
0
            inl -= i;
185
0
            num += i;
186
0
            ctx->obuf_len += i;
187
0
        }
188
        /* we now have a full buffer needing flushing */
189
0
        for (;;) {
190
0
            i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
191
0
                          ctx->obuf_len);
192
0
            if (i <= 0) {
193
0
                BIO_copy_next_retry(b);
194
195
0
                if (i < 0)
196
0
                    return ((num > 0) ? num : i);
197
0
                if (i == 0)
198
0
                    return num;
199
0
            }
200
0
            ctx->obuf_off += i;
201
0
            ctx->obuf_len -= i;
202
0
            if (ctx->obuf_len == 0)
203
0
                break;
204
0
        }
205
0
    }
206
    /*
207
     * we only get here if the buffer has been flushed and we still have
208
     * stuff to write
209
     */
210
0
    ctx->obuf_off = 0;
211
212
    /* we now have inl bytes to write */
213
0
    while (inl >= ctx->obuf_size) {
214
0
        i = BIO_write(b->next_bio, in, inl);
215
0
        if (i <= 0) {
216
0
            BIO_copy_next_retry(b);
217
0
            if (i < 0)
218
0
                return ((num > 0) ? num : i);
219
0
            if (i == 0)
220
0
                return num;
221
0
        }
222
0
        num += i;
223
0
        in += i;
224
0
        inl -= i;
225
0
        if (inl == 0)
226
0
            return num;
227
0
    }
228
229
    /*
230
     * copy the rest into the buffer since we have only a small amount left
231
     */
232
0
    goto start;
233
0
}
234
235
static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
236
0
{
237
0
    BIO *dbio;
238
0
    BIO_F_BUFFER_CTX *ctx;
239
0
    long ret = 1;
240
0
    char *p1, *p2;
241
0
    int r, i, *ip;
242
0
    int ibs, obs;
243
244
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
245
246
0
    switch (cmd) {
247
0
    case BIO_CTRL_RESET:
248
0
        ctx->ibuf_off = 0;
249
0
        ctx->ibuf_len = 0;
250
0
        ctx->obuf_off = 0;
251
0
        ctx->obuf_len = 0;
252
0
        if (b->next_bio == NULL)
253
0
            return 0;
254
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
255
0
        break;
256
0
    case BIO_CTRL_EOF:
257
0
        if (ctx->ibuf_len > 0)
258
0
            return 0;
259
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
260
0
        break;
261
0
    case BIO_CTRL_INFO:
262
0
        ret = (long)ctx->obuf_len;
263
0
        break;
264
0
    case BIO_C_GET_BUFF_NUM_LINES:
265
0
        ret = 0;
266
0
        p1 = ctx->ibuf;
267
0
        for (i = 0; i < ctx->ibuf_len; i++) {
268
0
            if (p1[ctx->ibuf_off + i] == '\n')
269
0
                ret++;
270
0
        }
271
0
        break;
272
0
    case BIO_CTRL_WPENDING:
273
0
        ret = (long)ctx->obuf_len;
274
0
        if (ret == 0) {
275
0
            if (b->next_bio == NULL)
276
0
                return 0;
277
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
278
0
        }
279
0
        break;
280
0
    case BIO_CTRL_PENDING:
281
0
        ret = (long)ctx->ibuf_len;
282
0
        if (ret == 0) {
283
0
            if (b->next_bio == NULL)
284
0
                return 0;
285
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
286
0
        }
287
0
        break;
288
0
    case BIO_C_SET_BUFF_READ_DATA:
289
0
        if (num > ctx->ibuf_size) {
290
0
            if (num <= 0)
291
0
                return 0;
292
0
            p1 = OPENSSL_malloc((size_t)num);
293
0
            if (p1 == NULL)
294
0
                return 0;
295
0
            OPENSSL_free(ctx->ibuf);
296
0
            ctx->ibuf = p1;
297
0
        }
298
0
        ctx->ibuf_off = 0;
299
0
        ctx->ibuf_len = (int)num;
300
0
        memcpy(ctx->ibuf, ptr, (int)num);
301
0
        ret = 1;
302
0
        break;
303
0
    case BIO_C_SET_BUFF_SIZE:
304
0
        if (ptr != NULL) {
305
0
            ip = (int *)ptr;
306
0
            if (*ip == 0) {
307
0
                ibs = (int)num;
308
0
                obs = ctx->obuf_size;
309
0
            } else {            /* if (*ip == 1) */
310
311
0
                ibs = ctx->ibuf_size;
312
0
                obs = (int)num;
313
0
            }
314
0
        } else {
315
0
            ibs = (int)num;
316
0
            obs = (int)num;
317
0
        }
318
0
        p1 = ctx->ibuf;
319
0
        p2 = ctx->obuf;
320
0
        if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
321
0
            if (num <= 0)
322
0
                return 0;
323
0
            p1 = OPENSSL_malloc((size_t)num);
324
0
            if (p1 == NULL)
325
0
                return 0;
326
0
        }
327
0
        if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
328
0
            p2 = OPENSSL_malloc((size_t)num);
329
0
            if (p2 == NULL) {
330
0
                if (p1 != ctx->ibuf)
331
0
                    OPENSSL_free(p1);
332
0
                return 0;
333
0
            }
334
0
        }
335
0
        if (ctx->ibuf != p1) {
336
0
            OPENSSL_free(ctx->ibuf);
337
0
            ctx->ibuf = p1;
338
0
            ctx->ibuf_off = 0;
339
0
            ctx->ibuf_len = 0;
340
0
            ctx->ibuf_size = ibs;
341
0
        }
342
0
        if (ctx->obuf != p2) {
343
0
            OPENSSL_free(ctx->obuf);
344
0
            ctx->obuf = p2;
345
0
            ctx->obuf_off = 0;
346
0
            ctx->obuf_len = 0;
347
0
            ctx->obuf_size = obs;
348
0
        }
349
0
        break;
350
0
    case BIO_C_DO_STATE_MACHINE:
351
0
        if (b->next_bio == NULL)
352
0
            return 0;
353
0
        BIO_clear_retry_flags(b);
354
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
355
0
        BIO_copy_next_retry(b);
356
0
        break;
357
358
0
    case BIO_CTRL_FLUSH:
359
0
        if (b->next_bio == NULL)
360
0
            return 0;
361
0
        if (ctx->obuf_len <= 0) {
362
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
363
0
            BIO_copy_next_retry(b);
364
0
            break;
365
0
        }
366
367
0
        for (;;) {
368
0
            BIO_clear_retry_flags(b);
369
0
            if (ctx->obuf_len > 0) {
370
0
                r = BIO_write(b->next_bio,
371
0
                              &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
372
0
                BIO_copy_next_retry(b);
373
0
                if (r <= 0)
374
0
                    return (long)r;
375
0
                ctx->obuf_off += r;
376
0
                ctx->obuf_len -= r;
377
0
            } else {
378
0
                ctx->obuf_len = 0;
379
0
                ctx->obuf_off = 0;
380
0
                break;
381
0
            }
382
0
        }
383
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
384
0
        BIO_copy_next_retry(b);
385
0
        break;
386
0
    case BIO_CTRL_DUP:
387
0
        dbio = (BIO *)ptr;
388
0
        if (BIO_set_read_buffer_size(dbio, ctx->ibuf_size) <= 0 ||
389
0
            BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
390
0
            ret = 0;
391
0
        break;
392
0
    case BIO_CTRL_PEEK:
393
        /* Ensure there's stuff in the input buffer */
394
0
        {
395
0
            char fake_buf[1];
396
0
            (void)buffer_read(b, fake_buf, 0);
397
0
        }
398
0
        if (num > ctx->ibuf_len)
399
0
            num = ctx->ibuf_len;
400
0
        memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num);
401
0
        ret = num;
402
0
        break;
403
0
    default:
404
0
        if (b->next_bio == NULL)
405
0
            return 0;
406
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
407
0
        break;
408
0
    }
409
0
    return ret;
410
0
}
411
412
static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
413
0
{
414
0
    if (b->next_bio == NULL)
415
0
        return 0;
416
0
    return BIO_callback_ctrl(b->next_bio, cmd, fp);
417
0
}
418
419
static int buffer_gets(BIO *b, char *buf, int size)
420
0
{
421
0
    BIO_F_BUFFER_CTX *ctx;
422
0
    int num = 0, i, flag;
423
0
    char *p;
424
425
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
426
0
    size--;                     /* reserve space for a '\0' */
427
0
    BIO_clear_retry_flags(b);
428
429
0
    for (;;) {
430
0
        if (ctx->ibuf_len > 0) {
431
0
            p = &(ctx->ibuf[ctx->ibuf_off]);
432
0
            flag = 0;
433
0
            for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
434
0
                *(buf++) = p[i];
435
0
                if (p[i] == '\n') {
436
0
                    flag = 1;
437
0
                    i++;
438
0
                    break;
439
0
                }
440
0
            }
441
0
            num += i;
442
0
            size -= i;
443
0
            ctx->ibuf_len -= i;
444
0
            ctx->ibuf_off += i;
445
0
            if (flag || size == 0) {
446
0
                *buf = '\0';
447
0
                return num;
448
0
            }
449
0
        } else {                /* read another chunk */
450
451
0
            i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
452
0
            if (i <= 0) {
453
0
                BIO_copy_next_retry(b);
454
0
                *buf = '\0';
455
0
                if (i < 0)
456
0
                    return ((num > 0) ? num : i);
457
0
                if (i == 0)
458
0
                    return num;
459
0
            }
460
0
            ctx->ibuf_len = i;
461
0
            ctx->ibuf_off = 0;
462
0
        }
463
0
    }
464
0
}
465
466
static int buffer_puts(BIO *b, const char *str)
467
0
{
468
0
    return buffer_write(b, str, strlen(str));
469
0
}