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 >= 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);
116 if (CRYPTO_atomic_add(&a->references, -1, &ret, a->lock) <= 0)
119 REF_PRINT_COUNT("BIO", a);
122 REF_ASSERT_ISNT(ret < 0);
124 if (a->callback != NULL || a->callback_ex != NULL) {
125 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
130 if ((a->method != NULL) && (a->method->destroy != NULL))
131 a->method->destroy(a);
133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
135 CRYPTO_THREAD_lock_free(a->lock);
142 void BIO_set_data(BIO *a, void *ptr)
147 void *BIO_get_data(BIO *a)
152 void BIO_set_init(BIO *a, int init)
157 int BIO_get_init(BIO *a)
162 void BIO_set_shutdown(BIO *a, int shut)
167 int BIO_get_shutdown(BIO *a)
172 void BIO_vfree(BIO *a)
177 int BIO_up_ref(BIO *a)
181 if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
184 REF_PRINT_COUNT("BIO", a);
185 REF_ASSERT_ISNT(i < 2);
186 return ((i > 1) ? 1 : 0);
189 void BIO_clear_flags(BIO *b, int flags)
194 int BIO_test_flags(const BIO *b, int flags)
196 return (b->flags & flags);
199 void BIO_set_flags(BIO *b, int flags)
204 BIO_callback_fn BIO_get_callback(const BIO *b)
209 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
214 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
216 return b->callback_ex;
219 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
224 void BIO_set_callback_arg(BIO *b, char *arg)
229 char *BIO_get_callback_arg(const BIO *b)
234 const char *BIO_method_name(const BIO *b)
236 return b->method->name;
239 int BIO_method_type(const BIO *b)
241 return b->method->type;
245 * This is essentially the same as BIO_read_ex() except that it allows
246 * 0 or a negative value to indicate failure (retryable or not) in the return.
247 * This is for compatibility with the old style BIO_read(), where existing code
248 * may make assumptions about the return value that it might get.
250 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
254 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
255 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
259 if ((b->callback != NULL || b->callback_ex != NULL) &&
260 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
265 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
269 ret = b->method->bread(b, data, dlen, readbytes);
272 b->num_read += (uint64_t)*read;
274 if (b->callback != NULL || b->callback_ex != NULL)
275 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
276 dlen, 0, 0L, ret, readbytes);
278 /* Shouldn't happen */
279 if (ret > 0 && *readbytes > dlen) {
280 BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
287 int BIO_read(BIO *b, void *data, int dlen)
295 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
298 /* *readbytes should always be <= dlen */
299 ret = (int)readbytes;
305 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
309 ret = bio_read_intern(b, data, dlen, readbytes);
319 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
327 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
328 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
332 if ((b->callback != NULL || b->callback_ex != NULL) &&
333 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
338 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
342 ret = b->method->bwrite(b, data, dlen, written);
345 b->num_write += (uint64_t)*written;
347 if (b->callback != NULL || b->callback_ex != NULL)
348 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
349 dlen, 0, 0L, ret, written);
354 int BIO_write(BIO *b, const void *data, int dlen)
362 ret = bio_write_intern(b, data, (size_t)dlen, &written);
365 /* *written should always be <= dlen */
372 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
376 ret = bio_write_intern(b, data, dlen, written);
386 int BIO_puts(BIO *b, const char *buf)
391 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
392 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
396 if (b->callback != NULL || b->callback_ex != NULL) {
397 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
403 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
407 ret = b->method->bputs(b, buf);
410 b->num_write += (uint64_t)ret;
415 if (b->callback != NULL || b->callback_ex != NULL)
416 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
420 if (written > INT_MAX) {
421 BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
431 int BIO_gets(BIO *b, char *buf, int size)
434 size_t readbytes = 0;
436 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
437 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
442 BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
446 if (b->callback != NULL || b->callback_ex != NULL) {
447 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
453 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
457 ret = b->method->bgets(b, buf, size);
464 if (b->callback != NULL || b->callback_ex != NULL)
465 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
466 0, 0L, ret, &readbytes);
469 /* Shouldn't happen */
470 if (readbytes > (size_t)size)
473 ret = (int)readbytes;
479 int BIO_indent(BIO *b, int indent, int max)
486 if (BIO_puts(b, " ") != 1)
491 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
496 return (BIO_ctrl(b, cmd, larg, (char *)&i));
499 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
503 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
509 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
516 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
517 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
521 if (b->callback != NULL || b->callback_ex != NULL) {
522 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
527 ret = b->method->ctrl(b, cmd, larg, parg);
529 if (b->callback != NULL || b->callback_ex != NULL)
530 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
536 long BIO_callback_ctrl(BIO *b, int cmd,
537 void (*fp) (struct bio_st *, int, const char *, int,
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;