Coverage Report

Created: 2018-08-29 13:53

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