Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/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.32M
{
74
8.32M
    return &mem_method;
75
8.32M
}
76
77
const BIO_METHOD *BIO_s_secmem(void)
78
2.21k
{
79
2.21k
    return &secmem_method;
80
2.21k
}
81
82
BIO *BIO_new_mem_buf(const void *buf, int len)
83
2.05M
{
84
2.05M
    BIO *ret;
85
2.05M
    BUF_MEM *b;
86
2.05M
    BIO_BUF_MEM *bb;
87
2.05M
    size_t sz;
88
89
2.05M
    if (buf == NULL) {
90
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
91
0
        return NULL;
92
0
    }
93
2.05M
    sz = (len < 0) ? strlen(buf) : (size_t)len;
94
2.05M
    if ((ret = BIO_new(BIO_s_mem())) == NULL)
95
0
        return NULL;
96
2.05M
    bb = (BIO_BUF_MEM *)ret->ptr;
97
2.05M
    b = bb->buf;
98
    /* Cast away const and trust in the MEM_RDONLY flag. */
99
2.05M
    b->data = (void *)buf;
100
2.05M
    b->length = sz;
101
2.05M
    b->max = sz;
102
2.05M
    *bb->readp = *bb->buf;
103
2.05M
    ret->flags |= BIO_FLAGS_MEM_RDONLY;
104
    /* Since this is static data retrying won't help */
105
2.05M
    ret->num = 0;
106
2.05M
    return ret;
107
2.05M
}
108
109
static int mem_init(BIO *bi, unsigned long flags)
110
8.56M
{
111
8.56M
    BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
112
113
8.56M
    if (bb == NULL)
114
0
        return 0;
115
8.56M
    if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
116
0
        OPENSSL_free(bb);
117
0
        return 0;
118
0
    }
119
8.56M
    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.56M
    *bb->readp = *bb->buf;
125
8.56M
    bi->shutdown = 1;
126
8.56M
    bi->init = 1;
127
8.56M
    bi->num = -1;
128
8.56M
    bi->flags |= BIO_FLAGS_MEM_LEGACY_EOF;
129
8.56M
    bi->ptr = (char *)bb;
130
8.56M
    return 1;
131
8.56M
}
132
133
static int mem_new(BIO *bi)
134
8.55M
{
135
8.55M
    return mem_init(bi, 0L);
136
8.55M
}
137
138
static int secmem_new(BIO *bi)
139
4.42k
{
140
4.42k
    return mem_init(bi, BUF_MEM_FLAG_SECURE);
141
4.42k
}
142
143
static int mem_free(BIO *a)
144
8.56M
{
145
8.56M
    BIO_BUF_MEM *bb;
146
147
8.56M
    if (a == NULL)
148
0
        return 0;
149
150
8.56M
    bb = (BIO_BUF_MEM *)a->ptr;
151
8.56M
    if (!mem_buf_free(a))
152
0
        return 0;
153
8.56M
    OPENSSL_free(bb->readp);
154
8.56M
    OPENSSL_free(bb);
155
8.56M
    return 1;
156
8.56M
}
157
158
static int mem_buf_free(BIO *a)
159
8.56M
{
160
8.56M
    if (a == NULL)
161
0
        return 0;
162
163
8.56M
    if (a->shutdown && a->init && a->ptr != NULL) {
164
8.56M
        BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
165
8.56M
        BUF_MEM *b = bb->buf;
166
167
8.56M
        if (a->flags & BIO_FLAGS_MEM_RDONLY)
168
2.05M
            b->data = NULL;
169
8.56M
        BUF_MEM_free(b);
170
8.56M
    }
171
8.56M
    return 1;
172
8.56M
}
173
174
/*
175
 * Reallocate memory buffer if read pointer differs
176
 * NOT FOR RDONLY
177
 */
