]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bio_lib.c
Fix error return values from BIO_ctrl_(w)pending()
[thirdparty/openssl.git] / crypto / bio / bio_lib.c
CommitLineData
b1322259 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
09abbca1 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
0800318a
TM
10#define OPENSSL_SUPPRESS_DEPRECATED
11
d02b48c6
RE
12#include <stdio.h>
13#include <errno.h>
ec577822 14#include <openssl/crypto.h>
706457b7 15#include "bio_local.h"
58964a49 16
98e553d2
MC
17/*
18 * Helper macro for the callback to determine whether an operator expects a
19 * len parameter or not
20 */
dc88a039
DDO
21#define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
22 || (o) == BIO_CB_GETS)
98e553d2 23
0800318a
TM
24#ifndef OPENSSL_NO_DEPRECATED_3_0
25# define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL)
26#else
27# define HAS_CALLBACK(b) ((b)->callback_ex != NULL)
28#endif
98e553d2
MC
29/*
30 * Helper function to work out whether to call the new style callback or the old
31 * one, and translate between the two.
32 *
33 * This has a long return type for consistency with the old callback. Similarly
34 * for the "long" used for "inret"
35 */
36static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
dc88a039
DDO
37 int argi, long argl, long inret,
38 size_t *processed)
98e553d2 39{
0800318a
TM
40 long ret = inret;
41#ifndef OPENSSL_NO_DEPRECATED_3_0
98e553d2
MC
42 int bareoper;
43
c911e5da 44 if (b->callback_ex != NULL)
0800318a 45#endif
98e553d2 46 return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
98e553d2 47
0800318a 48#ifndef OPENSSL_NO_DEPRECATED_3_0
98e553d2
MC
49 /* Strip off any BIO_CB_RETURN flag */
50 bareoper = oper & ~BIO_CB_RETURN;
51
52 /*
53 * We have an old style callback, so we will have to do nasty casts and
54 * check for overflows.
55 */
56 if (HAS_LEN_OPER(bareoper)) {
57 /* In this case |len| is set, and should be used instead of |argi| */
58 if (len > INT_MAX)
59 return -1;
60
61 argi = (int)len;
c911e5da 62 }
98e553d2 63
d97ce8d9 64 if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
c911e5da
BE
65 if (*processed > INT_MAX)
66 return -1;
67 inret = *processed;
98e553d2
MC
68 }
69
70 ret = b->callback(b, oper, argp, argi, argl, inret);
71
d97ce8d9 72 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
98e553d2
MC
73 *processed = (size_t)ret;
74 ret = 1;
75 }
0800318a 76#endif
98e553d2
MC
77 return ret;
78}
79
b0ee1de9 80BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
0f113f3e 81{
9d7bfb14 82 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
0f113f3e 83
9d7bfb14 84 if (bio == NULL) {
9311d0c4 85 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
26a7d938 86 return NULL;
0f113f3e 87 }
d02b48c6 88
b0ee1de9 89 bio->libctx = libctx;
0f113f3e 90 bio->method = method;
0f113f3e 91 bio->shutdown = 1;
0f113f3e 92 bio->references = 1;
9d7bfb14 93
25a807bc 94 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
9d7bfb14 95 goto err;
fb46be03
AG
96
97 bio->lock = CRYPTO_THREAD_lock_new();
98 if (bio->lock == NULL) {
9311d0c4 99 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
fb46be03 100 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
9d7bfb14 101 goto err;
fb46be03
AG
102 }
103
9d7bfb14 104 if (method->create != NULL && !method->create(bio)) {
9311d0c4 105 ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
9d7bfb14
F
106 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
107 CRYPTO_THREAD_lock_free(bio->lock);
108 goto err;
fb46be03 109 }
7f55808f
RL
110 if (method->create == NULL)
111 bio->init = 1;
fb46be03 112
9d7bfb14
F
113 return bio;
114
115err:
116 OPENSSL_free(bio);
117 return NULL;
0f113f3e 118}
d02b48c6 119
b0ee1de9
MC
120BIO *BIO_new(const BIO_METHOD *method)
121{
122 return BIO_new_ex(NULL, method);
123}
124
6b691a5c 125int BIO_free(BIO *a)
0f113f3e 126{
98e553d2 127 int ret;
d02b48c6 128
0f113f3e 129 if (a == NULL)
fb46be03
AG
130 return 0;
131
2f545ae4 132 if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
fb46be03 133 return 0;
d02b48c6 134
f3f1cf84 135 REF_PRINT_COUNT("BIO", a);
98e553d2 136 if (ret > 0)
fb46be03 137 return 1;
98e553d2
MC
138 REF_ASSERT_ISNT(ret < 0);
139
0800318a 140 if (HAS_CALLBACK(a)) {
98e553d2
MC
141 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
142 if (ret <= 0)
d8f6c533 143 return 0;
98e553d2 144 }
d02b48c6 145
a14a740d
F
146 if ((a->method != NULL) && (a->method->destroy != NULL))
147 a->method->destroy(a);
148
0f113f3e 149 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
58964a49 150
fb46be03
AG
151 CRYPTO_THREAD_lock_free(a->lock);
152
0f113f3e 153 OPENSSL_free(a);
fb46be03
AG
154
155 return 1;
0f113f3e 156}
d02b48c6 157
a146ae55
MC
158void BIO_set_data(BIO *a, void *ptr)
159{
160 a->ptr = ptr;
161}
162
163void *BIO_get_data(BIO *a)
164{
165 return a->ptr;
166}
167
168void BIO_set_init(BIO *a, int init)
169{
170 a->init = init;
171}
172
173int BIO_get_init(BIO *a)
174{
175 return a->init;
176}
177
178void BIO_set_shutdown(BIO *a, int shut)
179{
180 a->shutdown = shut;
181}
182
183int BIO_get_shutdown(BIO *a)
184{
185 return a->shutdown;
186}
187
371acb22 188void BIO_vfree(BIO *a)
0f113f3e
MC
189{
190 BIO_free(a);
191}
371acb22 192
fb46be03
AG
193int BIO_up_ref(BIO *a)
194{
195 int i;
196
2f545ae4 197 if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
fb46be03
AG
198 return 0;
199
200 REF_PRINT_COUNT("BIO", a);
201 REF_ASSERT_ISNT(i < 2);
dc88a039 202 return i > 1;
fb46be03
AG
203}
204
7806f3dd 205void BIO_clear_flags(BIO *b, int flags)
0f113f3e
MC
206{
207 b->flags &= ~flags;
208}
209
210int BIO_test_flags(const BIO *b, int flags)
211{
212 return (b->flags & flags);
213}
214
215void BIO_set_flags(BIO *b, int flags)
216{
217 b->flags |= flags;
218}
219
0800318a 220#ifndef OPENSSL_NO_DEPRECATED_3_0
d07aee2c
MC
221BIO_callback_fn BIO_get_callback(const BIO *b)
222{
0f113f3e
MC
223 return b->callback;
224}
225
d07aee2c 226void BIO_set_callback(BIO *b, BIO_callback_fn cb)
0f113f3e
MC
227{
228 b->callback = cb;
229}
0800318a 230#endif
7806f3dd 231
d07aee2c
MC
232BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
233{
234 return b->callback_ex;
235}
236
237void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
238{
239 b->callback_ex = cb;
240}
241
7806f3dd 242void BIO_set_callback_arg(BIO *b, char *arg)
0f113f3e
MC
243{
244 b->cb_arg = arg;
245}
7806f3dd 246
0f113f3e
MC
247char *BIO_get_callback_arg(const BIO *b)
248{
249 return b->cb_arg;
250}
7806f3dd 251
0f113f3e
MC
252const char *BIO_method_name(const BIO *b)
253{
254 return b->method->name;
255}
7806f3dd
NL
256
257int BIO_method_type(const BIO *b)
0f113f3e
MC
258{
259 return b->method->type;
260}
7806f3dd 261
bb5310be
MC
262/*
263 * This is essentially the same as BIO_read_ex() except that it allows
7bf79e33
MC
264 * 0 or a negative value to indicate failure (retryable or not) in the return.
265 * This is for compatibility with the old style BIO_read(), where existing code
266 * may make assumptions about the return value that it might get.
bb5310be 267 */
d62bf89c 268static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
d07aee2c
MC
269{
270 int ret;
d02b48c6 271
dc88a039 272 if (b == NULL) {
ed4a9b15 273 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
dc88a039
DDO
274 return -1;
275 }
276 if (b->method == NULL || b->method->bread == NULL) {
9311d0c4 277 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
bb5310be 278 return -2;
0f113f3e 279 }
d02b48c6 280
0800318a 281 if (HAS_CALLBACK(b) &&
7bf79e33 282 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
c911e5da 283 NULL)) <= 0))
d07aee2c 284 return ret;
d02b48c6 285
0f113f3e 286 if (!b->init) {
9311d0c4 287 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
dc88a039 288 return -1;
0f113f3e 289 }
d02b48c6 290
d62bf89c 291 ret = b->method->bread(b, data, dlen, readbytes);
dfeab068 292
d07aee2c 293 if (ret > 0)
82cb311f 294 b->num_read += (uint64_t)*readbytes;
d07aee2c 295
0800318a 296 if (HAS_CALLBACK(b))
42c60460 297 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
d62bf89c 298 dlen, 0, 0L, ret, readbytes);
d02b48c6 299
fbba62f6 300 /* Shouldn't happen */
d62bf89c 301 if (ret > 0 && *readbytes > dlen) {
9311d0c4 302 ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
fbba62f6 303 return -1;
7bf79e33 304 }
fbba62f6 305
d07aee2c 306 return ret;
0f113f3e 307}
d02b48c6 308
7bf79e33 309int BIO_read(BIO *b, void *data, int dlen)
0f113f3e 310{
d62bf89c 311 size_t readbytes;
3befffa3
MC
312 int ret;
313
7bf79e33 314 if (dlen < 0)
3befffa3
MC
315 return 0;
316
d62bf89c 317 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
3befffa3
MC
318
319 if (ret > 0) {
4e3973b4 320 /* *readbytes should always be <= dlen */
d62bf89c 321 ret = (int)readbytes;
3befffa3
MC
322 }
323
324 return ret;
325}
326
d62bf89c 327int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
bb5310be 328{
dc88a039 329 return bio_read_intern(b, data, dlen, readbytes) > 0;
bb5310be
MC
330}
331
7bf79e33 332static int bio_write_intern(BIO *b, const void *data, size_t dlen,
42c60460 333 size_t *written)
3befffa3 334{
5d43bfa7 335 size_t local_written;
3befffa3 336 int ret;
58964a49 337
5d43bfa7
DDO
338 if (written != NULL)
339 *written = 0;
f505161e
SL
340 /*
341 * b == NULL is not an error but just means that zero bytes are written.
342 * Do not raise an error here.
343 */
344 if (b == NULL)
345 return 0;
346
dc88a039 347 if (b->method == NULL || b->method->bwrite == NULL) {
9311d0c4 348 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
bb5310be 349 return -2;
0f113f3e 350 }
d02b48c6 351
0800318a 352 if (HAS_CALLBACK(b) &&
7bf79e33 353 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
c911e5da 354 NULL)) <= 0))
3befffa3 355 return ret;
d02b48c6 356
0f113f3e 357 if (!b->init) {
9311d0c4 358 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
dc88a039 359 return -1;
0f113f3e 360 }
d02b48c6 361
5d43bfa7 362 ret = b->method->bwrite(b, data, dlen, &local_written);
dfeab068 363
3befffa3 364 if (ret > 0)
5d43bfa7 365 b->num_write += (uint64_t)local_written;
d02b48c6 366
0800318a 367 if (HAS_CALLBACK(b))
42c60460 368 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
5d43bfa7 369 dlen, 0, 0L, ret, &local_written);
3befffa3 370
5d43bfa7
DDO
371 if (written != NULL)
372 *written = local_written;
3befffa3 373 return ret;
0f113f3e 374}
d02b48c6 375
7bf79e33 376int BIO_write(BIO *b, const void *data, int dlen)
bb5310be
MC
377{
378 size_t written;
379 int ret;
380
5d43bfa7 381 if (dlen <= 0)
bb5310be
MC
382 return 0;
383
7bf79e33 384 ret = bio_write_intern(b, data, (size_t)dlen, &written);
bb5310be
MC
385
386 if (ret > 0) {
5d43bfa7 387 /* written should always be <= dlen */
bb5310be
MC
388 ret = (int)written;
389 }
390
391 return ret;
392}
393
7bf79e33 394int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
bb5310be 395{
bb19b9d4
DDO
396 return bio_write_intern(b, data, dlen, written) > 0
397 || (b != NULL && dlen == 0); /* order is important for *written */
bb5310be
MC
398}
399
e0c4e43e
HL
400int BIO_sendmmsg(BIO *b, BIO_MSG *msg,
401 size_t stride, size_t num_msg, uint64_t flags,
402 size_t *msgs_processed)
403{
404 size_t ret;
405 BIO_MMSG_CB_ARGS args;
406
407 if (b == NULL) {
408 *msgs_processed = 0;
409 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
410 return 0;
411 }
412
413 if (b->method == NULL || b->method->bsendmmsg == NULL) {
414 *msgs_processed = 0;
415 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
416 return 0;
417 }
418
419 if (HAS_CALLBACK(b)) {
420 args.msg = msg;
421 args.stride = stride;
422 args.num_msg = num_msg;
423 args.flags = flags;
424 args.msgs_processed = msgs_processed;
425
426 ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args,
427 0, 0, 0, 0, NULL);
428 if (ret == 0)
429 return 0;
430 }
431
432 if (!b->init) {
433 *msgs_processed = 0;
434 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
435 return 0;
436 }
437
438 ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed);
439
440 if (HAS_CALLBACK(b))
441 ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN,
442 (void *)&args, ret, 0, 0, 0, NULL);
443
444 return ret;
445}
446
447int BIO_recvmmsg(BIO *b, BIO_MSG *msg,
448 size_t stride, size_t num_msg, uint64_t flags,
449 size_t *msgs_processed)
450{
451 size_t ret;
452 BIO_MMSG_CB_ARGS args;
453
454 if (b == NULL) {
455 *msgs_processed = 0;
456 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
457 return 0;
458 }
459
460 if (b->method == NULL || b->method->brecvmmsg == NULL) {
461 *msgs_processed = 0;
462 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
463 return 0;
464 }
465
466 if (HAS_CALLBACK(b)) {
467 args.msg = msg;
468 args.stride = stride;
469 args.num_msg = num_msg;
470 args.flags = flags;
471 args.msgs_processed = msgs_processed;
472
473 ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args,
474 0, 0, 0, 0, NULL);
475 if (ret == 0)
476 return 0;
477 }
478
479 if (!b->init) {
480 *msgs_processed = 0;
481 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
482 return 0;
483 }
484
485 ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed);
486
487 if (HAS_CALLBACK(b))
488 ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN,
489 (void *)&args, ret, 0, 0, 0, NULL);
490
491 return ret;
492}
493
4e3973b4 494int BIO_puts(BIO *b, const char *buf)
0f113f3e 495{
98e553d2 496 int ret;
47263ace 497 size_t written = 0;
d02b48c6 498
dc88a039 499 if (b == NULL) {
ed4a9b15 500 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
dc88a039
DDO
501 return -1;
502 }
503 if (b->method == NULL || b->method->bputs == NULL) {
9311d0c4 504 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
98e553d2 505 return -2;
0f113f3e 506 }
d02b48c6 507
0800318a 508 if (HAS_CALLBACK(b)) {
4e3973b4 509 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
98e553d2
MC
510 if (ret <= 0)
511 return ret;
512 }
d02b48c6 513
0f113f3e 514 if (!b->init) {
9311d0c4 515 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
dc88a039 516 return -1;
0f113f3e 517 }
d02b48c6 518
4e3973b4 519 ret = b->method->bputs(b, buf);
d02b48c6 520
98e553d2
MC
521 if (ret > 0) {
522 b->num_write += (uint64_t)ret;
523 written = ret;
524 ret = 1;
525 }
526
0800318a 527 if (HAS_CALLBACK(b))
4e3973b4 528 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
98e553d2
MC
529 0L, ret, &written);
530
531 if (ret > 0) {
7bf79e33 532 if (written > INT_MAX) {
9311d0c4 533 ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
98e553d2 534 ret = -1;
7bf79e33 535 } else {
98e553d2 536 ret = (int)written;
7bf79e33 537 }
98e553d2 538 }
7d95ff76 539
98e553d2 540 return ret;
0f113f3e 541}
d02b48c6 542
4e3973b4 543int BIO_gets(BIO *b, char *buf, int size)
0f113f3e 544{
98e553d2 545 int ret;
d62bf89c 546 size_t readbytes = 0;
0f113f3e 547
dc88a039 548 if (b == NULL) {
ed4a9b15 549 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
dc88a039
DDO
550 return -1;
551 }
552 if (b->method == NULL || b->method->bgets == NULL) {
9311d0c4 553 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
26a7d938 554 return -2;
0f113f3e
MC
555 }
556
4e3973b4 557 if (size < 0) {
9311d0c4 558 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
dc88a039 559 return -1;
fbba62f6
MC
560 }
561
0800318a 562 if (HAS_CALLBACK(b)) {
4e3973b4 563 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
98e553d2
MC
564 if (ret <= 0)
565 return ret;
566 }
0f113f3e
MC
567
568 if (!b->init) {
9311d0c4 569 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
dc88a039 570 return -1;
0f113f3e
MC
571 }
572
4e3973b4 573 ret = b->method->bgets(b, buf, size);
98e553d2
MC
574
575 if (ret > 0) {
d62bf89c 576 readbytes = ret;
98e553d2
MC
577 ret = 1;
578 }
579
0800318a 580 if (HAS_CALLBACK(b))
4e3973b4 581 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
d62bf89c 582 0, 0L, ret, &readbytes);
98e553d2
MC
583
584 if (ret > 0) {
fbba62f6 585 /* Shouldn't happen */
4e3973b4 586 if (readbytes > (size_t)size)
98e553d2
MC
587 ret = -1;
588 else
d62bf89c 589 ret = (int)readbytes;
98e553d2 590 }
0f113f3e 591
98e553d2 592 return ret;
0f113f3e
MC
593}
594
18423879
DDO
595int BIO_get_line(BIO *bio, char *buf, int size)
596{
597 int ret = 0;
598 char *ptr = buf;
599
600 if (buf == NULL) {
601 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
602 return -1;
603 }
604 if (size <= 0) {
605 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
606 return -1;
607 }
608 *buf = '\0';
609
610 if (bio == NULL) {
611 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
612 return -1;
613 }
614 if (!bio->init) {
615 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
616 return -1;
617 }
618
619 while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0)
620 if (*ptr++ == '\n')
621 break;
622 *ptr = '\0';
623 return ret > 0 || BIO_eof(bio) ? ptr - buf : ret;
624}
625
0f113f3e
MC
626int BIO_indent(BIO *b, int indent, int max)
627{
628 if (indent < 0)
629 indent = 0;
630 if (indent > max)
631 indent = max;
632 while (indent--)
633 if (BIO_puts(b, " ") != 1)
634 return 0;
635 return 1;
636}
54a656ef 637
6b691a5c 638long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
0f113f3e
MC
639{
640 int i;
d02b48c6 641
0f113f3e 642 i = iarg;
26a7d938 643 return BIO_ctrl(b, cmd, larg, (char *)&i);
0f113f3e 644}
d02b48c6 645
417be660 646void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
0f113f3e 647{
417be660 648 void *p = NULL;
58964a49 649
0f113f3e 650 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
26a7d938 651 return NULL;
0f113f3e 652 else
26a7d938 653 return p;
0f113f3e 654}
58964a49 655
95d29597 656long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
0f113f3e
MC
657{
658 long ret;
d02b48c6 659
398ae823 660 if (b == NULL)
dc88a039 661 return -1;
dc88a039 662 if (b->method == NULL || b->method->ctrl == NULL) {
9311d0c4 663 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
98e553d2 664 return -2;
0f113f3e 665 }
d02b48c6 666
0800318a 667 if (HAS_CALLBACK(b)) {
98e553d2
MC
668 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
669 if (ret <= 0)
670 return ret;
671 }
d02b48c6 672
0f113f3e 673 ret = b->method->ctrl(b, cmd, larg, parg);
d02b48c6 674
0800318a 675 if (HAS_CALLBACK(b))
98e553d2
MC
676 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
677 larg, ret, NULL);
678
679 return ret;
0f113f3e 680}
d02b48c6 681
fce78bd4 682long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
683{
684 long ret;
d3442bc7 685
398ae823 686 if (b == NULL)
ed4a9b15 687 return -2;
dc88a039
DDO
688 if (b->method == NULL || b->method->callback_ctrl == NULL
689 || cmd != BIO_CTRL_SET_CALLBACK) {
9311d0c4 690 ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
26a7d938 691 return -2;
0f113f3e 692 }
d3442bc7 693
0800318a 694 if (HAS_CALLBACK(b)) {
98e553d2
MC
695 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
696 NULL);
697 if (ret <= 0)
698 return ret;
699 }
d3442bc7 700
0f113f3e 701 ret = b->method->callback_ctrl(b, cmd, fp);
d3442bc7 702
0800318a 703 if (HAS_CALLBACK(b))
98e553d2
MC
704 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
705 cmd, 0, ret, NULL);
706
707 return ret;
0f113f3e 708}
d3442bc7 709
0f113f3e
MC
710/*
711 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
95d29597 712 * do; but those macros have inappropriate return type, and for interfacing
0f113f3e
MC
713 * from other programming languages, C macros aren't much of a help anyway.
714 */
95d29597 715size_t BIO_ctrl_pending(BIO *bio)
0f113f3e 716{
e9809f8a
TM
717 long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
718
719 if (ret < 0)
720 ret = 0;
721 return (size_t)ret;
0f113f3e 722}
95d29597
BM
723
724size_t BIO_ctrl_wpending(BIO *bio)
0f113f3e 725{
e9809f8a
TM
726 long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
727
728 if (ret < 0)
729 ret = 0;
730 return (size_t)ret;
0f113f3e 731}
95d29597 732
d02b48c6 733/* put the 'bio' on the end of b's list of operators */
6b691a5c 734BIO *BIO_push(BIO *b, BIO *bio)
0f113f3e
MC
735{
736 BIO *lb;
737
738 if (b == NULL)
26a7d938 739 return bio;
0f113f3e
MC
740 lb = b;
741 while (lb->next_bio != NULL)
742 lb = lb->next_bio;
743 lb->next_bio = bio;
744 if (bio != NULL)
745 bio->prev_bio = lb;
746 /* called to do internal processing */
747 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
26a7d938 748 return b;
0f113f3e 749}
d02b48c6
RE
750
751/* Remove the first and return the rest */
6b691a5c 752BIO *BIO_pop(BIO *b)
0f113f3e
MC
753{
754 BIO *ret;
d02b48c6 755
398ae823 756 if (b == NULL)
26a7d938 757 return NULL;
0f113f3e 758 ret = b->next_bio;
d02b48c6 759
0f113f3e 760 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
5d780bab 761
0f113f3e
MC
762 if (b->prev_bio != NULL)
763 b->prev_bio->next_bio = b->next_bio;
764 if (b->next_bio != NULL)
765 b->next_bio->prev_bio = b->prev_bio;
d02b48c6 766
0f113f3e
MC
767 b->next_bio = NULL;
768 b->prev_bio = NULL;
26a7d938 769 return ret;
0f113f3e 770}
d02b48c6 771
6b691a5c 772BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
0f113f3e
MC
773{
774 BIO *b, *last;
775
776 b = last = bio;
777 for (;;) {
778 if (!BIO_should_retry(b))
779 break;
780 last = b;
781 b = b->next_bio;
782 if (b == NULL)
783 break;
784 }
785 if (reason != NULL)
786 *reason = last->retry_reason;
26a7d938 787 return last;
0f113f3e 788}
d02b48c6 789
6b691a5c 790int BIO_get_retry_reason(BIO *bio)
0f113f3e 791{
26a7d938 792 return bio->retry_reason;
0f113f3e 793}
d02b48c6 794
a146ae55
MC
795void BIO_set_retry_reason(BIO *bio, int reason)
796{
797 bio->retry_reason = reason;
798}
799
6b691a5c 800BIO *BIO_find_type(BIO *bio, int type)
0f113f3e
MC
801{
802 int mt, mask;
803
dc88a039 804 if (bio == NULL) {
ed4a9b15 805 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e 806 return NULL;
dc88a039 807 }
0f113f3e
MC
808 mask = type & 0xff;
809 do {
810 if (bio->method != NULL) {
811 mt = bio->method->type;
812
813 if (!mask) {
814 if (mt & type)
26a7d938 815 return bio;
dc88a039 816 } else if (mt == type) {
26a7d938 817 return bio;
dc88a039 818 }
0f113f3e
MC
819 }
820 bio = bio->next_bio;
821 } while (bio != NULL);
26a7d938 822 return NULL;
0f113f3e 823}
d02b48c6 824
cfd3bb17 825BIO *BIO_next(BIO *b)
0f113f3e 826{
398ae823 827 if (b == NULL)
0f113f3e
MC
828 return NULL;
829 return b->next_bio;
830}
cfd3bb17 831
a146ae55
MC
832void BIO_set_next(BIO *b, BIO *next)
833{
834 b->next_bio = next;
835}
836
6b691a5c 837void BIO_free_all(BIO *bio)
0f113f3e
MC
838{
839 BIO *b;
840 int ref;
841
842 while (bio != NULL) {
843 b = bio;
844 ref = b->references;
845 bio = bio->next_bio;
846 BIO_free(b);
847 /* Since ref count > 1, don't free anyone else. */
848 if (ref > 1)
849 break;
850 }
851}
d02b48c6 852
6b691a5c 853BIO *BIO_dup_chain(BIO *in)
0f113f3e
MC
854{
855 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
856
857 for (bio = in; bio != NULL; bio = bio->next_bio) {
858 if ((new_bio = BIO_new(bio->method)) == NULL)
859 goto err;
0800318a 860#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 861 new_bio->callback = bio->callback;
0800318a 862#endif
98e553d2 863 new_bio->callback_ex = bio->callback_ex;
0f113f3e
MC
864 new_bio->cb_arg = bio->cb_arg;
865 new_bio->init = bio->init;
866 new_bio->shutdown = bio->shutdown;
867 new_bio->flags = bio->flags;
868
869 /* This will let SSL_s_sock() work with stdin/stdout */
870 new_bio->num = bio->num;
871
872 if (!BIO_dup_state(bio, (char *)new_bio)) {
873 BIO_free(new_bio);
874 goto err;
875 }
876
877 /* copy app data */
878 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
aec54108
MC
879 &bio->ex_data)) {
880 BIO_free(new_bio);
0f113f3e 881 goto err;
aec54108 882 }
0f113f3e
MC
883
884 if (ret == NULL) {
885 eoc = new_bio;
886 ret = eoc;
887 } else {
888 BIO_push(eoc, new_bio);
889 eoc = new_bio;
890 }
891 }
26a7d938 892 return ret;
0f113f3e 893 err:
aec54108
MC
894 BIO_free_all(ret);
895
26a7d938 896 return NULL;
0f113f3e 897}
d02b48c6 898
6b691a5c 899void BIO_copy_next_retry(BIO *b)
0f113f3e
MC
900{
901 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
902 b->retry_reason = b->next_bio->retry_reason;
903}
d02b48c6 904
dd9d233e 905int BIO_set_ex_data(BIO *bio, int idx, void *data)
0f113f3e 906{
26a7d938 907 return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
0f113f3e 908}
58964a49 909
8cc86b81 910void *BIO_get_ex_data(const BIO *bio, int idx)
0f113f3e 911{
26a7d938 912 return CRYPTO_get_ex_data(&(bio->ex_data), idx);
0f113f3e 913}
58964a49 914
b8b12aad 915uint64_t BIO_number_read(BIO *bio)
c3ed3b6e 916{
0f113f3e
MC
917 if (bio)
918 return bio->num_read;
919 return 0;
c3ed3b6e
DSH
920}
921
b8b12aad 922uint64_t BIO_number_written(BIO *bio)
c3ed3b6e 923{
0f113f3e
MC
924 if (bio)
925 return bio->num_write;
926 return 0;
c3ed3b6e 927}
ff234405 928
1ee7b8b9
MC
929void bio_free_ex_data(BIO *bio)
930{
931 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
932}
ff234405
MC
933
934void bio_cleanup(void)
935{
936#ifndef OPENSSL_NO_SOCK
937 bio_sock_cleanup_int();
938 CRYPTO_THREAD_lock_free(bio_lookup_lock);
939 bio_lookup_lock = NULL;
5a7ad1f0 940#endif
8b8d963d
RS
941 CRYPTO_THREAD_lock_free(bio_type_lock);
942 bio_type_lock = NULL;
ff234405 943}
e8d0819d
DDO
944
945/* Internal variant of the below BIO_wait() not calling BIOerr() */
e98c7350 946static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
e8d0819d 947{
d8c78e5f 948#ifndef OPENSSL_NO_SOCK
e8d0819d 949 int fd;
d8c78e5f 950#endif
e98c7350 951 long sec_diff;
e8d0819d 952
e98c7350 953 if (max_time == 0) /* no timeout */
e8d0819d
DDO
954 return 1;
955
956#ifndef OPENSSL_NO_SOCK
e98c7350 957 if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
e8d0819d
DDO
958 return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
959#endif
e98c7350 960 /* fall back to polling since no sockets are available */
e8d0819d 961
e98c7350
DDO
962 sec_diff = (long)(max_time - time(NULL)); /* might overflow */
963 if (sec_diff < 0)
964 return 0; /* clearly timeout */
965
966 /* now take a nap at most the given number of milliseconds */
967 if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
968 if (nap_milliseconds > 1000)
969 nap_milliseconds = 1000;
970 } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
971 if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
972 nap_milliseconds = (unsigned int)sec_diff * 1000;
e8d0819d 973 }
e98c7350 974 ossl_sleep(nap_milliseconds);
e8d0819d
DDO
975 return 1;
976}
977
e98c7350 978/*-
e8d0819d 979 * Wait on (typically socket-based) BIO at most until max_time.
e98c7350
DDO
980 * Succeed immediately if max_time == 0.
981 * If sockets are not available support polling: succeed after waiting at most
982 * the number of nap_milliseconds in order to avoid a tight busy loop.
983 * Call BIOerr(...) on timeout or error.
e8d0819d
DDO
984 * Returns -1 on error, 0 on timeout, and 1 on success.
985 */
e98c7350 986int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
e8d0819d 987{
e98c7350 988 int rv = bio_wait(bio, max_time, nap_milliseconds);
e8d0819d
DDO
989
990 if (rv <= 0)
9311d0c4
RL
991 ERR_raise(ERR_LIB_BIO,
992 rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
e8d0819d
DDO
993 return rv;
994}
995
996/*
e98c7350
DDO
997 * Connect via given BIO using BIO_do_connect() until success/timeout/error.
998 * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
999 * For non-blocking and potentially even non-socket BIOs perform polling with
1000 * the given density: between polls sleep nap_milliseconds using BIO_wait()
1001 * in order to avoid a tight busy loop.
e8d0819d
DDO
1002 * Returns -1 on error, 0 on timeout, and 1 on success.
1003 */
e98c7350 1004int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
e8d0819d 1005{
e98c7350 1006 int blocking = timeout <= 0;
e8d0819d
DDO
1007 time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1008 int rv;
1009
1010 if (bio == NULL) {
ed4a9b15 1011 ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
e8d0819d
DDO
1012 return -1;
1013 }
1014
e98c7350
DDO
1015 if (nap_milliseconds < 0)
1016 nap_milliseconds = 100;
1017 BIO_set_nbio(bio, !blocking);
1018
1019 retry:
afecd85d
TM
1020 ERR_set_mark();
1021 rv = BIO_do_connect(bio);
e98c7350
DDO
1022
1023 if (rv <= 0) { /* could be timeout or retryable error or fatal error */
1024 int err = ERR_peek_last_error();
1025 int reason = ERR_GET_REASON(err);
1026 int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
1027
1028 if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
1029 switch (reason) {
1030 case ERR_R_SYS_LIB:
1031 /*
1032 * likely retryable system error occurred, which may be
1033 * EAGAIN (resource temporarily unavailable) some 40 secs after
1034 * calling getaddrinfo(): Temporary failure in name resolution
1035 * or a premature ETIMEDOUT, some 30 seconds after connect()
1036 */
1037 case BIO_R_CONNECT_ERROR:
1038 case BIO_R_NBIO_CONNECT_ERROR:
1039 /* some likely retryable connection error occurred */
1040 (void)BIO_reset(bio); /* often needed to avoid retry failure */
1041 do_retry = 1;
1042 break;
1043 default:
1044 break;
1045 }
1046 }
1047 if (timeout >= 0 && do_retry) {
afecd85d 1048 ERR_pop_to_mark();
e98c7350
DDO
1049 /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
1050 rv = bio_wait(bio, max_time, nap_milliseconds);
e8d0819d
DDO
1051 if (rv > 0)
1052 goto retry;
9311d0c4
RL
1053 ERR_raise(ERR_LIB_BIO,
1054 rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
e8d0819d 1055 } else {
afecd85d 1056 ERR_clear_last_mark();
e8d0819d 1057 rv = -1;
e98c7350 1058 if (err == 0) /* missing error queue entry */
dc88a039
DDO
1059 /* workaround: general error */
1060 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
e8d0819d 1061 }
afecd85d
TM
1062 } else {
1063 ERR_clear_last_mark();
e8d0819d
DDO
1064 }
1065
1066 return rv;
1067}