1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/bio.h>
58
59 #include <limits.h>
60 #include <string.h>
61
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65
66 #include "../internal.h"
67
68
BIO_new_mem_buf(const void * buf,ossl_ssize_t len)69 BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
70 BIO *ret;
71 BUF_MEM *b;
72 const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
73
74 if (!buf && len != 0) {
75 OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
76 return NULL;
77 }
78
79 ret = BIO_new(BIO_s_mem());
80 if (ret == NULL) {
81 return NULL;
82 }
83
84 b = (BUF_MEM *)ret->ptr;
85 // BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to.
86 b->data = (void *)buf;
87 b->length = size;
88 b->max = size;
89
90 ret->flags |= BIO_FLAGS_MEM_RDONLY;
91
92 // |num| is used to store the value that this BIO will return when it runs
93 // out of data. If it's negative then the retry flags will also be set. Since
94 // this is static data, retrying wont help
95 ret->num = 0;
96
97 return ret;
98 }
99
mem_new(BIO * bio)100 static int mem_new(BIO *bio) {
101 BUF_MEM *b;
102
103 b = BUF_MEM_new();
104 if (b == NULL) {
105 return 0;
106 }
107
108 // |shutdown| is used to store the close flag: whether the BIO has ownership
109 // of the BUF_MEM.
110 bio->shutdown = 1;
111 bio->init = 1;
112 bio->num = -1;
113 bio->ptr = (char *)b;
114
115 return 1;
116 }
117
mem_free(BIO * bio)118 static int mem_free(BIO *bio) {
119 if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
120 return 1;
121 }
122
123 BUF_MEM *b = (BUF_MEM *)bio->ptr;
124 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
125 b->data = NULL;
126 }
127 BUF_MEM_free(b);
128 bio->ptr = NULL;
129 return 1;
130 }
131
mem_read(BIO * bio,char * out,int outl)132 static int mem_read(BIO *bio, char *out, int outl) {
133 BIO_clear_retry_flags(bio);
134 if (outl <= 0) {
135 return 0;
136 }
137
138 BUF_MEM *b = bio->ptr;
139 int ret = outl;
140 if ((size_t)ret > b->length) {
141 ret = (int)b->length;
142 }
143
144 if (ret > 0) {
145 OPENSSL_memcpy(out, b->data, ret);
146 b->length -= ret;
147 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
148 b->data += ret;
149 } else {
150 OPENSSL_memmove(b->data, &b->data[ret], b->length);
151 }
152 } else if (b->length == 0) {
153 ret = bio->num;
154 if (ret != 0) {
155 BIO_set_retry_read(bio);
156 }
157 }
158 return ret;
159 }
160
mem_write(BIO * bio,const char * in,int inl)161 static int mem_write(BIO *bio, const char *in, int inl) {
162 BIO_clear_retry_flags(bio);
163 if (inl <= 0) {
164 return 0; // Successfully write zero bytes.
165 }
166
167 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
168 OPENSSL_PUT_ERROR(BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
169 return -1;
170 }
171
172 BUF_MEM *b = bio->ptr;
173 if (!BUF_MEM_append(b, in, inl)) {
174 return -1;
175 }
176
177 return inl;
178 }
179
mem_gets(BIO * bio,char * buf,int size)180 static int mem_gets(BIO *bio, char *buf, int size) {
181 BIO_clear_retry_flags(bio);
182 if (size <= 0) {
183 return 0;
184 }
185
186 // The buffer size includes space for the trailing NUL, so we can read at most
187 // one fewer byte.
188 BUF_MEM *b = bio->ptr;
189 int ret = size - 1;
190 if ((size_t)ret > b->length) {
191 ret = (int)b->length;
192 }
193
194 // Stop at the first newline.
195 const char *newline = OPENSSL_memchr(b->data, '\n', ret);
196 if (newline != NULL) {
197 ret = (int)(newline - b->data + 1);
198 }
199
200 ret = mem_read(bio, buf, ret);
201 if (ret >= 0) {
202 buf[ret] = '\0';
203 }
204 return ret;
205 }
206
mem_ctrl(BIO * bio,int cmd,long num,void * ptr)207 static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
208 long ret = 1;
209 char **pptr;
210
211 BUF_MEM *b = (BUF_MEM *)bio->ptr;
212
213 switch (cmd) {
214 case BIO_CTRL_RESET:
215 if (b->data != NULL) {
216 // For read only case reset to the start again
217 if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
218 b->data -= b->max - b->length;
219 b->length = b->max;
220 } else {
221 OPENSSL_memset(b->data, 0, b->max);
222 b->length = 0;
223 }
224 }
225 break;
226 case BIO_CTRL_EOF:
227 ret = (long)(b->length == 0);
228 break;
229 case BIO_C_SET_BUF_MEM_EOF_RETURN:
230 bio->num = (int)num;
231 break;
232 case BIO_CTRL_INFO:
233 ret = (long)b->length;
234 if (ptr != NULL) {
235 pptr = (char **)ptr;
236 *pptr = (char *)&b->data[0];
237 }
238 break;
239 case BIO_C_SET_BUF_MEM:
240 mem_free(bio);
241 bio->shutdown = (int)num;
242 bio->ptr = ptr;
243 break;
244 case BIO_C_GET_BUF_MEM_PTR:
245 if (ptr != NULL) {
246 pptr = (char **)ptr;
247 *pptr = (char *)b;
248 }
249 break;
250 case BIO_CTRL_GET_CLOSE:
251 ret = (long)bio->shutdown;
252 break;
253 case BIO_CTRL_SET_CLOSE:
254 bio->shutdown = (int)num;
255 break;
256
257 case BIO_CTRL_WPENDING:
258 ret = 0L;
259 break;
260 case BIO_CTRL_PENDING:
261 ret = (long)b->length;
262 break;
263 case BIO_CTRL_FLUSH:
264 ret = 1;
265 break;
266 default:
267 ret = 0;
268 break;
269 }
270 return ret;
271 }
272
273 static const BIO_METHOD mem_method = {
274 BIO_TYPE_MEM, "memory buffer",
275 mem_write, mem_read,
276 NULL /* puts */, mem_gets,
277 mem_ctrl, mem_new,
278 mem_free, NULL /* callback_ctrl */,
279 };
280
BIO_s_mem(void)281 const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
282
BIO_mem_contents(const BIO * bio,const uint8_t ** out_contents,size_t * out_len)283 int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
284 size_t *out_len) {
285 const BUF_MEM *b;
286 if (bio->method != &mem_method) {
287 return 0;
288 }
289
290 b = (BUF_MEM *)bio->ptr;
291 *out_contents = (uint8_t *)b->data;
292 *out_len = b->length;
293 return 1;
294 }
295
BIO_get_mem_data(BIO * bio,char ** contents)296 long BIO_get_mem_data(BIO *bio, char **contents) {
297 return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
298 }
299
BIO_get_mem_ptr(BIO * bio,BUF_MEM ** out)300 int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
301 return (int)BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
302 }
303
BIO_set_mem_buf(BIO * bio,BUF_MEM * b,int take_ownership)304 int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
305 return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
306 }
307
BIO_set_mem_eof_return(BIO * bio,int eof_value)308 int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
309 return (int)BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
310 }
311