Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bss_mem.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 mem_write(BIO *h, const char *buf, int num);
16
static int mem_read(BIO *h, char *buf, int size);
17
static int mem_puts(BIO *h, const char *str);
18
static int mem_gets(BIO *h, char *str, int size);
19
static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20
static int mem_new(BIO *h);
21
static int secmem_new(BIO *h);
22
static int mem_free(BIO *data);
23
static int mem_buf_free(BIO *data);
24
static int mem_buf_sync(BIO *h);
25
26
static const BIO_METHOD mem_method = {
27
    BIO_TYPE_MEM,
28
    "memory buffer",
29
    bwrite_conv,
30
    mem_write,
31
    bread_conv,
32
    mem_read,
33
    mem_puts,
34
    mem_gets,
35
    mem_ctrl,
36
    mem_new,
37
    mem_free,
38
    NULL, /* mem_callback_ctrl */
39
};
40
41
static const BIO_METHOD secmem_method = {
42
    BIO_TYPE_MEM,
43
    "secure memory buffer",
44
    bwrite_conv,
45
    mem_write,
46
    bread_conv,
47
    mem_read,
48
    mem_puts,
49
    mem_gets,
50
    mem_ctrl,
51
    secmem_new,
52
    mem_free,
53
    NULL, /* mem_callback_ctrl */
54
};
55
56
/*
57
 * BIO memory stores buffer and read pointer
58
 * however the roles are different for read only BIOs.
59
 * In that case the readp just stores the original state
60
 * to be used for reset.
61
 */
62
typedef struct bio_buf_mem_st {
63
    struct buf_mem_st *buf; /* allocated buffer */
64
    struct buf_mem_st *readp; /* read pointer */
65
} BIO_BUF_MEM;
66
67
/*
68
 * bio->num is used to hold the value to return on 'empty', if it is 0,
69
 * should_retry is not set
70
 */
71
72
const BIO_METHOD *BIO_s_mem(void)
73
8.64M
{
74
8.64M
    return &mem_method;
75
8.64M
}
76
77
const BIO_METHOD *BIO_s_secmem(void)
78
2.13k
{
79
2.13k
    return &secmem_method;
80
2.13k
}
81
82
BIO *BIO_new_mem_buf(const void *buf, int len)
83
2.62M
{
84
2.62M
    BIO *ret;
85
2.62M
    BUF_MEM *b;
86
2.62M
    BIO_BUF_MEM *bb;
87
2.62M
    size_t sz;
88
89
2.62M
    if (buf == NULL) {
90
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
91
0
        return NULL;
92
0
    }
93
2.62M
    sz = (len < 0) ? strlen(buf) : (size_t)len;
94
2.62M
    if ((ret = BIO_new(BIO_s_mem())) == NULL)
95
0
        return NULL;
96
2.62M
    bb = (BIO_BUF_MEM *)ret->ptr;
97
2.62M
    b = bb->buf;
98
    /* Cast away const and trust in the MEM_RDONLY flag. */
99
2.62M
    b->data = (void *)buf;
100
2.62M
    b->length = sz;
101
2.62M
    b->max = sz;
102
2.62M
    *bb->readp = *bb->buf;
103
2.62M
    ret->flags |= BIO_FLAGS_MEM_RDONLY;
104
    /* Since this is static data retrying won't help */
105
2.62M
    ret->num = 0;
106
2.62M
    return ret;
107
2.62M
}
108
109
static int mem_init(BIO *bi, unsigned long flags)
110
8.93M
{
111
8.93M
    BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
112
113
8.93M
    if (bb == NULL)
114
0
        return 0;
115
8.93M
    if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
116
0
        OPENSSL_free(bb);
117
0
        return 0;
118
0
    }
119
8.93M
    if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
120
0
        BUF_MEM_free(bb->buf);
121
0
        OPENSSL_free(bb);
122
0
        return 0;
123
0
    }
124
8.93M
    *bb->readp = *bb->buf;
125
8.93M
    bi->shutdown = 1;
126
8.93M
    bi->init = 1;
127
8.93M
    bi->num = -1;
128
8.93M
    bi->flags |= BIO_FLAGS_MEM_LEGACY_EOF;
129
8.93M
    bi->ptr = (char *)bb;
130
8.93M
    return 1;
