Coverage Report

Created: 2025-04-22 06:18

/src/openssl/crypto/bio/bf_lbuf.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 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
#include <openssl/evp.h>
15
16
static int linebuffer_write(BIO *h, const char *buf, int num);
17
static int linebuffer_read(BIO *h, char *buf, int size);
18
static int linebuffer_puts(BIO *h, const char *str);
19
static int linebuffer_gets(BIO *h, char *str, int size);
20
static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
21
static int linebuffer_new(BIO *h);
22
static int linebuffer_free(BIO *data);
23
static long linebuffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
24
25
/* A 10k maximum should be enough for most purposes */
26
0
#define DEFAULT_LINEBUFFER_SIZE 1024*10
27
28
/* #define DEBUG */
29
30
static const BIO_METHOD methods_linebuffer = {
31
    BIO_TYPE_LINEBUFFER,
32
    "linebuffer",
33
    bwrite_conv,
34
    linebuffer_write,
35
    bread_conv,
36
    linebuffer_read,
37
    linebuffer_puts,
38
    linebuffer_gets,
39
    linebuffer_ctrl,
40
    linebuffer_new,
41
    linebuffer_free,
42
    linebuffer_callback_ctrl,
43
};
44
45
const BIO_METHOD *BIO_f_linebuffer(void)
46
0
{
47
0
    return &methods_linebuffer;
48
0
}
49
50
typedef struct bio_linebuffer_ctx_struct {
51
    char *obuf;                 /* the output char array */
52
    int obuf_size;              /* how big is the output buffer */
53
    int obuf_len;               /* how many bytes are in it */
54
} BIO_LINEBUFFER_CTX;
55
56
static int linebuffer_new(BIO *bi)
57
0
{
58
0
    BIO_LINEBUFFER_CTX *ctx;
59
60
0
    if ((ctx = OPENSSL_malloc(sizeof(*ctx))) == NULL) {
61
0
        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
62
0
        return 0;
63
0
    }
64
0
    ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
65
0
    if (ctx->obuf == NULL) {
66
0
        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
67
0
        OPENSSL_free(ctx);
68
0
        return 0;
69
0
    }
70
0
    ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
71
0
    ctx->obuf_len = 0;
72
73
0
    bi->init = 1;
74
0
    bi->ptr = (char *)ctx;
75
0
    bi->flags = 0;
76
0
    return 1;
77
0
}
78
79
static int linebuffer_free(BIO *a)
80
0
{
81
0
    BIO_LINEBUFFER_CTX *b;
82
83
0
    if (a == NULL)
84
0
        return 0;
85
0
    b = (BIO_LINEBUFFER_CTX *)a->ptr;
86
0
    OPENSSL_free(b->obuf);
87
0
    OPENSSL_free(a->ptr);
88
0
    a->ptr = NULL;
89
0
    a->init = 0;
90
0
    a->flags = 0;
91
0
    return 1;
92
0
}
93
94
static int linebuffer_read(BIO *b, char *out, int outl)
95
0
{
96
0
    int ret = 0;
97
98
0
    if (out == NULL)
99
0
        return 0;
100
0
    if (b->next_bio == NULL)
101
0
        return 0;
102
0
    ret = BIO_read(b->next_bio, out, outl);
103
0
    BIO_clear_retry_flags(b);
104
0
    BIO_copy_next_retry(b);
105
0
    return ret;
106
0
}
107
108
static int linebuffer_write(BIO *b, const char *in, int inl)
109
0
{
110
0
    int i, num = 0, foundnl;
111
0
    BIO_LINEBUFFER_CTX *ctx;
112
113
0
    if ((in == NULL) || (inl <= 0))
114
0
        return 0;
115
0
    ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
116
0
    if ((ctx == NULL) || (b->next_bio == NULL))
117
0
        return 0;
118
119
0
    BIO_clear_retry_flags(b);
120
121
0
    do {
122
0
        const char *p;
123
0
        char c;
124
125
0
        for (p = in, c = '\0'; p < in + inl && (c = *p) != '\n'; p++) ;
126
0
        if (c == '\n') {
127
0
            p++;
128
0
            foundnl = 1;
129
0
        } else
130
0
            foundnl = 0;
131
132
        /*
133
         * If a NL was found and we already have text in the save buffer,
134
         * concatenate them and write
135
         */
136
0
        while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
137
0
               && ctx->obuf_len > 0) {
138
0
            int orig_olen = ctx->obuf_len;
139
140
0
            i = ctx->obuf_size - ctx->obuf_len;
141
0
            if (p - in > 0) {
142
0
                if (i >= p - in) {
143
0
                    memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
144
0
                    ctx->obuf_len += p - in;
145
0
                    inl -= p - in;
146
0
                    num += p - in;
147
0
                    in = p;
148
0
                } else {
149
0
                    memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
150
0
                    ctx->obuf_len += i;
151
0
                    inl -= i;
152
0
                    in += i;
153
0
                    num += i;
154
0
                }
155
0
            }
156
0
            i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
157
0
            if (i <= 0) {
158
0
                ctx->obuf_len = orig_olen;
159
0
                BIO_copy_next_retry(b);
160
161
0
                if (i < 0)
162
0
                    return ((num > 0) ? num : i);
163
0
                if (i == 0)
164
0
                    return num;
165
0
            }
166
0
            if (i < ctx->obuf_len)
167
0
                memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
168
0
            ctx->obuf_len -= i;
169
0
        }
170
171
        /*
172
         * Now that the save buffer is emptied, let's write the input buffer
173
         * if a NL was found and there is anything to write.
174
         */
175
0
        if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
176
0
            i = BIO_write(b->next_bio, in, p - in);
177
0
            if (i <= 0) {
178
0
                BIO_copy_next_retry(b);
179
0
                if (i < 0)
180
0
                    return ((num > 0) ? num : i);
181
0
                if (i == 0)
182
0
                    return num;
183
0
            }
184
0
            num += i;
185
0
            in += i;
186
0
            inl -= i;
187
0
        }
