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;
247 int BIO_read(BIO *b, void *out, int outl)
255 ret = BIO_read_ex(b, out, (size_t)outl, &read);
258 /* *read should always be <= outl */
265 int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
269 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
270 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
274 if ((b->callback != NULL || b->callback_ex != NULL) &&
275 ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0, 0L, 1L,
280 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
284 ret = b->method->bread(b, out, outl, read);
287 b->num_read += (uint64_t)*read;
289 if (b->callback != NULL || b->callback_ex != NULL)
290 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, out, outl,
296 int BIO_write(BIO *b, const void *in, int inl)
304 ret = BIO_write_ex(b, in, (size_t)inl, &written);
307 /* *written should always be <= inl */
314 int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written)
321 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
322 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
326 if ((b->callback != NULL || b->callback_ex != NULL) &&
327 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0, 0L, 1L,
332 BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
336 ret = b->method->bwrite(b, in, inl, written);
339 b->num_write += (uint64_t)*written;
341 if (b->callback != NULL || b->callback_ex != NULL)
342 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl,
343 0, 0L, ret, written);
348 int BIO_puts(BIO *b, const char *in)
353 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
354 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
358 if (b->callback != NULL || b->callback_ex != NULL) {
359 ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
365 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
369 ret = b->method->bputs(b, in);
372 b->num_write += (uint64_t)ret;
377 if (b->callback != NULL || b->callback_ex != NULL)
378 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0,
382 if (written > INT_MAX)
391 int BIO_gets(BIO *b, char *out, int outl)
396 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
397 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
401 if (b->callback != NULL || b->callback_ex != NULL) {
402 ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
408 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
412 ret = b->method->bgets(b, out, outl);
419 if (b->callback != NULL || b->callback_ex != NULL)
420 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
433 int BIO_indent(BIO *b, int indent, int max)
440 if (BIO_puts(b, " ") != 1)
445 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
450 return (BIO_ctrl(b, cmd, larg, (char *)&i));
453 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
457 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
463 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
470 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
471 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
475 if (b->callback != NULL || b->callback_ex != NULL) {
476 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
481 ret = b->method->ctrl(b, cmd, larg, parg);
483 if (b->callback != NULL || b->callback_ex != NULL)
484 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
490 long BIO_callback_ctrl(BIO *b, int cmd,
491 void (*fp) (struct bio_st *, int, const char *, int,
499 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
500 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
504 if (b->callback != NULL || b->callback_ex != NULL) {
505 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
511 ret = b->method->callback_ctrl(b, cmd, fp);
513 if (b->callback != NULL || b->callback_ex != NULL)
514 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
521 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
522 * do; but those macros have inappropriate return type, and for interfacing
523 * from other programming languages, C macros aren't much of a help anyway.
525 size_t BIO_ctrl_pending(BIO *bio)
527 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
530 size_t BIO_ctrl_wpending(BIO *bio)
532 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
535 /* put the 'bio' on the end of b's list of operators */
536 BIO *BIO_push(BIO *b, BIO *bio)
543 while (lb->next_bio != NULL)
548 /* called to do internal processing */
549 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
553 /* Remove the first and return the rest */
562 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
564 if (b->prev_bio != NULL)
565 b->prev_bio->next_bio = b->next_bio;
566 if (b->next_bio != NULL)
567 b->next_bio->prev_bio = b->prev_bio;
574 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
580 if (!BIO_should_retry(b))
588 *reason = last->retry_reason;
592 int BIO_get_retry_reason(BIO *bio)
594 return (bio->retry_reason);
597 void BIO_set_retry_reason(BIO *bio, int reason)
599 bio->retry_reason = reason;
602 BIO *BIO_find_type(BIO *bio, int type)
610 if (bio->method != NULL) {
611 mt = bio->method->type;
616 } else if (mt == type)
620 } while (bio != NULL);
624 BIO *BIO_next(BIO *b)
631 void BIO_set_next(BIO *b, BIO *next)
636 void BIO_free_all(BIO *bio)
641 while (bio != NULL) {
646 /* Since ref count > 1, don't free anyone else. */
652 BIO *BIO_dup_chain(BIO *in)
654 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
656 for (bio = in; bio != NULL; bio = bio->next_bio) {
657 if ((new_bio = BIO_new(bio->method)) == NULL)
659 new_bio->callback = bio->callback;
660 new_bio->callback_ex = bio->callback_ex;
661 new_bio->cb_arg = bio->cb_arg;
662 new_bio->init = bio->init;
663 new_bio->shutdown = bio->shutdown;
664 new_bio->flags = bio->flags;
666 /* This will let SSL_s_sock() work with stdin/stdout */
667 new_bio->num = bio->num;
669 if (!BIO_dup_state(bio, (char *)new_bio)) {
675 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
685 BIO_push(eoc, new_bio);
696 void BIO_copy_next_retry(BIO *b)
698 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
699 b->retry_reason = b->next_bio->retry_reason;
702 int BIO_set_ex_data(BIO *bio, int idx, void *data)
704 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
707 void *BIO_get_ex_data(BIO *bio, int idx)
709 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
712 uint64_t BIO_number_read(BIO *bio)
715 return bio->num_read;
719 uint64_t BIO_number_written(BIO *bio)
722 return bio->num_write;
726 void bio_free_ex_data(BIO *bio)
728 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
731 void bio_cleanup(void)
733 #ifndef OPENSSL_NO_SOCK
734 bio_sock_cleanup_int();
735 CRYPTO_THREAD_lock_free(bio_lookup_lock);
736 bio_lookup_lock = NULL;
738 CRYPTO_THREAD_lock_free(bio_type_lock);
739 bio_type_lock = NULL;