Coverage Report

Created: 2026-02-22 06:11

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