update libressl to v2.7.4
[unleashed.git] / lib / libcrypto / bio / bio_lib.c
blobddab5428817da323f062b49191c0ad8d31f3d5a5
1 /* $OpenBSD: bio_lib.c,v 1.27 2018/02/22 16:38:43 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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
25 * are met:
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
51 * SUCH DAMAGE.
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.]
59 #include <errno.h>
60 #include <stdio.h>
62 #include <openssl/bio.h>
63 #include <openssl/crypto.h>
64 #include <openssl/err.h>
65 #include <openssl/stack.h>
67 int
68 BIO_get_new_index(void)
70 static int bio_type_index = BIO_TYPE_START;
71 int index;
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);
75 if (index > 255)
76 return -1;
78 return index;
81 BIO *
82 BIO_new(BIO_METHOD *method)
84 BIO *ret = NULL;
86 ret = malloc(sizeof(BIO));
87 if (ret == NULL) {
88 BIOerror(ERR_R_MALLOC_FAILURE);
89 return (NULL);
91 if (!BIO_set(ret, method)) {
92 free(ret);
93 ret = NULL;
95 return (ret);
98 int
99 BIO_set(BIO *bio, BIO_METHOD *method)
101 bio->method = method;
102 bio->callback = NULL;
103 bio->cb_arg = NULL;
104 bio->init = 0;
105 bio->shutdown = 1;
106 bio->flags = 0;
107 bio->retry_reason = 0;
108 bio->num = 0;
109 bio->ptr = NULL;
110 bio->prev_bio = NULL;
111 bio->next_bio = NULL;
112 bio->references = 1;
113 bio->num_read = 0L;
114 bio->num_write = 0L;
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,
119 &bio->ex_data);
120 return (0);
122 return (1);
126 BIO_free(BIO *a)
128 int i;
130 if (a == NULL)
131 return (0);
133 i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO);
134 if (i > 0)
135 return (1);
136 if ((a->callback != NULL) &&
137 ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
138 return (i);
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);
144 free(a);
145 return (1);
148 void
149 BIO_vfree(BIO *a)
151 BIO_free(a);
155 BIO_up_ref(BIO *bio)
157 int refs = CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO);
158 return (refs > 1) ? 1 : 0;
161 void *
162 BIO_get_data(BIO *a)
164 return (a->ptr);
167 void
168 BIO_set_data(BIO *a, void *ptr)
170 a->ptr = ptr;
173 void
174 BIO_set_init(BIO *a, int init)
176 a->init = init;
180 BIO_get_shutdown(BIO *a)
182 return (a->shutdown);
185 void
186 BIO_set_shutdown(BIO *a, int shut)
188 a->shutdown = shut;
191 void
192 BIO_clear_flags(BIO *b, int flags)
194 b->flags &= ~flags;
198 BIO_test_flags(const BIO *b, int flags)
200 return (b->flags & flags);
203 void
204 BIO_set_flags(BIO *b, int flags)
206 b->flags |= flags;
209 long
210 (*BIO_get_callback(const BIO *b))(struct bio_st *, int, const char *, int,
211 long, long)
213 return b->callback;
216 void
217 BIO_set_callback(BIO *b, long (*cb)(struct bio_st *, int, const char *, int,
218 long, long))
220 b->callback = cb;
223 void
224 BIO_set_callback_arg(BIO *b, char *arg)
226 b->cb_arg = arg;
229 char *
230 BIO_get_callback_arg(const BIO *b)
232 return b->cb_arg;
235 const char *
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)
250 int i;
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);
255 return (-2);
258 cb = b->callback;
259 if ((cb != NULL) &&
260 ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
261 return (i);
263 if (!b->init) {
264 BIOerror(BIO_R_UNINITIALIZED);
265 return (-2);
268 i = b->method->bread(b, out, outl);
270 if (i > 0)
271 b->num_read += (unsigned long)i;
273 if (cb != NULL)
274 i = (int)cb(b, BIO_CB_READ|BIO_CB_RETURN, out, outl,
275 0L, (long)i);
276 return (i);
280 BIO_write(BIO *b, const void *in, int inl)
282 int i;
283 long (*cb)(BIO *, int, const char *, int, long, long);
285 if (b == NULL)
286 return (0);
288 cb = b->callback;
289 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
290 BIOerror(BIO_R_UNSUPPORTED_METHOD);
291 return (-2);
294 if ((cb != NULL) &&
295 ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
296 return (i);
298 if (!b->init) {
299 BIOerror(BIO_R_UNINITIALIZED);
300 return (-2);
303 i = b->method->bwrite(b, in, inl);
305 if (i > 0)
306 b->num_write += (unsigned long)i;
308 if (cb != NULL)
309 i = (int)cb(b, BIO_CB_WRITE|BIO_CB_RETURN, in, inl,
310 0L, (long)i);
311 return (i);
315 BIO_puts(BIO *b, const char *in)
317 int i;
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);
322 return (-2);
325 cb = b->callback;
327 if ((cb != NULL) &&
328 ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
329 return (i);
331 if (!b->init) {
332 BIOerror(BIO_R_UNINITIALIZED);
333 return (-2);
336 i = b->method->bputs(b, in);
338 if (i > 0)
339 b->num_write += (unsigned long)i;
341 if (cb != NULL)
342 i = (int)cb(b, BIO_CB_PUTS|BIO_CB_RETURN, in, 0, 0L, (long)i);
343 return (i);
347 BIO_gets(BIO *b, char *in, int inl)
349 int i;
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);
354 return (-2);
357 cb = b->callback;
359 if ((cb != NULL) &&
360 ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
361 return (i);
363 if (!b->init) {
364 BIOerror(BIO_R_UNINITIALIZED);
365 return (-2);
368 i = b->method->bgets(b, in, inl);
370 if (cb != NULL)
371 i = (int)cb(b, BIO_CB_GETS|BIO_CB_RETURN, in, inl, 0L, (long)i);
372 return (i);
376 BIO_indent(BIO *b, int indent, int max)
378 if (indent < 0)
379 indent = 0;
380 if (indent > max)
381 indent = max;
382 while (indent--)
383 if (BIO_puts(b, " ") != 1)
384 return 0;
385 return 1;
388 long
389 BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
391 int i;
393 i = iarg;
394 return (BIO_ctrl(b, cmd, larg, (char *)&i));
397 char *
398 BIO_ptr_ctrl(BIO *b, int cmd, long larg)
400 char *p = NULL;
402 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
403 return (NULL);
404 else
405 return (p);
408 long
409 BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
411 long ret;
412 long (*cb)(BIO *, int, const char *, int, long, long);
414 if (b == NULL)
415 return (0);
417 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
418 BIOerror(BIO_R_UNSUPPORTED_METHOD);
419 return (-2);
422 cb = b->callback;
424 if ((cb != NULL) &&
425 ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
426 return (ret);
428 ret = b->method->ctrl(b, cmd, larg, parg);
430 if (cb != NULL)
431 ret = cb(b, BIO_CB_CTRL|BIO_CB_RETURN, parg, cmd, larg, ret);
432 return (ret);
435 long
436 BIO_callback_ctrl(BIO *b, int cmd,
437 void (*fp)(struct bio_st *, int, const char *, int, long, long))
439 long ret;
440 long (*cb)(BIO *, int, const char *, int, long, long);
442 if (b == NULL)
443 return (0);
445 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
446 BIOerror(BIO_R_UNSUPPORTED_METHOD);
447 return (-2);
450 cb = b->callback;
452 if ((cb != NULL) &&
453 ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
454 return (ret);
456 ret = b->method->callback_ctrl(b, cmd, fp);
458 if (cb != NULL)
459 ret = cb(b, BIO_CB_CTRL|BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
460 return (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. */
466 size_t
467 BIO_ctrl_pending(BIO *bio)
469 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
472 size_t
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 */
480 BIO *
481 BIO_push(BIO *b, BIO *bio)
483 BIO *lb;
485 if (b == NULL)
486 return (bio);
487 lb = b;
488 while (lb->next_bio != NULL)
489 lb = lb->next_bio;
490 lb->next_bio = bio;
491 if (bio != NULL)
492 bio->prev_bio = lb;
493 /* called to do internal processing */
494 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
495 return (b);
498 /* Remove the first and return the rest */
499 BIO *
500 BIO_pop(BIO *b)
502 BIO *ret;
504 if (b == NULL)
505 return (NULL);
506 ret = b->next_bio;
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;
515 b->next_bio = NULL;
516 b->prev_bio = NULL;
517 return (ret);
520 BIO *
521 BIO_get_retry_BIO(BIO *bio, int *reason)
523 BIO *b, *last;
525 b = last = bio;
526 for (;;) {
527 if (!BIO_should_retry(b))
528 break;
529 last = b;
530 b = b->next_bio;
531 if (b == NULL)
532 break;
534 if (reason != NULL)
535 *reason = last->retry_reason;
536 return (last);
540 BIO_get_retry_reason(BIO *bio)
542 return (bio->retry_reason);
545 BIO *
546 BIO_find_type(BIO *bio, int type)
548 int mt, mask;
550 if (!bio)
551 return NULL;
552 mask = type & 0xff;
553 do {
554 if (bio->method != NULL) {
555 mt = bio->method->type;
556 if (!mask) {
557 if (mt & type)
558 return (bio);
559 } else if (mt == type)
560 return (bio);
562 bio = bio->next_bio;
563 } while (bio != NULL);
564 return (NULL);
567 BIO *
568 BIO_next(BIO *b)
570 if (!b)
571 return NULL;
572 return b->next_bio;
575 void
576 BIO_free_all(BIO *bio)
578 BIO *b;
579 int ref;
581 while (bio != NULL) {
582 b = bio;
583 ref = b->references;
584 bio = bio->next_bio;
585 BIO_free(b);
586 /* Since ref count > 1, don't free anyone else. */
587 if (ref > 1)
588 break;
592 BIO *
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)
599 goto err;
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)) {
610 BIO_free(new_bio);
611 goto err;
614 /* copy app data */
615 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO,
616 &new_bio->ex_data, &bio->ex_data))
617 goto err;
619 if (ret == NULL) {
620 eoc = new_bio;
621 ret = eoc;
622 } else {
623 BIO_push(eoc, new_bio);
624 eoc = new_bio;
627 return (ret);
628 err:
629 BIO_free(ret);
630 return (NULL);
634 void
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));
655 void *
656 BIO_get_ex_data(BIO *bio, int idx)
658 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
661 unsigned long
662 BIO_number_read(BIO *bio)
664 if (bio)
665 return bio->num_read;
666 return 0;
669 unsigned long
670 BIO_number_written(BIO *bio)
672 if (bio)
673 return bio->num_write;
674 return 0;