]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bss_mem.c
Since return is inconsistent, I removed unnecessary parentheses and
[thirdparty/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 /* 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,
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,
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 return (mem_buf_free(a, 1));
144 }
145
146 static int mem_buf_free(BIO *a, int free_all)
147 {
148 if (a == NULL)
149 return (0);
150 if (a->shutdown) {
151 if ((a->init) && (a->ptr != NULL)) {
152 BUF_MEM *b;
153 BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
154
155 if (bb != NULL) {
156 b = bb->buf;
157 if (a->flags & BIO_FLAGS_MEM_RDONLY)
158 b->data = NULL;
159 BUF_MEM_free(b);
160 if (free_all) {
161 OPENSSL_free(bb->readp);
162 OPENSSL_free(bb);
163 }
164 }
165 a->ptr = NULL;
166 }
167 }
168 return 1;
169 }
170
171 /*
172 * Reallocate memory buffer if read pointer differs
173 */
174 static int mem_buf_sync(BIO *b)
175 {
176 if (b != NULL && b->init != 0 && b->ptr != NULL) {
177 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
178
179 if (bbm->readp->data != bbm->buf->data) {
180 memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
181 bbm->buf->length = bbm->readp->length;
182 bbm->readp->data = bbm->buf->data;
183 }
184 }
185 return (0);
186 }
187
188 static int mem_read(BIO *b, char *out, int outl)
189 {
190 int ret = -1;
191 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
192 BUF_MEM *bm = bbm->readp;
193
194 BIO_clear_retry_flags(b);
195 ret = (outl >= 0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
196 if ((out != NULL) && (ret > 0)) {
197 memcpy(out, bm->data, ret);
198 bm->length -= ret;
199 bm->data += ret;
200 } else if (bm->length == 0) {
201 ret = b->num;
202 if (ret != 0)
203 BIO_set_retry_read(b);
204 }
205 return (ret);
206 }
207
208 static int mem_write(BIO *b, const char *in, int inl)
209 {
210 int ret = -1;
211 int blen;
212 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
213
214 if (in == NULL) {
215 BIOerr(BIO_F_MEM_WRITE, BIO_R_NULL_PARAMETER);
216 goto end;
217 }
218 if (b->flags & BIO_FLAGS_MEM_RDONLY) {
219 BIOerr(BIO_F_MEM_WRITE, BIO_R_WRITE_TO_READ_ONLY_BIO);
220 goto end;
221 }
222 BIO_clear_retry_flags(b);
223 blen = bbm->readp->length;
224 mem_buf_sync(b);
225 if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
226 goto end;
227 memcpy(bbm->buf->data + blen, in, inl);
228 *bbm->readp = *bbm->buf;
229 ret = inl;
230 end:
231 return (ret);
232 }
233
234 static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
235 {
236 long ret = 1;
237 char **pptr;
238 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
239 BUF_MEM *bm;
240
241 switch (cmd) {
242 case BIO_CTRL_RESET:
243 bm = bbm->buf;
244 if (bm->data != NULL) {
245 /* For read only case reset to the start again */
246 if ((b->flags & BIO_FLAGS_MEM_RDONLY) || (b->flags & BIO_FLAGS_NONCLEAR_RST)) {
247 bm->length = bm->max;
248 } else {
249 memset(bm->data, 0, bm->max);
250 bm->length = 0;
251 }
252 *bbm->readp = *bbm->buf;
253 }
254 break;
255 case BIO_CTRL_EOF:
256 bm = bbm->readp;
257 ret = (long)(bm->length == 0);
258 break;
259 case BIO_C_SET_BUF_MEM_EOF_RETURN:
260 b->num = (int)num;
261 break;
262 case BIO_CTRL_INFO:
263 bm = bbm->readp;
264 ret = (long)bm->length;
265 if (ptr != NULL) {
266 pptr = (char **)ptr;
267 *pptr = (char *)&(bm->data[0]);
268 }
269 break;
270 case BIO_C_SET_BUF_MEM:
271 mem_buf_free(b, 0);
272 b->shutdown = (int)num;
273 bbm->buf = ptr;
274 *bbm->readp = *bbm->buf;
275 b->ptr = bbm;
276 break;
277 case BIO_C_GET_BUF_MEM_PTR:
278 if (ptr != NULL) {
279 mem_buf_sync(b);
280 bm = bbm->readp;
281 pptr = (char **)ptr;
282 *pptr = (char *)bm;
283 }
284 break;
285 case BIO_CTRL_GET_CLOSE:
286 ret = (long)b->shutdown;
287 break;
288 case BIO_CTRL_SET_CLOSE:
289 b->shutdown = (int)num;
290 break;
291 case BIO_CTRL_WPENDING:
292 ret = 0L;
293 break;
294 case BIO_CTRL_PENDING:
295 bm = bbm->readp;
296 ret = (long)bm->length;
297 break;
298 case BIO_CTRL_DUP:
299 case BIO_CTRL_FLUSH:
300 ret = 1;
301 break;
302 case BIO_CTRL_PUSH:
303 case BIO_CTRL_POP:
304 default:
305 ret = 0;
306 break;
307 }
308 return (ret);
309 }
310
311 static int mem_gets(BIO *bp, char *buf, int size)
312 {
313 int i, j;
314 int ret = -1;
315 char *p;
316 BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
317 BUF_MEM *bm = bbm->readp;
318
319 BIO_clear_retry_flags(bp);
320 j = bm->length;
321 if ((size - 1) < j)
322 j = size - 1;
323 if (j <= 0) {
324 *buf = '\0';
325 return 0;
326 }
327 p = bm->data;
328 for (i = 0; i < j; i++) {
329 if (p[i] == '\n') {
330 i++;
331 break;
332 }
333 }
334
335 /*
336 * i is now the max num of bytes to copy, either j or up to
337 * and including the first newline
338 */
339
340 i = mem_read(bp, buf, i);
341 if (i > 0)
342 buf[i] = '\0';
343 ret = i;
344 return (ret);
345 }
346
347 static int mem_puts(BIO *bp, const char *str)
348 {
349 int n, ret;
350
351 n = strlen(str);
352 ret = mem_write(bp, str, n);
353 /* memory semantics is that it will always work */
354 return (ret);
355 }