2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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, int free_all);
24 static int mem_buf_sync(BIO *h);
26 static const BIO_METHOD mem_method = {
30 /* TODO: Convert to new style read function */
41 static const BIO_METHOD secmem_method = {
43 "secure memory buffer",
45 /* TODO: Convert to new style read function */
56 /* BIO memory stores buffer and read pointer */
57 typedef struct bio_buf_mem_st {
58 struct buf_mem_st *buf; /* allocated buffer */
59 struct buf_mem_st *readp; /* read pointer */
63 * bio->num is used to hold the value to return on 'empty', if it is 0,
64 * should_retry is not set
67 const BIO_METHOD *BIO_s_mem(void)
72 const BIO_METHOD *BIO_s_secmem(void)
74 return(&secmem_method);
77 BIO *BIO_new_mem_buf(const void *buf, int len)
85 BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
88 sz = (len < 0) ? strlen(buf) : (size_t)len;
89 if ((ret = BIO_new(BIO_s_mem())) == NULL)
91 bb = (BIO_BUF_MEM *)ret->ptr;
93 /* Cast away const and trust in the MEM_RDONLY flag. */
94 b->data = (void *)buf;
97 *bb->readp = *bb->buf;
98 ret->flags |= BIO_FLAGS_MEM_RDONLY;
99 /* Since this is static data retrying won't help */
104 static int mem_init(BIO *bi, unsigned long flags)
106 BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
110 if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
114 if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
115 BUF_MEM_free(bb->buf);
119 *bb->readp = *bb->buf;
123 bi->ptr = (char *)bb;
127 static int mem_new(BIO *bi)
129 return (mem_init(bi, 0L));
132 static int secmem_new(BIO *bi)
134 return (mem_init(bi, BUF_MEM_FLAG_SECURE));
137 static int mem_free(BIO *a)
139 return (mem_buf_free(a, 1));
142 static int mem_buf_free(BIO *a, int free_all)
147 if ((a->init) && (a->ptr != NULL)) {
149 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
153 if (a->flags & BIO_FLAGS_MEM_RDONLY)
157 OPENSSL_free(bb->readp);
168 * Reallocate memory buffer if read pointer differs
170 static int mem_buf_sync(BIO *b)
172 if (b != NULL && b->init != 0 && b->ptr != NULL) {
173 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
175 if (bbm->readp->data != bbm->buf->data) {
176 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
177 bbm->buf->length = bbm->readp->length;
178 bbm->readp->data = bbm->buf->data;
184 static int mem_read(BIO *b, char *out, int outl)
187 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
188 BUF_MEM *bm = bbm->readp;
190 BIO_clear_retry_flags(b);
191 ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
192 if ((out != NULL) && (ret > 0)) {
193 memcpy(out, bm->data, ret);
196 } else if (bm->length == 0) {
199 BIO_set_retry_read(b);
204 static int mem_write(BIO *b, const char *in, int inl)
208 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
211 BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
214 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
215 BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
218 BIO_clear_retry_flags(b);
219 blen = bbm->readp->length;
221 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
223 memcpy(bbm->buf->data + blen, in, inl);
224 *bbm->readp = *bbm->buf;
230 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
234 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
240 if (bm->data != NULL) {
241 /* For read only case reset to the start again */
242 if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
243 bm->length = bm->max;
245 memset(bm->data, 0, bm->max);
248 *bbm->readp = *bbm->buf;
253 ret = (long)(bm->length == 0);
255 case BIO_C_SET_BUF_MEM_EOF_RETURN:
260 ret = (long)bm->length;
263 *pptr = (char *)&(bm->data[0]);
266 case BIO_C_SET_BUF_MEM:
268 b->shutdown = (int)num;
270 *bbm->readp = *bbm->buf;
273 case BIO_C_GET_BUF_MEM_PTR:
281 case BIO_CTRL_GET_CLOSE:
282 ret = (long)b->shutdown;
284 case BIO_CTRL_SET_CLOSE:
285 b->shutdown = (int)num;
287 case BIO_CTRL_WPENDING:
290 case BIO_CTRL_PENDING:
292 ret = (long)bm->length;
307 static int mem_gets(BIO *bp, char *buf, int size)
312 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
313 BUF_MEM *bm = bbm->readp;
315 BIO_clear_retry_flags(bp);
324 for (i = 0; i < j; i++) {
332 * i is now the max num of bytes to copy, either j or up to
333 * and including the first newline
336 i = mem_read(bp, buf, i);
343 static int mem_puts(BIO *bp, const char *str)
348 ret = mem_write(bp, str, n);
349 /* memory semantics is that it will always work */