2 * Copyright 1995-2016 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 > LONG_MAX || ret < LONG_MIN)
67 if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
68 *processed = (size_t)ret;
75 BIO *BIO_new(const BIO_METHOD *method)
77 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
80 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
88 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
91 bio->lock = CRYPTO_THREAD_lock_new();
92 if (bio->lock == NULL) {
93 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
94 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
98 if (method->create != NULL && !method->create(bio)) {
99 BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
100 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
101 CRYPTO_THREAD_lock_free(bio->lock);
119 if (CRYPTO_atomic_add(&a->references, -1, &ret, a->lock) <= 0)
122 REF_PRINT_COUNT("BIO", a);
125 REF_ASSERT_ISNT(ret < 0);
127 if (a->callback != NULL || a->callback_ex != NULL) {
128 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
133 if ((a->method != NULL) && (a->method->destroy != NULL))
134 a->method->destroy(a);
136 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
138 CRYPTO_THREAD_lock_free(a->lock);
145 void BIO_set_data(BIO *a, void *ptr)
150 void *BIO_get_data(BIO *a)
155 void BIO_set_init(BIO *a, int init)
160 int BIO_get_init(BIO *a)
165 void BIO_set_shutdown(BIO *a, int shut)
170 int BIO_get_shutdown(BIO *a)
175 void BIO_vfree(BIO *a)
180 int BIO_up_ref(BIO *a)
184 if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
187 REF_PRINT_COUNT("BIO", a);
188 REF_ASSERT_ISNT(i < 2);
189 return ((i > 1) ? 1 : 0);
192 void BIO_clear_flags(BIO *b, int flags)
197 int BIO_test_flags(const BIO *b, int flags)
199 return (b->flags & flags);
202 void BIO_set_flags(BIO *b, int flags)
207 BIO_callback_fn BIO_get_callback(const BIO *b)
212 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
217 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
219 return b->callback_ex;
222 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
227 void BIO_set_callback_arg(BIO *b, char *arg)
232 char *BIO_get_callback_arg(const BIO *b)
237 const char *BIO_method_name(const BIO *b)
239 return b->method->name;
242 int BIO_method_type(const BIO *b)
244 return b->method->type;
248 * This is essentially the same as BIO_read_ex() except that it allows
249 * 0 or a -ve value to indicate failure (retryable or not) in the return. This
250 * is for compatibility with the old style BIO_read(), where existing code may
251 * make assumptions about the return value that it might get.
253 static int bio_read_intern(BIO *b, void *out, size_t outl, size_t *read)
257 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
258 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
262 if ((b->callback != NULL || b->callback_ex != NULL) &&
263 ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0, 0L, 1L,
268 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
272 ret = b->method->bread(b, out, outl, read);
275 b->num_read += (uint64_t)*read;
277 if (b->callback != NULL || b->callback_ex != NULL)
278 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, out, outl,
284 int BIO_read(BIO *b, void *out, int outl)
292 ret = bio_read_intern(b, out, (size_t)outl, &read);
295 /* *read should always be <= outl */
302 int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
306 ret = bio_read_intern(b, out, outl, read);
316 static int bio_write_intern(BIO *b, const void *in, size_t inl, size_t *written)
323 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
324 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
328 if ((b->callback != NULL || b->callback_ex != NULL) &&
329 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0, 0L, 1L,
334 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
338 ret = b->method->bwrite(b, in, inl, written);
341 b->num_write += (uint64_t)*written;
343 if (b->callback != NULL || b->callback_ex != NULL)
344 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl,
345 0, 0L, ret, written);
350 int BIO_write(BIO *b, const void *in, int inl)
358 ret = bio_write_intern(b, in, (size_t)inl, &written);
361 /* *written should always be <= inl */
368 int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written)
372 ret = bio_write_intern(b, in, inl, written);
382 int BIO_puts(BIO *b, const char *in)
387 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
388 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
392 if (b->callback != NULL || b->callback_ex != NULL) {
393 ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
399 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
403 ret = b->method->bputs(b, in);
406 b->num_write += (uint64_t)ret;
411 if (b->callback != NULL || b->callback_ex != NULL)
412 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0,
416 if (written > INT_MAX)
425 int BIO_gets(BIO *b, char *out, int outl)
430 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
431 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
435 if (b->callback != NULL || b->callback_ex != NULL) {
436 ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
442 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
446 ret = b->method->bgets(b, out, outl);
453 if (b->callback != NULL || b->callback_ex != NULL)
454 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
467 int BIO_indent(BIO *b, int indent, int max)
474 if (BIO_puts(b, " ") != 1)
479 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
484 return (BIO_ctrl(b, cmd, larg, (char *)&i));
487 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
491 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
497 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
504 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
505 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
509 if (b->callback != NULL || b->callback_ex != NULL) {
510 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
515 ret = b->method->ctrl(b, cmd, larg, parg);
517 if (b->callback != NULL || b->callback_ex != NULL)
518 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
524 long BIO_callback_ctrl(BIO *b, int cmd,
525 void (*fp) (struct bio_st *, int, const char *, int,
533 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
534 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
538 if (b->callback != NULL || b->callback_ex != NULL) {
539 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
545 ret = b->method->callback_ctrl(b, cmd, fp);
547 if (b->callback != NULL || b->callback_ex != NULL)
548 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
555 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
556 * do; but those macros have inappropriate return type, and for interfacing
557 * from other programming languages, C macros aren't much of a help anyway.
559 size_t BIO_ctrl_pending(BIO *bio)
561 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
564 size_t BIO_ctrl_wpending(BIO *bio)
566 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
569 /* put the 'bio' on the end of b's list of operators */
570 BIO *BIO_push(BIO *b, BIO *bio)
577 while (lb->next_bio != NULL)
582 /* called to do internal processing */
583 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
587 /* Remove the first and return the rest */
596 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
598 if (b->prev_bio != NULL)
599 b->prev_bio->next_bio = b->next_bio;
600 if (b->next_bio != NULL)
601 b->next_bio->prev_bio = b->prev_bio;
608 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
614 if (!BIO_should_retry(b))
622 *reason = last->retry_reason;
626 int BIO_get_retry_reason(BIO *bio)
628 return (bio->retry_reason);
631 void BIO_set_retry_reason(BIO *bio, int reason)
633 bio->retry_reason = reason;
636 BIO *BIO_find_type(BIO *bio, int type)
644 if (bio->method != NULL) {
645 mt = bio->method->type;
650 } else if (mt == type)
654 } while (bio != NULL);
658 BIO *BIO_next(BIO *b)
665 void BIO_set_next(BIO *b, BIO *next)
670 void BIO_free_all(BIO *bio)
675 while (bio != NULL) {
680 /* Since ref count > 1, don't free anyone else. */
686 BIO *BIO_dup_chain(BIO *in)
688 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
690 for (bio = in; bio != NULL; bio = bio->next_bio) {
691 if ((new_bio = BIO_new(bio->method)) == NULL)
693 new_bio->callback = bio->callback;
694 new_bio->callback_ex = bio->callback_ex;
695 new_bio->cb_arg = bio->cb_arg;
696 new_bio->init = bio->init;
697 new_bio->shutdown = bio->shutdown;
698 new_bio->flags = bio->flags;
700 /* This will let SSL_s_sock() work with stdin/stdout */
701 new_bio->num = bio->num;
703 if (!BIO_dup_state(bio, (char *)new_bio)) {
709 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
719 BIO_push(eoc, new_bio);
730 void BIO_copy_next_retry(BIO *b)
732 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
733 b->retry_reason = b->next_bio->retry_reason;
736 int BIO_set_ex_data(BIO *bio, int idx, void *data)
738 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
741 void *BIO_get_ex_data(BIO *bio, int idx)
743 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
746 uint64_t BIO_number_read(BIO *bio)
749 return bio->num_read;
753 uint64_t BIO_number_written(BIO *bio)
756 return bio->num_write;
760 void bio_free_ex_data(BIO *bio)
762 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
765 void bio_cleanup(void)
767 #ifndef OPENSSL_NO_SOCK
768 bio_sock_cleanup_int();
769 CRYPTO_THREAD_lock_free(bio_lookup_lock);
770 bio_lookup_lock = NULL;
772 CRYPTO_THREAD_lock_free(bio_type_lock);
773 bio_type_lock = NULL;