]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bio_lib.c
crypto/rand/rand_lib.c: fix undefined reference to `clock_gettime'
[thirdparty/openssl.git] / crypto / bio / bio_lib.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <errno.h>
ec577822 12#include <openssl/crypto.h>
a146ae55 13#include "bio_lcl.h"
b39fc560 14#include "internal/cryptlib.h"
58964a49 15
98e553d2
MC
16
17/*
18 * Helper macro for the callback to determine whether an operator expects a
19 * len parameter or not
20 */
21#define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE || \
22 (o) == BIO_CB_GETS)
23
24/*
25 * Helper function to work out whether to call the new style callback or the old
26 * one, and translate between the two.
27 *
28 * This has a long return type for consistency with the old callback. Similarly
29 * for the "long" used for "inret"
30 */
31static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
32 int argi, long argl, long inret, size_t *processed)
33{
34 long ret;
35 int bareoper;
36
37 if (b->callback_ex != NULL) {
38 return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
39 }
40
41 /* Strip off any BIO_CB_RETURN flag */
42 bareoper = oper & ~BIO_CB_RETURN;
43
44 /*
45 * We have an old style callback, so we will have to do nasty casts and
46 * check for overflows.
47 */
48 if (HAS_LEN_OPER(bareoper)) {
49 /* In this case |len| is set, and should be used instead of |argi| */
50 if (len > INT_MAX)
51 return -1;
52
53 argi = (int)len;
54
55 if (inret && (oper & BIO_CB_RETURN)) {
56 if (*processed > INT_MAX)
57 return -1;
58 inret = *processed;
59 }
60 }
61
62 ret = b->callback(b, oper, argp, argi, argl, inret);
63
98e553d2
MC
64 if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
65 *processed = (size_t)ret;
66 ret = 1;
67 }
68
69 return ret;
70}
71
04f6b0fd 72BIO *BIO_new(const BIO_METHOD *method)
0f113f3e 73{
9d7bfb14 74 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
0f113f3e 75
9d7bfb14 76 if (bio == NULL) {
0f113f3e 77 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
26a7d938 78 return NULL;
0f113f3e 79 }
d02b48c6 80
0f113f3e 81 bio->method = method;
0f113f3e 82 bio->shutdown = 1;
0f113f3e 83 bio->references = 1;
9d7bfb14 84
25a807bc 85 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
9d7bfb14 86 goto err;
fb46be03
AG
87
88 bio->lock = CRYPTO_THREAD_lock_new();
89 if (bio->lock == NULL) {
9d7bfb14 90 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
fb46be03 91 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
9d7bfb14 92 goto err;
fb46be03
AG
93 }
94
9d7bfb14
F
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);
99 goto err;
fb46be03
AG
100 }
101
9d7bfb14
F
102 return bio;
103
104err:
105 OPENSSL_free(bio);
106 return NULL;
0f113f3e 107}
d02b48c6 108
6b691a5c 109int BIO_free(BIO *a)
0f113f3e 110{
98e553d2 111 int ret;
d02b48c6 112
0f113f3e 113 if (a == NULL)
fb46be03
AG
114 return 0;
115
2f545ae4 116 if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
fb46be03 117 return 0;
d02b48c6 118
f3f1cf84 119 REF_PRINT_COUNT("BIO", a);
98e553d2 120 if (ret > 0)
fb46be03 121 return 1;
98e553d2
MC
122 REF_ASSERT_ISNT(ret < 0);
123
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);
126 if (ret <= 0)
127 return ret;
128 }
d02b48c6 129
a14a740d
F
130 if ((a->method != NULL) && (a->method->destroy != NULL))
131 a->method->destroy(a);
132
0f113f3e 133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
58964a49 134
fb46be03
AG
135 CRYPTO_THREAD_lock_free(a->lock);
136
0f113f3e 137 OPENSSL_free(a);
fb46be03
AG
138
139 return 1;
0f113f3e 140}
d02b48c6 141
a146ae55
MC
142void BIO_set_data(BIO *a, void *ptr)
143{
144 a->ptr = ptr;
145}
146
147void *BIO_get_data(BIO *a)
148{
149 return a->ptr;
150}
151
152void BIO_set_init(BIO *a, int init)
153{
154 a->init = init;
155}
156
157int BIO_get_init(BIO *a)
158{
159 return a->init;
160}
161
162void BIO_set_shutdown(BIO *a, int shut)
163{
164 a->shutdown = shut;
165}
166
167int BIO_get_shutdown(BIO *a)
168{
169 return a->shutdown;
170}
171
371acb22 172void BIO_vfree(BIO *a)
0f113f3e
MC
173{
174 BIO_free(a);
175}
371acb22 176
fb46be03
AG
177int BIO_up_ref(BIO *a)
178{
179 int i;
180
2f545ae4 181 if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
fb46be03
AG
182 return 0;
183
184 REF_PRINT_COUNT("BIO", a);
185 REF_ASSERT_ISNT(i < 2);
186 return ((i > 1) ? 1 : 0);
187}
188
7806f3dd 189void BIO_clear_flags(BIO *b, int flags)
0f113f3e
MC
190{
191 b->flags &= ~flags;
192}
193
194int BIO_test_flags(const BIO *b, int flags)
195{
196 return (b->flags & flags);
197}
198
199void BIO_set_flags(BIO *b, int flags)
200{
201 b->flags |= flags;
202}
203
d07aee2c
MC
204BIO_callback_fn BIO_get_callback(const BIO *b)
205{
0f113f3e
MC
206 return b->callback;
207}
208
d07aee2c 209void BIO_set_callback(BIO *b, BIO_callback_fn cb)
0f113f3e
MC
210{
211 b->callback = cb;
212}
7806f3dd 213
d07aee2c
MC
214BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
215{
216 return b->callback_ex;
217}
218
219void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
220{
221 b->callback_ex = cb;
222}
223
7806f3dd 224void BIO_set_callback_arg(BIO *b, char *arg)
0f113f3e
MC
225{
226 b->cb_arg = arg;
227}
7806f3dd 228
0f113f3e
MC
229char *BIO_get_callback_arg(const BIO *b)
230{
231 return b->cb_arg;
232}
7806f3dd 233
0f113f3e
MC
234const char *BIO_method_name(const BIO *b)
235{
236 return b->method->name;
237}
7806f3dd
NL
238
239int BIO_method_type(const BIO *b)
0f113f3e
MC
240{
241 return b->method->type;
242}
7806f3dd 243
bb5310be
MC
244/*
245 * This is essentially the same as BIO_read_ex() except that it allows
7bf79e33
MC
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.
bb5310be 249 */
d62bf89c 250static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
d07aee2c
MC
251{
252 int ret;
d02b48c6 253
0f113f3e 254 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
7bf79e33 255 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
bb5310be 256 return -2;
0f113f3e 257 }
d02b48c6 258
d07aee2c 259 if ((b->callback != NULL || b->callback_ex != NULL) &&
7bf79e33 260 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
d62bf89c 261 readbytes)) <= 0))
d07aee2c 262 return ret;
d02b48c6 263
0f113f3e 264 if (!b->init) {
7bf79e33 265 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
d07aee2c 266 return -2;
0f113f3e 267 }
d02b48c6 268
d62bf89c 269 ret = b->method->bread(b, data, dlen, readbytes);
dfeab068 270
d07aee2c 271 if (ret > 0)
82cb311f 272 b->num_read += (uint64_t)*readbytes;
d07aee2c
MC
273
274 if (b->callback != NULL || b->callback_ex != NULL)
42c60460 275 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
d62bf89c 276 dlen, 0, 0L, ret, readbytes);
d02b48c6 277
fbba62f6 278 /* Shouldn't happen */
d62bf89c 279 if (ret > 0 && *readbytes > dlen) {
7bf79e33 280 BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
fbba62f6 281 return -1;
7bf79e33 282 }
fbba62f6 283
d07aee2c 284 return ret;
0f113f3e 285}
d02b48c6 286
7bf79e33 287int BIO_read(BIO *b, void *data, int dlen)
0f113f3e 288{
d62bf89c 289 size_t readbytes;
3befffa3
MC
290 int ret;
291
7bf79e33 292 if (dlen < 0)
3befffa3
MC
293 return 0;
294
d62bf89c 295 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
3befffa3
MC
296
297 if (ret > 0) {
4e3973b4 298 /* *readbytes should always be <= dlen */
d62bf89c 299 ret = (int)readbytes;
3befffa3
MC
300 }
301
302 return ret;
303}
304
d62bf89c 305int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
bb5310be
MC
306{
307 int ret;
308
d62bf89c 309 ret = bio_read_intern(b, data, dlen, readbytes);
bb5310be
MC
310
311 if (ret > 0)
312 ret = 1;
313 else
314 ret = 0;
315
316 return ret;
317}
318
7bf79e33 319static int bio_write_intern(BIO *b, const void *data, size_t dlen,
42c60460 320 size_t *written)
3befffa3
MC
321{
322 int ret;
58964a49 323
0f113f3e 324 if (b == NULL)
bb5310be 325 return 0;
d02b48c6 326
0f113f3e 327 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
7bf79e33 328 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
bb5310be 329 return -2;
0f113f3e 330 }
d02b48c6 331
3befffa3 332 if ((b->callback != NULL || b->callback_ex != NULL) &&
7bf79e33 333 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
98e553d2 334 written)) <= 0))
3befffa3 335 return ret;
d02b48c6 336
0f113f3e 337 if (!b->init) {
7bf79e33 338 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
3befffa3 339 return -2;
0f113f3e 340 }
d02b48c6 341
7bf79e33 342 ret = b->method->bwrite(b, data, dlen, written);
dfeab068 343
3befffa3
MC
344 if (ret > 0)
345 b->num_write += (uint64_t)*written;
d02b48c6 346
3befffa3 347 if (b->callback != NULL || b->callback_ex != NULL)
42c60460 348 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
7bf79e33 349 dlen, 0, 0L, ret, written);
3befffa3
MC
350
351 return ret;
0f113f3e 352}
d02b48c6 353
7bf79e33 354int BIO_write(BIO *b, const void *data, int dlen)
bb5310be
MC
355{
356 size_t written;
357 int ret;
358
7bf79e33 359 if (dlen < 0)
bb5310be
MC
360 return 0;
361
7bf79e33 362 ret = bio_write_intern(b, data, (size_t)dlen, &written);
bb5310be
MC
363
364 if (ret > 0) {
4e3973b4 365 /* *written should always be <= dlen */
bb5310be
MC
366 ret = (int)written;
367 }
368
369 return ret;
370}
371
7bf79e33 372int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
bb5310be
MC
373{
374 int ret;
375
7bf79e33 376 ret = bio_write_intern(b, data, dlen, written);
bb5310be
MC
377
378 if (ret > 0)
379 ret = 1;
380 else
381 ret = 0;
382
383 return ret;
384}
385
4e3973b4 386int BIO_puts(BIO *b, const char *buf)
0f113f3e 387{
98e553d2 388 int ret;
47263ace 389 size_t written = 0;
d02b48c6 390
0f113f3e
MC
391 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
392 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
98e553d2 393 return -2;
0f113f3e 394 }
d02b48c6 395
98e553d2 396 if (b->callback != NULL || b->callback_ex != NULL) {
4e3973b4 397 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
98e553d2
MC
398 if (ret <= 0)
399 return ret;
400 }
d02b48c6 401
0f113f3e
MC
402 if (!b->init) {
403 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
98e553d2 404 return -2;
0f113f3e 405 }
d02b48c6 406
4e3973b4 407 ret = b->method->bputs(b, buf);
d02b48c6 408
98e553d2
MC
409 if (ret > 0) {
410 b->num_write += (uint64_t)ret;
411 written = ret;
412 ret = 1;
413 }
414
415 if (b->callback != NULL || b->callback_ex != NULL)
4e3973b4 416 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
98e553d2
MC
417 0L, ret, &written);
418
419 if (ret > 0) {
7bf79e33
MC
420 if (written > INT_MAX) {
421 BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
98e553d2 422 ret = -1;
7bf79e33 423 } else {
98e553d2 424 ret = (int)written;
7bf79e33 425 }
98e553d2 426 }
7d95ff76 427
98e553d2 428 return ret;
0f113f3e 429}
d02b48c6 430
4e3973b4 431int BIO_gets(BIO *b, char *buf, int size)
0f113f3e 432{
98e553d2 433 int ret;
d62bf89c 434 size_t readbytes = 0;
0f113f3e
MC
435
436 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
437 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
26a7d938 438 return -2;
0f113f3e
MC
439 }
440
4e3973b4 441 if (size < 0) {
fbba62f6
MC
442 BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
443 return 0;
444 }
445
98e553d2 446 if (b->callback != NULL || b->callback_ex != NULL) {
4e3973b4 447 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
98e553d2
MC
448 if (ret <= 0)
449 return ret;
450 }
0f113f3e
MC
451
452 if (!b->init) {
453 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
26a7d938 454 return -2;
0f113f3e
MC
455 }
456
4e3973b4 457 ret = b->method->bgets(b, buf, size);
98e553d2
MC
458
459 if (ret > 0) {
d62bf89c 460 readbytes = ret;
98e553d2
MC
461 ret = 1;
462 }
463
464 if (b->callback != NULL || b->callback_ex != NULL)
4e3973b4 465 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
d62bf89c 466 0, 0L, ret, &readbytes);
98e553d2
MC
467
468 if (ret > 0) {
fbba62f6 469 /* Shouldn't happen */
4e3973b4 470 if (readbytes > (size_t)size)
98e553d2
MC
471 ret = -1;
472 else
d62bf89c 473 ret = (int)readbytes;
98e553d2 474 }
0f113f3e 475
98e553d2 476 return ret;
0f113f3e
MC
477}
478
479int BIO_indent(BIO *b, int indent, int max)
480{
481 if (indent < 0)
482 indent = 0;
483 if (indent > max)
484 indent = max;
485 while (indent--)
486 if (BIO_puts(b, " ") != 1)
487 return 0;
488 return 1;
489}
54a656ef 490
6b691a5c 491long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
0f113f3e
MC
492{
493 int i;
d02b48c6 494
0f113f3e 495 i = iarg;
26a7d938 496 return BIO_ctrl(b, cmd, larg, (char *)&i);
0f113f3e 497}
d02b48c6 498
417be660 499void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
0f113f3e 500{
417be660 501 void *p = NULL;
58964a49 502
0f113f3e 503 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
26a7d938 504 return NULL;
0f113f3e 505 else
26a7d938 506 return p;
0f113f3e 507}
58964a49 508
95d29597 509long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
0f113f3e
MC
510{
511 long ret;
d02b48c6 512
0f113f3e 513 if (b == NULL)
98e553d2 514 return 0;
d02b48c6 515
0f113f3e
MC
516 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
517 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
98e553d2 518 return -2;
0f113f3e 519 }
d02b48c6 520
98e553d2
MC
521 if (b->callback != NULL || b->callback_ex != NULL) {
522 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
523 if (ret <= 0)
524 return ret;
525 }
d02b48c6 526
0f113f3e 527 ret = b->method->ctrl(b, cmd, larg, parg);
d02b48c6 528
98e553d2
MC
529 if (b->callback != NULL || b->callback_ex != NULL)
530 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
531 larg, ret, NULL);
532
533 return ret;
0f113f3e 534}
d02b48c6 535
fce78bd4 536long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
537{
538 long ret;
d3442bc7 539
0f113f3e 540 if (b == NULL)
26a7d938 541 return 0;
d3442bc7 542
0f113f3e
MC
543 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
544 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
26a7d938 545 return -2;
0f113f3e 546 }
d3442bc7 547
98e553d2
MC
548 if (b->callback != NULL || b->callback_ex != NULL) {
549 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
550 NULL);
551 if (ret <= 0)
552 return ret;
553 }
d3442bc7 554
0f113f3e 555 ret = b->method->callback_ctrl(b, cmd, fp);
d3442bc7 556
98e553d2
MC
557 if (b->callback != NULL || b->callback_ex != NULL)
558 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
559 cmd, 0, ret, NULL);
560
561 return ret;
0f113f3e 562}
d3442bc7 563
0f113f3e
MC
564/*
565 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
95d29597 566 * do; but those macros have inappropriate return type, and for interfacing
0f113f3e
MC
567 * from other programming languages, C macros aren't much of a help anyway.
568 */
95d29597 569size_t BIO_ctrl_pending(BIO *bio)
0f113f3e
MC
570{
571 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
572}
95d29597
BM
573
574size_t BIO_ctrl_wpending(BIO *bio)
0f113f3e
MC
575{
576 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
577}
95d29597 578
d02b48c6 579/* put the 'bio' on the end of b's list of operators */
6b691a5c 580BIO *BIO_push(BIO *b, BIO *bio)
0f113f3e
MC
581{
582 BIO *lb;
583
584 if (b == NULL)
26a7d938 585 return bio;
0f113f3e
MC
586 lb = b;
587 while (lb->next_bio != NULL)
588 lb = lb->next_bio;
589 lb->next_bio = bio;
590 if (bio != NULL)
591 bio->prev_bio = lb;
592 /* called to do internal processing */
593 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
26a7d938 594 return b;
0f113f3e 595}
d02b48c6
RE
596
597/* Remove the first and return the rest */
6b691a5c 598BIO *BIO_pop(BIO *b)
0f113f3e
MC
599{
600 BIO *ret;
d02b48c6 601
0f113f3e 602 if (b == NULL)
26a7d938 603 return NULL;
0f113f3e 604 ret = b->next_bio;
d02b48c6 605
0f113f3e 606 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
5d780bab 607
0f113f3e
MC
608 if (b->prev_bio != NULL)
609 b->prev_bio->next_bio = b->next_bio;
610 if (b->next_bio != NULL)
611 b->next_bio->prev_bio = b->prev_bio;
d02b48c6 612
0f113f3e
MC
613 b->next_bio = NULL;
614 b->prev_bio = NULL;
26a7d938 615 return ret;
0f113f3e 616}
d02b48c6 617
6b691a5c 618BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
0f113f3e
MC
619{
620 BIO *b, *last;
621
622 b = last = bio;
623 for (;;) {
624 if (!BIO_should_retry(b))
625 break;
626 last = b;
627 b = b->next_bio;
628 if (b == NULL)
629 break;
630 }
631 if (reason != NULL)
632 *reason = last->retry_reason;
26a7d938 633 return last;
0f113f3e 634}
d02b48c6 635
6b691a5c 636int BIO_get_retry_reason(BIO *bio)
0f113f3e 637{
26a7d938 638 return bio->retry_reason;
0f113f3e 639}
d02b48c6 640
a146ae55
MC
641void BIO_set_retry_reason(BIO *bio, int reason)
642{
643 bio->retry_reason = reason;
644}
645
6b691a5c 646BIO *BIO_find_type(BIO *bio, int type)
0f113f3e
MC
647{
648 int mt, mask;
649
cc99bfa7 650 if (bio == NULL)
0f113f3e
MC
651 return NULL;
652 mask = type & 0xff;
653 do {
654 if (bio->method != NULL) {
655 mt = bio->method->type;
656
657 if (!mask) {
658 if (mt & type)
26a7d938 659 return bio;
0f113f3e 660 } else if (mt == type)
26a7d938 661 return bio;
0f113f3e
MC
662 }
663 bio = bio->next_bio;
664 } while (bio != NULL);
26a7d938 665 return NULL;
0f113f3e 666}
d02b48c6 667
cfd3bb17 668BIO *BIO_next(BIO *b)
0f113f3e 669{
cc99bfa7 670 if (b == NULL)
0f113f3e
MC
671 return NULL;
672 return b->next_bio;
673}
cfd3bb17 674
a146ae55
MC
675void BIO_set_next(BIO *b, BIO *next)
676{
677 b->next_bio = next;
678}
679
6b691a5c 680void BIO_free_all(BIO *bio)
0f113f3e
MC
681{
682 BIO *b;
683 int ref;
684
685 while (bio != NULL) {
686 b = bio;
687 ref = b->references;
688 bio = bio->next_bio;
689 BIO_free(b);
690 /* Since ref count > 1, don't free anyone else. */
691 if (ref > 1)
692 break;
693 }
694}
d02b48c6 695
6b691a5c 696BIO *BIO_dup_chain(BIO *in)
0f113f3e
MC
697{
698 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
699
700 for (bio = in; bio != NULL; bio = bio->next_bio) {
701 if ((new_bio = BIO_new(bio->method)) == NULL)
702 goto err;
703 new_bio->callback = bio->callback;
98e553d2 704 new_bio->callback_ex = bio->callback_ex;
0f113f3e
MC
705 new_bio->cb_arg = bio->cb_arg;
706 new_bio->init = bio->init;
707 new_bio->shutdown = bio->shutdown;
708 new_bio->flags = bio->flags;
709
710 /* This will let SSL_s_sock() work with stdin/stdout */
711 new_bio->num = bio->num;
712
713 if (!BIO_dup_state(bio, (char *)new_bio)) {
714 BIO_free(new_bio);
715 goto err;
716 }
717
718 /* copy app data */
719 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
aec54108
MC
720 &bio->ex_data)) {
721 BIO_free(new_bio);
0f113f3e 722 goto err;
aec54108 723 }
0f113f3e
MC
724
725 if (ret == NULL) {
726 eoc = new_bio;
727 ret = eoc;
728 } else {
729 BIO_push(eoc, new_bio);
730 eoc = new_bio;
731 }
732 }
26a7d938 733 return ret;
0f113f3e 734 err:
aec54108
MC
735 BIO_free_all(ret);
736
26a7d938 737 return NULL;
0f113f3e 738}
d02b48c6 739
6b691a5c 740void BIO_copy_next_retry(BIO *b)
0f113f3e
MC
741{
742 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
743 b->retry_reason = b->next_bio->retry_reason;
744}
d02b48c6 745
dd9d233e 746int BIO_set_ex_data(BIO *bio, int idx, void *data)
0f113f3e 747{
26a7d938 748 return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
0f113f3e 749}
58964a49 750
dd9d233e 751void *BIO_get_ex_data(BIO *bio, int idx)
0f113f3e 752{
26a7d938 753 return CRYPTO_get_ex_data(&(bio->ex_data), idx);
0f113f3e 754}
58964a49 755
b8b12aad 756uint64_t BIO_number_read(BIO *bio)
c3ed3b6e 757{
0f113f3e
MC
758 if (bio)
759 return bio->num_read;
760 return 0;
c3ed3b6e
DSH
761}
762
b8b12aad 763uint64_t BIO_number_written(BIO *bio)
c3ed3b6e 764{
0f113f3e
MC
765 if (bio)
766 return bio->num_write;
767 return 0;
c3ed3b6e 768}
ff234405 769
1ee7b8b9
MC
770void bio_free_ex_data(BIO *bio)
771{
772 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
773}
ff234405
MC
774
775void bio_cleanup(void)
776{
777#ifndef OPENSSL_NO_SOCK
778 bio_sock_cleanup_int();
779 CRYPTO_THREAD_lock_free(bio_lookup_lock);
780 bio_lookup_lock = NULL;
5a7ad1f0 781#endif
8b8d963d
RS
782 CRYPTO_THREAD_lock_free(bio_type_lock);
783 bio_type_lock = NULL;
ff234405 784}