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