Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/bio/bf_buff.c
Line
Count
Source (jump to first uncovered line)
1
/* crypto/bio/bf_buff.c */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <stdio.h>
60
#include <errno.h>
61
#include "cryptlib.h"
62
#include <openssl/bio.h>
63
64
static int buffer_write(BIO *h, const char *buf, int num);
65
static int buffer_read(BIO *h, char *buf, int size);
66
static int buffer_puts(BIO *h, const char *str);
67
static int buffer_gets(BIO *h, char *str, int size);
68
static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
69
static int buffer_new(BIO *h);
70
static int buffer_free(BIO *data);
71
static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
72
0
#define DEFAULT_BUFFER_SIZE     4096
73
74
static BIO_METHOD methods_buffer = {
75
    BIO_TYPE_BUFFER,
76
    "buffer",
77
    buffer_write,
78
    buffer_read,
79
    buffer_puts,
80
    buffer_gets,
81
    buffer_ctrl,
82
    buffer_new,
83
    buffer_free,
84
    buffer_callback_ctrl,
85
};
86
87
BIO_METHOD *BIO_f_buffer(void)
88
0
{
89
0
    return (&methods_buffer);
90
0
}
91
92
static int buffer_new(BIO *bi)
93
0
{
94
0
    BIO_F_BUFFER_CTX *ctx;
95
96
0
    ctx = (BIO_F_BUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_F_BUFFER_CTX));
97
0
    if (ctx == NULL)
98
0
        return (0);
99
0
    ctx->ibuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
100
0
    if (ctx->ibuf == NULL) {
101
0
        OPENSSL_free(ctx);
102
0
        return (0);
103
0
    }
104
0
    ctx->obuf = (char *)OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
105
0
    if (ctx->obuf == NULL) {
106
0
        OPENSSL_free(ctx->ibuf);
107
0
        OPENSSL_free(ctx);
108
0
        return (0);
109
0
    }
110
0
    ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
111
0
    ctx->obuf_size = DEFAULT_BUFFER_SIZE;
112
0
    ctx->ibuf_len = 0;
113
0
    ctx->ibuf_off = 0;
114
0
    ctx->obuf_len = 0;
115
0
    ctx->obuf_off = 0;
116
117
0
    bi->init = 1;
118
0
    bi->ptr = (char *)ctx;
119
0
    bi->flags = 0;
120
0
    return (1);
