Coverage Report

Created: 2025-06-13 06:55

/src/openssl/crypto/bio/bss_mem.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 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->ptr = (char *)bb;
129
0
    return 1;
130
0
}
131
132
static int mem_new(BIO *bi)
133
0
{
134
0
    return mem_init(bi, 0L);
135
0
}
136
137
static int secmem_new(BIO *bi)
138
0
{
139
0
    return mem_init(bi, BUF_MEM_FLAG_SECURE);
140
0
}
141
142
static int mem_free(BIO *a)
143
0
{
144
0
    BIO_BUF_MEM *bb;
145
146
0
    if (a == NULL)
147
0
        return 0;
148
149
0
    bb = (BIO_BUF_MEM *)a->ptr;
150
0
    if (!mem_buf_free(a))
151
0
        return 0;
152
0
    OPENSSL_free(bb->readp);
153
0
    OPENSSL_free(bb);
154
0
    return 1;
155
0
}
156
157
static int mem_buf_free(BIO *a)
158
0
{
159
0
    if (a == NULL)
160
0
        return 0;
161
162
0
    if (a->shutdown && a->init && a->ptr != NULL) {
163
0
        BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
164
0
        BUF_MEM *b = bb->buf;
165
166
0
        if (a->flags & BIO_FLAGS_MEM_RDONLY)
167
0
            b->data = NULL;
168
0
        BUF_MEM_free(b);
169
0
    }
170
0
    return 1;
171
0
}
172
173
/*
174
 * Reallocate memory buffer if read pointer differs
175
 * NOT FOR RDONLY
176
 */
177
static int mem_buf_sync(BIO *b)
178
0
{
179
0
    if (b != NULL && b->init != 0 && b->ptr != NULL) {
180
0
        BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
181
182
0
        if (bbm->readp->data != bbm->buf->data) {
183
0
            memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
184
0
            bbm->buf->length = bbm->readp->length;
185
0
            bbm->readp->data = bbm->buf->data;
186
0
        }
187
0
    }
188
0
    return 0;
189
0
}
190
191
static int mem_read(BIO *b, char *out, int outl)
192
0
{
193
0
    int ret = -1;
194
0
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
195
0
    BUF_MEM *bm = bbm->readp;
196
197
0
    if (b->flags & BIO_FLAGS_MEM_RDONLY)
198
0
        bm = bbm->buf;
199
0
    BIO_clear_retry_flags(b);
200
0
    ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
201
0
    if ((out != NULL) && (ret > 0)) {
202
0
        memcpy(out, bm->data, ret);
203
0
        bm->length -= ret;
204
0
        bm->max -= ret;
205
0
        bm->data += ret;
206
0
    } else if (bm->length == 0) {
207
0
        ret = b->num;
208
0
        if (ret != 0)
209
0
            BIO_set_retry_read(b);
210
0
    }
211
0
    return ret;
212
0
}
213
214
static int mem_write(BIO *b, const char *in, int inl)
215
0
{
216
0
    int ret = -1;
217
0
    int blen;
218
0
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
219
220
0
    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
221
0
        ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
222
0
        goto end;
223
0
    }
224
0
    BIO_clear_retry_flags(b);
225
0
    if (inl == 0)
226
0
        return 0;
227
0
    if (in == NULL) {
228
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
229
0
        goto end;
230
0
    }
231
0
    blen = bbm->readp->length;
232
0
    mem_buf_sync(b);
233
0
    if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
234
0
        goto end;
235
0
    memcpy(bbm->buf->data + blen, in, inl);
236
0
    *bbm->readp = *bbm->buf;
237
0
    ret = inl;
238
0
 end:
239
0
    return ret;
240
0
}
241
242
static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
243
0
{
244
0
    long ret = 1;
245
0
    char **pptr;
246
0
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
247
0
    BUF_MEM *bm, *bo;            /* bio_mem, bio_other */
248
0
    long off, remain;
249
250
0
    if (b->flags & BIO_FLAGS_MEM_RDONLY) {
251
0
        bm = bbm->buf;
252
0
        bo = bbm->readp;
253
0
    } else {
254
0
        bm = bbm->readp;
255
0
        bo = bbm->buf;
256
0
    }
257
0
    off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
258
0
    remain = bm->length;
259
260
0
    switch (cmd) {
261
0
    case BIO_CTRL_RESET:
262
0
        bm = bbm->buf;
263
0
        if (bm->data != NULL) {
264
0
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
265
0
                if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
266
0
                    memset(bm->data, 0, bm->max);
267
0
                    bm->length = 0;
268
0
                }
269
0
                *bbm->readp = *bbm->buf;
270
0
            } else {
271
                /* For read only case just reset to the start again */
272
0
                *bbm->buf = *bbm->readp;
273
0
            }
274
0
        }
