2 * Copyright 1995-2018 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>
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);
40 /* Strip off any BIO_CB_RETURN flag */
41 bareoper = oper & ~BIO_CB_RETURN;
44 * We have an old style callback, so we will have to do nasty casts and
45 * check for overflows.
47 if (HAS_LEN_OPER(bareoper)) {
48 /* In this case |len| is set, and should be used instead of |argi| */
55 if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
56 if (*processed > INT_MAX)
61 ret = b->callback(b, oper, argp, argi, argl, inret);
63 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
64 *processed = (size_t)ret;
71 BIO *BIO_new(const BIO_METHOD *method)
73 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
76 BIOerr(BIO_F_BIO_NEW, 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 BIOerr(BIO_F_BIO_NEW, 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 BIOerr(BIO_F_BIO_NEW, 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)
117 if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
120 REF_PRINT_COUNT("BIO", a);
123 REF_ASSERT_ISNT(ret < 0);
125 if (a->callback != NULL || a->callback_ex != NULL) {
126 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
131 if ((a->method != NULL) && (a->method->destroy != NULL))
132 a->method->destroy(a);
134 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
136 CRYPTO_THREAD_lock_free(a->lock);
143 void BIO_set_data(BIO *a, void *ptr)
148 void *BIO_get_data(BIO *a)
153 void BIO_set_init(BIO *a, int init)
158 int BIO_get_init(BIO *a)
163 void BIO_set_shutdown(BIO *a, int shut)
168 int BIO_get_shutdown(BIO *a)
173 void BIO_vfree(BIO *a)
178 int BIO_up_ref(BIO *a)
182 if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
185 REF_PRINT_COUNT("BIO", a);
186 REF_ASSERT_ISNT(i < 2);
187 return ((i > 1) ? 1 : 0);
190 void BIO_clear_flags(BIO *b, int flags)
195 int BIO_test_flags(const BIO *b, int flags)
197 return (b->flags & flags);
200 void BIO_set_flags(BIO *b, int flags)
205 BIO_callback_fn BIO_get_callback(const BIO *b)
210 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
215 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
217 return b->callback_ex;
220 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
225 void BIO_set_callback_arg(BIO *b, char *arg)
230 char *BIO_get_callback_arg(const BIO *b)
235 const char *BIO_method_name(const BIO *b)
237 return b->method->name;
240 int BIO_method_type(const BIO *b)
242 return b->method->type;
246 * This is essentially the same as BIO_read_ex() except that it allows
247 * 0 or a negative value to indicate failure (retryable or not) in the return.
248 * This is for compatibility with the old style BIO_read(), where existing code
249 * may make assumptions about the return value that it might get.
251 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
255 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
256 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
260 if ((b->callback != NULL || b->callback_ex != NULL) &&
261 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
266 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
270 ret = b->method->bread(b, data, dlen, readbytes);
273 b->num_read += (uint64_t)*readbytes;
275 if (b->callback != NULL || b->callback_ex != NULL)
276 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
277 dlen, 0, 0L, ret, readbytes);
279 /* Shouldn't happen */
280 if (ret > 0 && *readbytes > dlen) {
281 BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
288 int BIO_read(BIO *b, void *data, int dlen)
296 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
299 /* *readbytes should always be <= dlen */
300 ret = (int)readbytes;
306 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
310 ret = bio_read_intern(b, data, dlen, readbytes);
320 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
328 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
329 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
333 if ((b->callback != NULL || b->callback_ex != NULL) &&
334 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
339 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
343 ret = b->method->bwrite(b, data, dlen, written);
346 b->num_write += (uint64_t)*written;
348 if (b->callback != NULL || b->callback_ex != NULL)
349 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
350 dlen, 0, 0L, ret, written);
355 int BIO_write(BIO *b, const void *data, int dlen)
363 ret = bio_write_intern(b, data, (size_t)dlen, &written);
366 /* *written should always be <= dlen */
373 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
377 ret = bio_write_intern(b, data, dlen, written);
387 int BIO_puts(BIO *b, const char *buf)
392 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
393 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
397 if (b->callback != NULL || b->callback_ex != NULL) {
398 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
404 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
408 ret = b->method->bputs(b, buf);
411 b->num_write += (uint64_t)ret;
416 if (b->callback != NULL || b->callback_ex != NULL)
417 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
421 if (written > INT_MAX) {
422 BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
432 int BIO_gets(BIO *b, char *buf, int size)
435 size_t readbytes = 0;
437 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
438 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
443 BIOerr(BIO_F_BIO_GETS, 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 BIOerr(BIO_F_BIO_GETS, 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)
517 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
518 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
522 if (b->callback != NULL || b->callback_ex != NULL) {
523 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
528 ret = b->method->ctrl(b, cmd, larg, parg);
530 if (b->callback != NULL || b->callback_ex != NULL)
531 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
537 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
544 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)
545 || (cmd != BIO_CTRL_SET_CALLBACK)) {
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;