121
0
}
122
123
static int buffer_free(BIO *a)
124
0
{
125
0
    BIO_F_BUFFER_CTX *b;
126
127
0
    if (a == NULL)
128
0
        return (0);
129
0
    b = (BIO_F_BUFFER_CTX *)a->ptr;
130
0
    if (b->ibuf != NULL)
131
0
        OPENSSL_free(b->ibuf);
132
0
    if (b->obuf != NULL)
133
0
        OPENSSL_free(b->obuf);
134
0
    OPENSSL_free(a->ptr);
135
0
    a->ptr = NULL;
136
0
    a->init = 0;
137
0
    a->flags = 0;
138
0
    return (1);
139
0
}
140
141
static int buffer_read(BIO *b, char *out, int outl)
142
0
{
143
0
    int i, num = 0;
144
0
    BIO_F_BUFFER_CTX *ctx;
145
146
0
    if (out == NULL)
147
0
        return (0);
148
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
149
150
0
    if ((ctx == NULL) || (b->next_bio == NULL))
151
0
        return (0);
152
0
    num = 0;
153
0
    BIO_clear_retry_flags(b);
154
155
0
 start:
156
0
    i = ctx->ibuf_len;
157
    /* If there is stuff left over, grab it */
158
0
    if (i != 0) {
159
0
        if (i > outl)
160
0
            i = outl;
161
0
        memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
162
0
        ctx->ibuf_off += i;
163
0
        ctx->ibuf_len -= i;
164
0
        num += i;
165
0
        if (outl == i)
166
0
            return (num);
167
0
        outl -= i;
168
0
        out += i;
169
0
    }
170
171
    /*
172
     * We may have done a partial read. try to do more. We have nothing in
173
     * the buffer. If we get an error and have read some data, just return it
174
     * and let them retry to get the error again. copy direct to parent
175
     * address space
176
     */
177
0
    if (outl > ctx->ibuf_size) {
178
0
        for (;;) {
179
0
            i = BIO_read(b->next_bio, out, outl);
180
0
            if (i <= 0) {
181
0
                BIO_copy_next_retry(b);
182
0
                if (i < 0)
183
0
                    return ((num > 0) ? num : i);
184
0
                if (i == 0)
185
0
                    return (num);
186
0
            }
187
0
            num += i;
188
0
            if (outl == i)
189
0
                return (num);
190
0
            out += i;
191
0
            outl -= i;
192
0
        }
193
0
    }
194
    /* else */
195
196
    /* we are going to be doing some buffering */
197
0
    i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
198
0
    if (i <= 0) {
199
0
        BIO_copy_next_retry(b);
200
0
        if (i < 0)
201
0
            return ((num > 0) ? num : i);
202
0
        if (i == 0)
203
0
            return (num);
204
0
    }
205
0
    ctx->ibuf_off = 0;
206
0
    ctx->ibuf_len = i;
207
208
    /* Lets re-read using ourselves :-) */
209
0
    goto start;
210
0
}
211
212
static int buffer_write(BIO *b, const char *in, int inl)
213
0
{
214
0
    int i, num = 0;
215
0
    BIO_F_BUFFER_CTX *ctx;
216
217
0
    if ((in == NULL) || (inl <= 0))
218
0
        return (0);
219
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
220
0
    if ((ctx == NULL) || (b->next_bio == NULL))
221
0
        return (0);
222
223
0
    BIO_clear_retry_flags(b);
224
0
 start:
225
0
    i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
226
    /* add to buffer and return */
227
0
    if (i >= inl) {
228
0
        memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
229
0
        ctx->obuf_len += inl;
230
0
        return (num + inl);
231
0
    }
232
    /* else */
233
    /* stuff already in buffer, so add to it first, then flush */
234
0
    if (ctx->obuf_len != 0) {
235
0
        if (i > 0) {            /* lets fill it up if we can */
236
0
            memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
237
0
            in += i;
238
0
            inl -= i;
239
0
            num += i;
240
0
            ctx->obuf_len += i;
241
0
        }
242
        /* we now have a full buffer needing flushing */
243
0
        for (;;) {
244
0
            i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
245
0
                          ctx->obuf_len);
246
0
            if (i <= 0) {
247
0
                BIO_copy_next_retry(b);
248
249
0
                if (i < 0)
250
0
                    return ((num > 0) ? num : i);
251
0
                if (i == 0)
252
0
                    return (num);
253
0
            }
254
0
            ctx->obuf_off += i;
255
0
            ctx->obuf_len -= i;
256
0
            if (ctx->obuf_len == 0)
257
0
                break;
258
0
        }
259
0
    }
260
    /*
261
     * we only get here if the buffer has been flushed and we still have
262
     * stuff to write
263
     */
264
0
    ctx->obuf_off = 0;
265
266
    /* we now have inl bytes to write */
267
0
    while (inl >= ctx->obuf_size) {
268
0
        i = BIO_write(b->next_bio, in, inl);
269
0
        if (i <= 0) {
270
0
            BIO_copy_next_retry(b);
271
0
            if (i < 0)
272
0
                return ((num > 0) ? num : i);
273
0
            if (i == 0)
274
0
                return (num);
275
0
        }
276
0
        num += i;
277
0
        in += i;
278
0
        inl -= i;
279
0
        if (inl == 0)
280
0
            return (num);
281
0
    }
282
283
    /*
284
     * copy the rest into the buffer since we have only a small amount left
285
     */
286
0
    goto start;