188
0
    }
189
0
    while (foundnl && inl > 0);
190
    /*
191
     * We've written as much as we can.  The rest of the input buffer, if
192
     * any, is text that doesn't and with a NL and therefore needs to be
193
     * saved for the next trip.
194
     */
195
0
    if (inl > 0) {
196
0
        memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
197
0
        ctx->obuf_len += inl;
198
0
        num += inl;
199
0
    }
200
0
    return num;
201
0
}
202
203
static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
204
0
{
205
0
    BIO *dbio;
206
0
    BIO_LINEBUFFER_CTX *ctx;
207
0
    long ret = 1;
208
0
    char *p;
209
0
    int r;
210
0
    int obs;
211
212
0
    ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
213
214
0
    switch (cmd) {
215
0
    case BIO_CTRL_RESET:
216
0
        ctx->obuf_len = 0;
217
0
        if (b->next_bio == NULL)
218
0
            return 0;
219
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
220
0
        break;
221
0
    case BIO_CTRL_INFO:
222
0
        ret = (long)ctx->obuf_len;
223
0
        break;
224
0
    case BIO_CTRL_WPENDING:
225
0
        ret = (long)ctx->obuf_len;
226
0
        if (ret == 0) {
227
0
            if (b->next_bio == NULL)
228
0
                return 0;
229
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
230
0
        }
231
0
        break;
232
0
    case BIO_C_SET_BUFF_SIZE:
233
0
        if (num > INT_MAX)
234
0
            return 0;
235
0
        obs = (int)num;
236
0
        p = ctx->obuf;
237
0
        if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
238
0
            p = OPENSSL_malloc((size_t)obs);
239
0
            if (p == NULL)
240
0
                goto malloc_error;
241
0
        }
242
0
        if (ctx->obuf != p) {
243
0
            if (ctx->obuf_len > obs) {
244
0
                ctx->obuf_len = obs;
245
0
            }
246
0
            memcpy(p, ctx->obuf, ctx->obuf_len);
247
0
            OPENSSL_free(ctx->obuf);
248
0
            ctx->obuf = p;
249
0
            ctx->obuf_size = obs;
250
0
        }
251
0
        break;
252
0
    case BIO_C_DO_STATE_MACHINE:
253
0
        if (b->next_bio == NULL)
254
0
            return 0;
255
0
        BIO_clear_retry_flags(b);
256
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
257
0
        BIO_copy_next_retry(b);
258
0
        break;
259
260
0
    case BIO_CTRL_FLUSH:
261
0
        if (b->next_bio == NULL)
262
0
            return 0;
263
0
        if (ctx->obuf_len <= 0) {
264
0
            ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
265
0
            break;
266
0
        }
267
268
0
        for (;;) {
269
0
            BIO_clear_retry_flags(b);
270
0
            if (ctx->obuf_len > 0) {
271
0
                r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
272
0
                BIO_copy_next_retry(b);
273
0
                if (r <= 0)
274
0
                    return (long)r;
275
0
                if (r < ctx->obuf_len)
276
0
                    memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
277
0
                ctx->obuf_len -= r;
278
0
            } else {
279
0
                ctx->obuf_len = 0;
280
0
                break;
281
0
            }
282
0
        }
283
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
284
0
        break;
285
0
    case BIO_CTRL_DUP:
286
0
        dbio = (BIO *)ptr;
287
0
        if (BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
288
0
            ret = 0;
289
0
        break;
290
0
    default:
291
0
        if (b->next_bio == NULL)
292
0
            return 0;
293
0
        ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
294
0
        break;
295
0
    }
296
0
    return ret;
297
0
 malloc_error:
298
0
    ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
299
0
    return 0;
300
0
}
301
302
static long linebuffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
303
0
{
304
0
    if (b->next_bio == NULL)
305
0
        return 0;
306
0
    return BIO_callback_ctrl(b->next_bio, cmd, fp);
307
0
}
308
309
static int linebuffer_gets(BIO *b, char *buf, int size)
310
0
{
311
0
    if (b->next_bio == NULL)
312
0
        return 0;
313
0
    return BIO_gets(b->next_bio, buf, size);
314
0
}
315
316
static int linebuffer_puts(BIO *b, const char *str)
317
0
{
318
0
    return linebuffer_write(b, str, strlen(str));
319
0
}