131
8.93M
}
132
133
static int mem_new(BIO *bi)
134
8.93M
{
135
8.93M
    return mem_init(bi, 0L);
136
8.93M
}
137
138
static int secmem_new(BIO *bi)
139
4.26k
{
140
4.26k
    return mem_init(bi, BUF_MEM_FLAG_SECURE);
141
4.26k
}
142
143
static int mem_free(BIO *a)
144
8.93M
{
145
8.93M
    BIO_BUF_MEM *bb;
146
147
8.93M
    if (a == NULL)
148
0
        return 0;
149
150
8.93M
    bb = (BIO_BUF_MEM *)a->ptr;
151
8.93M
    if (!mem_buf_free(a))
152
0
        return 0;
153
8.93M
    OPENSSL_free(bb->readp);
154
8.93M
    OPENSSL_free(bb);
155
8.93M
    return 1;
156
8.93M
}
157
158
static int mem_buf_free(BIO *a)
159
8.93M
{
160
8.93M
    if (a == NULL)
161
0
        return 0;
162
163
8.93M
    if (a->shutdown && a->init && a->ptr != NULL) {
164
8.93M
        BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
165
8.93M
        BUF_MEM *b = bb->buf;
166
167
8.93M
        if (a->flags & BIO_FLAGS_MEM_RDONLY)
168
2.62M
            b->data = NULL;
169
8.93M
        BUF_MEM_free(b);
170
8.93M
    }
171
8.93M
    return 1;
172
8.93M
}
173
174
/*
175
 * Reallocate memory buffer if read pointer differs
176
 * NOT FOR RDONLY
177
 */
178
static int mem_buf_sync(BIO *b)
179
71.0M
{
180
71.0M
    if (b != NULL && b->init != 0 && b->ptr != NULL) {
181
71.0M
        BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
182
183
71.0M
        if (bbm->readp->data != bbm->buf->data) {
184
797
            memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
185
797
            bbm->buf->length = bbm->readp->length;
186
797
            bbm->readp->data = bbm->buf->data;
187
797
        }
188
71.0M
    }
189
71.0M
    return 0;
190
71.0M
}
191
192
static int mem_read(BIO *b, char *out, int outl)
193
2.18G
{
194
2.18G
    int ret = -1;
195
2.18G
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
196
2.18G
    BUF_MEM *bm = bbm->readp;
197
198
2.18G
    if (b->flags & BIO_FLAGS_MEM_RDONLY)
199
1.96G
        bm = bbm->buf;
200
2.18G
    BIO_clear_retry_flags(b);
201
2.18G
    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
202
2.18G
    if ((out != NULL) && (ret > 0)) {
203
2.18G
        memcpy(out, bm->data, ret);
204
2.18G
        bm->length -= ret;
205
2.18G
        bm->max -= ret;
206
2.18G
        bm->data += ret;
207
2.18G
    } else if (bm->length == 0) {
208
1.04M
        ret = b->num;
209
1.04M
        if (ret != 0)
210
84.5k
            BIO_set_retry_read(b);
211
1.04M
    }
212
2.18G
    return ret;
213
2.18G
}
214
215
static int mem_write(BIO *b, const char *in, int inl)
216
15.2M
{
217
15.2M
    int ret = -1;
218
15.2M
    size_t blen;
219
15.2M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
220
221
15.2M
    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
222
0
        ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
223
0
        goto end;
224
0
    }
225
15.2M
    BIO_clear_retry_flags(b);
226
15.2M
    if (inl <= 0)
227
1.87k
        return 0;
228
15.2M
    if (in == NULL) {
229
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
230
0
        goto end;
231
0
    }
232
15.2M
    blen = bbm->readp->length;
233
15.2M
    mem_buf_sync(b);
234
15.2M
    if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
235
0
        goto end;
236
15.2M
    memcpy(bbm->buf->data + blen, in, inl);
237
15.2M
    *bbm->readp = *bbm->buf;
238
15.2M
    ret = inl;
239
15.2M
end:
240
15.2M
    return ret;
241
15.2M
}
242
243
static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
244
4.37M
{
245
4.37M
    long ret = 1;
246
4.37M
    char **pptr;
247
4.37M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
248
4.37M
    BUF_MEM *bm, *bo; /* bio_mem, bio_other */
249
4.37M
    ossl_ssize_t off, remain;
250
251
4.37M
    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
252
2.62M
        bm = bbm->buf;
253
2.62M
        bo = bbm->readp;
254
2.62M
    } else {
255
1.74M
        bm = bbm->readp;
256
1.74M
        bo = bbm->buf;
257
1.74M
    }
258
4.37M
    off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
259
4.37M
    remain = bm->length;
260
261
4.37M
    switch (cmd) {
262
3
    case BIO_CTRL_RESET:
263
3
        bm = bbm->buf;
264
3
        if (bm->data != NULL) {
265
3
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
266
3
                if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
267
3
                    memset(bm->data, 0, bm->max);
268
3
                    bm->length = 0;
269
3
                }
270
3
                *bbm->readp = *bbm->buf;
271
3
            } else {
272
                /* For read only case just reset to the start again */
273
0
                *bbm->buf = *bbm->readp;
274
0
            }
275
3
        }
276
3
        break;
277
856k
    case BIO_C_FILE_SEEK:
278
856k
        if (num < 0 || num > off + remain)
