Coverage Report

Created: 2023-06-08 06:40

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