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
= {
29 /* TODO: Convert to new style write function */
32 /* TODO: Convert to new style read function */
43 static const BIO_METHOD secmem_method
= {
45 "secure memory buffer",
46 /* TODO: Convert to new style write function */
49 /* TODO: Convert to new style read function */
60 /* BIO memory stores buffer and read pointer */
61 typedef struct bio_buf_mem_st
{
62 struct buf_mem_st
*buf
; /* allocated buffer */
63 struct buf_mem_st
*readp
; /* read pointer */
67 * bio->num is used to hold the value to return on 'empty', if it is 0,
68 * should_retry is not set
71 const BIO_METHOD
*BIO_s_mem(void)
76 const BIO_METHOD
*BIO_s_secmem(void)
78 return(&secmem_method
);
81 BIO
*BIO_new_mem_buf(const void *buf
, int len
)
89 BIOerr(BIO_F_BIO_NEW_MEM_BUF
, BIO_R_NULL_PARAMETER
);
92 sz
= (len
< 0) ? strlen(buf
) : (size_t)len
;
93 if ((ret
= BIO_new(BIO_s_mem())) == NULL
)
95 bb
= (BIO_BUF_MEM
*)ret
->ptr
;
97 /* Cast away const and trust in the MEM_RDONLY flag. */
98 b
->data
= (void *)buf
;
101 *bb
->readp
= *bb
->buf
;
102 ret
->flags
|= BIO_FLAGS_MEM_RDONLY
;
103 /* Since this is static data retrying won't help */
108 static int mem_init(BIO
*bi
, unsigned long flags
)
110 BIO_BUF_MEM
*bb
= OPENSSL_zalloc(sizeof(*bb
));
114 if ((bb
->buf
= BUF_MEM_new_ex(flags
)) == NULL
) {
118 if ((bb
->readp
= OPENSSL_zalloc(sizeof(*bb
->readp
))) == NULL
) {
119 BUF_MEM_free(bb
->buf
);
123 *bb
->readp
= *bb
->buf
;
127 bi
->ptr
= (char *)bb
;
131 static int mem_new(BIO
*bi
)
133 return mem_init(bi
, 0L);
136 static int secmem_new(BIO
*bi
)
138 return mem_init(bi
, BUF_MEM_FLAG_SECURE
);
141 static int mem_free(BIO
*a
)
143 return mem_buf_free(a
, 1);
146 static int mem_buf_free(BIO
*a
, int free_all
)
151 if (a
->shutdown
&& a
->init
&& a
->ptr
!= NULL
) {
152 BIO_BUF_MEM
*bb
= (BIO_BUF_MEM
*)a
->ptr
;
153 BUF_MEM
*b
= bb
->buf
;
155 if (a
->flags
& BIO_FLAGS_MEM_RDONLY
)
159 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 */