287
0
}
288
289
static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
290
0
{
291
0
    BIO *dbio;
292
0
    BIO_F_BUFFER_CTX *ctx;
293
0
    long ret = 1;
294
0
    char *p1, *p2;
295
0
    int r, i, *ip;
296
0
    int ibs, obs;
297
298
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
299
300
0
    switch (cmd) {
301
0
    case BIO_CTRL_RESET:
302
0
        ctx->ibuf_off = 0;
303
0
        ctx->ibuf_len = 0;
304
0
        ctx->obuf_off = 0;
305
0
        ctx->obuf_len = 0;
306
0
        if (b->next_bio == NULL)
307
0
            return (0);
308
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
309
0
        break;
310
0
    case BIO_CTRL_INFO:
311
0
        ret = (long)ctx->obuf_len;
312
0
        break;
313
0
    case BIO_C_GET_BUFF_NUM_LINES:
314
0
        ret = 0;
315
0
        p1 = ctx->ibuf;
316
0
        for (i = 0; i < ctx->ibuf_len; i++) {
317
0
            if (p1[ctx->ibuf_off + i] == '\n')
318
0
                ret++;
319
0
        }
320
0
        break;
321
0
    case BIO_CTRL_WPENDING:
322
0
        ret = (long)ctx->obuf_len;
323
0
        if (ret == 0) {
324
0
            if (b->next_bio == NULL)
325
0
                return (0);
326
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
327
0
        }
328
0
        break;
329
0
    case BIO_CTRL_PENDING:
330
0
        ret = (long)ctx->ibuf_len;
331
0
        if (ret == 0) {
332
0
            if (b->next_bio == NULL)
333
0
                return (0);
334
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
335
0
        }
336
0
        break;
337
0
    case BIO_C_SET_BUFF_READ_DATA:
338
0
        if (num > ctx->ibuf_size) {
339
0
            p1 = OPENSSL_malloc((int)num);
340
0
            if (p1 == NULL)
341
0
                goto malloc_error;
342
0
            if (ctx->ibuf != NULL)
343
0
                OPENSSL_free(ctx->ibuf);
344
0
            ctx->ibuf = p1;
345
0
        }
346
0
        ctx->ibuf_off = 0;
347
0
        ctx->ibuf_len = (int)num;
348
0
        memcpy(ctx->ibuf, ptr, (int)num);
349
0
        ret = 1;
350
0
        break;
351
0
    case BIO_C_SET_BUFF_SIZE:
352
0
        if (ptr != NULL) {
353
0
            ip = (int *)ptr;
354
0
            if (*ip == 0) {
355
0
                ibs = (int)num;
356
0
                obs = ctx->obuf_size;
357
0
            } else {            /* if (*ip == 1) */
358
359
0
                ibs = ctx->ibuf_size;
360
0
                obs = (int)num;
361
0
            }
362
0
        } else {
363
0
            ibs = (int)num;
364
0
            obs = (int)num;
365
0
        }
366
0
        p1 = ctx->ibuf;
367
0
        p2 = ctx->obuf;
368
0
        if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
369
0
            p1 = (char *)OPENSSL_malloc((int)num);
370
0
            if (p1 == NULL)
371
0
                goto malloc_error;
372
0
        }
373
0
        if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
374
0
            p2 = (char *)OPENSSL_malloc((int)num);
375
0
            if (p2 == NULL) {
376
0
                if (p1 != ctx->ibuf)
377
0
                    OPENSSL_free(p1);
378
0
                goto malloc_error;
379
0
            }
380
0
        }
381
0
        if (ctx->ibuf != p1) {
382
0
            OPENSSL_free(ctx->ibuf);
383
0
            ctx->ibuf = p1;
384
0
            ctx->ibuf_off = 0;
385
0
            ctx->ibuf_len = 0;
386
0
            ctx->ibuf_size = ibs;
387
0
        }
388
0
        if (ctx->obuf != p2) {
389
0
            OPENSSL_free(ctx->obuf);
390
0
            ctx->obuf = p2;
391
0
            ctx->obuf_off = 0;
392
0
            ctx->obuf_len = 0;
393
0
            ctx->obuf_size = obs;
394
0
        }
