Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/bio/bss_mem.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* $OpenBSD: bss_mem.c,v 1.17 2018/05/12 18:51:59 tb Exp $ */ | - | ||||||||||||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | - | ||||||||||||
3 | * All rights reserved. | - | ||||||||||||
4 | * | - | ||||||||||||
5 | * This package is an SSL implementation written | - | ||||||||||||
6 | * by Eric Young (eay@cryptsoft.com). | - | ||||||||||||
7 | * The implementation was written so as to conform with Netscapes SSL. | - | ||||||||||||
8 | * | - | ||||||||||||
9 | * This library is free for commercial and non-commercial use as long as | - | ||||||||||||
10 | * the following conditions are aheared to. The following conditions | - | ||||||||||||
11 | * apply to all code found in this distribution, be it the RC4, RSA, | - | ||||||||||||
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | - | ||||||||||||
13 | * included with this distribution is covered by the same copyright terms | - | ||||||||||||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | - | ||||||||||||
15 | * | - | ||||||||||||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | - | ||||||||||||
17 | * the code are not to be removed. | - | ||||||||||||
18 | * If this package is used in a product, Eric Young should be given attribution | - | ||||||||||||
19 | * as the author of the parts of the library used. | - | ||||||||||||
20 | * This can be in the form of a textual message at program startup or | - | ||||||||||||
21 | * in documentation (online or textual) provided with the package. | - | ||||||||||||
22 | * | - | ||||||||||||
23 | * Redistribution and use in source and binary forms, with or without | - | ||||||||||||
24 | * modification, are permitted provided that the following conditions | - | ||||||||||||
25 | * are met: | - | ||||||||||||
26 | * 1. Redistributions of source code must retain the copyright | - | ||||||||||||
27 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||
28 | * 2. Redistributions in binary form must reproduce the above copyright | - | ||||||||||||
29 | * notice, this list of conditions and the following disclaimer in the | - | ||||||||||||
30 | * documentation and/or other materials provided with the distribution. | - | ||||||||||||
31 | * 3. All advertising materials mentioning features or use of this software | - | ||||||||||||
32 | * must display the following acknowledgement: | - | ||||||||||||
33 | * "This product includes cryptographic software written by | - | ||||||||||||
34 | * Eric Young (eay@cryptsoft.com)" | - | ||||||||||||
35 | * The word 'cryptographic' can be left out if the rouines from the library | - | ||||||||||||
36 | * being used are not cryptographic related :-). | - | ||||||||||||
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | - | ||||||||||||
38 | * the apps directory (application code) you must include an acknowledgement: | - | ||||||||||||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | - | ||||||||||||
40 | * | - | ||||||||||||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | - | ||||||||||||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - | ||||||||||||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | - | ||||||||||||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | - | ||||||||||||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | - | ||||||||||||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | - | ||||||||||||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | - | ||||||||||||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | - | ||||||||||||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | - | ||||||||||||
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | - | ||||||||||||
51 | * SUCH DAMAGE. | - | ||||||||||||
52 | * | - | ||||||||||||
53 | * The licence and distribution terms for any publically available version or | - | ||||||||||||
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | - | ||||||||||||
55 | * copied and put under another distribution licence | - | ||||||||||||
56 | * [including the GNU Public Licence.] | - | ||||||||||||
57 | */ | - | ||||||||||||
58 | - | |||||||||||||
59 | #include <errno.h> | - | ||||||||||||
60 | #include <stdio.h> | - | ||||||||||||
61 | #include <string.h> | - | ||||||||||||
62 | - | |||||||||||||
63 | #include <openssl/bio.h> | - | ||||||||||||
64 | #include <openssl/err.h> | - | ||||||||||||
65 | #include <openssl/buffer.h> | - | ||||||||||||
66 | - | |||||||||||||
67 | static int mem_write(BIO *h, const char *buf, int num); | - | ||||||||||||
68 | static int mem_read(BIO *h, char *buf, int size); | - | ||||||||||||
69 | static int mem_puts(BIO *h, const char *str); | - | ||||||||||||
70 | static int mem_gets(BIO *h, char *str, int size); | - | ||||||||||||
71 | static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2); | - | ||||||||||||
72 | static int mem_new(BIO *h); | - | ||||||||||||
73 | static int mem_free(BIO *data); | - | ||||||||||||
74 | - | |||||||||||||
75 | static const BIO_METHOD mem_method = { | - | ||||||||||||
76 | .type = BIO_TYPE_MEM, | - | ||||||||||||
77 | .name = "memory buffer", | - | ||||||||||||
78 | .bwrite = mem_write, | - | ||||||||||||
79 | .bread = mem_read, | - | ||||||||||||
80 | .bputs = mem_puts, | - | ||||||||||||
81 | .bgets = mem_gets, | - | ||||||||||||
82 | .ctrl = mem_ctrl, | - | ||||||||||||
83 | .create = mem_new, | - | ||||||||||||
84 | .destroy = mem_free | - | ||||||||||||
85 | }; | - | ||||||||||||
86 | - | |||||||||||||
87 | /* bio->num is used to hold the value to return on 'empty', if it is | - | ||||||||||||
88 | * 0, should_retry is not set */ | - | ||||||||||||
89 | - | |||||||||||||
90 | const BIO_METHOD * | - | ||||||||||||
91 | BIO_s_mem(void) | - | ||||||||||||
92 | { | - | ||||||||||||
93 | return (&mem_method); executed 424 times by 8 tests: return (&mem_method); Executed by:
| 424 | ||||||||||||
94 | } | - | ||||||||||||
95 | - | |||||||||||||
96 | BIO * | - | ||||||||||||
97 | BIO_new_mem_buf(const void *buf, int len) | - | ||||||||||||
98 | { | - | ||||||||||||
99 | BIO *ret; | - | ||||||||||||
100 | BUF_MEM *b; | - | ||||||||||||
101 | size_t sz; | - | ||||||||||||
102 | - | |||||||||||||
103 | if (!buf) {
| 0-137 | ||||||||||||
104 | BIOerror(BIO_R_NULL_PARAMETER); | - | ||||||||||||
105 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
106 | } | - | ||||||||||||
107 | sz = (len < 0) ? strlen(buf) : (size_t)len;
| 1-136 | ||||||||||||
108 | if (!(ret = BIO_new(BIO_s_mem())))
| 0-137 | ||||||||||||
109 | return NULL; never executed: return ((void *)0) ; | 0 | ||||||||||||
110 | b = (BUF_MEM *)ret->ptr; | - | ||||||||||||
111 | b->data = (void *)buf; /* Trust in the BIO_FLAGS_MEM_RDONLY flag. */ | - | ||||||||||||
112 | b->length = sz; | - | ||||||||||||
113 | b->max = sz; | - | ||||||||||||
114 | ret->flags |= BIO_FLAGS_MEM_RDONLY; | - | ||||||||||||
115 | /* Since this is static data retrying wont help */ | - | ||||||||||||
116 | ret->num = 0; | - | ||||||||||||
117 | return ret; executed 137 times by 7 tests: return ret; Executed by:
| 137 | ||||||||||||
118 | } | - | ||||||||||||
119 | - | |||||||||||||
120 | static int | - | ||||||||||||
121 | mem_new(BIO *bi) | - | ||||||||||||
122 | { | - | ||||||||||||
123 | BUF_MEM *b; | - | ||||||||||||
124 | - | |||||||||||||
125 | if ((b = BUF_MEM_new()) == NULL)
| 0-424 | ||||||||||||
126 | return (0); never executed: return (0); | 0 | ||||||||||||
127 | bi->shutdown = 1; | - | ||||||||||||
128 | bi->init = 1; | - | ||||||||||||
129 | bi->num = -1; | - | ||||||||||||
130 | bi->ptr = (char *)b; | - | ||||||||||||
131 | return (1); executed 424 times by 8 tests: return (1); Executed by:
| 424 | ||||||||||||
132 | } | - | ||||||||||||
133 | - | |||||||||||||
134 | static int | - | ||||||||||||
135 | mem_free(BIO *a) | - | ||||||||||||
136 | { | - | ||||||||||||
137 | if (a == NULL)
| 0-421 | ||||||||||||
138 | return (0); never executed: return (0); | 0 | ||||||||||||
139 | if (a->shutdown) {
| 0-421 | ||||||||||||
140 | if ((a->init) && (a->ptr != NULL)) {
| 0-421 | ||||||||||||
141 | BUF_MEM *b; | - | ||||||||||||
142 | b = (BUF_MEM *)a->ptr; | - | ||||||||||||
143 | if (a->flags & BIO_FLAGS_MEM_RDONLY)
| 137-284 | ||||||||||||
144 | b->data = NULL; executed 137 times by 7 tests: b->data = ((void *)0) ; Executed by:
| 137 | ||||||||||||
145 | BUF_MEM_free(b); | - | ||||||||||||
146 | a->ptr = NULL; | - | ||||||||||||
147 | } executed 421 times by 8 tests: end of block Executed by:
| 421 | ||||||||||||
148 | } executed 421 times by 8 tests: end of block Executed by:
| 421 | ||||||||||||
149 | return (1); executed 421 times by 8 tests: return (1); Executed by:
| 421 | ||||||||||||
150 | } | - | ||||||||||||
151 | - | |||||||||||||
152 | static int | - | ||||||||||||
153 | mem_read(BIO *b, char *out, int outl) | - | ||||||||||||
154 | { | - | ||||||||||||
155 | int ret = -1; | - | ||||||||||||
156 | BUF_MEM *bm; | - | ||||||||||||
157 | - | |||||||||||||
158 | bm = (BUF_MEM *)b->ptr; | - | ||||||||||||
159 | BIO_clear_retry_flags(b); | - | ||||||||||||
160 | ret = (outl >=0 && (size_t)outl > bm->length) ? (int)bm->length : outl;
| 0-2211 | ||||||||||||
161 | if ((out != NULL) && (ret > 0)) {
| 0-2211 | ||||||||||||
162 | memcpy(out, bm->data, ret); | - | ||||||||||||
163 | bm->length -= ret; | - | ||||||||||||
164 | if (b->flags & BIO_FLAGS_MEM_RDONLY)
| 907-1056 | ||||||||||||
165 | bm->data += ret; executed 907 times by 7 tests: bm->data += ret; Executed by:
| 907 | ||||||||||||
166 | else { | - | ||||||||||||
167 | memmove(&(bm->data[0]), &(bm->data[ret]), bm->length); | - | ||||||||||||
168 | } executed 1056 times by 2 tests: end of block Executed by:
| 1056 | ||||||||||||
169 | } else if (bm->length == 0) {
| 0-248 | ||||||||||||
170 | ret = b->num; | - | ||||||||||||
171 | if (ret != 0)
| 108-140 | ||||||||||||
172 | BIO_set_retry_read(b); executed 140 times by 1 test: BIO_set_flags(b, (0x01|0x08)); Executed by:
| 140 | ||||||||||||
173 | } executed 248 times by 5 tests: end of block Executed by:
| 248 | ||||||||||||
174 | return (ret); executed 2211 times by 8 tests: return (ret); Executed by:
| 2211 | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | static int | - | ||||||||||||
178 | mem_write(BIO *b, const char *in, int inl) | - | ||||||||||||
179 | { | - | ||||||||||||
180 | int ret = -1; | - | ||||||||||||
181 | int blen; | - | ||||||||||||
182 | BUF_MEM *bm; | - | ||||||||||||
183 | - | |||||||||||||
184 | bm = (BUF_MEM *)b->ptr; | - | ||||||||||||
185 | if (in == NULL) {
| 0-708 | ||||||||||||
186 | BIOerror(BIO_R_NULL_PARAMETER); | - | ||||||||||||
187 | goto end; never executed: goto end; | 0 | ||||||||||||
188 | } | - | ||||||||||||
189 | - | |||||||||||||
190 | if (b->flags & BIO_FLAGS_MEM_RDONLY) {
| 0-708 | ||||||||||||
191 | BIOerror(BIO_R_WRITE_TO_READ_ONLY_BIO); | - | ||||||||||||
192 | goto end; never executed: goto end; | 0 | ||||||||||||
193 | } | - | ||||||||||||
194 | - | |||||||||||||
195 | BIO_clear_retry_flags(b); | - | ||||||||||||
196 | blen = bm->length; | - | ||||||||||||
197 | if (BUF_MEM_grow_clean(bm, blen + inl) != (blen + inl))
| 0-708 | ||||||||||||
198 | goto end; never executed: goto end; | 0 | ||||||||||||
199 | memcpy(&(bm->data[blen]), in, inl); | - | ||||||||||||
200 | ret = inl; | - | ||||||||||||
201 | end: code before this statement executed 708 times by 6 tests: end: Executed by:
| 708 | ||||||||||||
202 | return (ret); executed 708 times by 6 tests: return (ret); Executed by:
| 708 | ||||||||||||
203 | } | - | ||||||||||||
204 | - | |||||||||||||
205 | static long | - | ||||||||||||
206 | mem_ctrl(BIO *b, int cmd, long num, void *ptr) | - | ||||||||||||
207 | { | - | ||||||||||||
208 | long ret = 1; | - | ||||||||||||
209 | char **pptr; | - | ||||||||||||
210 | - | |||||||||||||
211 | BUF_MEM *bm = (BUF_MEM *)b->ptr; | - | ||||||||||||
212 | - | |||||||||||||
213 | switch (cmd) { | - | ||||||||||||
214 | case BIO_CTRL_RESET: executed 4 times by 1 test: case 1: Executed by:
| 4 | ||||||||||||
215 | if (bm->data != NULL) {
| 0-4 | ||||||||||||
216 | /* For read only case reset to the start again */ | - | ||||||||||||
217 | if (b->flags & BIO_FLAGS_MEM_RDONLY) {
| 0-4 | ||||||||||||
218 | bm->data -= bm->max - bm->length; | - | ||||||||||||
219 | bm->length = bm->max; | - | ||||||||||||
220 | } else { executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
221 | memset(bm->data, 0, bm->max); | - | ||||||||||||
222 | bm->length = 0; | - | ||||||||||||
223 | } never executed: end of block | 0 | ||||||||||||
224 | } | - | ||||||||||||
225 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||
226 | case BIO_CTRL_EOF: never executed: case 2: | 0 | ||||||||||||
227 | ret = (long)(bm->length == 0); | - | ||||||||||||
228 | break; never executed: break; | 0 | ||||||||||||
229 | case BIO_C_SET_BUF_MEM_EOF_RETURN: executed 4 times by 1 test: case 130: Executed by:
| 4 | ||||||||||||
230 | b->num = (int)num; | - | ||||||||||||
231 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||
232 | case BIO_CTRL_INFO: executed 336 times by 6 tests: case 3: Executed by:
| 336 | ||||||||||||
233 | ret = (long)bm->length; | - | ||||||||||||
234 | if (ptr != NULL) {
| 0-336 | ||||||||||||
235 | pptr = (char **)ptr; | - | ||||||||||||
236 | *pptr = (char *)&(bm->data[0]); | - | ||||||||||||
237 | } executed 336 times by 6 tests: end of block Executed by:
| 336 | ||||||||||||
238 | break; executed 336 times by 6 tests: break; Executed by:
| 336 | ||||||||||||
239 | case BIO_C_SET_BUF_MEM: never executed: case 114: | 0 | ||||||||||||
240 | mem_free(b); | - | ||||||||||||
241 | b->shutdown = (int)num; | - | ||||||||||||
242 | b->ptr = ptr; | - | ||||||||||||
243 | break; never executed: break; | 0 | ||||||||||||
244 | case BIO_C_GET_BUF_MEM_PTR: executed 4 times by 1 test: case 115: Executed by:
| 4 | ||||||||||||
245 | if (ptr != NULL) {
| 0-4 | ||||||||||||
246 | pptr = (char **)ptr; | - | ||||||||||||
247 | *pptr = (char *)bm; | - | ||||||||||||
248 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
249 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||
250 | case BIO_CTRL_GET_CLOSE: never executed: case 8: | 0 | ||||||||||||
251 | ret = (long)b->shutdown; | - | ||||||||||||
252 | break; never executed: break; | 0 | ||||||||||||
253 | case BIO_CTRL_SET_CLOSE: executed 139 times by 4 tests: case 9: Executed by:
| 139 | ||||||||||||
254 | b->shutdown = (int)num; | - | ||||||||||||
255 | break; executed 139 times by 4 tests: break; Executed by:
| 139 | ||||||||||||
256 | - | |||||||||||||
257 | case BIO_CTRL_WPENDING: executed 45 times by 2 tests: case 13: Executed by:
| 45 | ||||||||||||
258 | ret = 0L; | - | ||||||||||||
259 | break; executed 45 times by 2 tests: break; Executed by:
| 45 | ||||||||||||
260 | case BIO_CTRL_PENDING: executed 560 times by 1 test: case 10: Executed by:
| 560 | ||||||||||||
261 | ret = (long)bm->length; | - | ||||||||||||
262 | break; executed 560 times by 1 test: break; Executed by:
| 560 | ||||||||||||
263 | case BIO_CTRL_DUP: never executed: case 12: | 0 | ||||||||||||
264 | case BIO_CTRL_FLUSH: executed 175 times by 4 tests: case 11: Executed by:
| 175 | ||||||||||||
265 | ret = 1; | - | ||||||||||||
266 | break; executed 175 times by 4 tests: break; Executed by:
| 175 | ||||||||||||
267 | case BIO_CTRL_PUSH: executed 243 times by 5 tests: case 6: Executed by:
| 243 | ||||||||||||
268 | case BIO_CTRL_POP: executed 85 times by 4 tests: case 7: Executed by:
| 85 | ||||||||||||
269 | default: executed 189 times by 2 tests: default: Executed by:
| 189 | ||||||||||||
270 | ret = 0; | - | ||||||||||||
271 | break; executed 517 times by 5 tests: break; Executed by:
| 517 | ||||||||||||
272 | } | - | ||||||||||||
273 | return (ret); executed 1784 times by 6 tests: return (ret); Executed by:
| 1784 | ||||||||||||
274 | } | - | ||||||||||||
275 | - | |||||||||||||
276 | static int | - | ||||||||||||
277 | mem_gets(BIO *bp, char *buf, int size) | - | ||||||||||||
278 | { | - | ||||||||||||
279 | int i, j; | - | ||||||||||||
280 | int ret = -1; | - | ||||||||||||
281 | char *p; | - | ||||||||||||
282 | BUF_MEM *bm = (BUF_MEM *)bp->ptr; | - | ||||||||||||
283 | - | |||||||||||||
284 | BIO_clear_retry_flags(bp); | - | ||||||||||||
285 | j = bm->length; | - | ||||||||||||
286 | if ((size - 1) < j)
| 113-817 | ||||||||||||
287 | j = size - 1; executed 817 times by 4 tests: j = size - 1; Executed by:
| 817 | ||||||||||||
288 | if (j <= 0) {
| 11-919 | ||||||||||||
289 | *buf = '\0'; | - | ||||||||||||
290 | return 0; executed 11 times by 2 tests: return 0; Executed by:
| 11 | ||||||||||||
291 | } | - | ||||||||||||
292 | p = bm->data; | - | ||||||||||||
293 | for (i = 0; i < j; i++) {
| 3-56923 | ||||||||||||
294 | if (p[i] == '\n') {
| 916-56007 | ||||||||||||
295 | i++; | - | ||||||||||||
296 | break; executed 916 times by 4 tests: break; Executed by:
| 916 | ||||||||||||
297 | } | - | ||||||||||||
298 | } executed 56007 times by 4 tests: end of block Executed by:
| 56007 | ||||||||||||
299 | - | |||||||||||||
300 | /* | - | ||||||||||||
301 | * i is now the max num of bytes to copy, either j or up to | - | ||||||||||||
302 | * and including the first newline | - | ||||||||||||
303 | */ | - | ||||||||||||
304 | - | |||||||||||||
305 | i = mem_read(bp, buf, i); | - | ||||||||||||
306 | if (i > 0)
| 0-919 | ||||||||||||
307 | buf[i] = '\0'; executed 919 times by 4 tests: buf[i] = '\0'; Executed by:
| 919 | ||||||||||||
308 | ret = i; | - | ||||||||||||
309 | return (ret); executed 919 times by 4 tests: return (ret); Executed by:
| 919 | ||||||||||||
310 | } | - | ||||||||||||
311 | - | |||||||||||||
312 | static int | - | ||||||||||||
313 | mem_puts(BIO *bp, const char *str) | - | ||||||||||||
314 | { | - | ||||||||||||
315 | int n, ret; | - | ||||||||||||
316 | - | |||||||||||||
317 | n = strlen(str); | - | ||||||||||||
318 | ret = mem_write(bp, str, n); | - | ||||||||||||
319 | /* memory semantics is that it will always work */ | - | ||||||||||||
320 | return (ret); never executed: return (ret); | 0 | ||||||||||||
321 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |