2 * Copyright 1995-2023 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 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);
26 static const BIO_METHOD mem_method = {
38 NULL, /* mem_callback_ctrl */
41 static const BIO_METHOD secmem_method = {
43 "secure memory buffer",
53 NULL, /* mem_callback_ctrl */
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.
62 typedef struct bio_buf_mem_st {
63 struct buf_mem_st *buf; /* allocated buffer */
64 struct buf_mem_st *readp; /* read pointer */
68 * bio->num is used to hold the value to return on 'empty', if it is 0,
69 * should_retry is not set
72 const BIO_METHOD *BIO_s_mem(void)
77 const BIO_METHOD *BIO_s_secmem(void)
79 return(&secmem_method);
82 BIO *BIO_new_mem_buf(const void *buf, int len)
90 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
93 sz = (len < 0) ? strlen(buf) : (size_t)len;
94 if ((ret = BIO_new(BIO_s_mem())) == NULL)
96 bb = (BIO_BUF_MEM *)ret->ptr;
98 /* Cast away const and trust in the MEM_RDONLY flag. */
99 b->data = (void *)buf;
102 *bb->readp = *bb->buf;
103 ret->flags |= BIO_FLAGS_MEM_RDONLY;
104 /* Since this is static data retrying won't help */
109 static int mem_init(BIO *bi, unsigned long flags)
111 BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
115 if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
119 if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
120 BUF_MEM_free(bb->buf);
124 *bb->readp = *bb->buf;
128 bi->ptr = (char *)bb;
132 static int mem_new(BIO *bi)
134 return mem_init(bi, 0L);
137 static int secmem_new(BIO *bi)
139 return mem_init(bi, BUF_MEM_FLAG_SECURE);
142 static int mem_free(BIO *a)
149 bb = (BIO_BUF_MEM *)a->ptr;
150 if (!mem_buf_free(a))
152 OPENSSL_free(bb->readp);
157 static int mem_buf_free(BIO *a)
162 if (a->shutdown && a->init && a->ptr != NULL) {
163 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
164 BUF_MEM *b = bb->buf;
166 if (a->flags & BIO_FLAGS_MEM_RDONLY)
174 * Reallocate memory buffer if read pointer differs
177 static int mem_buf_sync(BIO *b)
179 if (b != NULL && b->init != 0 && b->ptr != NULL) {
180 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
182 if (bbm->readp->data != bbm->buf->data) {
183 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
184 bbm->buf->length = bbm->readp->length;
185 bbm->readp->data = bbm->buf->data;
191 static int mem_read(BIO *b, char *out, int outl)
194 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
195 BUF_MEM *bm = bbm->readp;
197 if (b->flags & BIO_FLAGS_MEM_RDONLY)
199 BIO_clear_retry_flags(b);
200 ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
201 if ((out != NULL) && (ret > 0)) {
202 memcpy(out, bm->data, ret);
206 } else if (bm->length == 0) {
209 BIO_set_retry_read(b);
214 static int mem_write(BIO *b, const char *in, int inl)
218 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
220 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
221 ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
224 BIO_clear_retry_flags(b);
228 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
231 blen = bbm->readp->length;
233 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
235 memcpy(bbm->buf->data + blen, in, inl);
236 *bbm->readp = *bbm->buf;
242 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
246 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
247 BUF_MEM *bm, *bo; /* bio_mem, bio_other */
250 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
257 off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
263 if (bm->data != NULL) {
264 if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
265 if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
266 memset(bm->data, 0, bm->max);
269 *bbm->readp = *bbm->buf;
271 /* For read only case just reset to the start again */
272 *bbm->buf = *bbm->readp;
276 case BIO_C_FILE_SEEK:
277 if (num < 0 || num > off + remain)
278 return -1; /* Can't see outside of the current buffer */
280 bm->data = (num != 0) ? bo->data + num : bo->data;
281 bm->length = bo->length - num;
282 bm->max = bo->max - num;
285 case BIO_C_FILE_TELL:
289 ret = (long)(bm->length == 0);
291 case BIO_C_SET_BUF_MEM_EOF_RETURN:
295 ret = (long)bm->length;
298 *pptr = (char *)(bm->data);
301 case BIO_C_SET_BUF_MEM:
303 b->shutdown = (int)num;
305 *bbm->readp = *bbm->buf;
307 case BIO_C_GET_BUF_MEM_PTR:
309 if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
316 case BIO_CTRL_GET_CLOSE:
317 ret = (long)b->shutdown;
319 case BIO_CTRL_SET_CLOSE:
320 b->shutdown = (int)num;
322 case BIO_CTRL_WPENDING:
325 case BIO_CTRL_PENDING:
326 ret = (long)bm->length;
341 static int mem_gets(BIO *bp, char *buf, int size)
346 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
347 BUF_MEM *bm = bbm->readp;
349 if (bp->flags & BIO_FLAGS_MEM_RDONLY)
351 BIO_clear_retry_flags(bp);
360 for (i = 0; i < j; i++) {
368 * i is now the max num of bytes to copy, either j or up to
369 * and including the first newline
372 i = mem_read(bp, buf, i);
379 static int mem_puts(BIO *bp, const char *str)
384 ret = mem_write(bp, str, n);
385 /* memory semantics is that it will always work */