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
12 #include <openssl/crypto.h>
13 #include "bio_local.h"
16 * Helper macro for the callback to determine whether an operator expects a
17 * len parameter or not
19 #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
20 || (o) == BIO_CB_GETS)
23 * Helper function to work out whether to call the new style callback or the old
24 * one, and translate between the two.
26 * This has a long return type for consistency with the old callback. Similarly
27 * for the "long" used for "inret"
29 static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
30 int argi, long argl, long inret,
36 if (b->callback_ex != NULL)
37 return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
39 /* Strip off any BIO_CB_RETURN flag */
40 bareoper = oper & ~BIO_CB_RETURN;
43 * We have an old style callback, so we will have to do nasty casts and
44 * check for overflows.
46 if (HAS_LEN_OPER(bareoper)) {
47 /* In this case |len| is set, and should be used instead of |argi| */
54 if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
55 if (*processed > INT_MAX)
60 ret = b->callback(b, oper, argp, argi, argl, inret);
62 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
63 *processed = (size_t)ret;
70 BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
72 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
75 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
84 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
87 bio->lock = CRYPTO_THREAD_lock_new();
88 if (bio->lock == NULL) {
89 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
90 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
94 if (method->create != NULL && !method->create(bio)) {
95 ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
96 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
97 CRYPTO_THREAD_lock_free(bio->lock);
100 if (method->create == NULL)
110 BIO *BIO_new(const BIO_METHOD *method)
112 return BIO_new_ex(NULL, method);
122 if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
125 REF_PRINT_COUNT("BIO", a);
128 REF_ASSERT_ISNT(ret < 0);
130 if (a->callback != NULL || a->callback_ex != NULL) {
131 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
136 if ((a->method != NULL) && (a->method->destroy != NULL))
137 a->method->destroy(a);
139 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
141 CRYPTO_THREAD_lock_free(a->lock);
148 void BIO_set_data(BIO *a, void *ptr)
153 void *BIO_get_data(BIO *a)
158 void BIO_set_init(BIO *a, int init)
163 int BIO_get_init(BIO *a)
168 void BIO_set_shutdown(BIO *a, int shut)
173 int BIO_get_shutdown(BIO *a)
178 void BIO_vfree(BIO *a)
183 int BIO_up_ref(BIO *a)
187 if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
190 REF_PRINT_COUNT("BIO", a);
191 REF_ASSERT_ISNT(i < 2);
195 void BIO_clear_flags(BIO *b, int flags)
200 int BIO_test_flags(const BIO *b, int flags)
202 return (b->flags & flags);
205 void BIO_set_flags(BIO *b, int flags)
210 BIO_callback_fn BIO_get_callback(const BIO *b)
215 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
220 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
222 return b->callback_ex;
225 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
230 void BIO_set_callback_arg(BIO *b, char *arg)
235 char *BIO_get_callback_arg(const BIO *b)
240 const char *BIO_method_name(const BIO *b)
242 return b->method->name;
245 int BIO_method_type(const BIO *b)
247 return b->method->type;
251 * This is essentially the same as BIO_read_ex() except that it allows
252 * 0 or a negative value to indicate failure (retryable or not) in the return.
253 * This is for compatibility with the old style BIO_read(), where existing code
254 * may make assumptions about the return value that it might get.
256 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
261 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
264 if (b->method == NULL || b->method->bread == NULL) {
265 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
269 if ((b->callback != NULL || b->callback_ex != NULL) &&
270 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
275 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
279 ret = b->method->bread(b, data, dlen, readbytes);
282 b->num_read += (uint64_t)*readbytes;
284 if (b->callback != NULL || b->callback_ex != NULL)
285 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
286 dlen, 0, 0L, ret, readbytes);
288 /* Shouldn't happen */
289 if (ret > 0 && *readbytes > dlen) {
290 ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
297 int BIO_read(BIO *b, void *data, int dlen)
305 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
308 /* *readbytes should always be <= dlen */
309 ret = (int)readbytes;
315 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
317 return bio_read_intern(b, data, dlen, readbytes) > 0;
320 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
326 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
329 if (b->method == NULL || b->method->bwrite == NULL) {
330 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
334 if ((b->callback != NULL || b->callback_ex != NULL) &&
335 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
340 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
344 ret = b->method->bwrite(b, data, dlen, written);
347 b->num_write += (uint64_t)*written;
349 if (b->callback != NULL || b->callback_ex != NULL)
350 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
351 dlen, 0, 0L, ret, written);
356 int BIO_write(BIO *b, const void *data, int dlen)
364 ret = bio_write_intern(b, data, (size_t)dlen, &written);
367 /* *written should always be <= dlen */
374 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
376 return bio_write_intern(b, data, dlen, written) > 0;
379 int BIO_puts(BIO *b, const char *buf)
385 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
388 if (b->method == NULL || b->method->bputs == NULL) {
389 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
393 if (b->callback != NULL || b->callback_ex != NULL) {
394 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
400 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
404 ret = b->method->bputs(b, buf);
407 b->num_write += (uint64_t)ret;
412 if (b->callback != NULL || b->callback_ex != NULL)
413 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
417 if (written > INT_MAX) {
418 ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
428 int BIO_gets(BIO *b, char *buf, int size)
431 size_t readbytes = 0;
434 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
437 if (b->method == NULL || b->method->bgets == NULL) {
438 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
443 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
447 if (b->callback != NULL || b->callback_ex != NULL) {
448 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
454 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
458 ret = b->method->bgets(b, buf, size);
465 if (b->callback != NULL || b->callback_ex != NULL)
466 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
467 0, 0L, ret, &readbytes);
470 /* Shouldn't happen */
471 if (readbytes > (size_t)size)
474 ret = (int)readbytes;
480 int BIO_indent(BIO *b, int indent, int max)
487 if (BIO_puts(b, " ") != 1)
492 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
497 return BIO_ctrl(b, cmd, larg, (char *)&i);
500 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
504 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
510 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
515 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
518 if (b->method == NULL || b->method->ctrl == NULL) {
519 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
523 if (b->callback != NULL || b->callback_ex != NULL) {
524 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
529 ret = b->method->ctrl(b, cmd, larg, parg);
531 if (b->callback != NULL || b->callback_ex != NULL)
532 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
538 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
543 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
546 if (b->method == NULL || b->method->callback_ctrl == NULL
547 || cmd != BIO_CTRL_SET_CALLBACK) {
548 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
552 if (b->callback != NULL || b->callback_ex != NULL) {
553 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
559 ret = b->method->callback_ctrl(b, cmd, fp);
561 if (b->callback != NULL || b->callback_ex != NULL)
562 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
569 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
570 * do; but those macros have inappropriate return type, and for interfacing
571 * from other programming languages, C macros aren't much of a help anyway.
573 size_t BIO_ctrl_pending(BIO *bio)
575 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
578 size_t BIO_ctrl_wpending(BIO *bio)
580 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
583 /* put the 'bio' on the end of b's list of operators */
584 BIO *BIO_push(BIO *b, BIO *bio)
591 while (lb->next_bio != NULL)
596 /* called to do internal processing */
597 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
601 /* Remove the first and return the rest */
607 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
612 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
614 if (b->prev_bio != NULL)
615 b->prev_bio->next_bio = b->next_bio;
616 if (b->next_bio != NULL)
617 b->next_bio->prev_bio = b->prev_bio;
624 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
630 if (!BIO_should_retry(b))
638 *reason = last->retry_reason;
642 int BIO_get_retry_reason(BIO *bio)
644 return bio->retry_reason;
647 void BIO_set_retry_reason(BIO *bio, int reason)
649 bio->retry_reason = reason;
652 BIO *BIO_find_type(BIO *bio, int type)
657 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
662 if (bio->method != NULL) {
663 mt = bio->method->type;
668 } else if (mt == type) {
673 } while (bio != NULL);
677 BIO *BIO_next(BIO *b)
680 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
686 void BIO_set_next(BIO *b, BIO *next)
691 void BIO_free_all(BIO *bio)
696 while (bio != NULL) {
701 /* Since ref count > 1, don't free anyone else. */
707 BIO *BIO_dup_chain(BIO *in)
709 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
711 for (bio = in; bio != NULL; bio = bio->next_bio) {
712 if ((new_bio = BIO_new(bio->method)) == NULL)
714 new_bio->callback = bio->callback;
715 new_bio->callback_ex = bio->callback_ex;
716 new_bio->cb_arg = bio->cb_arg;
717 new_bio->init = bio->init;
718 new_bio->shutdown = bio->shutdown;
719 new_bio->flags = bio->flags;
721 /* This will let SSL_s_sock() work with stdin/stdout */
722 new_bio->num = bio->num;
724 if (!BIO_dup_state(bio, (char *)new_bio)) {
730 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
740 BIO_push(eoc, new_bio);
751 void BIO_copy_next_retry(BIO *b)
753 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
754 b->retry_reason = b->next_bio->retry_reason;
757 int BIO_set_ex_data(BIO *bio, int idx, void *data)
759 return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
762 void *BIO_get_ex_data(const BIO *bio, int idx)
764 return CRYPTO_get_ex_data(&(bio->ex_data), idx);
767 uint64_t BIO_number_read(BIO *bio)
770 return bio->num_read;
774 uint64_t BIO_number_written(BIO *bio)
777 return bio->num_write;
781 void bio_free_ex_data(BIO *bio)
783 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
786 void bio_cleanup(void)
788 #ifndef OPENSSL_NO_SOCK
789 bio_sock_cleanup_int();
790 CRYPTO_THREAD_lock_free(bio_lookup_lock);
791 bio_lookup_lock = NULL;
793 CRYPTO_THREAD_lock_free(bio_type_lock);
794 bio_type_lock = NULL;
797 /* Internal variant of the below BIO_wait() not calling BIOerr() */
798 static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
800 #ifndef OPENSSL_NO_SOCK
805 if (max_time == 0) /* no timeout */
808 #ifndef OPENSSL_NO_SOCK
809 if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
810 return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
812 /* fall back to polling since no sockets are available */
814 sec_diff = (long)(max_time - time(NULL)); /* might overflow */
816 return 0; /* clearly timeout */
818 /* now take a nap at most the given number of milliseconds */
819 if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
820 if (nap_milliseconds > 1000)
821 nap_milliseconds = 1000;
822 } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
823 if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
824 nap_milliseconds = (unsigned int)sec_diff * 1000;
826 ossl_sleep(nap_milliseconds);
831 * Wait on (typically socket-based) BIO at most until max_time.
832 * Succeed immediately if max_time == 0.
833 * If sockets are not available support polling: succeed after waiting at most
834 * the number of nap_milliseconds in order to avoid a tight busy loop.
835 * Call BIOerr(...) on timeout or error.
836 * Returns -1 on error, 0 on timeout, and 1 on success.
838 int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
840 int rv = bio_wait(bio, max_time, nap_milliseconds);
843 ERR_raise(ERR_LIB_BIO,
844 rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
849 * Connect via given BIO using BIO_do_connect() until success/timeout/error.
850 * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
851 * For non-blocking and potentially even non-socket BIOs perform polling with
852 * the given density: between polls sleep nap_milliseconds using BIO_wait()
853 * in order to avoid a tight busy loop.
854 * Returns -1 on error, 0 on timeout, and 1 on success.
856 int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
858 int blocking = timeout <= 0;
859 time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
863 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
867 if (nap_milliseconds < 0)
868 nap_milliseconds = 100;
869 BIO_set_nbio(bio, !blocking);
873 rv = BIO_do_connect(bio);
875 if (rv <= 0) { /* could be timeout or retryable error or fatal error */
876 int err = ERR_peek_last_error();
877 int reason = ERR_GET_REASON(err);
878 int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
880 if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
884 * likely retryable system error occurred, which may be
885 * EAGAIN (resource temporarily unavailable) some 40 secs after
886 * calling getaddrinfo(): Temporary failure in name resolution
887 * or a premature ETIMEDOUT, some 30 seconds after connect()
889 case BIO_R_CONNECT_ERROR:
890 case BIO_R_NBIO_CONNECT_ERROR:
891 /* some likely retryable connection error occurred */
892 (void)BIO_reset(bio); /* often needed to avoid retry failure */
899 if (timeout >= 0 && do_retry) {
901 /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
902 rv = bio_wait(bio, max_time, nap_milliseconds);
905 ERR_raise(ERR_LIB_BIO,
906 rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
908 ERR_clear_last_mark();
910 if (err == 0) /* missing error queue entry */
911 /* workaround: general error */
912 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
915 ERR_clear_last_mark();