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 *data, size_t datal, 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, data, datal, 0, 0L, 1L,
268 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
272 ret = b->method->bread(b, data, datal, 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, data,
279 datal, 0, 0L, ret, read);
284 int BIO_read(BIO *b, void *data, int datal)
292 ret = bio_read_intern(b, data, (size_t)datal, &read);
295 /* *read should always be <= outl */
302 int BIO_read_ex(BIO *b, void *data, size_t datal, size_t *read)
306 ret = bio_read_intern(b, data, datal, read);
316 static int bio_write_intern(BIO *b, const void *data, size_t datal,
324 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
325 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
329 if ((b->callback != NULL || b->callback_ex != NULL) &&
330 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, datal, 0, 0L, 1L,
335 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
339 ret = b->method->bwrite(b, data, datal, written);
342 b->num_write += (uint64_t)*written;
344 if (b->callback != NULL || b->callback_ex != NULL)
345 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
346 datal, 0, 0L, ret, written);
351 int BIO_write(BIO *b, const void *data, int datal)
359 ret = bio_write_intern(b, data, (size_t)datal, &written);
362 /* *written should always be <= inl */
369 int BIO_write_ex(BIO *b, const void *data, size_t datal, size_t *written)
373 ret = bio_write_intern(b, data, datal, written);
383 int BIO_puts(BIO *b, const char *in)
388 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
389 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
393 if (b->callback != NULL || b->callback_ex != NULL) {
394 ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
400 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
404 ret = b->method->bputs(b, in);
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, in, 0, 0,
417 if (written > INT_MAX)
426 int BIO_gets(BIO *b, char *out, int outl)
431 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
432 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
436 if (b->callback != NULL || b->callback_ex != NULL) {
437 ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
443 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
447 ret = b->method->bgets(b, out, outl);
454 if (b->callback != NULL || b->callback_ex != NULL)
455 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
468 int BIO_indent(BIO *b, int indent, int max)
475 if (BIO_puts(b, " ") != 1)
480 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
485 return (BIO_ctrl(b, cmd, larg, (char *)&i));
488 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
492 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
498 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
505 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
506 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
510 if (b->callback != NULL || b->callback_ex != NULL) {
511 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
516 ret = b->method->ctrl(b, cmd, larg, parg);
518 if (b->callback != NULL || b->callback_ex != NULL)
519 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
525 long BIO_callback_ctrl(BIO *b, int cmd,
526 void (*fp) (struct bio_st *, int, const char *, int,
534 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
535 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
539 if (b->callback != NULL || b->callback_ex != NULL) {
540 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
546 ret = b->method->callback_ctrl(b, cmd, fp);
548 if (b->callback != NULL || b->callback_ex != NULL)
549 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
556 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
557 * do; but those macros have inappropriate return type, and for interfacing
558 * from other programming languages, C macros aren't much of a help anyway.
560 size_t BIO_ctrl_pending(BIO *bio)
562 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
565 size_t BIO_ctrl_wpending(BIO *bio)
567 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
570 /* put the 'bio' on the end of b's list of operators */
571 BIO *BIO_push(BIO *b, BIO *bio)
578 while (lb->next_bio != NULL)
583 /* called to do internal processing */
584 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
588 /* Remove the first and return the rest */
597 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
599 if (b->prev_bio != NULL)
600 b->prev_bio->next_bio = b->next_bio;
601 if (b->next_bio != NULL)
602 b->next_bio->prev_bio = b->prev_bio;
609 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
615 if (!BIO_should_retry(b))
623 *reason = last->retry_reason;
627 int BIO_get_retry_reason(BIO *bio)
629 return (bio->retry_reason);
632 void BIO_set_retry_reason(BIO *bio, int reason)
634 bio->retry_reason = reason;
637 BIO *BIO_find_type(BIO *bio, int type)
645 if (bio->method != NULL) {
646 mt = bio->method->type;
651 } else if (mt == type)
655 } while (bio != NULL);
659 BIO *BIO_next(BIO *b)
666 void BIO_set_next(BIO *b, BIO *next)
671 void BIO_free_all(BIO *bio)
676 while (bio != NULL) {
681 /* Since ref count > 1, don't free anyone else. */
687 BIO *BIO_dup_chain(BIO *in)
689 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
691 for (bio = in; bio != NULL; bio = bio->next_bio) {
692 if ((new_bio = BIO_new(bio->method)) == NULL)
694 new_bio->callback = bio->callback;
695 new_bio->callback_ex = bio->callback_ex;
696 new_bio->cb_arg = bio->cb_arg;
697 new_bio->init = bio->init;
698 new_bio->shutdown = bio->shutdown;
699 new_bio->flags = bio->flags;
701 /* This will let SSL_s_sock() work with stdin/stdout */
702 new_bio->num = bio->num;
704 if (!BIO_dup_state(bio, (char *)new_bio)) {
710 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
720 BIO_push(eoc, new_bio);
731 void BIO_copy_next_retry(BIO *b)
733 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
734 b->retry_reason = b->next_bio->retry_reason;
737 int BIO_set_ex_data(BIO *bio, int idx, void *data)
739 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
742 void *BIO_get_ex_data(BIO *bio, int idx)
744 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
747 uint64_t BIO_number_read(BIO *bio)
750 return bio->num_read;
754 uint64_t BIO_number_written(BIO *bio)
757 return bio->num_write;
761 void bio_free_ex_data(BIO *bio)
763 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
766 void bio_cleanup(void)
768 #ifndef OPENSSL_NO_SOCK
769 bio_sock_cleanup_int();
770 CRYPTO_THREAD_lock_free(bio_lookup_lock);
771 bio_lookup_lock = NULL;
773 CRYPTO_THREAD_lock_free(bio_type_lock);
774 bio_type_lock = NULL;