Coverage Report

Created: 2025-06-13 06:58

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