014acf29637c537dde4353e812ced489db6019f4
[openssl.git] / crypto / bio / bss_mem.c
1 /*
2  * Copyright 1995-2021 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 dgram_mem_new(BIO *h);
22 static int secmem_new(BIO *h);
23 static int mem_free(BIO *data);
24 static int mem_buf_free(BIO *data);
25 static int mem_buf_sync(BIO *h);
26
27 static const BIO_METHOD mem_method = {
28     BIO_TYPE_MEM,
29     "memory buffer",
30     bwrite_conv,
31     mem_write,
32     bread_conv,
33     mem_read,
34     mem_puts,
35     mem_gets,
36     mem_ctrl,
37     mem_new,
38     mem_free,
39     NULL,                      /* mem_callback_ctrl */
40 };
41
42 static const BIO_METHOD dgram_mem_method = {
43     BIO_TYPE_MEM,
44     "datagram memory buffer",
45     bwrite_conv,
46     mem_write,
47     bread_conv,
48     mem_read,
49     mem_puts,
50     mem_gets,
51     mem_ctrl,
52     dgram_mem_new,
53     mem_free,
54     NULL,                      /* mem_callback_ctrl */
55 };
56
57 static const BIO_METHOD secmem_method = {
58     BIO_TYPE_MEM,
59     "secure memory buffer",
60     bwrite_conv,
61     mem_write,
62     bread_conv,
63     mem_read,
64     mem_puts,
65     mem_gets,
66     mem_ctrl,
67     secmem_new,
68     mem_free,
69     NULL,                      /* mem_callback_ctrl */
70 };
71
72 struct buf_mem_dgram_st {
73     char *dgram;  /* Pointer into the buffer for where the dgram starts */
74     size_t dgramlen;  /* Length of the dgram */
75     struct buf_mem_dgram_st *next; /* Next dgram to read */
76 };
77
78 /*
79  * BIO memory stores buffer and read pointer
80  * however the roles are different for read only BIOs.
81  * In that case the readp just stores the original state
82  * to be used for reset.
83  */
84 typedef struct bio_buf_mem_st {
85     struct buf_mem_st *buf;   /* allocated buffer */
86     struct buf_mem_st *readp; /* read pointer */
87     struct buf_mem_dgram_st *dgrams; /* linked list of dgram data */
88     struct buf_mem_dgram_st *last; /* last dgram in the linked list */
89     int use_dgrams;
90 } BIO_BUF_MEM;
91
92 /*
93  * bio->num is used to hold the value to return on 'empty', if it is 0,
94  * should_retry is not set
95  */
96
97 const BIO_METHOD *BIO_s_mem(void)
98 {
99     return &mem_method;
100 }
101
102 const BIO_METHOD *BIO_s_dgram_mem(void)
103 {
104     return &dgram_mem_method;
105 }
106
107 const BIO_METHOD *BIO_s_secmem(void)
108 {
109     return(&secmem_method);
110 }
111
112 BIO *BIO_new_mem_buf(const void *buf, int len)
113 {
114     BIO *ret;
115     BUF_MEM *b;
116     BIO_BUF_MEM *bb;
117     size_t sz;
118
119     if (buf == NULL) {
120         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
121         return NULL;
122     }
123     sz = (len < 0) ? strlen(buf) : (size_t)len;
124     if ((ret = BIO_new(BIO_s_mem())) == NULL)
125         return NULL;
126     bb = (BIO_BUF_MEM *)ret->ptr;
127     b = bb->buf;
128     /* Cast away const and trust in the MEM_RDONLY flag. */
129     b->data = (void *)buf;
130     b->length = sz;
131     b->max = sz;
132     *bb->readp = *bb->buf;
133     ret->flags |= BIO_FLAGS_MEM_RDONLY;
134     /* Since this is static data retrying won't help */
135     ret->num = 0;
136     return ret;
137 }
138
139 static int mem_init(BIO *bi, unsigned long flags)
140 {
141     BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
142
143     if (bb == NULL)
144         return 0;
145     if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
146         OPENSSL_free(bb);
147         return 0;
148     }
149     if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
150         BUF_MEM_free(bb->buf);
151         OPENSSL_free(bb);
152         return 0;
153     }
154     *bb->readp = *bb->buf;
155     bi->shutdown = 1;
156     bi->init = 1;
157     bi->num = -1;
158     bi->ptr = (char *)bb;
159     return 1;
160 }
161
162 static int mem_new(BIO *bi)
163 {
164     return mem_init(bi, 0L);
165 }
166
167 static int dgram_mem_new(BIO *bi)
168 {
169     BIO_BUF_MEM *bbm;
170
171     if (!mem_init(bi, 0L))
172         return 0;
173
174     bbm = (BIO_BUF_MEM *)bi->ptr;
175
176     bbm->use_dgrams = 1;
177     bi->num = -1;
178
179     return 1;
180 }
181
182 static int secmem_new(BIO *bi)
183 {
184     return mem_init(bi, BUF_MEM_FLAG_SECURE);
185 }
186
187 static void clear_all_dgrams(BIO_BUF_MEM *bbm)
188 {
189     struct buf_mem_dgram_st *dgrams = bbm->dgrams;
190
191     while (dgrams != NULL) {
192         struct buf_mem_dgram_st *tmp = dgrams;
193
194         dgrams = dgrams->next;
195         OPENSSL_free(tmp);
196     }
197     bbm->dgrams = NULL;
198 }
199
200 static int mem_free(BIO *a)
201 {
202     BIO_BUF_MEM *bb;
203
204     if (a == NULL)
205         return 0;
206
207     bb = (BIO_BUF_MEM *)a->ptr;
208     if (!mem_buf_free(a))
209         return 0;
210     OPENSSL_free(bb->readp);
211     clear_all_dgrams(bb);
212     OPENSSL_free(bb);
213     return 1;
214 }
215
216 static int mem_buf_free(BIO *a)
217 {
218     if (a == NULL)
219         return 0;
220
221     if (a->shutdown && a->init && a->ptr != NULL) {
222         BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
223         BUF_MEM *b = bb->buf;
224
225         if (a->flags & BIO_FLAGS_MEM_RDONLY)
226             b->data = NULL;
227         BUF_MEM_free(b);
228     }
229     return 1;
230 }
231
232 /*
233  * Reallocate memory buffer if read pointer differs
234  * NOT FOR RDONLY
235  */
236 static int mem_buf_sync(BIO *b)
237 {
238     if (b != NULL && b->init != 0 && b->ptr != NULL) {
239         BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
240
241         if (bbm->readp->data != bbm->buf->data) {
242             memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
243             bbm->buf->length = bbm->readp->length;
244             bbm->readp->data = bbm->buf->data;
245         }
246     }
247     return 0;
248 }
249
250 static int mem_read(BIO *b, char *out, int outl)
251 {
252     int ret = -1;
253     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
254     BUF_MEM *bm = bbm->readp;
255     size_t maxreadlen = 0;
256     int eof = 0;
257
258     if (b->flags & BIO_FLAGS_MEM_RDONLY)
259         bm = bbm->buf;
260     BIO_clear_retry_flags(b);
261     if (bbm->use_dgrams) {
262         if (bbm->dgrams != NULL) {
263             maxreadlen = bbm->dgrams->dgramlen;
264             if (!ossl_assert(maxreadlen <= bm->length))
265                 return 0;
266         } else {
267             eof = 1;
268         }
269     } else {
270         maxreadlen = bm->length;
271         eof = (maxreadlen == 0);
272     }
273     ret = (outl >= 0 && (size_t)outl > maxreadlen) ? (int)maxreadlen : outl;
274     if ((out != NULL) && (ret > 0)) {
275         size_t flushlen;
276
277         memcpy(out, bm->data, ret);
278         flushlen = bbm->use_dgrams ? maxreadlen : (size_t)ret;
279             
280         bm->length -= flushlen;
281         bm->max -= flushlen;
282         bm->data += flushlen;
283         if (bbm->use_dgrams) {
284             struct buf_mem_dgram_st *tmp = bbm->dgrams;
285
286             bbm->dgrams = tmp->next;
287             OPENSSL_free(tmp);
288             if (bbm->dgrams == NULL)
289                 bbm->last = NULL;
290         }
291     } else if (eof) {
292         ret = b->num;
293         if (ret != 0)
294             BIO_set_retry_read(b);
295     }
296     return ret;
297 }
298
299 static int mem_write(BIO *b, const char *in, int inl)
300 {
301     int ret = -1;
302     int blen;
303     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
304
305     if (b->flags & BIO_FLAGS_MEM_RDONLY) {
306         ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
307         goto end;
308     }
309     BIO_clear_retry_flags(b);
310
311     if (inl == 0)
312         return 0;
313
314     if (in == NULL) {
315         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
316         goto end;
317     }
318     blen = bbm->readp->length;
319     mem_buf_sync(b);
320     if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
321         goto end;
322
323     memcpy(bbm->buf->data + blen, in, inl);
324     *bbm->readp = *bbm->buf;
325
326     if (bbm->use_dgrams) {
327         struct buf_mem_dgram_st *dgram = OPENSSL_malloc(sizeof(*dgram));
328
329         if (dgram == NULL)
330             goto end;
331
332         dgram->dgram = bbm->buf->data + blen;
333         dgram->dgramlen = inl;
334         dgram->next = NULL;
335         if (bbm->dgrams == NULL)
336             bbm->dgrams = dgram;
337         else
338             bbm->last->next = dgram;
339         bbm->last = dgram;
340     }
341
342     ret = inl;
343  end:
344     return ret;
345 }
346
347 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
348 {
349     long ret = 1;
350     char **pptr;
351     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
352     BUF_MEM *bm, *bo;            /* bio_mem, bio_other */
353     long off, remain;
354
355     if (b->flags & BIO_FLAGS_MEM_RDONLY) {
356         bm = bbm->buf;
357         bo = bbm->readp;
358     } else {
359         bm = bbm->readp;
360         bo = bbm->buf;
361     }
362     off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
363     remain = bm->length;
364
365     switch (cmd) {
366     case BIO_CTRL_RESET:
367         bm = bbm->buf;
368         if (bm->data != NULL) {
369             if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
370                 if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
371                     memset(bm->data, 0, bm->max);
372                     bm->length = 0;
373                 }
374                 *bbm->readp = *bbm->buf;
375             } else {
376                 /* For read only case just reset to the start again */
377                 *bbm->buf = *bbm->readp;
378             }
379         }
380         clear_all_dgrams(bbm);
381         break;
382     case BIO_C_FILE_SEEK:
383         if (num < 0 || num > off + remain)
384             return -1;   /* Can't see outside of the current buffer */
385
386         bm->data = (num != 0) ? bo->data + num : bo->data;
387         bm->length = bo->length - num;
388         bm->max = bo->max - num;
389         off = num;
390         /* FALLTHRU */
391     case BIO_C_FILE_TELL:
392         ret = off;
393         break;
394     case BIO_CTRL_EOF:
395         ret = (long)(bm->length == 0 && bbm->use_dgrams == 0);
396         break;
397     case BIO_C_SET_BUF_MEM_EOF_RETURN:
398         if (!bbm->use_dgrams)
399             b->num = (int)num;
400         else
401             ret = -1;
402         break;
403     case BIO_CTRL_INFO:
404         ret = (long)bm->length;
405         if (ptr != NULL) {
406             pptr = (char **)ptr;
407             *pptr = (char *)(bm->data);
408         }
409         break;
410     case BIO_C_SET_BUF_MEM:
411         mem_buf_free(b);
412         b->shutdown = (int)num;
413         bbm->buf = ptr;
414         *bbm->readp = *bbm->buf;
415         break;
416     case BIO_C_GET_BUF_MEM_PTR:
417         if (ptr != NULL) {
418             if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
419                 mem_buf_sync(b);
420             bm = bbm->buf;
421             pptr = (char **)ptr;
422             *pptr = (char *)bm;
423         }
424         break;
425     case BIO_CTRL_GET_CLOSE:
426         ret = (long)b->shutdown;
427         break;
428     case BIO_CTRL_SET_CLOSE:
429         b->shutdown = (int)num;
430         break;
431     case BIO_CTRL_WPENDING:
432         ret = 0L;
433         break;
434     case BIO_CTRL_PENDING:
435         ret = (long)bm->length;
436         break;
437     case BIO_CTRL_DUP:
438     case BIO_CTRL_FLUSH:
439         ret = 1;
440         break;
441     case BIO_CTRL_PUSH:
442     case BIO_CTRL_POP:
443     default:
444         ret = 0;
445         break;
446     }
447     return ret;
448 }
449
450 static int mem_gets(BIO *bp, char *buf, int size)
451 {
452     int i, j;
453     int ret = -1;
454     char *p;
455     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
456     BUF_MEM *bm = bbm->readp;
457
458     if (bp->flags & BIO_FLAGS_MEM_RDONLY)
459         bm = bbm->buf;
460     BIO_clear_retry_flags(bp);
461     j = (!bbm->use_dgrams || bbm->dgrams == NULL) ? bm->length
462                                                   : bbm->dgrams->dgramlen;
463     if ((size - 1) < j)
464         j = size - 1;
465     if (j <= 0) {
466         *buf = '\0';
467         return 0;
468     }
469     p = bm->data;
470     for (i = 0; i < j; i++) {
471         if (p[i] == '\n') {
472             i++;
473             break;
474         }
475     }
476
477     /*
478      * i is now the max num of bytes to copy, either j or up to
479      * and including the first newline
480      */
481
482     i = mem_read(bp, buf, i);
483     if (i > 0)
484         buf[i] = '\0';
485     ret = i;
486     return ret;
487 }
488
489 static int mem_puts(BIO *bp, const char *str)
490 {
491     int n, ret;
492
493     n = strlen(str);
494     ret = mem_write(bp, str, n);
495     /* memory semantics is that it will always work */
496     return ret;
497 }