2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include <openssl/crypto.h>
15 #include "bio_local.h"
18 * Helper macro for the callback to determine whether an operator expects a
19 * len parameter or not
21 #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
22 || (o) == BIO_CB_GETS)
24 #ifndef OPENSSL_NO_DEPRECATED_3_0
25 # define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL)
27 # define HAS_CALLBACK(b) ((b)->callback_ex != NULL)
30 * Helper function to work out whether to call the new style callback or the old
31 * one, and translate between the two.
33 * This has a long return type for consistency with the old callback. Similarly
34 * for the "long" used for "inret"
36 static long bio_call_callback(BIO
*b
, int oper
, const char *argp
, size_t len
,
37 int argi
, long argl
, long inret
,
41 #ifndef OPENSSL_NO_DEPRECATED_3_0
44 if (b
->callback_ex
!= NULL
)
46 return b
->callback_ex(b
, oper
, argp
, len
, argi
, argl
, inret
, processed
);
48 #ifndef OPENSSL_NO_DEPRECATED_3_0
49 /* Strip off any BIO_CB_RETURN flag */
50 bareoper
= oper
& ~BIO_CB_RETURN
;
53 * We have an old style callback, so we will have to do nasty casts and
54 * check for overflows.
56 if (HAS_LEN_OPER(bareoper
)) {
57 /* In this case |len| is set, and should be used instead of |argi| */
64 if (inret
> 0 && (oper
& BIO_CB_RETURN
) && bareoper
!= BIO_CB_CTRL
) {
65 if (*processed
> INT_MAX
)
70 ret
= b
->callback(b
, oper
, argp
, argi
, argl
, inret
);
72 if (ret
> 0 && (oper
& BIO_CB_RETURN
) && bareoper
!= BIO_CB_CTRL
) {
73 *processed
= (size_t)ret
;
80 BIO
*BIO_new_ex(OSSL_LIB_CTX
*libctx
, const BIO_METHOD
*method
)
82 BIO
*bio
= OPENSSL_zalloc(sizeof(*bio
));
85 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
))
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
);
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
);
110 if (method
->create
== NULL
)
120 BIO
*BIO_new(const BIO_METHOD
*method
)
122 return BIO_new_ex(NULL
, method
);
132 if (CRYPTO_DOWN_REF(&a
->references
, &ret
, a
->lock
) <= 0)
135 REF_PRINT_COUNT("BIO", a
);
138 REF_ASSERT_ISNT(ret
< 0);
140 if (HAS_CALLBACK(a
)) {
141 ret
= (int)bio_call_callback(a
, BIO_CB_FREE
, NULL
, 0, 0, 0L, 1L, NULL
);
146 if ((a
->method
!= NULL
) && (a
->method
->destroy
!= NULL
))
147 a
->method
->destroy(a
);
149 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, a
, &a
->ex_data
);
151 CRYPTO_THREAD_lock_free(a
->lock
);
158 void BIO_set_data(BIO
*a
, void *ptr
)
163 void *BIO_get_data(BIO
*a
)
168 void BIO_set_init(BIO
*a
, int init
)
173 int BIO_get_init(BIO
*a
)
178 void BIO_set_shutdown(BIO
*a
, int shut
)
183 int BIO_get_shutdown(BIO
*a
)
188 void BIO_vfree(BIO
*a
)
193 int BIO_up_ref(BIO
*a
)
197 if (CRYPTO_UP_REF(&a
->references
, &i
, a
->lock
) <= 0)
200 REF_PRINT_COUNT("BIO", a
);
201 REF_ASSERT_ISNT(i
< 2);
205 void BIO_clear_flags(BIO
*b
, int flags
)
210 int BIO_test_flags(const BIO
*b
, int flags
)
212 return (b
->flags
& flags
);
215 void BIO_set_flags(BIO
*b
, int flags
)
220 #ifndef OPENSSL_NO_DEPRECATED_3_0
221 BIO_callback_fn
BIO_get_callback(const BIO
*b
)
226 void BIO_set_callback(BIO
*b
, BIO_callback_fn cb
)
232 BIO_callback_fn_ex
BIO_get_callback_ex(const BIO
*b
)
234 return b
->callback_ex
;
237 void BIO_set_callback_ex(BIO
*b
, BIO_callback_fn_ex cb
)
242 void BIO_set_callback_arg(BIO
*b
, char *arg
)
247 char *BIO_get_callback_arg(const BIO
*b
)
252 const char *BIO_method_name(const BIO
*b
)
254 return b
->method
->name
;
257 int BIO_method_type(const BIO
*b
)
259 return b
->method
->type
;
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.
268 static int bio_read_intern(BIO
*b
, void *data
, size_t dlen
, size_t *readbytes
)
273 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
276 if (b
->method
== NULL
|| b
->method
->bread
== NULL
) {
277 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
281 if (HAS_CALLBACK(b
) &&
282 ((ret
= (int)bio_call_callback(b
, BIO_CB_READ
, data
, dlen
, 0, 0L, 1L,
287 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
291 ret
= b
->method
->bread(b
, data
, dlen
, readbytes
);
294 b
->num_read
+= (uint64_t)*readbytes
;
297 ret
= (int)bio_call_callback(b
, BIO_CB_READ
| BIO_CB_RETURN
, data
,
298 dlen
, 0, 0L, ret
, readbytes
);
300 /* Shouldn't happen */
301 if (ret
> 0 && *readbytes
> dlen
) {
302 ERR_raise(ERR_LIB_BIO
, ERR_R_INTERNAL_ERROR
);
309 int BIO_read(BIO
*b
, void *data
, int dlen
)
317 ret
= bio_read_intern(b
, data
, (size_t)dlen
, &readbytes
);
320 /* *readbytes should always be <= dlen */
321 ret
= (int)readbytes
;
327 int BIO_read_ex(BIO
*b
, void *data
, size_t dlen
, size_t *readbytes
)
329 return bio_read_intern(b
, data
, dlen
, readbytes
) > 0;
332 static int bio_write_intern(BIO
*b
, const void *data
, size_t dlen
,
335 size_t local_written
;
341 * b == NULL is not an error but just means that zero bytes are written.
342 * Do not raise an error here.
347 if (b
->method
== NULL
|| b
->method
->bwrite
== NULL
) {
348 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
352 if (HAS_CALLBACK(b
) &&
353 ((ret
= (int)bio_call_callback(b
, BIO_CB_WRITE
, data
, dlen
, 0, 0L, 1L,
358 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
362 ret
= b
->method
->bwrite(b
, data
, dlen
, &local_written
);
365 b
->num_write
+= (uint64_t)local_written
;
368 ret
= (int)bio_call_callback(b
, BIO_CB_WRITE
| BIO_CB_RETURN
, data
,
369 dlen
, 0, 0L, ret
, &local_written
);
372 *written
= local_written
;
376 int BIO_write(BIO
*b
, const void *data
, int dlen
)
384 ret
= bio_write_intern(b
, data
, (size_t)dlen
, &written
);
387 /* written should always be <= dlen */
394 int BIO_write_ex(BIO
*b
, const void *data
, size_t dlen
, size_t *written
)
396 return bio_write_intern(b
, data
, dlen
, written
) >= 0;
399 int BIO_puts(BIO
*b
, const char *buf
)
405 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
408 if (b
->method
== NULL
|| b
->method
->bputs
== NULL
) {
409 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
413 if (HAS_CALLBACK(b
)) {
414 ret
= (int)bio_call_callback(b
, BIO_CB_PUTS
, buf
, 0, 0, 0L, 1L, NULL
);
420 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
424 ret
= b
->method
->bputs(b
, buf
);
427 b
->num_write
+= (uint64_t)ret
;
433 ret
= (int)bio_call_callback(b
, BIO_CB_PUTS
| BIO_CB_RETURN
, buf
, 0, 0,
437 if (written
> INT_MAX
) {
438 ERR_raise(ERR_LIB_BIO
, BIO_R_LENGTH_TOO_LONG
);
448 int BIO_gets(BIO
*b
, char *buf
, int size
)
451 size_t readbytes
= 0;
454 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
457 if (b
->method
== NULL
|| b
->method
->bgets
== NULL
) {
458 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
463 ERR_raise(ERR_LIB_BIO
, BIO_R_INVALID_ARGUMENT
);
467 if (HAS_CALLBACK(b
)) {
468 ret
= (int)bio_call_callback(b
, BIO_CB_GETS
, buf
, size
, 0, 0L, 1, NULL
);
474 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
478 ret
= b
->method
->bgets(b
, buf
, size
);
486 ret
= (int)bio_call_callback(b
, BIO_CB_GETS
| BIO_CB_RETURN
, buf
, size
,
487 0, 0L, ret
, &readbytes
);
490 /* Shouldn't happen */
491 if (readbytes
> (size_t)size
)
494 ret
= (int)readbytes
;
500 int BIO_get_line(BIO
*bio
, char *buf
, int size
)
506 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
510 ERR_raise(ERR_LIB_BIO
, BIO_R_INVALID_ARGUMENT
);
516 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
520 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
524 while (size
-- > 1 && (ret
= BIO_read(bio
, ptr
, 1)) > 0)
528 return ret
> 0 || BIO_eof(bio
) ? ptr
- buf
: ret
;
531 int BIO_indent(BIO
*b
, int indent
, int max
)
538 if (BIO_puts(b
, " ") != 1)
543 long BIO_int_ctrl(BIO
*b
, int cmd
, long larg
, int iarg
)
548 return BIO_ctrl(b
, cmd
, larg
, (char *)&i
);
551 void *BIO_ptr_ctrl(BIO
*b
, int cmd
, long larg
)
555 if (BIO_ctrl(b
, cmd
, larg
, (char *)&p
) <= 0)
561 long BIO_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
566 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
569 if (b
->method
== NULL
|| b
->method
->ctrl
== NULL
) {
570 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
574 if (HAS_CALLBACK(b
)) {
575 ret
= bio_call_callback(b
, BIO_CB_CTRL
, parg
, 0, cmd
, larg
, 1L, NULL
);
580 ret
= b
->method
->ctrl(b
, cmd
, larg
, parg
);
583 ret
= bio_call_callback(b
, BIO_CB_CTRL
| BIO_CB_RETURN
, parg
, 0, cmd
,
589 long BIO_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
594 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
597 if (b
->method
== NULL
|| b
->method
->callback_ctrl
== NULL
598 || cmd
!= BIO_CTRL_SET_CALLBACK
) {
599 ERR_raise(ERR_LIB_BIO
, BIO_R_UNSUPPORTED_METHOD
);
603 if (HAS_CALLBACK(b
)) {
604 ret
= bio_call_callback(b
, BIO_CB_CTRL
, (void *)&fp
, 0, cmd
, 0, 1L,
610 ret
= b
->method
->callback_ctrl(b
, cmd
, fp
);
613 ret
= bio_call_callback(b
, BIO_CB_CTRL
| BIO_CB_RETURN
, (void *)&fp
, 0,
620 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
621 * do; but those macros have inappropriate return type, and for interfacing
622 * from other programming languages, C macros aren't much of a help anyway.
624 size_t BIO_ctrl_pending(BIO
*bio
)
626 return BIO_ctrl(bio
, BIO_CTRL_PENDING
, 0, NULL
);
629 size_t BIO_ctrl_wpending(BIO
*bio
)
631 return BIO_ctrl(bio
, BIO_CTRL_WPENDING
, 0, NULL
);
634 /* put the 'bio' on the end of b's list of operators */
635 BIO
*BIO_push(BIO
*b
, BIO
*bio
)
642 while (lb
->next_bio
!= NULL
)
647 /* called to do internal processing */
648 BIO_ctrl(b
, BIO_CTRL_PUSH
, 0, lb
);
652 /* Remove the first and return the rest */
658 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
663 BIO_ctrl(b
, BIO_CTRL_POP
, 0, b
);
665 if (b
->prev_bio
!= NULL
)
666 b
->prev_bio
->next_bio
= b
->next_bio
;
667 if (b
->next_bio
!= NULL
)
668 b
->next_bio
->prev_bio
= b
->prev_bio
;
675 BIO
*BIO_get_retry_BIO(BIO
*bio
, int *reason
)
681 if (!BIO_should_retry(b
))
689 *reason
= last
->retry_reason
;
693 int BIO_get_retry_reason(BIO
*bio
)
695 return bio
->retry_reason
;
698 void BIO_set_retry_reason(BIO
*bio
, int reason
)
700 bio
->retry_reason
= reason
;
703 BIO
*BIO_find_type(BIO
*bio
, int type
)
708 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
713 if (bio
->method
!= NULL
) {
714 mt
= bio
->method
->type
;
719 } else if (mt
== type
) {
724 } while (bio
!= NULL
);
728 BIO
*BIO_next(BIO
*b
)
731 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
737 void BIO_set_next(BIO
*b
, BIO
*next
)
742 void BIO_free_all(BIO
*bio
)
747 while (bio
!= NULL
) {
752 /* Since ref count > 1, don't free anyone else. */
758 BIO
*BIO_dup_chain(BIO
*in
)
760 BIO
*ret
= NULL
, *eoc
= NULL
, *bio
, *new_bio
;
762 for (bio
= in
; bio
!= NULL
; bio
= bio
->next_bio
) {
763 if ((new_bio
= BIO_new(bio
->method
)) == NULL
)
765 #ifndef OPENSSL_NO_DEPRECATED_3_0
766 new_bio
->callback
= bio
->callback
;
768 new_bio
->callback_ex
= bio
->callback_ex
;
769 new_bio
->cb_arg
= bio
->cb_arg
;
770 new_bio
->init
= bio
->init
;
771 new_bio
->shutdown
= bio
->shutdown
;
772 new_bio
->flags
= bio
->flags
;
774 /* This will let SSL_s_sock() work with stdin/stdout */
775 new_bio
->num
= bio
->num
;
777 if (!BIO_dup_state(bio
, (char *)new_bio
)) {
783 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO
, &new_bio
->ex_data
,
793 BIO_push(eoc
, new_bio
);
804 void BIO_copy_next_retry(BIO
*b
)
806 BIO_set_flags(b
, BIO_get_retry_flags(b
->next_bio
));
807 b
->retry_reason
= b
->next_bio
->retry_reason
;
810 int BIO_set_ex_data(BIO
*bio
, int idx
, void *data
)
812 return CRYPTO_set_ex_data(&(bio
->ex_data
), idx
, data
);
815 void *BIO_get_ex_data(const BIO
*bio
, int idx
)
817 return CRYPTO_get_ex_data(&(bio
->ex_data
), idx
);
820 uint64_t BIO_number_read(BIO
*bio
)
823 return bio
->num_read
;
827 uint64_t BIO_number_written(BIO
*bio
)
830 return bio
->num_write
;
834 void bio_free_ex_data(BIO
*bio
)
836 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
);
839 void bio_cleanup(void)
841 #ifndef OPENSSL_NO_SOCK
842 bio_sock_cleanup_int();
843 CRYPTO_THREAD_lock_free(bio_lookup_lock
);
844 bio_lookup_lock
= NULL
;
846 CRYPTO_THREAD_lock_free(bio_type_lock
);
847 bio_type_lock
= NULL
;
850 /* Internal variant of the below BIO_wait() not calling BIOerr() */
851 static int bio_wait(BIO
*bio
, time_t max_time
, unsigned int nap_milliseconds
)
853 #ifndef OPENSSL_NO_SOCK
858 if (max_time
== 0) /* no timeout */
861 #ifndef OPENSSL_NO_SOCK
862 if (BIO_get_fd(bio
, &fd
) > 0 && fd
< FD_SETSIZE
)
863 return BIO_socket_wait(fd
, BIO_should_read(bio
), max_time
);
865 /* fall back to polling since no sockets are available */
867 sec_diff
= (long)(max_time
- time(NULL
)); /* might overflow */
869 return 0; /* clearly timeout */
871 /* now take a nap at most the given number of milliseconds */
872 if (sec_diff
== 0) { /* we are below the 1 seconds resolution of max_time */
873 if (nap_milliseconds
> 1000)
874 nap_milliseconds
= 1000;
875 } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
876 if ((unsigned long)sec_diff
* 1000 < nap_milliseconds
)
877 nap_milliseconds
= (unsigned int)sec_diff
* 1000;
879 ossl_sleep(nap_milliseconds
);
884 * Wait on (typically socket-based) BIO at most until max_time.
885 * Succeed immediately if max_time == 0.
886 * If sockets are not available support polling: succeed after waiting at most
887 * the number of nap_milliseconds in order to avoid a tight busy loop.
888 * Call BIOerr(...) on timeout or error.
889 * Returns -1 on error, 0 on timeout, and 1 on success.
891 int BIO_wait(BIO
*bio
, time_t max_time
, unsigned int nap_milliseconds
)
893 int rv
= bio_wait(bio
, max_time
, nap_milliseconds
);
896 ERR_raise(ERR_LIB_BIO
,
897 rv
== 0 ? BIO_R_TRANSFER_TIMEOUT
: BIO_R_TRANSFER_ERROR
);
902 * Connect via given BIO using BIO_do_connect() until success/timeout/error.
903 * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
904 * For non-blocking and potentially even non-socket BIOs perform polling with
905 * the given density: between polls sleep nap_milliseconds using BIO_wait()
906 * in order to avoid a tight busy loop.
907 * Returns -1 on error, 0 on timeout, and 1 on success.
909 int BIO_do_connect_retry(BIO
*bio
, int timeout
, int nap_milliseconds
)
911 int blocking
= timeout
<= 0;
912 time_t max_time
= timeout
> 0 ? time(NULL
) + timeout
: 0;
916 ERR_raise(ERR_LIB_BIO
, ERR_R_PASSED_NULL_PARAMETER
);
920 if (nap_milliseconds
< 0)
921 nap_milliseconds
= 100;
922 BIO_set_nbio(bio
, !blocking
);
926 rv
= BIO_do_connect(bio
);
928 if (rv
<= 0) { /* could be timeout or retryable error or fatal error */
929 int err
= ERR_peek_last_error();
930 int reason
= ERR_GET_REASON(err
);
931 int do_retry
= BIO_should_retry(bio
); /* may be 1 only if !blocking */
933 if (ERR_GET_LIB(err
) == ERR_LIB_BIO
) {
937 * likely retryable system error occurred, which may be
938 * EAGAIN (resource temporarily unavailable) some 40 secs after
939 * calling getaddrinfo(): Temporary failure in name resolution
940 * or a premature ETIMEDOUT, some 30 seconds after connect()
942 case BIO_R_CONNECT_ERROR
:
943 case BIO_R_NBIO_CONNECT_ERROR
:
944 /* some likely retryable connection error occurred */
945 (void)BIO_reset(bio
); /* often needed to avoid retry failure */
952 if (timeout
>= 0 && do_retry
) {
954 /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
955 rv
= bio_wait(bio
, max_time
, nap_milliseconds
);
958 ERR_raise(ERR_LIB_BIO
,
959 rv
== 0 ? BIO_R_CONNECT_TIMEOUT
: BIO_R_CONNECT_ERROR
);
961 ERR_clear_last_mark();
963 if (err
== 0) /* missing error queue entry */
964 /* workaround: general error */
965 ERR_raise(ERR_LIB_BIO
, BIO_R_CONNECT_ERROR
);
968 ERR_clear_last_mark();