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