]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bio_lib.c
557d609cd101869aa99313be5c11cb7f87656c73
2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
14 #include "internal/cryptlib.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 || \
25 * Helper function to work out whether to call the new style callback or the old
26 * one, and translate between the two.
28 * This has a long return type for consistency with the old callback. Similarly
29 * for the "long" used for "inret"
31 static long bio_call_callback(BIO
*b
, int oper
, const char *argp
, size_t len
,
32 int argi
, long argl
, long inret
, size_t *processed
)
37 if (b
->callback_ex
!= NULL
) {
38 return b
->callback_ex(b
, oper
, argp
, len
, argi
, argl
, inret
, processed
);
41 /* Strip off any BIO_CB_RETURN flag */
42 bareoper
= oper
& ~BIO_CB_RETURN
;
45 * We have an old style callback, so we will have to do nasty casts and
46 * check for overflows.
48 if (HAS_LEN_OPER(bareoper
)) {
49 /* In this case |len| is set, and should be used instead of |argi| */
55 if (inret
&& (oper
& BIO_CB_RETURN
)) {
56 if (*processed
> INT_MAX
)
62 ret
= b
->callback(b
, oper
, argp
, argi
, argl
, inret
);
64 if (ret
>= 0 && (HAS_LEN_OPER(bareoper
) || bareoper
== BIO_CB_PUTS
)) {
65 *processed
= (size_t)ret
;
72 BIO
*BIO_new(const BIO_METHOD
*method
)
74 BIO
*bio
= OPENSSL_zalloc(sizeof(*bio
));
77 BIOerr(BIO_F_BIO_NEW
, ERR_R_MALLOC_FAILURE
);
85 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
))
88 bio
->lock
= CRYPTO_THREAD_lock_new();
89 if (bio
->lock
== NULL
) {
90 BIOerr(BIO_F_BIO_NEW
, ERR_R_MALLOC_FAILURE
);
91 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
);
95 if (method
->create
!= NULL
&& !method
->create(bio
)) {
96 BIOerr(BIO_F_BIO_NEW
, ERR_R_INIT_FAIL
);
97 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
);
98 CRYPTO_THREAD_lock_free(bio
->lock
);
101 if (method
->create
== NULL
)
118 if (CRYPTO_DOWN_REF(&a
->references
, &ret
, a
->lock
) <= 0)
121 REF_PRINT_COUNT("BIO", a
);
124 REF_ASSERT_ISNT(ret
< 0);
126 if (a
->callback
!= NULL
|| a
->callback_ex
!= NULL
) {
127 ret
= (int)bio_call_callback(a
, BIO_CB_FREE
, NULL
, 0, 0, 0L, 1L, NULL
);
132 if ((a
->method
!= NULL
) && (a
->method
->destroy
!= NULL
))
133 a
->method
->destroy(a
);
135 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, a
, &a
->ex_data
);
137 CRYPTO_THREAD_lock_free(a
->lock
);
144 void BIO_set_data(BIO
*a
, void *ptr
)
149 void *BIO_get_data(BIO
*a
)
154 void BIO_set_init(BIO
*a
, int init
)
159 int BIO_get_init(BIO
*a
)
164 void BIO_set_shutdown(BIO
*a
, int shut
)
169 int BIO_get_shutdown(BIO
*a
)
174 void BIO_vfree(BIO
*a
)
179 int BIO_up_ref(BIO
*a
)
183 if (CRYPTO_UP_REF(&a
->references
, &i
, a
->lock
) <= 0)
186 REF_PRINT_COUNT("BIO", a
);
187 REF_ASSERT_ISNT(i
< 2);
188 return ((i
> 1) ? 1 : 0);
191 void BIO_clear_flags(BIO
*b
, int flags
)
196 int BIO_test_flags(const BIO
*b
, int flags
)
198 return (b
->flags
& flags
);
201 void BIO_set_flags(BIO
*b
, int flags
)
206 BIO_callback_fn
BIO_get_callback(const BIO
*b
)
211 void BIO_set_callback(BIO
*b
, BIO_callback_fn cb
)
216 BIO_callback_fn_ex
BIO_get_callback_ex(const BIO
*b
)
218 return b
->callback_ex
;
221 void BIO_set_callback_ex(BIO
*b
, BIO_callback_fn_ex cb
)
226 void BIO_set_callback_arg(BIO
*b
, char *arg
)
231 char *BIO_get_callback_arg(const BIO
*b
)
236 const char *BIO_method_name(const BIO
*b
)
238 return b
->method
->name
;
241 int BIO_method_type(const BIO
*b
)
243 return b
->method
->type
;
247 * This is essentially the same as BIO_read_ex() except that it allows
248 * 0 or a negative value to indicate failure (retryable or not) in the return.
249 * This is for compatibility with the old style BIO_read(), where existing code
250 * may make assumptions about the return value that it might get.
252 static int bio_read_intern(BIO
*b
, void *data
, size_t dlen
, size_t *readbytes
)
256 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bread
== NULL
)) {
257 BIOerr(BIO_F_BIO_READ_INTERN
, BIO_R_UNSUPPORTED_METHOD
);
261 if ((b
->callback
!= NULL
|| b
->callback_ex
!= NULL
) &&
262 ((ret
= (int)bio_call_callback(b
, BIO_CB_READ
, data
, dlen
, 0, 0L, 1L,
267 BIOerr(BIO_F_BIO_READ_INTERN
, BIO_R_UNINITIALIZED
);
271 ret
= b
->method
->bread(b
, data
, dlen
, readbytes
);
274 b
->num_read
+= (uint64_t)*readbytes
;
276 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
)
277 ret
= (int)bio_call_callback(b
, BIO_CB_READ
| BIO_CB_RETURN
, data
,
278 dlen
, 0, 0L, ret
, readbytes
);
280 /* Shouldn't happen */
281 if (ret
> 0 && *readbytes
> dlen
) {
282 BIOerr(BIO_F_BIO_READ_INTERN
, ERR_R_INTERNAL_ERROR
);
289 int BIO_read(BIO
*b
, void *data
, int dlen
)
297 ret
= bio_read_intern(b
, data
, (size_t)dlen
, &readbytes
);
300 /* *readbytes should always be <= dlen */
301 ret
= (int)readbytes
;
307 int BIO_read_ex(BIO
*b
, void *data
, size_t dlen
, size_t *readbytes
)
311 ret
= bio_read_intern(b
, data
, dlen
, readbytes
);
321 static int bio_write_intern(BIO
*b
, const void *data
, size_t dlen
,
329 if ((b
->method
== NULL
) || (b
->method
->bwrite
== NULL
)) {
330 BIOerr(BIO_F_BIO_WRITE_INTERN
, 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 BIOerr(BIO_F_BIO_WRITE_INTERN
, 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
)
378 ret
= bio_write_intern(b
, data
, dlen
, written
);
388 int BIO_puts(BIO
*b
, const char *buf
)
393 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bputs
== NULL
)) {
394 BIOerr(BIO_F_BIO_PUTS
, BIO_R_UNSUPPORTED_METHOD
);
398 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
) {
399 ret
= (int)bio_call_callback(b
, BIO_CB_PUTS
, buf
, 0, 0, 0L, 1L, NULL
);
405 BIOerr(BIO_F_BIO_PUTS
, BIO_R_UNINITIALIZED
);
409 ret
= b
->method
->bputs(b
, buf
);
412 b
->num_write
+= (uint64_t)ret
;
417 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
)
418 ret
= (int)bio_call_callback(b
, BIO_CB_PUTS
| BIO_CB_RETURN
, buf
, 0, 0,
422 if (written
> INT_MAX
) {
423 BIOerr(BIO_F_BIO_PUTS
, BIO_R_LENGTH_TOO_LONG
);
433 int BIO_gets(BIO
*b
, char *buf
, int size
)
436 size_t readbytes
= 0;
438 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bgets
== NULL
)) {
439 BIOerr(BIO_F_BIO_GETS
, BIO_R_UNSUPPORTED_METHOD
);
444 BIOerr(BIO_F_BIO_GETS
, BIO_R_INVALID_ARGUMENT
);
448 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
) {
449 ret
= (int)bio_call_callback(b
, BIO_CB_GETS
, buf
, size
, 0, 0L, 1, NULL
);
455 BIOerr(BIO_F_BIO_GETS
, BIO_R_UNINITIALIZED
);
459 ret
= b
->method
->bgets(b
, buf
, size
);
466 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
)
467 ret
= (int)bio_call_callback(b
, BIO_CB_GETS
| BIO_CB_RETURN
, buf
, size
,
468 0, 0L, ret
, &readbytes
);
471 /* Shouldn't happen */
472 if (readbytes
> (size_t)size
)
475 ret
= (int)readbytes
;
481 int BIO_indent(BIO
*b
, int indent
, int max
)
488 if (BIO_puts(b
, " ") != 1)
493 long BIO_int_ctrl(BIO
*b
, int cmd
, long larg
, int iarg
)
498 return BIO_ctrl(b
, cmd
, larg
, (char *)&i
);
501 void *BIO_ptr_ctrl(BIO
*b
, int cmd
, long larg
)
505 if (BIO_ctrl(b
, cmd
, larg
, (char *)&p
) <= 0)
511 long BIO_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
518 if ((b
->method
== NULL
) || (b
->method
->ctrl
== NULL
)) {
519 BIOerr(BIO_F_BIO_CTRL
, 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
)
545 if ((b
->method
== NULL
) || (b
->method
->callback_ctrl
== NULL
)) {
546 BIOerr(BIO_F_BIO_CALLBACK_CTRL
, BIO_R_UNSUPPORTED_METHOD
);
550 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
) {
551 ret
= bio_call_callback(b
, BIO_CB_CTRL
, (void *)&fp
, 0, cmd
, 0, 1L,
557 ret
= b
->method
->callback_ctrl(b
, cmd
, fp
);
559 if (b
->callback
!= NULL
|| b
->callback_ex
!= NULL
)
560 ret
= bio_call_callback(b
, BIO_CB_CTRL
| BIO_CB_RETURN
, (void *)&fp
, 0,
567 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
568 * do; but those macros have inappropriate return type, and for interfacing
569 * from other programming languages, C macros aren't much of a help anyway.
571 size_t BIO_ctrl_pending(BIO
*bio
)
573 return BIO_ctrl(bio
, BIO_CTRL_PENDING
, 0, NULL
);
576 size_t BIO_ctrl_wpending(BIO
*bio
)
578 return BIO_ctrl(bio
, BIO_CTRL_WPENDING
, 0, NULL
);
581 /* put the 'bio' on the end of b's list of operators */
582 BIO
*BIO_push(BIO
*b
, BIO
*bio
)
589 while (lb
->next_bio
!= NULL
)
594 /* called to do internal processing */
595 BIO_ctrl(b
, BIO_CTRL_PUSH
, 0, lb
);
599 /* Remove the first and return the rest */
608 BIO_ctrl(b
, BIO_CTRL_POP
, 0, b
);
610 if (b
->prev_bio
!= NULL
)
611 b
->prev_bio
->next_bio
= b
->next_bio
;
612 if (b
->next_bio
!= NULL
)
613 b
->next_bio
->prev_bio
= b
->prev_bio
;
620 BIO
*BIO_get_retry_BIO(BIO
*bio
, int *reason
)
626 if (!BIO_should_retry(b
))
634 *reason
= last
->retry_reason
;
638 int BIO_get_retry_reason(BIO
*bio
)
640 return bio
->retry_reason
;
643 void BIO_set_retry_reason(BIO
*bio
, int reason
)
645 bio
->retry_reason
= reason
;
648 BIO
*BIO_find_type(BIO
*bio
, int type
)
656 if (bio
->method
!= NULL
) {
657 mt
= bio
->method
->type
;
662 } else if (mt
== type
)
666 } while (bio
!= NULL
);
670 BIO
*BIO_next(BIO
*b
)
677 void BIO_set_next(BIO
*b
, BIO
*next
)
682 void BIO_free_all(BIO
*bio
)
687 while (bio
!= NULL
) {
692 /* Since ref count > 1, don't free anyone else. */
698 BIO
*BIO_dup_chain(BIO
*in
)
700 BIO
*ret
= NULL
, *eoc
= NULL
, *bio
, *new_bio
;
702 for (bio
= in
; bio
!= NULL
; bio
= bio
->next_bio
) {
703 if ((new_bio
= BIO_new(bio
->method
)) == NULL
)
705 new_bio
->callback
= bio
->callback
;
706 new_bio
->callback_ex
= bio
->callback_ex
;
707 new_bio
->cb_arg
= bio
->cb_arg
;
708 new_bio
->init
= bio
->init
;
709 new_bio
->shutdown
= bio
->shutdown
;
710 new_bio
->flags
= bio
->flags
;
712 /* This will let SSL_s_sock() work with stdin/stdout */
713 new_bio
->num
= bio
->num
;
715 if (!BIO_dup_state(bio
, (char *)new_bio
)) {
721 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO
, &new_bio
->ex_data
,
731 BIO_push(eoc
, new_bio
);
742 void BIO_copy_next_retry(BIO
*b
)
744 BIO_set_flags(b
, BIO_get_retry_flags(b
->next_bio
));
745 b
->retry_reason
= b
->next_bio
->retry_reason
;
748 int BIO_set_ex_data(BIO
*bio
, int idx
, void *data
)
750 return CRYPTO_set_ex_data(&(bio
->ex_data
), idx
, data
);
753 void *BIO_get_ex_data(BIO
*bio
, int idx
)
755 return CRYPTO_get_ex_data(&(bio
->ex_data
), idx
);
758 uint64_t BIO_number_read(BIO
*bio
)
761 return bio
->num_read
;
765 uint64_t BIO_number_written(BIO
*bio
)
768 return bio
->num_write
;
772 void bio_free_ex_data(BIO
*bio
)
774 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
);
777 void bio_cleanup(void)
779 #ifndef OPENSSL_NO_SOCK
780 bio_sock_cleanup_int();
781 CRYPTO_THREAD_lock_free(bio_lookup_lock
);
782 bio_lookup_lock
= NULL
;
784 CRYPTO_THREAD_lock_free(bio_type_lock
);
785 bio_type_lock
= NULL
;