2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
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 dgram_mem_new(BIO *h);
22 static int secmem_new(BIO *h);
23 static int mem_free(BIO *data);
24 static int mem_buf_free(BIO *data);
25 static int mem_buf_sync(BIO *h);
27 static const BIO_METHOD mem_method = {
39 NULL, /* mem_callback_ctrl */
42 static const BIO_METHOD dgram_mem_method = {
44 "datagram memory buffer",
54 NULL, /* mem_callback_ctrl */
57 static const BIO_METHOD secmem_method = {
59 "secure memory buffer",
69 NULL, /* mem_callback_ctrl */
72 struct buf_mem_dgram_st {
73 char *dgram; /* Pointer into the buffer for where the dgram starts */
74 size_t dgramlen; /* Length of the dgram */
75 struct buf_mem_dgram_st *next; /* Next dgram to read */
79 * BIO memory stores buffer and read pointer
80 * however the roles are different for read only BIOs.
81 * In that case the readp just stores the original state
82 * to be used for reset.
84 typedef struct bio_buf_mem_st {
85 struct buf_mem_st *buf; /* allocated buffer */
86 struct buf_mem_st *readp; /* read pointer */
87 struct buf_mem_dgram_st *dgrams; /* linked list of dgram data */
88 struct buf_mem_dgram_st *last; /* last dgram in the linked list */
93 * bio->num is used to hold the value to return on 'empty', if it is 0,
94 * should_retry is not set
97 const BIO_METHOD *BIO_s_mem(void)
102 const BIO_METHOD *BIO_s_dgram_mem(void)
104 return &dgram_mem_method;
107 const BIO_METHOD *BIO_s_secmem(void)
109 return(&secmem_method);
112 BIO *BIO_new_mem_buf(const void *buf, int len)
120 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
123 sz = (len < 0) ? strlen(buf) : (size_t)len;
124 if ((ret = BIO_new(BIO_s_mem())) == NULL)
126 bb = (BIO_BUF_MEM *)ret->ptr;
128 /* Cast away const and trust in the MEM_RDONLY flag. */
129 b->data = (void *)buf;
132 *bb->readp = *bb->buf;
133 ret->flags |= BIO_FLAGS_MEM_RDONLY;
134 /* Since this is static data retrying won't help */
139 static int mem_init(BIO *bi, unsigned long flags)
141 BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
145 if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
149 if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
150 BUF_MEM_free(bb->buf);
154 *bb->readp = *bb->buf;
158 bi->ptr = (char *)bb;
162 static int mem_new(BIO *bi)
164 return mem_init(bi, 0L);
167 static int dgram_mem_new(BIO *bi)
171 if (!mem_init(bi, 0L))
174 bbm = (BIO_BUF_MEM *)bi->ptr;
182 static int secmem_new(BIO *bi)
184 return mem_init(bi, BUF_MEM_FLAG_SECURE);
187 static void clear_all_dgrams(BIO_BUF_MEM *bbm)
189 struct buf_mem_dgram_st *dgrams = bbm->dgrams;
191 while (dgrams != NULL) {
192 struct buf_mem_dgram_st *tmp = dgrams;
194 dgrams = dgrams->next;
200 static int mem_free(BIO *a)
207 bb = (BIO_BUF_MEM *)a->ptr;
208 if (!mem_buf_free(a))
210 OPENSSL_free(bb->readp);
211 clear_all_dgrams(bb);
216 static int mem_buf_free(BIO *a)
221 if (a->shutdown && a->init && a->ptr != NULL) {
222 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
223 BUF_MEM *b = bb->buf;
225 if (a->flags & BIO_FLAGS_MEM_RDONLY)
233 * Reallocate memory buffer if read pointer differs
236 static int mem_buf_sync(BIO *b)
238 if (b != NULL && b->init != 0 && b->ptr != NULL) {
239 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
241 if (bbm->readp->data != bbm->buf->data) {
242 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
243 bbm->buf->length = bbm->readp->length;
244 bbm->readp->data = bbm->buf->data;
250 static int mem_read(BIO *b, char *out, int outl)
253 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
254 BUF_MEM *bm = bbm->readp;
255 size_t maxreadlen = 0;
258 if (b->flags & BIO_FLAGS_MEM_RDONLY)
260 BIO_clear_retry_flags(b);
261 if (bbm->use_dgrams) {
262 if (bbm->dgrams != NULL) {
263 maxreadlen = bbm->dgrams->dgramlen;
264 if (!ossl_assert(maxreadlen <= bm->length))
270 maxreadlen = bm->length;
271 eof = (maxreadlen == 0);
273 ret = (outl >= 0 && (size_t)outl > maxreadlen) ? (int)maxreadlen : outl;
274 if ((out != NULL) && (ret > 0)) {
277 memcpy(out, bm->data, ret);
278 flushlen = bbm->use_dgrams ? maxreadlen : (size_t)ret;
280 bm->length -= flushlen;
282 bm->data += flushlen;
283 if (bbm->use_dgrams) {
284 struct buf_mem_dgram_st *tmp = bbm->dgrams;
286 bbm->dgrams = tmp->next;
288 if (bbm->dgrams == NULL)
294 BIO_set_retry_read(b);
299 static int mem_write(BIO *b, const char *in, int inl)
303 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
305 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
306 ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
309 BIO_clear_retry_flags(b);
315 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
318 blen = bbm->readp->length;
320 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
323 memcpy(bbm->buf->data + blen, in, inl);
324 *bbm->readp = *bbm->buf;
326 if (bbm->use_dgrams) {
327 struct buf_mem_dgram_st *dgram = OPENSSL_malloc(sizeof(*dgram));
332 dgram->dgram = bbm->buf->data + blen;
333 dgram->dgramlen = inl;
335 if (bbm->dgrams == NULL)
338 bbm->last->next = dgram;
347 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
351 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
352 BUF_MEM *bm, *bo; /* bio_mem, bio_other */
355 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
362 off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
368 if (bm->data != NULL) {
369 if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
370 if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
371 memset(bm->data, 0, bm->max);
374 *bbm->readp = *bbm->buf;
376 /* For read only case just reset to the start again */
377 *bbm->buf = *bbm->readp;
380 clear_all_dgrams(bbm);
382 case BIO_C_FILE_SEEK:
383 if (num < 0 || num > off + remain)
384 return -1; /* Can't see outside of the current buffer */
386 bm->data = (num != 0) ? bo->data + num : bo->data;
387 bm->length = bo->length - num;
388 bm->max = bo->max - num;
391 case BIO_C_FILE_TELL:
395 ret = (long)(bm->length == 0 && bbm->use_dgrams == 0);
397 case BIO_C_SET_BUF_MEM_EOF_RETURN:
398 if (!bbm->use_dgrams)
404 ret = (long)bm->length;
407 *pptr = (char *)(bm->data);
410 case BIO_C_SET_BUF_MEM:
412 b->shutdown = (int)num;
414 *bbm->readp = *bbm->buf;
416 case BIO_C_GET_BUF_MEM_PTR:
418 if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
425 case BIO_CTRL_GET_CLOSE:
426 ret = (long)b->shutdown;
428 case BIO_CTRL_SET_CLOSE:
429 b->shutdown = (int)num;
431 case BIO_CTRL_WPENDING:
434 case BIO_CTRL_PENDING:
435 ret = (long)bm->length;
450 static int mem_gets(BIO *bp, char *buf, int size)
455 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
456 BUF_MEM *bm = bbm->readp;
458 if (bp->flags & BIO_FLAGS_MEM_RDONLY)
460 BIO_clear_retry_flags(bp);
461 j = (!bbm->use_dgrams || bbm->dgrams == NULL) ? bm->length
462 : bbm->dgrams->dgramlen;
470 for (i = 0; i < j; i++) {
478 * i is now the max num of bytes to copy, either j or up to
479 * and including the first newline
482 i = mem_read(bp, buf, i);
489 static int mem_puts(BIO *bp, const char *str)
494 ret = mem_write(bp, str, n);
495 /* memory semantics is that it will always work */