279
0
            return -1; /* Can't see outside of the current buffer */
280
281
856k
        bm->data = (num != 0) ? bo->data + num : bo->data;
282
856k
        bm->length = bo->length - num;
283
856k
        bm->max = bo->max - num;
284
856k
        off = (ossl_ssize_t)num;
285
        /* FALLTHRU */
286
2.51M
    case BIO_C_FILE_TELL:
287
2.51M
        ret = (long)off;
288
2.51M
        if (off > LONG_MAX)
289
0
            ret = -1;
290
2.51M
        break;
291
106k
    case BIO_CTRL_EOF:
292
106k
        if (b->num == 0 || (b->flags & BIO_FLAGS_MEM_LEGACY_EOF) != 0)
293
106k
            ret = (long)(bm->length == 0);
294
0
        else
295
0
            ret = 0;
296
106k
        break;
297
1.16M
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
298
1.16M
        b->num = (int)num;
299
1.16M
        b->flags &= ~BIO_FLAGS_MEM_LEGACY_EOF;
300
1.16M
        break;
301
180k
    case BIO_CTRL_INFO:
302
180k
        ret = (long)bm->length;
303
180k
        if (ptr != NULL) {
304
139k
            pptr = (char **)ptr;
305
139k
            *pptr = (char *)(bm->data);
306
139k
        }
307
180k
        break;
308
0
    case BIO_C_SET_BUF_MEM:
309
0
        mem_buf_free(b);
310
0
        b->shutdown = (int)num;
311
0
        bbm->buf = ptr;
312
0
        *bbm->readp = *bbm->buf;
313
0
        break;
314
41.8k
    case BIO_C_GET_BUF_MEM_PTR:
315
41.8k
        if (ptr != NULL) {
316
41.8k
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
317
41.8k
                mem_buf_sync(b);
318
41.8k
            bm = bbm->buf;
319
41.8k
            pptr = (char **)ptr;
320
41.8k
            *pptr = (char *)bm;
321
41.8k
        }
322
41.8k
        break;
323
0
    case BIO_CTRL_GET_CLOSE:
324
0
        ret = (long)b->shutdown;
325
0
        break;
326
65.2k
    case BIO_CTRL_SET_CLOSE:
327
65.2k
        b->shutdown = (int)num;
328
65.2k
        break;
329
11.0k
    case BIO_CTRL_WPENDING:
330
11.0k
        ret = 0L;
331
11.0k
        break;
332
0
    case BIO_CTRL_PENDING:
333
0
        ret = (long)bm->length;
334
0
        break;
335
0
    case BIO_CTRL_DUP:
336
82.1k
    case BIO_CTRL_FLUSH:
337
82.1k
        ret = 1;
338
82.1k
        break;
339
53.0k
    case BIO_CTRL_PUSH:
340
106k
    case BIO_CTRL_POP:
341
207k
    default:
342
207k
        ret = 0;
343
207k
        break;
344
4.37M
    }
345
4.37M
    return ret;
346
4.37M
}
347
348
static int mem_gets(BIO *bp, char *buf, int size)
349
16.0M
{
350
16.0M
    int i, j;
351
16.0M
    int ret = -1;
352
16.0M
    char *p;
353
16.0M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
354
16.0M
    BUF_MEM *bm = bbm->readp;
355
356
16.0M
    if (bp->flags & BIO_FLAGS_MEM_RDONLY)
357
8.59M
        bm = bbm->buf;
358
16.0M
    BIO_clear_retry_flags(bp);
359
16.0M
    j = bm->length < INT_MAX ? (int)bm->length : INT_MAX;
360
16.0M
    if ((size - 1) < j)
361
15.4M
        j = size - 1;
362
16.0M
    if (j <= 0) {
363
28.7k
        *buf = '\0';
364
28.7k
        return 0;
365
28.7k
    }
366
16.0M
    p = bm->data;
367
450M
    for (i = 0; i < j; i++) {
368
449M
        if (p[i] == '\n') {
369
15.0M
            i++;
370
15.0M
            break;
371
15.0M
        }
372
449M
    }
373
374
    /*
375
     * i is now the max num of bytes to copy, either j or up to
376
     * and including the first newline
377
     */
378
379
16.0M
    i = mem_read(bp, buf, i);
380
16.0M
    if (i > 0)
381
16.0M
        buf[i] = '\0';
382
16.0M
    ret = i;
383
16.0M
    return ret;
384
16.0M
}
385
386
static int mem_puts(BIO *bp, const char *str)
387
26.5M
{
388
26.5M
    int ret;
389
26.5M
    size_t n = strlen(str);
390
391
26.5M
    if (n > INT_MAX)
392
0
        return -1;
393
26.5M
    ret = mem_write(bp, str, (int)n);
394
    /* memory semantics is that it will always work */
395
26.5M
    return ret;
396
26.5M
}