1 /* $OpenBSD: bio_lib.c,v 1.28 2018/05/01 13:29:09 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
62 #include <openssl/bio.h>
63 #include <openssl/crypto.h>
64 #include <openssl/err.h>
65 #include <openssl/stack.h>
68 BIO_get_new_index(void)
70 static int bio_type_index = BIO_TYPE_START;
73 /* The index will collide with the BIO flag bits if it exceeds 255. */
74 index = CRYPTO_add(&bio_type_index, 1, CRYPTO_LOCK_BIO);
82 BIO_new(const BIO_METHOD *method)
86 ret = malloc(sizeof(BIO));
88 BIOerror(ERR_R_MALLOC_FAILURE);
91 if (!BIO_set(ret, method)) {
99 BIO_set(BIO *bio, const BIO_METHOD *method)
101 bio->method = method;
102 bio->callback = NULL;
107 bio->retry_reason = 0;
110 bio->prev_bio = NULL;
111 bio->next_bio = NULL;
115 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
116 if (method->create != NULL)
117 if (!method->create(bio)) {
118 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
133 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO);
136 if ((a->callback != NULL) &&
137 ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
140 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
142 if (a->method != NULL && a->method->destroy != NULL)
143 a->method->destroy(a);
157 int refs = CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO);
158 return (refs > 1) ? 1 : 0;
168 BIO_set_data(BIO *a, void *ptr)
174 BIO_set_init(BIO *a, int init)
180 BIO_get_shutdown(BIO *a)
182 return (a->shutdown);
186 BIO_set_shutdown(BIO *a, int shut)
192 BIO_clear_flags(BIO *b, int flags)
198 BIO_test_flags(const BIO *b, int flags)
200 return (b->flags & flags);
204 BIO_set_flags(BIO *b, int flags)
210 (*BIO_get_callback(const BIO *b))(struct bio_st *, int, const char *, int,
217 BIO_set_callback(BIO *b, long (*cb)(struct bio_st *, int, const char *, int,
224 BIO_set_callback_arg(BIO *b, char *arg)
230 BIO_get_callback_arg(const BIO *b)
236 BIO_method_name(const BIO *b)
238 return b->method->name;
242 BIO_method_type(const BIO *b)
244 return b->method->type;
248 BIO_read(BIO *b, void *out, int outl)
251 long (*cb)(BIO *, int, const char *, int, long, long);
253 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
254 BIOerror(BIO_R_UNSUPPORTED_METHOD);
260 ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
264 BIOerror(BIO_R_UNINITIALIZED);
268 i = b->method->bread(b, out, outl);
271 b->num_read += (unsigned long)i;
274 i = (int)cb(b, BIO_CB_READ|BIO_CB_RETURN, out, outl,
280 BIO_write(BIO *b, const void *in, int inl)
283 long (*cb)(BIO *, int, const char *, int, long, long);
289 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
290 BIOerror(BIO_R_UNSUPPORTED_METHOD);
295 ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
299 BIOerror(BIO_R_UNINITIALIZED);
303 i = b->method->bwrite(b, in, inl);
306 b->num_write += (unsigned long)i;
309 i = (int)cb(b, BIO_CB_WRITE|BIO_CB_RETURN, in, inl,
315 BIO_puts(BIO *b, const char *in)
318 long (*cb)(BIO *, int, const char *, int, long, long);
320 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
321 BIOerror(BIO_R_UNSUPPORTED_METHOD);
328 ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
332 BIOerror(BIO_R_UNINITIALIZED);
336 i = b->method->bputs(b, in);
339 b->num_write += (unsigned long)i;
342 i = (int)cb(b, BIO_CB_PUTS|BIO_CB_RETURN, in, 0, 0L, (long)i);
347 BIO_gets(BIO *b, char *in, int inl)
350 long (*cb)(BIO *, int, const char *, int, long, long);
352 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
353 BIOerror(BIO_R_UNSUPPORTED_METHOD);
360 ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
364 BIOerror(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);
376 BIO_indent(BIO *b, int indent, int max)
383 if (BIO_puts(b, " ") != 1)
389 BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
394 return (BIO_ctrl(b, cmd, larg, (char *)&i));
398 BIO_ptr_ctrl(BIO *b, int cmd, long larg)
402 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
409 BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
412 long (*cb)(BIO *, int, const char *, int, long, long);
417 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
418 BIOerror(BIO_R_UNSUPPORTED_METHOD);
425 ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
428 ret = b->method->ctrl(b, cmd, larg, parg);
431 ret = cb(b, BIO_CB_CTRL|BIO_CB_RETURN, parg, cmd, larg, ret);
436 BIO_callback_ctrl(BIO *b, int cmd,
437 void (*fp)(struct bio_st *, int, const char *, int, long, long))
440 long (*cb)(BIO *, int, const char *, int, long, long);
445 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
446 BIOerror(BIO_R_UNSUPPORTED_METHOD);
453 ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
456 ret = b->method->callback_ctrl(b, cmd, fp);
459 ret = cb(b, BIO_CB_CTRL|BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
463 /* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
464 * do; but those macros have inappropriate return type, and for interfacing
465 * from other programming languages, C macros aren't much of a help anyway. */
467 BIO_ctrl_pending(BIO *bio)
469 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
473 BIO_ctrl_wpending(BIO *bio)
475 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
479 /* put the 'bio' on the end of b's list of operators */
481 BIO_push(BIO *b, BIO *bio)
488 while (lb->next_bio != NULL)
493 /* called to do internal processing */
494 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
498 /* Remove the first and return the rest */
508 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
510 if (b->prev_bio != NULL)
511 b->prev_bio->next_bio = b->next_bio;
512 if (b->next_bio != NULL)
513 b->next_bio->prev_bio = b->prev_bio;
521 BIO_get_retry_BIO(BIO *bio, int *reason)
527 if (!BIO_should_retry(b))
535 *reason = last->retry_reason;
540 BIO_get_retry_reason(BIO *bio)
542 return (bio->retry_reason);
546 BIO_find_type(BIO *bio, int type)
554 if (bio->method != NULL) {
555 mt = bio->method->type;
559 } else if (mt == type)
563 } while (bio != NULL);
576 BIO_free_all(BIO *bio)
581 while (bio != NULL) {
586 /* Since ref count > 1, don't free anyone else. */
593 BIO_dup_chain(BIO *in)
595 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
597 for (bio = in; bio != NULL; bio = bio->next_bio) {
598 if ((new_bio = BIO_new(bio->method)) == NULL)
600 new_bio->callback = bio->callback;
601 new_bio->cb_arg = bio->cb_arg;
602 new_bio->init = bio->init;
603 new_bio->shutdown = bio->shutdown;
604 new_bio->flags = bio->flags;
606 /* This will let SSL_s_sock() work with stdin/stdout */
607 new_bio->num = bio->num;
609 if (!BIO_dup_state(bio, (char *)new_bio)) {
615 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO,
616 &new_bio->ex_data, &bio->ex_data))
623 BIO_push(eoc, new_bio);
635 BIO_copy_next_retry(BIO *b)
637 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
638 b->retry_reason = b->next_bio->retry_reason;
642 BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
643 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
645 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
646 new_func, dup_func, free_func);
650 BIO_set_ex_data(BIO *bio, int idx, void *data)
652 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
656 BIO_get_ex_data(BIO *bio, int idx)
658 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
662 BIO_number_read(BIO *bio)
665 return bio->num_read;
670 BIO_number_written(BIO *bio)
673 return bio->num_write;