/src/openssl30/crypto/bio/bf_buff.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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_local.h" |
13 | | #include "internal/cryptlib.h" |
14 | | |
15 | | static int buffer_write(BIO *h, const char *buf, int num); |
16 | | static int buffer_read(BIO *h, char *buf, int size); |
17 | | static int buffer_puts(BIO *h, const char *str); |
18 | | static int buffer_gets(BIO *h, char *str, int size); |
19 | | static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
20 | | static int buffer_new(BIO *h); |
21 | | static int buffer_free(BIO *data); |
22 | | static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
23 | 347k | #define DEFAULT_BUFFER_SIZE 4096 |
24 | | |
25 | | static const BIO_METHOD methods_buffer = { |
26 | | BIO_TYPE_BUFFER, |
27 | | "buffer", |
28 | | bwrite_conv, |
29 | | buffer_write, |
30 | | bread_conv, |
31 | | buffer_read, |
32 | | buffer_puts, |
33 | | buffer_gets, |
34 | | buffer_ctrl, |
35 | | buffer_new, |
36 | | buffer_free, |
37 | | buffer_callback_ctrl, |
38 | | }; |
39 | | |
40 | | const BIO_METHOD *BIO_f_buffer(void) |
41 | 148k | { |
42 | 148k | return &methods_buffer; |
43 | 148k | } |
44 | | |
45 | | static int buffer_new(BIO *bi) |
46 | 148k | { |
47 | 148k | BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
48 | | |
49 | 148k | if (ctx == NULL) |
50 | 0 | return 0; |
51 | 148k | ctx->ibuf_size = DEFAULT_BUFFER_SIZE; |
52 | 148k | ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE); |
53 | 148k | if (ctx->ibuf == NULL) { |
54 | 0 | OPENSSL_free(ctx); |
55 | 0 | return 0; |
56 | 0 | } |
57 | 148k | ctx->obuf_size = DEFAULT_BUFFER_SIZE; |
58 | 148k | ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE); |
59 | 148k | if (ctx->obuf == NULL) { |
60 | 0 | OPENSSL_free(ctx->ibuf); |
61 | 0 | OPENSSL_free(ctx); |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | 148k | bi->init = 1; |
66 | 148k | bi->ptr = (char *)ctx; |
67 | 148k | bi->flags = 0; |
68 | 148k | return 1; |
69 | 148k | } |
70 | | |
71 | | static int buffer_free(BIO *a) |
72 | 148k | { |
73 | 148k | BIO_F_BUFFER_CTX *b; |
74 | | |
75 | 148k | if (a == NULL) |
76 | 0 | return 0; |
77 | 148k | b = (BIO_F_BUFFER_CTX *)a->ptr; |
78 | 148k | OPENSSL_free(b->ibuf); |
79 | 148k | OPENSSL_free(b->obuf); |
80 | 148k | OPENSSL_free(a->ptr); |
81 | 148k | a->ptr = NULL; |
82 | 148k | a->init = 0; |
83 | 148k | a->flags = 0; |
84 | 148k | return 1; |
85 | 148k | } |
86 | | |
87 | | static int buffer_read(BIO *b, char *out, int outl) |
88 | 0 | { |
89 | 0 | int i, num = 0; |
90 | 0 | BIO_F_BUFFER_CTX *ctx; |
91 | |
|
92 | 0 | if (out == NULL) |
93 | 0 | return 0; |
94 | 0 | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
95 | |
|
96 | 0 | if ((ctx == NULL) || (b->next_bio == NULL)) |
97 | 0 | return 0; |
98 | 0 | num = 0; |
99 | 0 | BIO_clear_retry_flags(b); |
100 | |
|
101 | 0 | start: |
102 | 0 | i = ctx->ibuf_len; |
103 | | /* If there is stuff left over, grab it */ |
104 | 0 | if (i != 0) { |
105 | 0 | if (i > outl) |
106 | 0 | i = outl; |
107 | 0 | memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i); |
108 | 0 | ctx->ibuf_off += i; |
109 | 0 | ctx->ibuf_len -= i; |
110 | 0 | num += i; |
111 | 0 | if (outl == i) |
112 | 0 | return num; |
113 | 0 | outl -= i; |
114 | 0 | out += i; |
115 | 0 | } |
116 | | |
117 | | /* |
118 | | * We may have done a partial read. try to do more. We have nothing in |
119 | | * the buffer. If we get an error and have read some data, just return it |
120 | | * and let them retry to get the error again. copy direct to parent |
121 | | * address space |
122 | | */ |
123 | 0 | if (outl > ctx->ibuf_size) { |
124 | 0 | for (;;) { |
125 | 0 | i = BIO_read(b->next_bio, out, outl); |
126 | 0 | if (i <= 0) { |
127 | 0 | BIO_copy_next_retry(b); |
128 | 0 | if (i < 0) |
129 | 0 | return ((num > 0) ? num : i); |
130 | 0 | if (i == 0) |
131 | 0 | return num; |
132 | 0 | } |
133 | 0 | num += i; |
134 | 0 | if (outl == i) |
135 | 0 | return num; |
136 | 0 | out += i; |
137 | 0 | outl -= i; |
138 | 0 | } |
139 | 0 | } |
140 | | /* else */ |
141 | | |
142 | | /* we are going to be doing some buffering */ |
143 | 0 | i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size); |
144 | 0 | if (i <= 0) { |
145 | 0 | BIO_copy_next_retry(b); |
146 | 0 | if (i < 0) |
147 | 0 | return ((num > 0) ? num : i); |
148 | 0 | if (i == 0) |
149 | 0 | return num; |
150 | 0 | } |
151 | 0 | ctx->ibuf_off = 0; |
152 | 0 | ctx->ibuf_len = i; |
153 | | |
154 | | /* Lets re-read using ourselves :-) */ |
155 | 0 | goto start; |
156 | 0 | } |
157 | | |
158 | | static int buffer_write(BIO *b, const char *in, int inl) |
159 | 224k | { |
160 | 224k | int i, num = 0; |
161 | 224k | BIO_F_BUFFER_CTX *ctx; |
162 | | |
163 | 224k | if ((in == NULL) || (inl <= 0)) |
164 | 0 | return 0; |
165 | 224k | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
166 | 224k | if ((ctx == NULL) || (b->next_bio == NULL)) |
167 | 0 | return 0; |
168 | | |
169 | 224k | BIO_clear_retry_flags(b); |
170 | 224k | start: |
171 | 224k | i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off); |
172 | | /* add to buffer and return */ |
173 | 224k | if (i >= inl) { |
174 | 224k | memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl); |
175 | 224k | ctx->obuf_len += inl; |
176 | 224k | return (num + inl); |
177 | 224k | } |
178 | | /* else */ |
179 | | /* stuff already in buffer, so add to it first, then flush */ |
180 | 0 | if (ctx->obuf_len != 0) { |
181 | 0 | if (i > 0) { /* lets fill it up if we can */ |
182 | 0 | memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i); |
183 | 0 | in += i; |
184 | 0 | inl -= i; |
185 | 0 | num += i; |
186 | 0 | ctx->obuf_len += i; |
187 | 0 | } |
188 | | /* we now have a full buffer needing flushing */ |
189 | 0 | for (;;) { |
190 | 0 | i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]), |
191 | 0 | ctx->obuf_len); |
192 | 0 | if (i <= 0) { |
193 | 0 | BIO_copy_next_retry(b); |
194 | |
|
195 | 0 | if (i < 0) |
196 | 0 | return ((num > 0) ? num : i); |
197 | 0 | if (i == 0) |
198 | 0 | return num; |
199 | 0 | } |
200 | 0 | ctx->obuf_off += i; |
201 | 0 | ctx->obuf_len -= i; |
202 | 0 | if (ctx->obuf_len == 0) |
203 | 0 | break; |
204 | 0 | } |
205 | 0 | } |
206 | | /* |
207 | | * we only get here if the buffer has been flushed and we still have |
208 | | * stuff to write |
209 | | */ |
210 | 0 | ctx->obuf_off = 0; |
211 | | |
212 | | /* we now have inl bytes to write */ |
213 | 0 | while (inl >= ctx->obuf_size) { |
214 | 0 | i = BIO_write(b->next_bio, in, inl); |
215 | 0 | if (i <= 0) { |
216 | 0 | BIO_copy_next_retry(b); |
217 | 0 | if (i < 0) |
218 | 0 | return ((num > 0) ? num : i); |
219 | 0 | if (i == 0) |
220 | 0 | return num; |
221 | 0 | } |
222 | 0 | num += i; |
223 | 0 | in += i; |
224 | 0 | inl -= i; |
225 | 0 | if (inl == 0) |
226 | 0 | return num; |
227 | 0 | } |
228 | | |
229 | | /* |
230 | | * copy the rest into the buffer since we have only a small amount left |
231 | | */ |
232 | 0 | goto start; |
233 | 0 | } |
234 | | |
235 | | static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr) |
236 | 115k | { |
237 | 115k | BIO *dbio; |
238 | 115k | BIO_F_BUFFER_CTX *ctx; |
239 | 115k | long ret = 1; |
240 | 115k | char *p1, *p2; |
241 | 115k | int r, i, *ip; |
242 | 115k | int ibs, obs; |
243 | | |
244 | 115k | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
245 | | |
246 | 115k | switch (cmd) { |
247 | 0 | case BIO_CTRL_RESET: |
248 | 0 | ctx->ibuf_off = 0; |
249 | 0 | ctx->ibuf_len = 0; |
250 | 0 | ctx->obuf_off = 0; |
251 | 0 | ctx->obuf_len = 0; |
252 | 0 | if (b->next_bio == NULL) |
253 | 0 | return 0; |
254 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
255 | 0 | break; |
256 | 0 | case BIO_CTRL_EOF: |
257 | 0 | if (ctx->ibuf_len > 0) |
258 | 0 | return 0; |
259 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
260 | 0 | break; |
261 | 0 | case BIO_CTRL_INFO: |
262 | 0 | ret = (long)ctx->obuf_len; |
263 | 0 | break; |
264 | 0 | case BIO_C_GET_BUFF_NUM_LINES: |
265 | 0 | ret = 0; |
266 | 0 | p1 = ctx->ibuf; |
267 | 0 | for (i = 0; i < ctx->ibuf_len; i++) { |
268 | 0 | if (p1[ctx->ibuf_off + i] == '\n') |
269 | 0 | ret++; |
270 | 0 | } |
271 | 0 | break; |
272 | 0 | case BIO_CTRL_WPENDING: |
273 | 0 | ret = (long)ctx->obuf_len; |
274 | 0 | if (ret == 0) { |
275 | 0 | if (b->next_bio == NULL) |
276 | 0 | return 0; |
277 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
278 | 0 | } |
279 | 0 | break; |
280 | 0 | case BIO_CTRL_PENDING: |
281 | 0 | ret = (long)ctx->ibuf_len; |
282 | 0 | if (ret == 0) { |
283 | 0 | if (b->next_bio == NULL) |
284 | 0 | return 0; |
285 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
286 | 0 | } |
287 | 0 | break; |
288 | 0 | case BIO_C_SET_BUFF_READ_DATA: |
289 | 0 | if (num > ctx->ibuf_size) { |
290 | 0 | if (num <= 0) |
291 | 0 | return 0; |
292 | 0 | p1 = OPENSSL_malloc((size_t)num); |
293 | 0 | if (p1 == NULL) |
294 | 0 | goto malloc_error; |
295 | 0 | OPENSSL_free(ctx->ibuf); |
296 | 0 | ctx->ibuf = p1; |
297 | 0 | } |
298 | 0 | ctx->ibuf_off = 0; |
299 | 0 | ctx->ibuf_len = (int)num; |
300 | 0 | memcpy(ctx->ibuf, ptr, (int)num); |
301 | 0 | ret = 1; |
302 | 0 | break; |
303 | 24.9k | case BIO_C_SET_BUFF_SIZE: |
304 | 24.9k | if (ptr != NULL) { |
305 | 24.9k | ip = (int *)ptr; |
306 | 24.9k | if (*ip == 0) { |
307 | 24.9k | ibs = (int)num; |
308 | 24.9k | obs = ctx->obuf_size; |
309 | 24.9k | } else { /* if (*ip == 1) */ |
310 | |
|
311 | 0 | ibs = ctx->ibuf_size; |
312 | 0 | obs = (int)num; |
313 | 0 | } |
314 | 24.9k | } else { |
315 | 0 | ibs = (int)num; |
316 | 0 | obs = (int)num; |
317 | 0 | } |
318 | 24.9k | p1 = ctx->ibuf; |
319 | 24.9k | p2 = ctx->obuf; |
320 | 24.9k | if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) { |
321 | 0 | if (num <= 0) |
322 | 0 | return 0; |
323 | 0 | p1 = OPENSSL_malloc((size_t)num); |
324 | 0 | if (p1 == NULL) |
325 | 0 | goto malloc_error; |
326 | 0 | } |
327 | 24.9k | if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) { |
328 | 0 | p2 = OPENSSL_malloc((size_t)num); |
329 | 0 | if (p2 == NULL) { |
330 | 0 | if (p1 != ctx->ibuf) |
331 | 0 | OPENSSL_free(p1); |
332 | 0 | goto malloc_error; |
333 | 0 | } |
334 | 0 | } |
335 | 24.9k | if (ctx->ibuf != p1) { |
336 | 0 | OPENSSL_free(ctx->ibuf); |
337 | 0 | ctx->ibuf = p1; |
338 | 0 | ctx->ibuf_off = 0; |
339 | 0 | ctx->ibuf_len = 0; |
340 | 0 | ctx->ibuf_size = ibs; |
341 | 0 | } |
342 | 24.9k | if (ctx->obuf != p2) { |
343 | 0 | OPENSSL_free(ctx->obuf); |
344 | 0 | ctx->obuf = p2; |
345 | 0 | ctx->obuf_off = 0; |
346 | 0 | ctx->obuf_len = 0; |
347 | 0 | ctx->obuf_size = obs; |
348 | 0 | } |
349 | 24.9k | break; |
350 | 0 | case BIO_C_DO_STATE_MACHINE: |
351 | 0 | if (b->next_bio == NULL) |
352 | 0 | return 0; |
353 | 0 | BIO_clear_retry_flags(b); |
354 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
355 | 0 | BIO_copy_next_retry(b); |
356 | 0 | break; |
357 | | |
358 | 40.3k | case BIO_CTRL_FLUSH: |
359 | 40.3k | if (b->next_bio == NULL) |
360 | 0 | return 0; |
361 | 40.3k | if (ctx->obuf_len <= 0) { |
362 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
363 | 0 | break; |
364 | 0 | } |
365 | | |
366 | 80.6k | for (;;) { |
367 | 80.6k | BIO_clear_retry_flags(b); |
368 | 80.6k | if (ctx->obuf_len > 0) { |
369 | 40.3k | r = BIO_write(b->next_bio, |
370 | 40.3k | &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len); |
371 | 40.3k | BIO_copy_next_retry(b); |
372 | 40.3k | if (r <= 0) |
373 | 0 | return (long)r; |
374 | 40.3k | ctx->obuf_off += r; |
375 | 40.3k | ctx->obuf_len -= r; |
376 | 40.3k | } else { |
377 | 40.3k | ctx->obuf_len = 0; |
378 | 40.3k | ctx->obuf_off = 0; |
379 | 40.3k | break; |
380 | 40.3k | } |
381 | 80.6k | } |
382 | 40.3k | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
383 | 40.3k | break; |
384 | 0 | case BIO_CTRL_DUP: |
385 | 0 | dbio = (BIO *)ptr; |
386 | 0 | if (BIO_set_read_buffer_size(dbio, ctx->ibuf_size) <= 0 || |
387 | 0 | BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0) |
388 | 0 | ret = 0; |
389 | 0 | break; |
390 | 0 | case BIO_CTRL_PEEK: |
391 | | /* Ensure there's stuff in the input buffer */ |
392 | 0 | { |
393 | 0 | char fake_buf[1]; |
394 | 0 | (void)buffer_read(b, fake_buf, 0); |
395 | 0 | } |
396 | 0 | if (num > ctx->ibuf_len) |
397 | 0 | num = ctx->ibuf_len; |
398 | 0 | memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num); |
399 | 0 | ret = num; |
400 | 0 | break; |
401 | 49.9k | default: |
402 | 49.9k | if (b->next_bio == NULL) |
403 | 0 | return 0; |
404 | 49.9k | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
405 | 49.9k | break; |
406 | 115k | } |
407 | 115k | return ret; |
408 | 0 | malloc_error: |
409 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
410 | 0 | return 0; |
411 | 115k | } |
412 | | |
413 | | static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
414 | 0 | { |
415 | 0 | if (b->next_bio == NULL) |
416 | 0 | return 0; |
417 | 0 | return BIO_callback_ctrl(b->next_bio, cmd, fp); |
418 | 0 | } |
419 | | |
420 | | static int buffer_gets(BIO *b, char *buf, int size) |
421 | 0 | { |
422 | 0 | BIO_F_BUFFER_CTX *ctx; |
423 | 0 | int num = 0, i, flag; |
424 | 0 | char *p; |
425 | |
|
426 | 0 | ctx = (BIO_F_BUFFER_CTX *)b->ptr; |
427 | 0 | size--; /* reserve space for a '\0' */ |
428 | 0 | BIO_clear_retry_flags(b); |
429 | |
|
430 | 0 | for (;;) { |
431 | 0 | if (ctx->ibuf_len > 0) { |
432 | 0 | p = &(ctx->ibuf[ctx->ibuf_off]); |
433 | 0 | flag = 0; |
434 | 0 | for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) { |
435 | 0 | *(buf++) = p[i]; |
436 | 0 | if (p[i] == '\n') { |
437 | 0 | flag = 1; |
438 | 0 | i++; |
439 | 0 | break; |
440 | 0 | } |
441 | 0 | } |
442 | 0 | num += i; |
443 | 0 | size -= i; |
444 | 0 | ctx->ibuf_len -= i; |
445 | 0 | ctx->ibuf_off += i; |
446 | 0 | if (flag || size == 0) { |
447 | 0 | *buf = '\0'; |
448 | 0 | return num; |
449 | 0 | } |
450 | 0 | } else { /* read another chunk */ |
451 | |
|
452 | 0 | i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size); |
453 | 0 | if (i <= 0) { |
454 | 0 | BIO_copy_next_retry(b); |
455 | 0 | *buf = '\0'; |
456 | 0 | if (i < 0) |
457 | 0 | return ((num > 0) ? num : i); |
458 | 0 | if (i == 0) |
459 | 0 | return num; |
460 | 0 | } |
461 | 0 | ctx->ibuf_len = i; |
462 | 0 | ctx->ibuf_off = 0; |
463 | 0 | } |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | | static int buffer_puts(BIO *b, const char *str) |
468 | 0 | { |
469 | 0 | return buffer_write(b, str, strlen(str)); |
470 | 0 | } |