xref: /openbsd-src/lib/libcrypto/bio/bss_mem.c (revision 94b1984e8297c4fb48e7f12b2c5d5fb952257fde)
1 /* $OpenBSD: bss_mem.c,v 1.18 2022/01/07 09:02:17 tb 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 <openssl/bio.h>
64 #include <openssl/err.h>
65 #include <openssl/buffer.h>
66 
67 #include "bio_local.h"
68 
69 static int mem_write(BIO *h, const char *buf, int num);
70 static int mem_read(BIO *h, char *buf, int size);
71 static int mem_puts(BIO *h, const char *str);
72 static int mem_gets(BIO *h, char *str, int size);
73 static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
74 static int mem_new(BIO *h);
75 static int mem_free(BIO *data);
76 
77 static const BIO_METHOD mem_method = {
78 	.type = BIO_TYPE_MEM,
79 	.name = "memory buffer",
80 	.bwrite = mem_write,
81 	.bread = mem_read,
82 	.bputs = mem_puts,
83 	.bgets = mem_gets,
84 	.ctrl = mem_ctrl,
85 	.create = mem_new,
86 	.destroy = mem_free
87 };
88 
89 /* bio->num is used to hold the value to return on 'empty', if it is
90  * 0, should_retry is not set */
91 
92 const BIO_METHOD *
93 BIO_s_mem(void)
94 {
95 	return (&mem_method);
96 }
97 
98 BIO *
99 BIO_new_mem_buf(const void *buf, int len)
100 {
101 	BIO *ret;
102 	BUF_MEM *b;
103 	size_t sz;
104 
105 	if (!buf) {
106 		BIOerror(BIO_R_NULL_PARAMETER);
107 		return NULL;
108 	}
109 	sz = (len < 0) ? strlen(buf) : (size_t)len;
110 	if (!(ret = BIO_new(BIO_s_mem())))
111 		return NULL;
112 	b = (BUF_MEM *)ret->ptr;
113 	b->data = (void *)buf;	/* Trust in the BIO_FLAGS_MEM_RDONLY flag. */
114 	b->length = sz;
115 	b->max = sz;
116 	ret->flags |= BIO_FLAGS_MEM_RDONLY;
117 	/* Since this is static data retrying wont help */
118 	ret->num = 0;
119 	return ret;
120 }
121 
122 static int
123 mem_new(BIO *bi)
124 {
125 	BUF_MEM *b;
126 
127 	if ((b = BUF_MEM_new()) == NULL)
128 		return (0);
129 	bi->shutdown = 1;
130 	bi->init = 1;
131 	bi->num = -1;
132 	bi->ptr = (char *)b;
133 	return (1);
134 }
135 
136 static int
137 mem_free(BIO *a)
138 {
139 	if (a == NULL)
140 		return (0);
141 	if (a->shutdown) {
142 		if ((a->init) && (a->ptr != NULL)) {
143 			BUF_MEM *b;
144 			b = (BUF_MEM *)a->ptr;
145 			if (a->flags & BIO_FLAGS_MEM_RDONLY)
146 				b->data = NULL;
147 			BUF_MEM_free(b);
148 			a->ptr = NULL;
149 		}
150 	}
151 	return (1);
152 }
153 
154 static int
155 mem_read(BIO *b, char *out, int outl)
156 {
157 	int ret = -1;
158 	BUF_MEM *bm;
159 
160 	bm = (BUF_MEM *)b->ptr;
161 	BIO_clear_retry_flags(b);
162 	ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
163 	if ((out != NULL) && (ret > 0)) {
164 		memcpy(out, bm->data, ret);
165 		bm->length -= ret;
166 		if (b->flags & BIO_FLAGS_MEM_RDONLY)
167 			bm->data += ret;
168 		else {
169 			memmove(&(bm->data[0]), &(bm->data[ret]), bm->length);
170 		}
171 	} else if (bm->length == 0) {
172 		ret = b->num;
173 		if (ret != 0)
174 			BIO_set_retry_read(b);
175 	}
176 	return (ret);
177 }
178 
179 static int
180 mem_write(BIO *b, const char *in, int inl)
181 {
182 	int ret = -1;
183 	int blen;
184 	BUF_MEM *bm;
185 
186 	bm = (BUF_MEM *)b->ptr;
187 	if (in == NULL) {
188 		BIOerror(BIO_R_NULL_PARAMETER);
189 		goto end;
190 	}
191 
192 	if (b->flags & BIO_FLAGS_MEM_RDONLY) {
193 		BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO);
194 		goto end;
195 	}
196 
197 	BIO_clear_retry_flags(b);
198 	blen = bm->length;
199 	if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
200 		goto end;
201 	memcpy(&(bm->data[blen]), in, inl);
202 	ret = inl;
203 end:
204 	return (ret);
205 }
206 
207 static long
208 mem_ctrl(BIO *b, int cmd, long num, void *ptr)
209 {
210 	long ret = 1;
211 	char **pptr;
212 
213 	BUF_MEM *bm = (BUF_MEM *)b->ptr;
214 
215 	switch (cmd) {
216 	case BIO_CTRL_RESET:
217 		if (bm->data != NULL) {
218 			/* For read only case reset to the start again */
219 			if (b->flags & BIO_FLAGS_MEM_RDONLY) {
220 				bm->data -= bm->max - bm->length;
221 				bm->length = bm->max;
222 			} else {
223 				memset(bm->data, 0, bm->max);
224 				bm->length = 0;
225 			}
226 		}
227 		break;
228 	case BIO_CTRL_EOF:
229 		ret = (long)(bm->length == 0);
230 		break;
231 	case BIO_C_SET_BUF_MEM_EOF_RETURN:
232 		b->num = (int)num;
233 		break;
234 	case BIO_CTRL_INFO:
235 		ret = (long)bm->length;
236 		if (ptr != NULL) {
237 			pptr = (char **)ptr;
238 			*pptr = (char *)&(bm->data[0]);
239 		}
240 		break;
241 	case BIO_C_SET_BUF_MEM:
242 		mem_free(b);
243 		b->shutdown = (int)num;
244 		b->ptr = ptr;
245 		break;
246 	case BIO_C_GET_BUF_MEM_PTR:
247 		if (ptr != NULL) {
248 			pptr = (char **)ptr;
249 			*pptr = (char *)bm;
250 		}
251 		break;
252 	case BIO_CTRL_GET_CLOSE:
253 		ret = (long)b->shutdown;
254 		break;
255 	case BIO_CTRL_SET_CLOSE:
256 		b->shutdown = (int)num;
257 		break;
258 
259 	case BIO_CTRL_WPENDING:
260 		ret = 0L;
261 		break;
262 	case BIO_CTRL_PENDING:
263 		ret = (long)bm->length;
264 		break;
265 	case BIO_CTRL_DUP:
266 	case BIO_CTRL_FLUSH:
267 		ret = 1;
268 		break;
269 	case BIO_CTRL_PUSH:
270 	case BIO_CTRL_POP:
271 	default:
272 		ret = 0;
273 		break;
274 	}
275 	return (ret);
276 }
277 
278 static int
279 mem_gets(BIO *bp, char *buf, int size)
280 {
281 	int i, j;
282 	int ret = -1;
283 	char *p;
284 	BUF_MEM *bm = (BUF_MEM *)bp->ptr;
285 
286 	BIO_clear_retry_flags(bp);
287 	j = bm->length;
288 	if ((size - 1) < j)
289 		j = size - 1;
290 	if (j <= 0) {
291 		*buf = '\0';
292 		return 0;
293 	}
294 	p = bm->data;
295 	for (i = 0; i < j; i++) {
296 		if (p[i] == '\n') {
297 			i++;
298 			break;
299 		}
300 	}
301 
302 	/*
303 	 * i is now the max num of bytes to copy, either j or up to
304 	 * and including the first newline
305 	 */
306 
307 	i = mem_read(bp, buf, i);
308 	if (i > 0)
309 		buf[i] = '\0';
310 	ret = i;
311 	return (ret);
312 }
313 
314 static int
315 mem_puts(BIO *bp, const char *str)
316 {
317 	int n, ret;
318 
319 	n = strlen(str);
320 	ret = mem_write(bp, str, n);
321 	/* memory semantics is that it will always work */
322 	return (ret);
323 }
324