275
0
        break;
276
0
    case BIO_C_FILE_SEEK:
277
0
        if (num < 0 || num > off + remain)
278
0
            return -1;   /* Can't see outside of the current buffer */
279
280
0
        bm->data = (num != 0) ? bo->data + num : bo->data;
281
0
        bm->length = bo->length - num;
282
0
        bm->max = bo->max - num;
283
0
        off = num;
284
        /* FALLTHRU */
285
0
    case BIO_C_FILE_TELL:
286
0
        ret = off;
287
0
        break;
288
0
    case BIO_CTRL_EOF:
289
0
        ret = (long)(bm->length == 0);
290
0
        break;
291
0
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
292
0
        b->num = (int)num;
293
0
        break;
294
0
    case BIO_CTRL_INFO:
295
0
        ret = (long)bm->length;
296
0
        if (ptr != NULL) {
297
0
            pptr = (char **)ptr;
298
0
            *pptr = (char *)(bm->data);
299
0
        }
300
0
        break;
301
0
    case BIO_C_SET_BUF_MEM:
302
0
        mem_buf_free(b);
303
0
        b->shutdown = (int)num;
304
0
        bbm->buf = ptr;
305
0
        *bbm->readp = *bbm->buf;
306
0
        break;
307
0
    case BIO_C_GET_BUF_MEM_PTR:
308
0
        if (ptr != NULL) {
309
0
            if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
310
0
                mem_buf_sync(b);
311
0
            bm = bbm->buf;
312
0
            pptr = (char **)ptr;
313
0
            *pptr = (char *)bm;
314
0
        }
315
0
        break;
316
0
    case BIO_CTRL_GET_CLOSE:
317
0
        ret = (long)b->shutdown;
318
0
        break;
319
0
    case BIO_CTRL_SET_CLOSE:
320
0
        b->shutdown = (int)num;
321
0
        break;
322
0
    case BIO_CTRL_WPENDING:
323
0
        ret = 0L;
324
0
        break;
325
0
    case BIO_CTRL_PENDING:
326
0
        ret = (long)bm->length;
327
0
        break;
328
0
    case BIO_CTRL_DUP:
329
0
    case BIO_CTRL_FLUSH:
330
0
        ret = 1;
331
0
        break;
332
0
    case BIO_CTRL_PUSH:
333
0
    case BIO_CTRL_POP:
334
0
    default:
335
0
        ret = 0;
336
0
        break;
337
0
    }
338
0
    return ret;
339
0
}
340
341
static int mem_gets(BIO *bp, char *buf, int size)
342
0
{
343
0
    int i, j;
344
0
    int ret = -1;
345
0
    char *p;
346
0
    BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
347
0
    BUF_MEM *bm = bbm->readp;
348
349
0
    if (bp->flags & BIO_FLAGS_MEM_RDONLY)
350
0
        bm = bbm->buf;
351
0
    BIO_clear_retry_flags(bp);
352
0
    j = bm->length;
353
0
    if ((size - 1) < j)
354
0
        j = size - 1;
355
0
    if (j <= 0) {
356
0
        *buf = '\0';
357
0
        return 0;
358
0
    }
359
0
    p = bm->data;
360
0
    for (i = 0; i < j; i++) {
361
0
        if (p[i] == '\n') {
362
0
            i++;
363
0
            break;
364
0
        }
365
0
    }
366
367
    /*
368
     * i is now the max num of bytes to copy, either j or up to
369
     * and including the first newline
370
     */
371
372
0
    i = mem_read(bp, buf, i);
373
0
    if (i > 0)
374
0
        buf[i] = '\0';
375
0
    ret = i;
376
0
    return ret;
377
0
}
378
379
static int mem_puts(BIO *bp, const char *str)
380
0
{
381
0
    int n, ret;
382
383
0
    n = strlen(str);
384
0
    ret = mem_write(bp, str, n);
385
    /* memory semantics is that it will always work */
386
0
    return ret;
387
0
}