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