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