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"
15 #include <openssl/stack.h>
17 BIO *BIO_new(const BIO_METHOD *method)
19 BIO *ret = OPENSSL_malloc(sizeof(*ret));
22 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
25 if (!BIO_set(ret, method)) {
32 int BIO_set(BIO *bio, const BIO_METHOD *method)
40 bio->retry_reason = 0;
48 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
51 bio->lock = CRYPTO_THREAD_lock_new();
52 if (bio->lock == NULL) {
53 BIOerr(BIO_F_BIO_SET, ERR_R_MALLOC_FAILURE);
54 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
58 if (method->create != NULL) {
59 if (!method->create(bio)) {
60 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
61 CRYPTO_THREAD_lock_free(bio->lock);
76 if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
79 REF_PRINT_COUNT("BIO", a);
82 REF_ASSERT_ISNT(i < 0);
83 if ((a->callback != NULL) &&
84 ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
87 if ((a->method != NULL) && (a->method->destroy != NULL))
88 a->method->destroy(a);
90 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
92 CRYPTO_THREAD_lock_free(a->lock);
99 void BIO_set_data(BIO *a, void *ptr)
104 void *BIO_get_data(BIO *a)
109 void BIO_set_init(BIO *a, int init)
114 int BIO_get_init(BIO *a)
119 void BIO_set_shutdown(BIO *a, int shut)
124 int BIO_get_shutdown(BIO *a)
129 void BIO_vfree(BIO *a)
134 int BIO_up_ref(BIO *a)
138 if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
141 REF_PRINT_COUNT("BIO", a);
142 REF_ASSERT_ISNT(i < 2);
143 return ((i > 1) ? 1 : 0);
146 void BIO_clear_flags(BIO *b, int flags)
151 int BIO_test_flags(const BIO *b, int flags)
153 return (b->flags & flags);
156 void BIO_set_flags(BIO *b, int flags)
161 long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
166 void BIO_set_callback(BIO *b,
167 long (*cb) (struct bio_st *, int, const char *, int,
173 void BIO_set_callback_arg(BIO *b, char *arg)
178 char *BIO_get_callback_arg(const BIO *b)
183 const char *BIO_method_name(const BIO *b)
185 return b->method->name;
188 int BIO_method_type(const BIO *b)
190 return b->method->type;
193 int BIO_read(BIO *b, void *out, int outl)
196 long (*cb) (BIO *, int, const char *, int, long, long);
198 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
199 BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
205 ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
209 BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
213 i = b->method->bread(b, out, outl);
216 b->num_read += (uint64_t)i;
219 i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
223 int BIO_write(BIO *b, const void *in, int inl)
226 long (*cb) (BIO *, int, const char *, int, long, long);
232 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
233 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
238 ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
242 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
246 i = b->method->bwrite(b, in, inl);
249 b->num_write += (uint64_t)i;
252 i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
256 int BIO_puts(BIO *b, const char *in)
259 long (*cb) (BIO *, int, const char *, int, long, long);
261 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
262 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
268 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
272 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
276 i = b->method->bputs(b, in);
279 b->num_write += (uint64_t)i;
282 i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
286 int BIO_gets(BIO *b, char *in, int inl)
289 long (*cb) (BIO *, int, const char *, int, long, long);
291 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
292 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
298 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
302 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
306 i = b->method->bgets(b, in, inl);
309 i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
313 int BIO_indent(BIO *b, int indent, int max)
320 if (BIO_puts(b, " ") != 1)
325 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
330 return (BIO_ctrl(b, cmd, larg, (char *)&i));
333 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
337 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
343 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
346 long (*cb) (BIO *, int, const char *, int, long, long);
351 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
352 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
359 ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
362 ret = b->method->ctrl(b, cmd, larg, parg);
365 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
369 long BIO_callback_ctrl(BIO *b, int cmd,
370 void (*fp) (struct bio_st *, int, const char *, int,
374 long (*cb) (BIO *, int, const char *, int, long, long);
379 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
380 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
387 ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
390 ret = b->method->callback_ctrl(b, cmd, fp);
393 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
398 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
399 * do; but those macros have inappropriate return type, and for interfacing
400 * from other programming languages, C macros aren't much of a help anyway.
402 size_t BIO_ctrl_pending(BIO *bio)
404 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
407 size_t BIO_ctrl_wpending(BIO *bio)
409 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
412 /* put the 'bio' on the end of b's list of operators */
413 BIO *BIO_push(BIO *b, BIO *bio)
420 while (lb->next_bio != NULL)
425 /* called to do internal processing */
426 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
430 /* Remove the first and return the rest */
439 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
441 if (b->prev_bio != NULL)
442 b->prev_bio->next_bio = b->next_bio;
443 if (b->next_bio != NULL)
444 b->next_bio->prev_bio = b->prev_bio;
451 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
457 if (!BIO_should_retry(b))
465 *reason = last->retry_reason;
469 int BIO_get_retry_reason(BIO *bio)
471 return (bio->retry_reason);
474 void BIO_set_retry_reason(BIO *bio, int reason)
476 bio->retry_reason = reason;
479 BIO *BIO_find_type(BIO *bio, int type)
487 if (bio->method != NULL) {
488 mt = bio->method->type;
493 } else if (mt == type)
497 } while (bio != NULL);
501 BIO *BIO_next(BIO *b)
508 void BIO_set_next(BIO *b, BIO *next)
513 void BIO_free_all(BIO *bio)
518 while (bio != NULL) {
523 /* Since ref count > 1, don't free anyone else. */
529 BIO *BIO_dup_chain(BIO *in)
531 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
533 for (bio = in; bio != NULL; bio = bio->next_bio) {
534 if ((new_bio = BIO_new(bio->method)) == NULL)
536 new_bio->callback = bio->callback;
537 new_bio->cb_arg = bio->cb_arg;
538 new_bio->init = bio->init;
539 new_bio->shutdown = bio->shutdown;
540 new_bio->flags = bio->flags;
542 /* This will let SSL_s_sock() work with stdin/stdout */
543 new_bio->num = bio->num;
545 if (!BIO_dup_state(bio, (char *)new_bio)) {
551 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
561 BIO_push(eoc, new_bio);
572 void BIO_copy_next_retry(BIO *b)
574 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
575 b->retry_reason = b->next_bio->retry_reason;
578 int BIO_set_ex_data(BIO *bio, int idx, void *data)
580 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
583 void *BIO_get_ex_data(BIO *bio, int idx)
585 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
588 uint64_t BIO_number_read(BIO *bio)
591 return bio->num_read;
595 uint64_t BIO_number_written(BIO *bio)
598 return bio->num_write;
602 void bio_free_ex_data(BIO *bio)
604 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
607 void bio_cleanup(void)
609 #ifndef OPENSSL_NO_SOCK
610 bio_sock_cleanup_int();
611 CRYPTO_THREAD_lock_free(bio_lookup_lock);
612 bio_lookup_lock = NULL;