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