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
->init
) && (a
->ptr
!= NULL
)) {
153 BIO_BUF_MEM
*bb
= (BIO_BUF_MEM
*)a
->ptr
;
157 if (a
->flags
& BIO_FLAGS_MEM_RDONLY
)
161 OPENSSL_free(bb
->readp
);
172 * Reallocate memory buffer if read pointer differs
174 static int mem_buf_sync(BIO
*b
)
176 if (b
!= NULL
&& b
->init
!= 0 && b
->ptr
!= NULL
) {
177 BIO_BUF_MEM
*bbm
= (BIO_BUF_MEM
*)b
->ptr
;
179 if (bbm
->readp
->data
!= bbm
->buf
->data
) {
180 memmove(bbm
->buf
->data
, bbm
->readp
->data
, bbm
->readp
->length
);
181 bbm
->buf
->length
= bbm
->readp
->length
;
182 bbm
->readp
->data
= bbm
->buf
->data
;
188 static int mem_read(BIO
*b
, char *out
, int outl
)
191 BIO_BUF_MEM
*bbm
= (BIO_BUF_MEM
*)b
->ptr
;
192 BUF_MEM
*bm
= bbm
->readp
;
194 BIO_clear_retry_flags(b
);
195 ret
= (outl
>= 0 && (size_t)outl
> bm
->length
) ? (int)bm
->length
: outl
;
196 if ((out
!= NULL
) && (ret
> 0)) {
197 memcpy(out
, bm
->data
, ret
);
200 } else if (bm
->length
== 0) {
203 BIO_set_retry_read(b
);
208 static int mem_write(BIO
*b
, const char *in
, int inl
)
212 BIO_BUF_MEM
*bbm
= (BIO_BUF_MEM
*)b
->ptr
;
215 BIOerr(BIO_F_MEM_WRITE
, BIO_R_NULL_PARAMETER
);
218 if (b
->flags
& BIO_FLAGS_MEM_RDONLY
) {
219 BIOerr(BIO_F_MEM_WRITE
, BIO_R_WRITE_TO_READ_ONLY_BIO
);
222 BIO_clear_retry_flags(b
);
223 blen
= bbm
->readp
->length
;
225 if (BUF_MEM_grow_clean(bbm
->buf
, blen
+ inl
) == 0)
227 memcpy(bbm
->buf
->data
+ blen
, in
, inl
);
228 *bbm
->readp
= *bbm
->buf
;
234 static long mem_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
238 BIO_BUF_MEM
*bbm
= (BIO_BUF_MEM
*)b
->ptr
;
244 if (bm
->data
!= NULL
) {
245 /* For read only case reset to the start again */
246 if ((b
->flags
& BIO_FLAGS_MEM_RDONLY
) || (b
->flags
& BIO_FLAGS_NONCLEAR_RST
)) {
247 bm
->length
= bm
->max
;
249 memset(bm
->data
, 0, bm
->max
);
252 *bbm
->readp
= *bbm
->buf
;
257 ret
= (long)(bm
->length
== 0);
259 case BIO_C_SET_BUF_MEM_EOF_RETURN
:
264 ret
= (long)bm
->length
;
267 *pptr
= (char *)&(bm
->data
[0]);
270 case BIO_C_SET_BUF_MEM
:
272 b
->shutdown
= (int)num
;
274 *bbm
->readp
= *bbm
->buf
;
277 case BIO_C_GET_BUF_MEM_PTR
:
285 case BIO_CTRL_GET_CLOSE
:
286 ret
= (long)b
->shutdown
;
288 case BIO_CTRL_SET_CLOSE
:
289 b
->shutdown
= (int)num
;
291 case BIO_CTRL_WPENDING
:
294 case BIO_CTRL_PENDING
:
296 ret
= (long)bm
->length
;
311 static int mem_gets(BIO
*bp
, char *buf
, int size
)
316 BIO_BUF_MEM
*bbm
= (BIO_BUF_MEM
*)bp
->ptr
;
317 BUF_MEM
*bm
= bbm
->readp
;
319 BIO_clear_retry_flags(bp
);
328 for (i
= 0; i
< j
; i
++) {
336 * i is now the max num of bytes to copy, either j or up to
337 * and including the first newline
340 i
= mem_read(bp
, buf
, i
);
347 static int mem_puts(BIO
*bp
, const char *str
)
352 ret
= mem_write(bp
, str
, n
);
353 /* memory semantics is that it will always work */