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