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