178
static int mem_buf_sync(BIO *b)
179
56.1M
{
180
56.1M
    if (b != NULL && b->init != 0 && b->ptr != NULL) {
181
56.1M
        BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
182
183
56.1M
        if (bbm->readp->data != bbm->buf->data) {
184
795
            memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
185
795
            bbm->buf->length = bbm->readp->length;
186
795
            bbm->readp->data = bbm->buf->data;
187
795
        }
188
56.1M
    }
189
56.1M
    return 0;
190
56.1M
}
191
192
static int mem_read(BIO *b, char *out, int outl)
193
1.51G
{
194
1.51G
    int ret = -1;
195
1.51G
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
196
1.51G
    BUF_MEM *bm = bbm->readp;
197
198
1.51G
    if (b->flags & BIO_FLAGS_MEM_RDONLY)
199
1.35G
        bm = bbm->buf;
200
1.51G
    BIO_clear_retry_flags(b);
201
1.51G
    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
202
1.51G
    if ((out != NULL) && (ret > 0)) {
203
1.51G
        memcpy(out, bm->data, ret);
204
1.51G
        bm->length -= ret;
205
1.51G
        bm->max -= ret;
206
1.51G
        bm->data += ret;
207
1.51G
    } else if (bm->length == 0) {
208
435k
        ret = b->num;
209
435k
        if (ret != 0)
210
48.5k
            BIO_set_retry_read(b);
211
435k
    }
212
1.51G
    return ret;
213
1.51G
}
214
215
static int mem_write(BIO *b, const char *in, int inl)
216
18.0M
{
217
18.0M
    int ret = -1;
218
18.0M
    size_t blen;
219
18.0M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
220
221
18.0M
    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
18.0M
    BIO_clear_retry_flags(b);
226
18.0M
    if (inl <= 0)
227
2.67k
        return 0;
228
18.0M
    if (in == NULL) {
229
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
230
0
        goto end;
231
0
    }
232
18.0M
    blen = bbm->readp->length;
233
18.0M
    mem_buf_sync(b);
234
18.0M
    if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
235
0
        goto end;
236
18.0M
    memcpy(bbm->buf->data + blen, in, inl);
237
18.0M
    *bbm->readp = *bbm->buf;
238
18.0M
    ret = inl;
239
18.0M
end:
240
18.0M
    return ret;
241
18.0M
}
242
243
static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
244
7.49M
{
245
7.49M
    long ret = 1;
246
7.49M
    char **pptr;
247
7.49M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
248
7.49M
    BUF_MEM *bm, *bo; /* bio_mem, bio_other */
249
7.49M
    ossl_ssize_t off, remain;
250
251
7.49M
    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
252
5.03M
        bm = bbm->buf;
253
5.03M
        bo = bbm->readp;
254
5.03M
    } else {
255
2.45M
        bm = bbm->readp;
256
2.45M
        bo = bbm->buf;
257
2.45M
    }
258
7.49M
    off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
259
7.49M
    remain = bm->length;
260
261
7.49M
    switch (cmd) {
262
2
    case BIO_CTRL_RESET:
263
2
        bm = bbm->buf;
264
2
        if (bm->data != NULL) {
265
2
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
266
2
                if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
267
2
                    memset(bm->data, 0, bm->max);
268
2
                    bm->length = 0;
269
2
                }
270
2
                *bbm->readp = *bbm->buf;
271
2
            } else {
272
                /* For read only case just reset to the start again */
273
0
                *bbm->buf = *bbm->readp;
274
0
            }
275
2
        }
276
2
        break;
277
1.68M
    case BIO_C_FILE_SEEK:
278
1.68M
        if (num < 0 || num > off + remain)
279
0
            return -1; /* Can't see outside of the current buffer */
280
281
1.68M
        bm->data = (num != 0) ? bo->data + num : bo->data;
282
1.68M
        bm->length = bo->length - num;
283
1.68M
        bm->max = bo->max - num;
284
1.68M
        off = (ossl_ssize_t)num;
285
        /* FALLTHRU */
286
4.64M
    case BIO_C_FILE_TELL:
287
4.64M
        ret = (long)off;
288
4.64M
        if (off > LONG_MAX)
