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"
16 BIO *BIO_new(const BIO_METHOD *method)
18 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
21 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
29 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
32 bio->lock = CRYPTO_THREAD_lock_new();
33 if (bio->lock == NULL) {
34 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
35 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
39 if (method->create != NULL && !method->create(bio)) {
40 BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
41 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
42 CRYPTO_THREAD_lock_free(bio->lock);
60 if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
63 REF_PRINT_COUNT("BIO", a);
66 REF_ASSERT_ISNT(i < 0);
67 if ((a->callback != NULL) &&
68 ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
71 if ((a->method != NULL) && (a->method->destroy != NULL))
72 a->method->destroy(a);
74 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
76 CRYPTO_THREAD_lock_free(a->lock);
83 void BIO_set_data(BIO *a, void *ptr)
88 void *BIO_get_data(BIO *a)
93 void BIO_set_init(BIO *a, int init)
98 int BIO_get_init(BIO *a)
103 void BIO_set_shutdown(BIO *a, int shut)
108 int BIO_get_shutdown(BIO *a)
113 void BIO_vfree(BIO *a)
118 int BIO_up_ref(BIO *a)
122 if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
125 REF_PRINT_COUNT("BIO", a);
126 REF_ASSERT_ISNT(i < 2);
127 return ((i > 1) ? 1 : 0);
130 void BIO_clear_flags(BIO *b, int flags)
135 int BIO_test_flags(const BIO *b, int flags)
137 return (b->flags & flags);
140 void BIO_set_flags(BIO *b, int flags)
145 BIO_callback_fn BIO_get_callback(const BIO *b)
150 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
155 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
157 return b->callback_ex;
160 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
165 void BIO_set_callback_arg(BIO *b, char *arg)
170 char *BIO_get_callback_arg(const BIO *b)
175 const char *BIO_method_name(const BIO *b)
177 return b->method->name;
180 int BIO_method_type(const BIO *b)
182 return b->method->type;
185 static int bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
186 int argi, long argl, int inret, size_t *processed,
192 if (b->callback_ex != NULL) {
193 return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed,
197 /* Strip off the BIO_CB_RETURN flag */
198 bareoper = oper & ~BIO_CB_RETURN;
200 * We have an old style callback, so we will have to do nasty casts and
201 * check for overflows.
203 if (bareoper == BIO_CB_READ || bareoper == BIO_CB_WRITE
204 || bareoper == BIO_CB_GETS) {
205 /* In this case |len| is set, and should be used instead of |argi| */
211 if (inret && (oper & BIO_CB_RETURN)) {
212 if (*processed > INT_MAX)
218 ret = b->callback(b, oper, argp, argi, argl, inret);
219 if (bareoper == BIO_CB_CTRL)
222 if (ret > INT_MAX || ret < INT_MIN)
229 *processed = (size_t)ret;
236 int BIO_read(BIO *b, void *out, int outl)
244 ret = BIO_read_ex(b, out, (size_t)outl, &read);
247 /* *read should always be <= outl */
254 int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
258 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
259 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
263 if ((b->callback != NULL || b->callback_ex != NULL) &&
264 ((ret = bio_call_callback(b, BIO_CB_READ, out, outl, 0, 0L, 1L, read,
269 BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
273 ret = b->method->bread(b, out, outl, read);
276 b->num_read += (uint64_t)*read;
278 if (b->callback != NULL || b->callback_ex != NULL)
279 ret = bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0,
280 0L, ret, read, NULL);
285 int BIO_write(BIO *b, const void *in, int inl)
288 long (*cb) (BIO *, int, const char *, int, long, long);
294 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
295 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
300 ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
304 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
308 i = b->method->bwrite(b, in, inl);
311 b->num_write += (uint64_t)i;
314 i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
318 int BIO_puts(BIO *b, const char *in)
321 long (*cb) (BIO *, int, const char *, int, long, long);
323 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
324 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
330 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
334 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
338 i = b->method->bputs(b, in);
341 b->num_write += (uint64_t)i;
344 i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
348 int BIO_gets(BIO *b, char *in, int inl)
351 long (*cb) (BIO *, int, const char *, int, long, long);
353 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
354 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
360 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
364 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
368 i = b->method->bgets(b, in, inl);
371 i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
375 int BIO_indent(BIO *b, int indent, int max)
382 if (BIO_puts(b, " ") != 1)
387 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
392 return (BIO_ctrl(b, cmd, larg, (char *)&i));
395 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
399 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
405 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
408 long (*cb) (BIO *, int, const char *, int, long, long);
413 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
414 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
421 ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
424 ret = b->method->ctrl(b, cmd, larg, parg);
427 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
431 long BIO_callback_ctrl(BIO *b, int cmd,
432 void (*fp) (struct bio_st *, int, const char *, int,
436 long (*cb) (BIO *, int, const char *, int, long, long);
441 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
442 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
449 ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
452 ret = b->method->callback_ctrl(b, cmd, fp);
455 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
460 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
461 * do; but those macros have inappropriate return type, and for interfacing
462 * from other programming languages, C macros aren't much of a help anyway.
464 size_t BIO_ctrl_pending(BIO *bio)
466 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
469 size_t BIO_ctrl_wpending(BIO *bio)
471 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
474 /* put the 'bio' on the end of b's list of operators */
475 BIO *BIO_push(BIO *b, BIO *bio)
482 while (lb->next_bio != NULL)
487 /* called to do internal processing */
488 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
492 /* Remove the first and return the rest */
501 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
503 if (b->prev_bio != NULL)
504 b->prev_bio->next_bio = b->next_bio;
505 if (b->next_bio != NULL)
506 b->next_bio->prev_bio = b->prev_bio;
513 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
519 if (!BIO_should_retry(b))
527 *reason = last->retry_reason;
531 int BIO_get_retry_reason(BIO *bio)
533 return (bio->retry_reason);
536 void BIO_set_retry_reason(BIO *bio, int reason)
538 bio->retry_reason = reason;
541 BIO *BIO_find_type(BIO *bio, int type)
549 if (bio->method != NULL) {
550 mt = bio->method->type;
555 } else if (mt == type)
559 } while (bio != NULL);
563 BIO *BIO_next(BIO *b)
570 void BIO_set_next(BIO *b, BIO *next)
575 void BIO_free_all(BIO *bio)
580 while (bio != NULL) {
585 /* Since ref count > 1, don't free anyone else. */
591 BIO *BIO_dup_chain(BIO *in)
593 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
595 for (bio = in; bio != NULL; bio = bio->next_bio) {
596 if ((new_bio = BIO_new(bio->method)) == NULL)
598 new_bio->callback = bio->callback;
599 new_bio->cb_arg = bio->cb_arg;
600 new_bio->init = bio->init;
601 new_bio->shutdown = bio->shutdown;
602 new_bio->flags = bio->flags;
604 /* This will let SSL_s_sock() work with stdin/stdout */
605 new_bio->num = bio->num;
607 if (!BIO_dup_state(bio, (char *)new_bio)) {
613 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
623 BIO_push(eoc, new_bio);
634 void BIO_copy_next_retry(BIO *b)
636 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
637 b->retry_reason = b->next_bio->retry_reason;
640 int BIO_set_ex_data(BIO *bio, int idx, void *data)
642 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
645 void *BIO_get_ex_data(BIO *bio, int idx)
647 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
650 uint64_t BIO_number_read(BIO *bio)
653 return bio->num_read;
657 uint64_t BIO_number_written(BIO *bio)
660 return bio->num_write;
664 void bio_free_ex_data(BIO *bio)
666 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
669 void bio_cleanup(void)
671 #ifndef OPENSSL_NO_SOCK
672 bio_sock_cleanup_int();
673 CRYPTO_THREAD_lock_free(bio_lookup_lock);
674 bio_lookup_lock = NULL;
676 CRYPTO_THREAD_lock_free(bio_type_lock);
677 bio_type_lock = NULL;