Coverage Report

Created: 2026-10-28 06:28

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