2 * Copyright 1995-2023 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 "internal/numbers.h"
16 #include "bio_local.h"
19 * Helper macro for the callback to determine whether an operator expects a
20 * len parameter or not
22 #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
23 || (o) == BIO_CB_GETS)
25 #ifndef OPENSSL_NO_DEPRECATED_3_0
26 # define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL)
28 # define HAS_CALLBACK(b) ((b)->callback_ex != NULL)
31 * Helper function to work out whether to call the new style callback or the old
32 * one, and translate between the two.
34 * This has a long return type for consistency with the old callback. Similarly
35 * for the "long" used for "inret"
37 static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
38 int argi, long argl, long inret,
42 #ifndef OPENSSL_NO_DEPRECATED_3_0
45 if (b->callback_ex != NULL)
47 return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
49 #ifndef OPENSSL_NO_DEPRECATED_3_0
50 /* Strip off any BIO_CB_RETURN flag */
51 bareoper = oper & ~BIO_CB_RETURN;
54 * We have an old style callback, so we will have to do nasty casts and
55 * check for overflows.
57 if (HAS_LEN_OPER(bareoper)) {
58 /* In this case |len| is set, and should be used instead of |argi| */
65 if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
66 if (*processed > INT_MAX)
71 ret = b->callback(b, oper, argp, argi, argl, inret);
73 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
74 *processed = (size_t)ret;
81 BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
83 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
92 if (!CRYPTO_NEW_REF(&bio->references, 1))
95 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
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);
103 if (method->create == NULL)
109 CRYPTO_FREE_REF(&bio->references);
114 BIO *BIO_new(const BIO_METHOD *method)
116 return BIO_new_ex(NULL, method);
126 if (CRYPTO_DOWN_REF(&a->references, &ret) <= 0)
129 REF_PRINT_COUNT("BIO", a);
132 REF_ASSERT_ISNT(ret < 0);
134 if (HAS_CALLBACK(a)) {
135 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
140 if ((a->method != NULL) && (a->method->destroy != NULL))
141 a->method->destroy(a);
143 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
145 CRYPTO_FREE_REF(&a->references);
152 void BIO_set_data(BIO *a, void *ptr)
157 void *BIO_get_data(BIO *a)
162 void BIO_set_init(BIO *a, int init)
167 int BIO_get_init(BIO *a)
172 void BIO_set_shutdown(BIO *a, int shut)
177 int BIO_get_shutdown(BIO *a)
182 void BIO_vfree(BIO *a)
187 int BIO_up_ref(BIO *a)
191 if (CRYPTO_UP_REF(&a->references, &i) <= 0)
194 REF_PRINT_COUNT("BIO", a);
195 REF_ASSERT_ISNT(i < 2);
199 void BIO_clear_flags(BIO *b, int flags)
204 int BIO_test_flags(const BIO *b, int flags)
206 return (b->flags & flags);
209 void BIO_set_flags(BIO *b, int flags)
214 #ifndef OPENSSL_NO_DEPRECATED_3_0
215 BIO_callback_fn BIO_get_callback(const BIO *b)
220 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
226 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
228 return b->callback_ex;
231 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
236 void BIO_set_callback_arg(BIO *b, char *arg)
241 char *BIO_get_callback_arg(const BIO *b)
246 const char *BIO_method_name(const BIO *b)
248 return b->method->name;
251 int BIO_method_type(const BIO *b)
253 return b->method->type;
257 * This is essentially the same as BIO_read_ex() except that it allows
258 * 0 or a negative value to indicate failure (retryable or not) in the return.
259 * This is for compatibility with the old style BIO_read(), where existing code
260 * may make assumptions about the return value that it might get.
262 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
267 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
270 if (b->method == NULL || b->method->bread == NULL) {
271 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
275 if (HAS_CALLBACK(b) &&
276 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
281 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
285 ret = b->method->bread(b, data, dlen, readbytes);
288 b->num_read += (uint64_t)*readbytes;
291 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
292 dlen, 0, 0L, ret, readbytes);
294 /* Shouldn't happen */
295 if (ret > 0 && *readbytes > dlen) {
296 ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
303 int BIO_read(BIO *b, void *data, int dlen)
311 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
314 /* *readbytes should always be <= dlen */
315 ret = (int)readbytes;
321 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
323 return bio_read_intern(b, data, dlen, readbytes) > 0;
326 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
329 size_t local_written;
335 * b == NULL is not an error but just means that zero bytes are written.
336 * Do not raise an error here.
341 if (b->method == NULL || b->method->bwrite == NULL) {
342 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
346 if (HAS_CALLBACK(b) &&
347 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
352 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
356 ret = b->method->bwrite(b, data, dlen, &local_written);
359 b->num_write += (uint64_t)local_written;
362 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
363 dlen, 0, 0L, ret, &local_written);
366 *written = local_written;
370 int BIO_write(BIO *b, const void *data, int dlen)
378 ret = bio_write_intern(b, data, (size_t)dlen, &written);
381 /* written should always be <= dlen */
388 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
390 return bio_write_intern(b, data, dlen, written) > 0
391 || (b != NULL && dlen == 0); /* order is important for *written */
394 int BIO_sendmmsg(BIO *b, BIO_MSG *msg,
395 size_t stride, size_t num_msg, uint64_t flags,
396 size_t *msgs_processed)
399 BIO_MMSG_CB_ARGS args;
403 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
407 if (b->method == NULL || b->method->bsendmmsg == NULL) {
409 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
413 if (HAS_CALLBACK(b)) {
415 args.stride = stride;
416 args.num_msg = num_msg;
418 args.msgs_processed = msgs_processed;
420 ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args,
428 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
432 ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed);
435 ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN,
436 (void *)&args, ret, 0, 0, ret, NULL);
441 int BIO_recvmmsg(BIO *b, BIO_MSG *msg,
442 size_t stride, size_t num_msg, uint64_t flags,
443 size_t *msgs_processed)
446 BIO_MMSG_CB_ARGS args;
450 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
454 if (b->method == NULL || b->method->brecvmmsg == NULL) {
456 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
460 if (HAS_CALLBACK(b)) {
462 args.stride = stride;
463 args.num_msg = num_msg;
465 args.msgs_processed = msgs_processed;
467 ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args,
475 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
479 ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed);
482 ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN,
483 (void *)&args, ret, 0, 0, ret, NULL);
488 int BIO_get_rpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc)
490 return BIO_ctrl(b, BIO_CTRL_GET_RPOLL_DESCRIPTOR, 0, desc);
493 int BIO_get_wpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc)
495 return BIO_ctrl(b, BIO_CTRL_GET_WPOLL_DESCRIPTOR, 0, desc);
498 int BIO_puts(BIO *b, const char *buf)
504 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
507 if (b->method == NULL || b->method->bputs == NULL) {
508 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
512 if (HAS_CALLBACK(b)) {
513 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
519 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
523 ret = b->method->bputs(b, buf);
526 b->num_write += (uint64_t)ret;
532 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
536 if (written > INT_MAX) {
537 ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
547 int BIO_gets(BIO *b, char *buf, int size)
550 size_t readbytes = 0;
553 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
556 if (b->method == NULL || b->method->bgets == NULL) {
557 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
562 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
566 if (HAS_CALLBACK(b)) {
567 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
573 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
577 ret = b->method->bgets(b, buf, size);
585 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
586 0, 0L, ret, &readbytes);
589 /* Shouldn't happen */
590 if (readbytes > (size_t)size)
593 ret = (int)readbytes;
599 int BIO_get_line(BIO *bio, char *buf, int size)
605 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
609 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
615 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
619 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
623 while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0)
627 return ret > 0 || BIO_eof(bio) ? ptr - buf : ret;
630 int BIO_indent(BIO *b, int indent, int max)
637 if (BIO_puts(b, " ") != 1)
642 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
647 return BIO_ctrl(b, cmd, larg, (char *)&i);
650 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
654 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
660 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
666 if (b->method == NULL || b->method->ctrl == NULL) {
667 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
671 if (HAS_CALLBACK(b)) {
672 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
677 ret = b->method->ctrl(b, cmd, larg, parg);
680 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
686 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
692 if (b->method == NULL || b->method->callback_ctrl == NULL
693 || cmd != BIO_CTRL_SET_CALLBACK) {
694 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
698 if (HAS_CALLBACK(b)) {
699 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
705 ret = b->method->callback_ctrl(b, cmd, fp);
708 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
715 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
716 * do; but those macros have inappropriate return type, and for interfacing
717 * from other programming languages, C macros aren't much of a help anyway.
719 size_t BIO_ctrl_pending(BIO *bio)
721 long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
725 #if LONG_MAX > SIZE_MAX
732 size_t BIO_ctrl_wpending(BIO *bio)
734 long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
738 #if LONG_MAX > SIZE_MAX
745 /* put the 'bio' on the end of b's list of operators */
746 BIO *BIO_push(BIO *b, BIO *bio)
753 while (lb->next_bio != NULL)
758 /* called to do internal processing */
759 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
763 /* Remove the first and return the rest */
772 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
774 if (b->prev_bio != NULL)
775 b->prev_bio->next_bio = b->next_bio;
776 if (b->next_bio != NULL)
777 b->next_bio->prev_bio = b->prev_bio;
784 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
790 if (!BIO_should_retry(b))
798 *reason = last->retry_reason;
802 int BIO_get_retry_reason(BIO *bio)
804 return bio->retry_reason;
807 void BIO_set_retry_reason(BIO *bio, int reason)
809 bio->retry_reason = reason;
812 BIO *BIO_find_type(BIO *bio, int type)
817 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
820 mask = type & BIO_TYPE_MASK;
822 if (bio->method != NULL) {
823 mt = bio->method->type;
828 } else if (mt == type) {
833 } while (bio != NULL);
837 BIO *BIO_next(BIO *b)
844 void BIO_set_next(BIO *b, BIO *next)
849 void BIO_free_all(BIO *bio)
854 while (bio != NULL) {
856 CRYPTO_GET_REF(&b->references, &ref);
859 /* Since ref count > 1, don't free anyone else. */
865 BIO *BIO_dup_chain(BIO *in)
867 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
869 for (bio = in; bio != NULL; bio = bio->next_bio) {
870 if ((new_bio = BIO_new(bio->method)) == NULL)
872 #ifndef OPENSSL_NO_DEPRECATED_3_0
873 new_bio->callback = bio->callback;
875 new_bio->callback_ex = bio->callback_ex;
876 new_bio->cb_arg = bio->cb_arg;
877 new_bio->init = bio->init;
878 new_bio->shutdown = bio->shutdown;
879 new_bio->flags = bio->flags;
881 /* This will let SSL_s_sock() work with stdin/stdout */
882 new_bio->num = bio->num;
884 if (BIO_dup_state(bio, (char *)new_bio) <= 0) {
890 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
900 BIO_push(eoc, new_bio);
911 void BIO_copy_next_retry(BIO *b)
913 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
914 b->retry_reason = b->next_bio->retry_reason;
917 int BIO_set_ex_data(BIO *bio, int idx, void *data)
919 return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
922 void *BIO_get_ex_data(const BIO *bio, int idx)
924 return CRYPTO_get_ex_data(&(bio->ex_data), idx);
927 uint64_t BIO_number_read(BIO *bio)
930 return bio->num_read;
934 uint64_t BIO_number_written(BIO *bio)
937 return bio->num_write;
941 void bio_free_ex_data(BIO *bio)
943 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
946 void bio_cleanup(void)
948 #ifndef OPENSSL_NO_SOCK
949 bio_sock_cleanup_int();
950 CRYPTO_THREAD_lock_free(bio_lookup_lock);
951 bio_lookup_lock = NULL;
953 CRYPTO_FREE_REF(&bio_type_count);
956 /* Internal variant of the below BIO_wait() not calling ERR_raise(...) */
957 static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
959 #ifndef OPENSSL_NO_SOCK
964 if (max_time == 0) /* no timeout */
967 #ifndef OPENSSL_NO_SOCK
968 if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
969 return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
971 /* fall back to polling since no sockets are available */
973 sec_diff = (long)(max_time - time(NULL)); /* might overflow */
975 return 0; /* clearly timeout */
977 /* now take a nap at most the given number of milliseconds */
978 if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
979 if (nap_milliseconds > 1000)
980 nap_milliseconds = 1000;
981 } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
982 if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
983 nap_milliseconds = (unsigned int)sec_diff * 1000;
985 OSSL_sleep(nap_milliseconds);
990 * Wait on (typically socket-based) BIO at most until max_time.
991 * Succeed immediately if max_time == 0.
992 * If sockets are not available support polling: succeed after waiting at most
993 * the number of nap_milliseconds in order to avoid a tight busy loop.
994 * Call ERR_raise(ERR_LIB_BIO, ...) on timeout or error.
995 * Returns -1 on error, 0 on timeout, and 1 on success.
997 int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
999 int rv = bio_wait(bio, max_time, nap_milliseconds);
1002 ERR_raise(ERR_LIB_BIO,
1003 rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
1008 * Connect via given BIO using BIO_do_connect() until success/timeout/error.
1009 * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
1010 * For non-blocking and potentially even non-socket BIOs perform polling with
1011 * the given density: between polls sleep nap_milliseconds using BIO_wait()
1012 * in order to avoid a tight busy loop.
1013 * Returns -1 on error, 0 on timeout, and 1 on success.
1015 int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
1017 int blocking = timeout <= 0;
1018 time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1022 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
1026 if (nap_milliseconds < 0)
1027 nap_milliseconds = 100;
1028 BIO_set_nbio(bio, !blocking);
1032 rv = BIO_do_connect(bio);
1034 if (rv <= 0) { /* could be timeout or retryable error or fatal error */
1035 int err = ERR_peek_last_error();
1036 int reason = ERR_GET_REASON(err);
1037 int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
1039 if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
1043 * likely retryable system error occurred, which may be
1044 * EAGAIN (resource temporarily unavailable) some 40 secs after
1045 * calling getaddrinfo(): Temporary failure in name resolution
1046 * or a premature ETIMEDOUT, some 30 seconds after connect()
1048 case BIO_R_CONNECT_ERROR:
1049 case BIO_R_NBIO_CONNECT_ERROR:
1050 /* some likely retryable connection error occurred */
1051 (void)BIO_reset(bio); /* often needed to avoid retry failure */
1058 if (timeout >= 0 && do_retry) {
1060 /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
1061 rv = bio_wait(bio, max_time, nap_milliseconds);
1064 ERR_raise(ERR_LIB_BIO,
1065 rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
1067 ERR_clear_last_mark();
1069 if (err == 0) /* missing error queue entry */
1070 /* workaround: general error */
1071 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
1074 ERR_clear_last_mark();