]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_mem.c
57b7a7449ee4642c66f20617f689f87a851a5f6c
[thirdparty/openssl.git] / crypto / bio / bss_mem.c
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_local.h"
13 #include "internal/cryptlib.h"
14
15 static int mem_write(BIO *h, const char *buf, int num);
16 static int mem_read(BIO *h, char *buf, int size);
17 static int mem_puts(BIO *h, const char *str);
18 static int mem_gets(BIO *h, char *str, int size);
19 static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
20 static int mem_new(BIO *h);
21 static int secmem_new(BIO *h);
22 static int mem_free(BIO *data);
23 static int mem_buf_free(BIO *data);
24 static int mem_buf_sync(BIO *h);
25
26 static const BIO_METHOD mem_method = {
27 BIO_TYPE_MEM,
28 "memory buffer",
29 /* TODO: Convert to new style write function */
30 bwrite_conv,
31 mem_write,
32 /* TODO: Convert to new style read function */
33 bread_conv,
34 mem_read,
35 mem_puts,
36 mem_gets,
37 mem_ctrl,
38 mem_new,
39 mem_free,
40 NULL, /* mem_callback_ctrl */
41 };
42
43 static const BIO_METHOD secmem_method = {
44 BIO_TYPE_MEM,
45 "secure memory buffer",
46 /* TODO: Convert to new style write function */
47 bwrite_conv,
48 mem_write,
49 /* TODO: Convert to new style read function */
50 bread_conv,
51 mem_read,
52 mem_puts,
53 mem_gets,
54 mem_ctrl,
55 secmem_new,
56 mem_free,
57 NULL, /* mem_callback_ctrl */
58 };
59
60 /*
61 * BIO memory stores buffer and read pointer
62 * however the roles are different for read only BIOs.
63 * In that case the readp just stores the original state
64 * to be used for reset.
65 */
66 typedef struct bio_buf_mem_st {
67 struct buf_mem_st *buf; /* allocated buffer */
68 struct buf_mem_st *readp; /* read pointer */
69 } BIO_BUF_MEM;
70
71 /*
72 * bio->num is used to hold the value to return on 'empty', if it is 0,
73 * should_retry is not set
74 */
75
76 const BIO_METHOD *BIO_s_mem(void)
77 {
78 return &mem_method;
79 }
80
81 const BIO_METHOD *BIO_s_secmem(void)
82 {
83 return(&secmem_method);
84 }
85
86 BIO *BIO_new_mem_buf(const void *buf, int len)
87 {
88 BIO *ret;
89 BUF_MEM *b;
90 BIO_BUF_MEM *bb;
91 size_t sz;
92
93 if (buf == NULL) {
94 BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
95 return NULL;
96 }
97 sz = (len < 0) ? strlen(buf) : (size_t)len;
98 if ((ret = BIO_new(BIO_s_mem())) == NULL)
99 return NULL;
100 bb = (BIO_BUF_MEM *)ret->ptr;
101 b = bb->buf;
102 /* Cast away const and trust in the MEM_RDONLY flag. */
103 b->data = (void *)buf;
104 b->length = sz;
105 b->max = sz;
106 *bb->readp = *bb->buf;
107 ret->flags |= BIO_FLAGS_MEM_RDONLY;
108 /* Since this is static data retrying won't help */
109 ret->num = 0;
110 return ret;
111 }
112
113 static int mem_init(BIO *bi, unsigned long flags)
114 {
115 BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
116
117 if (bb == NULL)
118 return 0;
119 if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
120 OPENSSL_free(bb);
121 return 0;
122 }
123 if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
124 BUF_MEM_free(bb->buf);
125 OPENSSL_free(bb);
126 return 0;
127 }
128 *bb->readp = *bb->buf;
129 bi->shutdown = 1;
130 bi->init = 1;
131 bi->num = -1;
132 bi->ptr = (char *)bb;
133 return 1;
134 }
135
136 static int mem_new(BIO *bi)
137 {
138 return mem_init(bi, 0L);
139 }
140
141 static int secmem_new(BIO *bi)
142 {
143 return mem_init(bi, BUF_MEM_FLAG_SECURE);
144 }
145
146 static int mem_free(BIO *a)
147 {
148 BIO_BUF_MEM *bb;
149
150 if (a == NULL)
151 return 0;
152
153 bb = (BIO_BUF_MEM *)a->ptr;
154 if (!mem_buf_free(a))
155 return 0;
156 OPENSSL_free(bb->readp);
157 OPENSSL_free(bb);
158 return 1;
159 }
160
161 static int mem_buf_free(BIO *a)
162 {
163 if (a == NULL)
164 return 0;
165
166 if (a->shutdown && a->init && a->ptr != NULL) {
167 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
168 BUF_MEM *b = bb->buf;
169
170 if (a->flags & BIO_FLAGS_MEM_RDONLY)
171 b->data = NULL;
172 BUF_MEM_free(b);
173 }
174 return 1;
175 }
176
177 /*
178 * Reallocate memory buffer if read pointer differs
179 * NOT FOR RDONLY
180 */
181 static int mem_buf_sync(BIO *b)
182 {
183 if (b != NULL && b->init != 0 && b->ptr != NULL) {
184 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
185
186 if (bbm->readp->data != bbm->buf->data) {
187 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
188 bbm->buf->length = bbm->readp->length;
189 bbm->readp->data = bbm->buf->data;
190 }
191 }
192 return 0;
193 }
194
195 static int mem_read(BIO *b, char *out, int outl)
196 {
197 int ret = -1;
198 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
199 BUF_MEM *bm = bbm->readp;
200
201 if (b->flags & BIO_FLAGS_MEM_RDONLY)
202 bm = bbm->buf;
203 BIO_clear_retry_flags(b);
204 ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
205 if ((out != NULL) && (ret > 0)) {
206 memcpy(out, bm->data, ret);
207 bm->length -= ret;
208 bm->max -= ret;
209 bm->data += ret;
210 } else if (bm->length == 0) {
211 ret = b->num;
212 if (ret != 0)
213 BIO_set_retry_read(b);
214 }
215 return ret;
216 }
217
218 static int mem_write(BIO *b, const char *in, int inl)
219 {
220 int ret = -1;
221 int blen;
222 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
223
224 if (in == NULL) {
225 BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
226 goto end;
227 }
228 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
229 BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
230 goto end;
231 }
232 BIO_clear_retry_flags(b);
233 if (inl == 0)
234 return 0;
235 blen = bbm->readp->length;
236 mem_buf_sync(b);
237 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
238 goto end;
239 memcpy(bbm->buf->data + blen, in, inl);
240 *bbm->readp = *bbm->buf;
241 ret = inl;
242 end:
243 return ret;
244 }
245
246 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
247 {
248 long ret = 1;
249 char **pptr;
250 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
251 BUF_MEM *bm, *bo; /* bio_mem, bio_other */
252 long off, remain;
253
254 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
255 bm = bbm->buf;
256 bo = bbm->readp;
257 } else {
258 bm = bbm->readp;
259 bo = bbm->buf;
260 }
261 off = bm->data - bo->data;
262 remain = bm->length;
263
264 switch (cmd) {
265 case BIO_CTRL_RESET:
266 bm = bbm->buf;
267 if (bm->data != NULL) {
268 if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
269 if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
270 memset(bm->data, 0, bm->max);
271 bm->length = 0;
272 }
273 *bbm->readp = *bbm->buf;
274 } else {
275 /* For read only case just reset to the start again */
276 *bbm->buf = *bbm->readp;
277 }
278 }
279 break;
280 case BIO_C_FILE_SEEK:
281 if (num < 0 || num > off + remain)
282 return -1; /* Can't see outside of the current buffer */
283
284 bm->data = bo->data + num;
285 bm->length = bo->length - num;
286 bm->max = bo->max - num;
287 off = num;
288 /* FALLTHRU */
289 case BIO_C_FILE_TELL:
290 ret = off;
291 break;
292 case BIO_CTRL_EOF:
293 ret = (long)(bm->length == 0);
294 break;
295 case BIO_C_SET_BUF_MEM_EOF_RETURN:
296 b->num = (int)num;
297 break;
298 case BIO_CTRL_INFO:
299 ret = (long)bm->length;
300 if (ptr != NULL) {
301 pptr = (char **)ptr;
302 *pptr = (char *)&(bm->data[0]);
303 }
304 break;
305 case BIO_C_SET_BUF_MEM:
306 mem_buf_free(b);
307 b->shutdown = (int)num;
308 bbm->buf = ptr;
309 *bbm->readp = *bbm->buf;
310 break;
311 case BIO_C_GET_BUF_MEM_PTR:
312 if (ptr != NULL) {
313 if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
314 mem_buf_sync(b);
315 bm = bbm->buf;
316 pptr = (char **)ptr;
317 *pptr = (char *)bm;
318 }
319 break;
320 case BIO_CTRL_GET_CLOSE:
321 ret = (long)b->shutdown;
322 break;
323 case BIO_CTRL_SET_CLOSE:
324 b->shutdown = (int)num;
325 break;
326 case BIO_CTRL_WPENDING:
327 ret = 0L;
328 break;
329 case BIO_CTRL_PENDING:
330 ret = (long)bm->length;
331 break;
332 case BIO_CTRL_DUP:
333 case BIO_CTRL_FLUSH:
334 ret = 1;
335 break;
336 case BIO_CTRL_PUSH:
337 case BIO_CTRL_POP:
338 default:
339 ret = 0;
340 break;
341 }
342 return ret;
343 }
344
345 static int mem_gets(BIO *bp, char *buf, int size)
346 {
347 int i, j;
348 int ret = -1;
349 char *p;
350 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
351 BUF_MEM *bm = bbm->readp;
352
353 if (bp->flags & BIO_FLAGS_MEM_RDONLY)
354 bm = bbm->buf;
355 BIO_clear_retry_flags(bp);
356 j = bm->length;
357 if ((size - 1) < j)
358 j = size - 1;
359 if (j <= 0) {
360 *buf = '\0';
361 return 0;
362 }
363 p = bm->data;
364 for (i = 0; i < j; i++) {
365 if (p[i] == '\n') {
366 i++;
367 break;
368 }
369 }
370
371 /*
372 * i is now the max num of bytes to copy, either j or up to
373 * and including the first newline
374 */
375
376 i = mem_read(bp, buf, i);
377 if (i > 0)
378 buf[i] = '\0';
379 ret = i;
380 return ret;
381 }
382
383 static int mem_puts(BIO *bp, const char *str)
384 {
385 int n, ret;
386
387 n = strlen(str);
388 ret = mem_write(bp, str, n);
389 /* memory semantics is that it will always work */
390 return ret;
391 }