395
0
        break;
396
0
    case BIO_C_DO_STATE_MACHINE:
397
0
        if (b->next_bio == NULL)
398
0
            return (0);
399
0
        BIO_clear_retry_flags(b);
400
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
401
0
        BIO_copy_next_retry(b);
402
0
        break;
403
404
0
    case BIO_CTRL_FLUSH:
405
0
        if (b->next_bio == NULL)
406
0
            return (0);
407
0
        if (ctx->obuf_len <= 0) {
408
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
409
0
            break;
410
0
        }
411
412
0
        for (;;) {
413
0
            BIO_clear_retry_flags(b);
414
0
            if (ctx->obuf_len > 0) {
415
0
                r = BIO_write(b->next_bio,
416
0
                              &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
417
#if 0
418
                fprintf(stderr, "FLUSH [%3d] %3d -> %3d\n", ctx->obuf_off,
419
                        ctx->obuf_len, r);
420
#endif
421
0
                BIO_copy_next_retry(b);
422
0
                if (r <= 0)
423
0
                    return ((long)r);
424
0
                ctx->obuf_off += r;
425
0
                ctx->obuf_len -= r;
426
0
            } else {
427
0
                ctx->obuf_len = 0;
428
0
                ctx->obuf_off = 0;
429
0
                ret = 1;
430
0
                break;
431
0
            }
432
0
        }
433
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
434
0
        break;
435
0
    case BIO_CTRL_DUP:
436
0
        dbio = (BIO *)ptr;
437
0
        if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
438
0
            !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
439
0
            ret = 0;
440
0
        break;
441
0
    default:
442
0
        if (b->next_bio == NULL)
443
0
            return (0);
444
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
445
0
        break;
446
0
    }
447
0
    return (ret);
448
0
 malloc_error:
449
0
    BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
450
0
    return (0);
451
0
}
452
453
static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
454
0
{
455
0
    long ret = 1;
456
457
0
    if (b->next_bio == NULL)
458
0
        return (0);
459
0
    switch (cmd) {
460
0
    default:
461
0
        ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
462
0
        break;
463
0
    }
464
0
    return (ret);
465
0
}
466
467
static int buffer_gets(BIO *b, char *buf, int size)
468
0
{
469
0
    BIO_F_BUFFER_CTX *ctx;
470
0
    int num = 0, i, flag;
471
0
    char *p;
472
473
0
    ctx = (BIO_F_BUFFER_CTX *)b->ptr;
474
0
    size--;                     /* reserve space for a '\0' */
475
0
    BIO_clear_retry_flags(b);
476
477
0
    for (;;) {
478
0
        if (ctx->ibuf_len > 0) {
479
0
            p = &(ctx->ibuf[ctx->ibuf_off]);
480
0
            flag = 0;
481
0
            for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
482
0
                *(buf++) = p[i];
483
0
                if (p[i] == '\n') {
484
0
                    flag = 1;
485
0
                    i++;
486
0
                    break;
487
0
                }
488
0
            }
489
0
            num += i;
490
0
            size -= i;
491
0
            ctx->ibuf_len -= i;
492
0
            ctx->ibuf_off += i;
493
0
            if (flag || size == 0) {
494
0
                *buf = '\0';
495
0
                return (num);
496
0
            }
497
0
        } else {                /* read another chunk */
498
499
0
            i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
500
0
            if (i <= 0) {
501
0
                BIO_copy_next_retry(b);
502
0
                *buf = '\0';
503
0
                if (i < 0)
504
0
                    return ((num > 0) ? num : i);
505
0
                if (i == 0)
506
0
                    return (num);
507
0
            }
508
0
            ctx->ibuf_len = i;
509
0
            ctx->ibuf_off = 0;
510
0
        }
511
0
    }
512
0
}
513
514
static int buffer_puts(BIO *b, const char *str)
515
0
{
516
0
    return (buffer_write(b, str, strlen(str)));
517
0
}