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