]>
Commit | Line | Data |
---|---|---|
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 | #define OPENSSL_SUPPRESS_DEPRECATED | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <errno.h> | |
14 | #include <openssl/crypto.h> | |
15 | #include "internal/numbers.h" | |
16 | #include "bio_local.h" | |
17 | ||
18 | /* | |
19 | * Helper macro for the callback to determine whether an operator expects a | |
20 | * len parameter or not | |
21 | */ | |
22 | #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \ | |
23 | || (o) == BIO_CB_GETS) | |
24 | ||
25 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
26 | # define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL) | |
27 | #else | |
28 | # define HAS_CALLBACK(b) ((b)->callback_ex != NULL) | |
29 | #endif | |
30 | /* | |
31 | * Helper function to work out whether to call the new style callback or the old | |
32 | * one, and translate between the two. | |
33 | * | |
34 | * This has a long return type for consistency with the old callback. Similarly | |
35 | * for the "long" used for "inret" | |
36 | */ | |
37 | static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, | |
38 | int argi, long argl, long inret, | |
39 | size_t *processed) | |
40 | { | |
41 | long ret = inret; | |
42 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
43 | int bareoper; | |
44 | ||
45 | if (b->callback_ex != NULL) | |
46 | #endif | |
47 | return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); | |
48 | ||
49 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
50 | /* Strip off any BIO_CB_RETURN flag */ | |
51 | bareoper = oper & ~BIO_CB_RETURN; | |
52 | ||
53 | /* | |
54 | * We have an old style callback, so we will have to do nasty casts and | |
55 | * check for overflows. | |
56 | */ | |
57 | if (HAS_LEN_OPER(bareoper)) { | |
58 | /* In this case |len| is set, and should be used instead of |argi| */ | |
59 | if (len > INT_MAX) | |
60 | return -1; | |
61 | ||
62 | argi = (int)len; | |
63 | } | |
64 | ||
65 | if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { | |
66 | if (*processed > INT_MAX) | |
67 | return -1; | |
68 | inret = *processed; | |
69 | } | |
70 | ||
71 | ret = b->callback(b, oper, argp, argi, argl, inret); | |
72 | ||
73 | if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { | |
74 | *processed = (size_t)ret; | |
75 | ret = 1; | |
76 | } | |
77 | #endif | |
78 | return ret; | |
79 | } | |
80 | ||
81 | BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method) | |
82 | { | |
83 | BIO *bio = OPENSSL_zalloc(sizeof(*bio)); | |
84 | ||
85 | if (bio == NULL) | |
86 | return NULL; | |
87 | ||
88 | bio->libctx = libctx; | |
89 | bio->method = method; | |
90 | bio->shutdown = 1; | |
91 | ||
92 | if (!CRYPTO_NEW_REF(&bio->references, 1)) | |
93 | goto err; | |
94 | ||
95 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) | |
96 | goto err; | |
97 | ||
98 | if (method->create != NULL && !method->create(bio)) { | |
99 | ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL); | |
100 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | |
101 | CRYPTO_FREE_REF(&bio->references); | |
102 | goto err; | |
103 | } | |
104 | if (method->create == NULL) | |
105 | bio->init = 1; | |
106 | ||
107 | return bio; | |
108 | ||
109 | err: | |
110 | CRYPTO_FREE_REF(&bio->references); | |
111 | OPENSSL_free(bio); | |
112 | return NULL; | |
113 | } | |
114 | ||
115 | BIO *BIO_new(const BIO_METHOD *method) | |
116 | { | |
117 | return BIO_new_ex(NULL, method); | |
118 | } | |
119 | ||
120 | int BIO_free(BIO *a) | |
121 | { | |
122 | int ret; | |
123 | ||
124 | if (a == NULL) | |
125 | return 0; | |
126 | ||
127 | if (CRYPTO_DOWN_REF(&a->references, &ret) <= 0) | |
128 | return 0; | |
129 | ||
130 | REF_PRINT_COUNT("BIO", a); | |
131 | if (ret > 0) | |
132 | return 1; | |
133 | REF_ASSERT_ISNT(ret < 0); | |
134 | ||
135 | if (HAS_CALLBACK(a)) { | |
136 | ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); | |
137 | if (ret <= 0) | |
138 | return 0; | |
139 | } | |
140 | ||
141 | if ((a->method != NULL) && (a->method->destroy != NULL)) | |
142 | a->method->destroy(a); | |
143 | ||
144 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); | |
145 | ||
146 | CRYPTO_FREE_REF(&a->references); | |
147 | ||
148 | OPENSSL_free(a); | |
149 | ||
150 | return 1; | |
151 | } | |
152 | ||
153 | void BIO_set_data(BIO *a, void *ptr) | |
154 | { | |
155 | a->ptr = ptr; | |
156 | } | |
157 | ||
158 | void *BIO_get_data(BIO *a) | |
159 | { | |
160 | return a->ptr; | |
161 | } | |
162 | ||
163 | void BIO_set_init(BIO *a, int init) | |
164 | { | |
165 | a->init = init; | |
166 | } | |
167 | ||
168 | int BIO_get_init(BIO *a) | |
169 | { | |
170 | return a->init; | |
171 | } | |
172 | ||
173 | void BIO_set_shutdown(BIO *a, int shut) | |
174 | { | |
175 | a->shutdown = shut; | |
176 | } | |
177 | ||
178 | int BIO_get_shutdown(BIO *a) | |
179 | { | |
180 | return a->shutdown; | |
181 | } | |
182 | ||
183 | void BIO_vfree(BIO *a) | |
184 | { | |
185 | BIO_free(a); | |
186 | } | |
187 | ||
188 | int BIO_up_ref(BIO *a) | |
189 | { | |
190 | int i; | |
191 | ||
192 | if (CRYPTO_UP_REF(&a->references, &i) <= 0) | |
193 | return 0; | |
194 | ||
195 | REF_PRINT_COUNT("BIO", a); | |
196 | REF_ASSERT_ISNT(i < 2); | |
197 | return i > 1; | |
198 | } | |
199 | ||
200 | void BIO_clear_flags(BIO *b, int flags) | |
201 | { | |
202 | b->flags &= ~flags; | |
203 | } | |
204 | ||
205 | int BIO_test_flags(const BIO *b, int flags) | |
206 | { | |
207 | return (b->flags & flags); | |
208 | } | |
209 | ||
210 | void BIO_set_flags(BIO *b, int flags) | |
211 | { | |
212 | b->flags |= flags; | |
213 | } | |
214 | ||
215 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
216 | BIO_callback_fn BIO_get_callback(const BIO *b) | |
217 | { | |
218 | return b->callback; | |
219 | } | |
220 | ||
221 | void BIO_set_callback(BIO *b, BIO_callback_fn cb) | |
222 | { | |
223 | b->callback = cb; | |
224 | } | |
225 | #endif | |
226 | ||
227 | BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) | |
228 | { | |
229 | return b->callback_ex; | |
230 | } | |
231 | ||
232 | void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb) | |
233 | { | |
234 | b->callback_ex = cb; | |
235 | } | |
236 | ||
237 | void BIO_set_callback_arg(BIO *b, char *arg) | |
238 | { | |
239 | b->cb_arg = arg; | |
240 | } | |
241 | ||
242 | char *BIO_get_callback_arg(const BIO *b) | |
243 | { | |
244 | return b->cb_arg; | |
245 | } | |
246 | ||
247 | const char *BIO_method_name(const BIO *b) | |
248 | { | |
249 | return b->method->name; | |
250 | } | |
251 | ||
252 | int BIO_method_type(const BIO *b) | |
253 | { | |
254 | return b->method->type; | |
255 | } | |
256 | ||
257 | /* | |
258 | * This is essentially the same as BIO_read_ex() except that it allows | |
259 | * 0 or a negative value to indicate failure (retryable or not) in the return. | |
260 | * This is for compatibility with the old style BIO_read(), where existing code | |
261 | * may make assumptions about the return value that it might get. | |
262 | */ | |
263 | static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) | |
264 | { | |
265 | int ret; | |
266 | ||
267 | if (b == NULL) { | |
268 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
269 | return -1; | |
270 | } | |
271 | if (b->method == NULL || b->method->bread == NULL) { | |
272 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
273 | return -2; | |
274 | } | |
275 | ||
276 | if (HAS_CALLBACK(b) && | |
277 | ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, | |
278 | NULL)) <= 0)) | |
279 | return ret; | |
280 | ||
281 | if (!b->init) { | |
282 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
283 | return -1; | |
284 | } | |
285 | ||
286 | ret = b->method->bread(b, data, dlen, readbytes); | |
287 | ||
288 | if (ret > 0) | |
289 | b->num_read += (uint64_t)*readbytes; | |
290 | ||
291 | if (HAS_CALLBACK(b)) | |
292 | ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, | |
293 | dlen, 0, 0L, ret, readbytes); | |
294 | ||
295 | /* Shouldn't happen */ | |
296 | if (ret > 0 && *readbytes > dlen) { | |
297 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); | |
298 | return -1; | |
299 | } | |
300 | ||
301 | return ret; | |
302 | } | |
303 | ||
304 | int BIO_read(BIO *b, void *data, int dlen) | |
305 | { | |
306 | size_t readbytes; | |
307 | int ret; | |
308 | ||
309 | if (dlen < 0) | |
310 | return 0; | |
311 | ||
312 | ret = bio_read_intern(b, data, (size_t)dlen, &readbytes); | |
313 | ||
314 | if (ret > 0) { | |
315 | /* *readbytes should always be <= dlen */ | |
316 | ret = (int)readbytes; | |
317 | } | |
318 | ||
319 | return ret; | |
320 | } | |
321 | ||
322 | int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) | |
323 | { | |
324 | return bio_read_intern(b, data, dlen, readbytes) > 0; | |
325 | } | |
326 | ||
327 | static int bio_write_intern(BIO *b, const void *data, size_t dlen, | |
328 | size_t *written) | |
329 | { | |
330 | size_t local_written; | |
331 | int ret; | |
332 | ||
333 | if (written != NULL) | |
334 | *written = 0; | |
335 | /* | |
336 | * b == NULL is not an error but just means that zero bytes are written. | |
337 | * Do not raise an error here. | |
338 | */ | |
339 | if (b == NULL) | |
340 | return 0; | |
341 | ||
342 | if (b->method == NULL || b->method->bwrite == NULL) { | |
343 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
344 | return -2; | |
345 | } | |
346 | ||
347 | if (HAS_CALLBACK(b) && | |
348 | ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, | |
349 | NULL)) <= 0)) | |
350 | return ret; | |
351 | ||
352 | if (!b->init) { | |
353 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
354 | return -1; | |
355 | } | |
356 | ||
357 | ret = b->method->bwrite(b, data, dlen, &local_written); | |
358 | ||
359 | if (ret > 0) | |
360 | b->num_write += (uint64_t)local_written; | |
361 | ||
362 | if (HAS_CALLBACK(b)) | |
363 | ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, | |
364 | dlen, 0, 0L, ret, &local_written); | |
365 | ||
366 | if (written != NULL) | |
367 | *written = local_written; | |
368 | return ret; | |
369 | } | |
370 | ||
371 | int BIO_write(BIO *b, const void *data, int dlen) | |
372 | { | |
373 | size_t written; | |
374 | int ret; | |
375 | ||
376 | if (dlen <= 0) | |
377 | return 0; | |
378 | ||
379 | ret = bio_write_intern(b, data, (size_t)dlen, &written); | |
380 | ||
381 | if (ret > 0) { | |
382 | /* written should always be <= dlen */ | |
383 | ret = (int)written; | |
384 | } | |
385 | ||
386 | return ret; | |
387 | } | |
388 | ||
389 | int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) | |
390 | { | |
391 | return bio_write_intern(b, data, dlen, written) > 0 | |
392 | || (b != NULL && dlen == 0); /* order is important for *written */ | |
393 | } | |
394 | ||
395 | int BIO_sendmmsg(BIO *b, BIO_MSG *msg, | |
396 | size_t stride, size_t num_msg, uint64_t flags, | |
397 | size_t *msgs_processed) | |
398 | { | |
399 | size_t ret; | |
400 | BIO_MMSG_CB_ARGS args; | |
401 | ||
402 | if (b == NULL) { | |
403 | *msgs_processed = 0; | |
404 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | if (b->method == NULL || b->method->bsendmmsg == NULL) { | |
409 | *msgs_processed = 0; | |
410 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | if (HAS_CALLBACK(b)) { | |
415 | args.msg = msg; | |
416 | args.stride = stride; | |
417 | args.num_msg = num_msg; | |
418 | args.flags = flags; | |
419 | args.msgs_processed = msgs_processed; | |
420 | ||
421 | ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args, | |
422 | 0, 0, 0, 0, NULL); | |
423 | if (ret == 0) | |
424 | return 0; | |
425 | } | |
426 | ||
427 | if (!b->init) { | |
428 | *msgs_processed = 0; | |
429 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
430 | return 0; | |
431 | } | |
432 | ||
433 | ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed); | |
434 | ||
435 | if (HAS_CALLBACK(b)) | |
436 | ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN, | |
437 | (void *)&args, ret, 0, 0, 0, NULL); | |
438 | ||
439 | return ret; | |
440 | } | |
441 | ||
442 | int BIO_recvmmsg(BIO *b, BIO_MSG *msg, | |
443 | size_t stride, size_t num_msg, uint64_t flags, | |
444 | size_t *msgs_processed) | |
445 | { | |
446 | size_t ret; | |
447 | BIO_MMSG_CB_ARGS args; | |
448 | ||
449 | if (b == NULL) { | |
450 | *msgs_processed = 0; | |
451 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
452 | return 0; | |
453 | } | |
454 | ||
455 | if (b->method == NULL || b->method->brecvmmsg == NULL) { | |
456 | *msgs_processed = 0; | |
457 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
458 | return 0; | |
459 | } | |
460 | ||
461 | if (HAS_CALLBACK(b)) { | |
462 | args.msg = msg; | |
463 | args.stride = stride; | |
464 | args.num_msg = num_msg; | |
465 | args.flags = flags; | |
466 | args.msgs_processed = msgs_processed; | |
467 | ||
468 | ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args, | |
469 | 0, 0, 0, 0, NULL); | |
470 | if (ret == 0) | |
471 | return 0; | |
472 | } | |
473 | ||
474 | if (!b->init) { | |
475 | *msgs_processed = 0; | |
476 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
477 | return 0; | |
478 | } | |
479 | ||
480 | ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed); | |
481 | ||
482 | if (HAS_CALLBACK(b)) | |
483 | ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN, | |
484 | (void *)&args, ret, 0, 0, 0, NULL); | |
485 | ||
486 | return ret; | |
487 | } | |
488 | ||
489 | int BIO_get_rpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc) | |
490 | { | |
491 | return BIO_ctrl(b, BIO_CTRL_GET_RPOLL_DESCRIPTOR, 0, desc); | |
492 | } | |
493 | ||
494 | int BIO_get_wpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc) | |
495 | { | |
496 | return BIO_ctrl(b, BIO_CTRL_GET_WPOLL_DESCRIPTOR, 0, desc); | |
497 | } | |
498 | ||
499 | int BIO_puts(BIO *b, const char *buf) | |
500 | { | |
501 | int ret; | |
502 | size_t written = 0; | |
503 | ||
504 | if (b == NULL) { | |
505 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
506 | return -1; | |
507 | } | |
508 | if (b->method == NULL || b->method->bputs == NULL) { | |
509 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
510 | return -2; | |
511 | } | |
512 | ||
513 | if (HAS_CALLBACK(b)) { | |
514 | ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); | |
515 | if (ret <= 0) | |
516 | return ret; | |
517 | } | |
518 | ||
519 | if (!b->init) { | |
520 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
521 | return -1; | |
522 | } | |
523 | ||
524 | ret = b->method->bputs(b, buf); | |
525 | ||
526 | if (ret > 0) { | |
527 | b->num_write += (uint64_t)ret; | |
528 | written = ret; | |
529 | ret = 1; | |
530 | } | |
531 | ||
532 | if (HAS_CALLBACK(b)) | |
533 | ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, | |
534 | 0L, ret, &written); | |
535 | ||
536 | if (ret > 0) { | |
537 | if (written > INT_MAX) { | |
538 | ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG); | |
539 | ret = -1; | |
540 | } else { | |
541 | ret = (int)written; | |
542 | } | |
543 | } | |
544 | ||
545 | return ret; | |
546 | } | |
547 | ||
548 | int BIO_gets(BIO *b, char *buf, int size) | |
549 | { | |
550 | int ret; | |
551 | size_t readbytes = 0; | |
552 | ||
553 | if (b == NULL) { | |
554 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
555 | return -1; | |
556 | } | |
557 | if (b->method == NULL || b->method->bgets == NULL) { | |
558 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
559 | return -2; | |
560 | } | |
561 | ||
562 | if (size < 0) { | |
563 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); | |
564 | return -1; | |
565 | } | |
566 | ||
567 | if (HAS_CALLBACK(b)) { | |
568 | ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); | |
569 | if (ret <= 0) | |
570 | return ret; | |
571 | } | |
572 | ||
573 | if (!b->init) { | |
574 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
575 | return -1; | |
576 | } | |
577 | ||
578 | ret = b->method->bgets(b, buf, size); | |
579 | ||
580 | if (ret > 0) { | |
581 | readbytes = ret; | |
582 | ret = 1; | |
583 | } | |
584 | ||
585 | if (HAS_CALLBACK(b)) | |
586 | ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, | |
587 | 0, 0L, ret, &readbytes); | |
588 | ||
589 | if (ret > 0) { | |
590 | /* Shouldn't happen */ | |
591 | if (readbytes > (size_t)size) | |
592 | ret = -1; | |
593 | else | |
594 | ret = (int)readbytes; | |
595 | } | |
596 | ||
597 | return ret; | |
598 | } | |
599 | ||
600 | int BIO_get_line(BIO *bio, char *buf, int size) | |
601 | { | |
602 | int ret = 0; | |
603 | char *ptr = buf; | |
604 | ||
605 | if (buf == NULL) { | |
606 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
607 | return -1; | |
608 | } | |
609 | if (size <= 0) { | |
610 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); | |
611 | return -1; | |
612 | } | |
613 | *buf = '\0'; | |
614 | ||
615 | if (bio == NULL) { | |
616 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
617 | return -1; | |
618 | } | |
619 | if (!bio->init) { | |
620 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
621 | return -1; | |
622 | } | |
623 | ||
624 | while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0) | |
625 | if (*ptr++ == '\n') | |
626 | break; | |
627 | *ptr = '\0'; | |
628 | return ret > 0 || BIO_eof(bio) ? ptr - buf : ret; | |
629 | } | |
630 | ||
631 | int BIO_indent(BIO *b, int indent, int max) | |
632 | { | |
633 | if (indent < 0) | |
634 | indent = 0; | |
635 | if (indent > max) | |
636 | indent = max; | |
637 | while (indent--) | |
638 | if (BIO_puts(b, " ") != 1) | |
639 | return 0; | |
640 | return 1; | |
641 | } | |
642 | ||
643 | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) | |
644 | { | |
645 | int i; | |
646 | ||
647 | i = iarg; | |
648 | return BIO_ctrl(b, cmd, larg, (char *)&i); | |
649 | } | |
650 | ||
651 | void *BIO_ptr_ctrl(BIO *b, int cmd, long larg) | |
652 | { | |
653 | void *p = NULL; | |
654 | ||
655 | if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0) | |
656 | return NULL; | |
657 | else | |
658 | return p; | |
659 | } | |
660 | ||
661 | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) | |
662 | { | |
663 | long ret; | |
664 | ||
665 | if (b == NULL) | |
666 | return -1; | |
667 | if (b->method == NULL || b->method->ctrl == NULL) { | |
668 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
669 | return -2; | |
670 | } | |
671 | ||
672 | if (HAS_CALLBACK(b)) { | |
673 | ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); | |
674 | if (ret <= 0) | |
675 | return ret; | |
676 | } | |
677 | ||
678 | ret = b->method->ctrl(b, cmd, larg, parg); | |
679 | ||
680 | if (HAS_CALLBACK(b)) | |
681 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, | |
682 | larg, ret, NULL); | |
683 | ||
684 | return ret; | |
685 | } | |
686 | ||
687 | long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) | |
688 | { | |
689 | long ret; | |
690 | ||
691 | if (b == NULL) | |
692 | return -2; | |
693 | if (b->method == NULL || b->method->callback_ctrl == NULL | |
694 | || cmd != BIO_CTRL_SET_CALLBACK) { | |
695 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
696 | return -2; | |
697 | } | |
698 | ||
699 | if (HAS_CALLBACK(b)) { | |
700 | ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, | |
701 | NULL); | |
702 | if (ret <= 0) | |
703 | return ret; | |
704 | } | |
705 | ||
706 | ret = b->method->callback_ctrl(b, cmd, fp); | |
707 | ||
708 | if (HAS_CALLBACK(b)) | |
709 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, | |
710 | cmd, 0, ret, NULL); | |
711 | ||
712 | return ret; | |
713 | } | |
714 | ||
715 | /* | |
716 | * It is unfortunate to duplicate in functions what the BIO_(w)pending macros | |
717 | * do; but those macros have inappropriate return type, and for interfacing | |
718 | * from other programming languages, C macros aren't much of a help anyway. | |
719 | */ | |
720 | size_t BIO_ctrl_pending(BIO *bio) | |
721 | { | |
722 | long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); | |
723 | ||
724 | if (ret < 0) | |
725 | ret = 0; | |
726 | #if LONG_MAX > SIZE_MAX | |
727 | if (ret > SIZE_MAX) | |
728 | ret = SIZE_MAX; | |
729 | #endif | |
730 | return (size_t)ret; | |
731 | } | |
732 | ||
733 | size_t BIO_ctrl_wpending(BIO *bio) | |
734 | { | |
735 | long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); | |
736 | ||
737 | if (ret < 0) | |
738 | ret = 0; | |
739 | #if LONG_MAX > SIZE_MAX | |
740 | if (ret > SIZE_MAX) | |
741 | ret = SIZE_MAX; | |
742 | #endif | |
743 | return (size_t)ret; | |
744 | } | |
745 | ||
746 | /* put the 'bio' on the end of b's list of operators */ | |
747 | BIO *BIO_push(BIO *b, BIO *bio) | |
748 | { | |
749 | BIO *lb; | |
750 | ||
751 | if (b == NULL) | |
752 | return bio; | |
753 | lb = b; | |
754 | while (lb->next_bio != NULL) | |
755 | lb = lb->next_bio; | |
756 | lb->next_bio = bio; | |
757 | if (bio != NULL) | |
758 | bio->prev_bio = lb; | |
759 | /* called to do internal processing */ | |
760 | BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb); | |
761 | return b; | |
762 | } | |
763 | ||
764 | /* Remove the first and return the rest */ | |
765 | BIO *BIO_pop(BIO *b) | |
766 | { | |
767 | BIO *ret; | |
768 | ||
769 | if (b == NULL) | |
770 | return NULL; | |
771 | ret = b->next_bio; | |
772 | ||
773 | BIO_ctrl(b, BIO_CTRL_POP, 0, b); | |
774 | ||
775 | if (b->prev_bio != NULL) | |
776 | b->prev_bio->next_bio = b->next_bio; | |
777 | if (b->next_bio != NULL) | |
778 | b->next_bio->prev_bio = b->prev_bio; | |
779 | ||
780 | b->next_bio = NULL; | |
781 | b->prev_bio = NULL; | |
782 | return ret; | |
783 | } | |
784 | ||
785 | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) | |
786 | { | |
787 | BIO *b, *last; | |
788 | ||
789 | b = last = bio; | |
790 | for (;;) { | |
791 | if (!BIO_should_retry(b)) | |
792 | break; | |
793 | last = b; | |
794 | b = b->next_bio; | |
795 | if (b == NULL) | |
796 | break; | |
797 | } | |
798 | if (reason != NULL) | |
799 | *reason = last->retry_reason; | |
800 | return last; | |
801 | } | |
802 | ||
803 | int BIO_get_retry_reason(BIO *bio) | |
804 | { | |
805 | return bio->retry_reason; | |
806 | } | |
807 | ||
808 | void BIO_set_retry_reason(BIO *bio, int reason) | |
809 | { | |
810 | bio->retry_reason = reason; | |
811 | } | |
812 | ||
813 | BIO *BIO_find_type(BIO *bio, int type) | |
814 | { | |
815 | int mt, mask; | |
816 | ||
817 | if (bio == NULL) { | |
818 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
819 | return NULL; | |
820 | } | |
821 | mask = type & 0xff; | |
822 | do { | |
823 | if (bio->method != NULL) { | |
824 | mt = bio->method->type; | |
825 | ||
826 | if (!mask) { | |
827 | if (mt & type) | |
828 | return bio; | |
829 | } else if (mt == type) { | |
830 | return bio; | |
831 | } | |
832 | } | |
833 | bio = bio->next_bio; | |
834 | } while (bio != NULL); | |
835 | return NULL; | |
836 | } | |
837 | ||
838 | BIO *BIO_next(BIO *b) | |
839 | { | |
840 | if (b == NULL) | |
841 | return NULL; | |
842 | return b->next_bio; | |
843 | } | |
844 | ||
845 | void BIO_set_next(BIO *b, BIO *next) | |
846 | { | |
847 | b->next_bio = next; | |
848 | } | |
849 | ||
850 | void BIO_free_all(BIO *bio) | |
851 | { | |
852 | BIO *b; | |
853 | int ref; | |
854 | ||
855 | while (bio != NULL) { | |
856 | b = bio; | |
857 | CRYPTO_GET_REF(&b->references, &ref); | |
858 | bio = bio->next_bio; | |
859 | BIO_free(b); | |
860 | /* Since ref count > 1, don't free anyone else. */ | |
861 | if (ref > 1) | |
862 | break; | |
863 | } | |
864 | } | |
865 | ||
866 | BIO *BIO_dup_chain(BIO *in) | |
867 | { | |
868 | BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; | |
869 | ||
870 | for (bio = in; bio != NULL; bio = bio->next_bio) { | |
871 | if ((new_bio = BIO_new(bio->method)) == NULL) | |
872 | goto err; | |
873 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
874 | new_bio->callback = bio->callback; | |
875 | #endif | |
876 | new_bio->callback_ex = bio->callback_ex; | |
877 | new_bio->cb_arg = bio->cb_arg; | |
878 | new_bio->init = bio->init; | |
879 | new_bio->shutdown = bio->shutdown; | |
880 | new_bio->flags = bio->flags; | |
881 | ||
882 | /* This will let SSL_s_sock() work with stdin/stdout */ | |
883 | new_bio->num = bio->num; | |
884 | ||
885 | if (BIO_dup_state(bio, (char *)new_bio) <= 0) { | |
886 | BIO_free(new_bio); | |
887 | goto err; | |
888 | } | |
889 | ||
890 | /* copy app data */ | |
891 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data, | |
892 | &bio->ex_data)) { | |
893 | BIO_free(new_bio); | |
894 | goto err; | |
895 | } | |
896 | ||
897 | if (ret == NULL) { | |
898 | eoc = new_bio; | |
899 | ret = eoc; | |
900 | } else { | |
901 | BIO_push(eoc, new_bio); | |
902 | eoc = new_bio; | |
903 | } | |
904 | } | |
905 | return ret; | |
906 | err: | |
907 | BIO_free_all(ret); | |
908 | ||
909 | return NULL; | |
910 | } | |
911 | ||
912 | void BIO_copy_next_retry(BIO *b) | |
913 | { | |
914 | BIO_set_flags(b, BIO_get_retry_flags(b->next_bio)); | |
915 | b->retry_reason = b->next_bio->retry_reason; | |
916 | } | |
917 | ||
918 | int BIO_set_ex_data(BIO *bio, int idx, void *data) | |
919 | { | |
920 | return CRYPTO_set_ex_data(&(bio->ex_data), idx, data); | |
921 | } | |
922 | ||
923 | void *BIO_get_ex_data(const BIO *bio, int idx) | |
924 | { | |
925 | return CRYPTO_get_ex_data(&(bio->ex_data), idx); | |
926 | } | |
927 | ||
928 | uint64_t BIO_number_read(BIO *bio) | |
929 | { | |
930 | if (bio) | |
931 | return bio->num_read; | |
932 | return 0; | |
933 | } | |
934 | ||
935 | uint64_t BIO_number_written(BIO *bio) | |
936 | { | |
937 | if (bio) | |
938 | return bio->num_write; | |
939 | return 0; | |
940 | } | |
941 | ||
942 | void bio_free_ex_data(BIO *bio) | |
943 | { | |
944 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | |
945 | } | |
946 | ||
947 | void bio_cleanup(void) | |
948 | { | |
949 | #ifndef OPENSSL_NO_SOCK | |
950 | bio_sock_cleanup_int(); | |
951 | CRYPTO_THREAD_lock_free(bio_lookup_lock); | |
952 | bio_lookup_lock = NULL; | |
953 | #endif | |
954 | CRYPTO_FREE_REF(&bio_type_count); | |
955 | } | |
956 | ||
957 | /* Internal variant of the below BIO_wait() not calling ERR_raise(...) */ | |
958 | static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) | |
959 | { | |
960 | #ifndef OPENSSL_NO_SOCK | |
961 | int fd; | |
962 | #endif | |
963 | long sec_diff; | |
964 | ||
965 | if (max_time == 0) /* no timeout */ | |
966 | return 1; | |
967 | ||
968 | #ifndef OPENSSL_NO_SOCK | |
969 | if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE) | |
970 | return BIO_socket_wait(fd, BIO_should_read(bio), max_time); | |
971 | #endif | |
972 | /* fall back to polling since no sockets are available */ | |
973 | ||
974 | sec_diff = (long)(max_time - time(NULL)); /* might overflow */ | |
975 | if (sec_diff < 0) | |
976 | return 0; /* clearly timeout */ | |
977 | ||
978 | /* now take a nap at most the given number of milliseconds */ | |
979 | if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */ | |
980 | if (nap_milliseconds > 1000) | |
981 | nap_milliseconds = 1000; | |
982 | } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */ | |
983 | if ((unsigned long)sec_diff * 1000 < nap_milliseconds) | |
984 | nap_milliseconds = (unsigned int)sec_diff * 1000; | |
985 | } | |
986 | OSSL_sleep(nap_milliseconds); | |
987 | return 1; | |
988 | } | |
989 | ||
990 | /*- | |
991 | * Wait on (typically socket-based) BIO at most until max_time. | |
992 | * Succeed immediately if max_time == 0. | |
993 | * If sockets are not available support polling: succeed after waiting at most | |
994 | * the number of nap_milliseconds in order to avoid a tight busy loop. | |
995 | * Call ERR_raise(ERR_LIB_BIO, ...) on timeout or error. | |
996 | * Returns -1 on error, 0 on timeout, and 1 on success. | |
997 | */ | |
998 | int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) | |
999 | { | |
1000 | int rv = bio_wait(bio, max_time, nap_milliseconds); | |
1001 | ||
1002 | if (rv <= 0) | |
1003 | ERR_raise(ERR_LIB_BIO, | |
1004 | rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR); | |
1005 | return rv; | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Connect via given BIO using BIO_do_connect() until success/timeout/error. | |
1010 | * Parameter timeout == 0 means no timeout, < 0 means exactly one try. | |
1011 | * For non-blocking and potentially even non-socket BIOs perform polling with | |
1012 | * the given density: between polls sleep nap_milliseconds using BIO_wait() | |
1013 | * in order to avoid a tight busy loop. | |
1014 | * Returns -1 on error, 0 on timeout, and 1 on success. | |
1015 | */ | |
1016 | int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds) | |
1017 | { | |
1018 | int blocking = timeout <= 0; | |
1019 | time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; | |
1020 | int rv; | |
1021 | ||
1022 | if (bio == NULL) { | |
1023 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
1024 | return -1; | |
1025 | } | |
1026 | ||
1027 | if (nap_milliseconds < 0) | |
1028 | nap_milliseconds = 100; | |
1029 | BIO_set_nbio(bio, !blocking); | |
1030 | ||
1031 | retry: | |
1032 | ERR_set_mark(); | |
1033 | rv = BIO_do_connect(bio); | |
1034 | ||
1035 | if (rv <= 0) { /* could be timeout or retryable error or fatal error */ | |
1036 | int err = ERR_peek_last_error(); | |
1037 | int reason = ERR_GET_REASON(err); | |
1038 | int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */ | |
1039 | ||
1040 | if (ERR_GET_LIB(err) == ERR_LIB_BIO) { | |
1041 | switch (reason) { | |
1042 | case ERR_R_SYS_LIB: | |
1043 | /* | |
1044 | * likely retryable system error occurred, which may be | |
1045 | * EAGAIN (resource temporarily unavailable) some 40 secs after | |
1046 | * calling getaddrinfo(): Temporary failure in name resolution | |
1047 | * or a premature ETIMEDOUT, some 30 seconds after connect() | |
1048 | */ | |
1049 | case BIO_R_CONNECT_ERROR: | |
1050 | case BIO_R_NBIO_CONNECT_ERROR: | |
1051 | /* some likely retryable connection error occurred */ | |
1052 | (void)BIO_reset(bio); /* often needed to avoid retry failure */ | |
1053 | do_retry = 1; | |
1054 | break; | |
1055 | default: | |
1056 | break; | |
1057 | } | |
1058 | } | |
1059 | if (timeout >= 0 && do_retry) { | |
1060 | ERR_pop_to_mark(); | |
1061 | /* will not actually wait if timeout == 0 (i.e., blocking BIO): */ | |
1062 | rv = bio_wait(bio, max_time, nap_milliseconds); | |
1063 | if (rv > 0) | |
1064 | goto retry; | |
1065 | ERR_raise(ERR_LIB_BIO, | |
1066 | rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR); | |
1067 | } else { | |
1068 | ERR_clear_last_mark(); | |
1069 | rv = -1; | |
1070 | if (err == 0) /* missing error queue entry */ | |
1071 | /* workaround: general error */ | |
1072 | ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); | |
1073 | } | |
1074 | } else { | |
1075 | ERR_clear_last_mark(); | |
1076 | } | |
1077 | ||
1078 | return rv; | |
1079 | } |