xref: /freebsd/crypto/openssl/crypto/bio/bss_mem.c (revision bda54b8f964b4035dcf5aba394d14ed24370a6f2)
1 /*
2  * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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_lcl.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 /* BIO memory stores buffer and read pointer  */
61 typedef struct bio_buf_mem_st {
62     struct buf_mem_st *buf;   /* allocated buffer */
63     struct buf_mem_st *readp; /* read pointer */
64 } BIO_BUF_MEM;
65 
66 /*
67  * bio->num is used to hold the value to return on 'empty', if it is 0,
68  * should_retry is not set
69  */
70 
71 const BIO_METHOD *BIO_s_mem(void)
72 {
73     return &mem_method;
74 }
75 
76 const BIO_METHOD *BIO_s_secmem(void)
77 {
78     return(&secmem_method);
79 }
80 
81 BIO *BIO_new_mem_buf(const void *buf, int len)
82 {
83     BIO *ret;
84     BUF_MEM *b;
85     BIO_BUF_MEM *bb;
86     size_t sz;
87 
88     if (buf == NULL) {
89         BIOerr(BIO_F_BIO_NEW_MEM_BUF, BIO_R_NULL_PARAMETER);
90         return NULL;
91     }
92     sz = (len < 0) ? strlen(buf) : (size_t)len;
93     if ((ret = BIO_new(BIO_s_mem())) == NULL)
94         return NULL;
95     bb = (BIO_BUF_MEM *)ret->ptr;
96     b = bb->buf;
97     /* Cast away const and trust in the MEM_RDONLY flag. */
98     b->data = (void *)buf;
99     b->length = sz;
100     b->max = sz;
101     *bb->readp = *bb->buf;
102     ret->flags |= BIO_FLAGS_MEM_RDONLY;
103     /* Since this is static data retrying won't help */
104     ret->num = 0;
105     return ret;
106 }
107 
108 static int mem_init(BIO *bi, unsigned long flags)
109 {
110     BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
111 
112     if (bb == NULL)
113         return 0;
114     if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
115         OPENSSL_free(bb);
116         return 0;
117     }
118     if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
119         BUF_MEM_free(bb->buf);
120         OPENSSL_free(bb);
121         return 0;
122     }
123     *bb->readp = *bb->buf;
124     bi->shutdown = 1;
125     bi->init = 1;
126     bi->num = -1;
127     bi->ptr = (char *)bb;
128     return 1;
129 }
130 
131 static int mem_new(BIO *bi)
132 {
133     return mem_init(bi, 0L);
134 }
135 
136 static int secmem_new(BIO *bi)
137 {
138     return mem_init(bi, BUF_MEM_FLAG_SECURE);
139 }
140 
141 static int mem_free(BIO *a)
142 {
143     BIO_BUF_MEM *bb;
144 
145     if (a == NULL)
146         return 0;
147 
148     bb = (BIO_BUF_MEM *)a->ptr;
149     if (!mem_buf_free(a))
150         return 0;
151     OPENSSL_free(bb->readp);
152     OPENSSL_free(bb);
153     return 1;
154 }
155 
156 static int mem_buf_free(BIO *a)
157 {
158     if (a == NULL)
159         return 0;
160 
161     if (a->shutdown && a->init && a->ptr != NULL) {
162         BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
163         BUF_MEM *b = bb->buf;
164 
165         if (a->flags & BIO_FLAGS_MEM_RDONLY)
166             b->data = NULL;
167         BUF_MEM_free(b);
168     }
169     return 1;
170 }
171 
172 /*
173  * Reallocate memory buffer if read pointer differs
174  */
175 static int mem_buf_sync(BIO *b)
176 {
177     if (b != NULL && b->init != 0 && b->ptr != NULL) {
178         BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
179 
180         if (bbm->readp->data != bbm->buf->data) {
181             memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
182             bbm->buf->length = bbm->readp->length;
183             bbm->readp->data = bbm->buf->data;
184         }
185     }
186     return 0;
187 }
188 
189 static int mem_read(BIO *b, char *out, int outl)
190 {
191     int ret = -1;
192     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
193     BUF_MEM *bm = bbm->readp;
194 
195     BIO_clear_retry_flags(b);
196     ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
197     if ((out != NULL) && (ret > 0)) {
198         memcpy(out, bm->data, ret);
199         bm->length -= ret;
200         bm->data += ret;
201     } else if (bm->length == 0) {
202         ret = b->num;
203         if (ret != 0)
204             BIO_set_retry_read(b);
205     }
206     return ret;
207 }
208 
209 static int mem_write(BIO *b, const char *in, int inl)
210 {
211     int ret = -1;
212     int blen;
213     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
214 
215     if (in == NULL) {
216         BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
217         goto end;
218     }
219     if (b->flags & BIO_FLAGS_MEM_RDONLY) {
220         BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
221         goto end;
222     }
223     BIO_clear_retry_flags(b);
224     if (inl == 0)
225         return 0;
226     blen = bbm->readp->length;
227     mem_buf_sync(b);
228     if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
229         goto end;
230     memcpy(bbm->buf->data + blen, in, inl);
231     *bbm->readp = *bbm->buf;
232     ret = inl;
233  end:
234     return ret;
235 }
236 
237 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
238 {
239     long ret = 1;
240     char **pptr;
241     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
242     BUF_MEM *bm;
243 
244     switch (cmd) {
245     case BIO_CTRL_RESET:
246         bm = bbm->buf;
247         if (bm->data != NULL) {
248             /* For read only case reset to the start again */
249             if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
250                 bm->length = bm->max;
251             } else {
252                 memset(bm->data, 0, bm->max);
253                 bm->length = 0;
254             }
255             *bbm->readp = *bbm->buf;
256         }
257         break;
258     case BIO_CTRL_EOF:
259         bm = bbm->readp;
260         ret = (long)(bm->length == 0);
261         break;
262     case BIO_C_SET_BUF_MEM_EOF_RETURN:
263         b->num = (int)num;
264         break;
265     case BIO_CTRL_INFO:
266         bm = bbm->readp;
267         ret = (long)bm->length;
268         if (ptr != NULL) {
269             pptr = (char **)ptr;
270             *pptr = (char *)&(bm->data[0]);
271         }
272         break;
273     case BIO_C_SET_BUF_MEM:
274         mem_buf_free(b);
275         b->shutdown = (int)num;
276         bbm->buf = ptr;
277         *bbm->readp = *bbm->buf;
278         break;
279     case BIO_C_GET_BUF_MEM_PTR:
280         if (ptr != NULL) {
281             mem_buf_sync(b);
282             bm = bbm->readp;
283             pptr = (char **)ptr;
284             *pptr = (char *)bm;
285         }
286         break;
287     case BIO_CTRL_GET_CLOSE:
288         ret = (long)b->shutdown;
289         break;
290     case BIO_CTRL_SET_CLOSE:
291         b->shutdown = (int)num;
292         break;
293     case BIO_CTRL_WPENDING:
294         ret = 0L;
295         break;
296     case BIO_CTRL_PENDING:
297         bm = bbm->readp;
298         ret = (long)bm->length;
299         break;
300     case BIO_CTRL_DUP:
301     case BIO_CTRL_FLUSH:
302         ret = 1;
303         break;
304     case BIO_CTRL_PUSH:
305     case BIO_CTRL_POP:
306     default:
307         ret = 0;
308         break;
309     }
310     return ret;
311 }
312 
313 static int mem_gets(BIO *bp, char *buf, int size)
314 {
315     int i, j;
316     int ret = -1;
317     char *p;
318     BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
319     BUF_MEM *bm = bbm->readp;
320 
321     BIO_clear_retry_flags(bp);
322     j = bm->length;
323     if ((size - 1) < j)
324         j = size - 1;
325     if (j <= 0) {
326         *buf = '\0';
327         return 0;
328     }
329     p = bm->data;
330     for (i = 0; i < j; i++) {
331         if (p[i] == '\n') {
332             i++;
333             break;
334         }
335     }
336 
337     /*
338      * i is now the max num of bytes to copy, either j or up to
339      * and including the first newline
340      */
341 
342     i = mem_read(bp, buf, i);
343     if (i > 0)
344         buf[i] = '\0';
345     ret = i;
346     return ret;
347 }
348 
349 static int mem_puts(BIO *bp, const char *str)
350 {
351     int n, ret;
352 
353     n = strlen(str);
354     ret = mem_write(bp, str, n);
355     /* memory semantics is that it will always work */
356     return ret;
357 }
358