289
0
            ret = -1;
290
4.64M
        break;
291
420k
    case BIO_CTRL_EOF:
292
420k
        if (b->num == 0 || (b->flags & BIO_FLAGS_MEM_LEGACY_EOF) != 0)
293
420k
            ret = (long)(bm->length == 0);
294
0
        else
295
0
            ret = 0;
296
420k
        break;
297
1.54M
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
298
1.54M
        b->num = (int)num;
299
1.54M
        b->flags &= ~BIO_FLAGS_MEM_LEGACY_EOF;
300
1.54M
        break;
301
290k
    case BIO_CTRL_INFO:
302
290k
        ret = (long)bm->length;
303
290k
        if (ptr != NULL) {
304
213k
            pptr = (char **)ptr;
305
213k
            *pptr = (char *)(bm->data);
306
213k
        }
307
290k
        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
79.8k
    case BIO_C_GET_BUF_MEM_PTR:
315
79.8k
        if (ptr != NULL) {
316
79.8k
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
317
79.8k
                mem_buf_sync(b);
318
79.8k
            bm = bbm->buf;
319
79.8k
            pptr = (char **)ptr;
320
79.8k
            *pptr = (char *)bm;
321
79.8k
        }
322
79.8k
        break;
323
0
    case BIO_CTRL_GET_CLOSE:
324
0
        ret = (long)b->shutdown;
325
0
        break;
326
86.7k
    case BIO_CTRL_SET_CLOSE:
327
86.7k
        b->shutdown = (int)num;
328
86.7k
        break;
329
13.8k
    case BIO_CTRL_WPENDING:
330
13.8k
        ret = 0L;
331
13.8k
        break;
332
0
    case BIO_CTRL_PENDING:
333
0
        ret = (long)bm->length;
334
0
        break;
335
0
    case BIO_CTRL_DUP:
336
146k
    case BIO_CTRL_FLUSH:
337
146k
        ret = 1;
338
146k
        break;
339
69.3k
    case BIO_CTRL_PUSH:
340
138k
    case BIO_CTRL_POP:
341
269k
    default:
342
269k
        ret = 0;
343
269k
        break;
344
7.49M
    }
345
7.49M
    return ret;
346
7.49M
}
347
348
static int mem_gets(BIO *bp, char *buf, int size)
349
20.8M
{
350
20.8M
    int i, j;
351
20.8M
    int ret = -1;
352
20.8M
    char *p;
353
20.8M
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
354
20.8M
    BUF_MEM *bm = bbm->readp;
355
356
20.8M
    if (bp->flags & BIO_FLAGS_MEM_RDONLY)
357
12.8M
        bm = bbm->buf;
358
20.8M
    BIO_clear_retry_flags(bp);
359
20.8M
    j = bm->length < INT_MAX ? (int)bm->length : INT_MAX;
360
20.8M
    if ((size - 1) < j)
361
19.8M
        j = size - 1;
362
20.8M
    if (j <= 0) {
363
51.6k
        *buf = '\0';
364
51.6k
        return 0;
365
51.6k
    }
366
20.7M
    p = bm->data;
367
564M
    for (i = 0; i < j; i++) {
368
563M
        if (p[i] == '\n') {
369
19.5M
            i++;
370
19.5M
            break;
371
19.5M
        }
372
563M
    }
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
20.7M
    i = mem_read(bp, buf, i);
380
20.7M
    if (i > 0)
381
20.7M
        buf[i] = '\0';
382
20.7M
    ret = i;
383
20.7M
    return ret;
384
20.8M
}
385
386
static int mem_puts(BIO *bp, const char *str)
387
27.8M
{
388
27.8M
    int ret;
389
27.8M
    size_t n = strlen(str);
390
391
27.8M
    if (n > INT_MAX)
392
0
        return -1;
393
27.8M
    ret = mem_write(bp, str, (int)n);
394
    /* memory semantics is that it will always work */
395
27.8M
    return ret;
396
27.8M
}