Coverage Report

Created: 2023-06-08 06:40

/src/openssl30/crypto/bio/bio_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 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
50.8M
# 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
22.7k
{
83
22.7k
    BIO *bio = OPENSSL_zalloc(sizeof(*bio));
84
85
22.7k
    if (bio == NULL) {
86
0
        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
87
0
        return NULL;
88
0
    }
89
90
22.7k
    bio->libctx = libctx;
91
22.7k
    bio->method = method;
92
22.7k
    bio->shutdown = 1;
93
22.7k
    bio->references = 1;
94
95
22.7k
    if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
96
0
        goto err;
97
98
22.7k
    bio->lock = CRYPTO_THREAD_lock_new();
99
22.7k
    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
22.7k
    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
22.7k
    if (method->create == NULL)
112
4.39k
        bio->init = 1;
113
114
22.7k
    return bio;
115
116
0
err:
117
0
    OPENSSL_free(bio);
118
0
    return NULL;
119
22.7k
}
120
121
BIO *BIO_new(const BIO_METHOD *method)
122
22.7k
{
123
22.7k
    return BIO_new_ex(NULL, method);
124
22.7k
}
125
126
int BIO_free(BIO *a)
127
34.8k
{
128
34.8k
    int ret;
129
130
34.8k
    if (a == NULL)
131
5.24k
        return 0;
132
133
29.5k
    if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
134
0
        return 0;
135
136
29.5k
    REF_PRINT_COUNT("BIO", a);
137
29.5k
    if (ret > 0)
138
6.79k
        return 1;
139
22.7k
    REF_ASSERT_ISNT(ret < 0);
140
141
22.7k
    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
22.7k
    if ((a->method != NULL) && (a->method->destroy != NULL))
148
18.3k
        a->method->destroy(a);
149
150
22.7k
    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
151
152
22.7k
    CRYPTO_THREAD_lock_free(a->lock);
153
154
22.7k
    OPENSSL_free(a);
155
156
22.7k
    return 1;
157
22.7k
}
158
159
void BIO_set_data(BIO *a, void *ptr)
160
7.42k
{
161
7.42k
    a->ptr = ptr;
162
7.42k
}
163
164
void *BIO_get_data(BIO *a)
165
1.30M
{
166
1.30M
    return a->ptr;
167
1.30M
}
168
169
void BIO_set_init(BIO *a, int init)
170
14.5k
{
171
14.5k
    a->init = init;
172
14.5k
}
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
0
{
191
0
    BIO_free(a);
192
0
}
193
194
int BIO_up_ref(BIO *a)
195
6.79k
{
196
6.79k
    int i;
197
198
6.79k
    if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
199
0
        return 0;
200
201
6.79k
    REF_PRINT_COUNT("BIO", a);
202
6.79k
    REF_ASSERT_ISNT(i < 2);
203
6.79k
    return i > 1;
204
6.79k
}
205
206
void BIO_clear_flags(BIO *b, int flags)
207
1.18M
{
208
1.18M
    b->flags &= ~flags;
209
1.18M
}
210
211
int BIO_test_flags(const BIO *b, int flags)
212
0
{
213
0
    return (b->flags & flags);
214
0
}
215
216
void BIO_set_flags(BIO *b, int flags)
217
0
{
218
0
    b->flags |= flags;
219
0
}
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.36M
{
271
2.36M
    int ret;
272
273
2.36M
    if (b == NULL) {
274
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
275
0
        return -1;
276
0
    }
277
2.36M
    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.36M
    if (HAS_CALLBACK(b) &&
283
2.36M
        ((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.36M
    if (!b->init) {
288
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
289
0
        return -1;
290
0
    }
291
292
2.36M
    ret = b->method->bread(b, data, dlen, readbytes);
293
294
2.36M
    if (ret > 0)
295
2.36M
        b->num_read += (uint64_t)*readbytes;
296
297
2.36M
    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.36M
    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.36M
    return ret;
308
2.36M
}
309
310
int BIO_read(BIO *b, void *data, int dlen)
311
1.18M
{
312
1.18M
    size_t readbytes;
313
1.18M
    int ret;
314
315
1.18M
    if (dlen < 0)
316
0
        return 0;
317
318
1.18M
    ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
319
320
1.18M
    if (ret > 0) {
321
        /* *readbytes should always be <= dlen */
322
1.18M
        ret = (int)readbytes;
323
1.18M
    }
324
325
1.18M
    return ret;
326
1.18M
}
327
328
int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
329
1.18M
{
330
1.18M
    return bio_read_intern(b, data, dlen, readbytes) > 0;
331
1.18M
}
332
333
static int bio_write_intern(BIO *b, const void *data, size_t dlen,
334
                            size_t *written)
335
11.6M
{
336
11.6M
    size_t local_written;
337
11.6M
    int ret;
338
339
11.6M
    if (written != NULL)
340
11.6M
        *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
11.6M
    if (b == NULL)
346
0
        return 0;
347
348
11.6M
    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
11.6M
    if (HAS_CALLBACK(b) &&
354
11.6M
        ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
355
0
                                       NULL)) <= 0))
356
0
        return ret;
357
358
11.6M
    if (!b->init) {
359
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
360
0
        return -1;
361
0
    }
362
363
11.6M
    ret = b->method->bwrite(b, data, dlen, &local_written);
364
365
11.6M
    if (ret > 0)
366
11.6M
        b->num_write += (uint64_t)local_written;
367
368
11.6M
    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
11.6M
    if (written != NULL)
373
11.6M
        *written = local_written;
374
11.6M
    return ret;
375
11.6M
}
376
377
int BIO_write(BIO *b, const void *data, int dlen)
378
11.5M
{
379
11.5M
    size_t written;
380
11.5M
    int ret;
381
382
11.5M
    if (dlen <= 0)
383
0
        return 0;
384
385
11.5M
    ret = bio_write_intern(b, data, (size_t)dlen, &written);
386
387
11.5M
    if (ret > 0) {
388
        /* written should always be <= dlen */
389
11.5M
        ret = (int)written;
390
11.5M
    }
391
392
11.5M
    return ret;
393
11.5M
}
394
395
int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
396
110k
{
397
110k
    return bio_write_intern(b, data, dlen, written) > 0
398
110k
        || (b != NULL && dlen == 0); /* order is important for *written */
399
110k
}
400
401
int BIO_puts(BIO *b, const char *buf)
402
4.39M
{
403
4.39M
    int ret;
404
4.39M
    size_t written = 0;
405
406
4.39M
    if (b == NULL) {
407
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
408
0
        return -1;
409
0
    }
410
4.39M
    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
4.39M
    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
4.39M
    if (!b->init) {
422
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
423
0
        return -1;
424
0
    }
425
426
4.39M
    ret = b->method->bputs(b, buf);
427
428
4.39M
    if (ret > 0) {
429
4.39M
        b->num_write += (uint64_t)ret;
430
4.39M
        written = ret;
431
4.39M
        ret = 1;
432
4.39M
    }
433
434
4.39M
    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
4.39M
    if (ret > 0) {
439
4.39M
        if (written > INT_MAX) {
440
0
            ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
441
0
            ret = -1;
442
4.39M
        } else {
443
4.39M
            ret = (int)written;
444
4.39M
        }
445
4.39M
    }
446
447
4.39M
    return ret;
448
4.39M
}
449
450
int BIO_gets(BIO *b, char *buf, int size)
451
0
{
452
0
    int ret;
453
0
    size_t readbytes = 0;
454
455
0
    if (b == NULL) {
456
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
457
0
        return -1;
458
0
    }
459
0
    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
0
    if (size < 0) {
465
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
466
0
        return -1;
467
0
    }
468
469
0
    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
0
    if (!b->init) {
476
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
477
0
        return -1;
478
0
    }
479
480
0
    ret = b->method->bgets(b, buf, size);
481
482
0
    if (ret > 0) {
483
0
        readbytes = ret;
484
0
        ret = 1;
485
0
    }
486
487
0
    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
0
    if (ret > 0) {
492
        /* Shouldn't happen */
493
0
        if (readbytes > (size_t)size)
494
0
            ret = -1;
495
0
        else
496
0
            ret = (int)readbytes;
497
0
    }
498
499
0
    return ret;
500
0
}
501
502
int BIO_get_line(BIO *bio, char *buf, int size)
503
0
{
504
0
    int ret = 0;
505
0
    char *ptr = buf;
506
507
0
    if (buf == NULL) {
508
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
509
0
        return -1;
510
0
    }
511
0
    if (size <= 0) {
512
0
        ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
513
0
        return -1;
514
0
    }
515
0
    *buf = '\0';
516
517
0
    if (bio == NULL) {
518
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
519
0
        return -1;
520
0
    }
521
0
    if (!bio->init) {
522
0
        ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
523
0
        return -1;
524
0
    }
525
526
0
    while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0)
527
0
        if (*ptr++ == '\n')
528
0
            break;
529
0
    *ptr = '\0';
530
0
    return ret > 0 || BIO_eof(bio) ? ptr - buf : ret;
531
0
}
532
533
int BIO_indent(BIO *b, int indent, int max)
534
542k
{
535
542k
    if (indent < 0)
536
0
        indent = 0;
537
542k
    if (indent > max)
538
0
        indent = max;
539
4.88M
    while (indent--)
540
4.34M
        if (BIO_puts(b, " ") != 1)
541
0
            return 0;
542
542k
    return 1;
543
542k
}
544
545
long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
546
0
{
547
0
    int i;
548
549
0
    i = iarg;
550
0
    return BIO_ctrl(b, cmd, larg, (char *)&i);
551
0
}
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
27.2k
{
565
27.2k
    long ret;
566
567
27.2k
    if (b == NULL)
568
0
        return -1;
569
27.2k
    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
27.2k
    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
27.2k
    ret = b->method->ctrl(b, cmd, larg, parg);
581
582
27.2k
    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
27.2k
    return ret;
587
27.2k
}
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
335
{
651
335
    BIO *lb;
652
653
335
    if (b == NULL)
654
0
        return bio;
655
335
    lb = b;
656
335
    while (lb->next_bio != NULL)
657
0
        lb = lb->next_bio;
658
335
    lb->next_bio = bio;
659
335
    if (bio != NULL)
660
335
        bio->prev_bio = lb;
661
    /* called to do internal processing */
662
335
    BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
663
335
    return b;
664
335
}
665
666
/* Remove the first and return the rest */
667
BIO *BIO_pop(BIO *b)
668
335
{
669
335
    BIO *ret;
670
671
335
    if (b == NULL)
672
0
        return NULL;
673
335
    ret = b->next_bio;
674
675
335
    BIO_ctrl(b, BIO_CTRL_POP, 0, b);
676
677
335
    if (b->prev_bio != NULL)
678
0
        b->prev_bio->next_bio = b->next_bio;
679
335
    if (b->next_bio != NULL)
680
335
        b->next_bio->prev_bio = b->prev_bio;
681
682
335
    b->next_bio = NULL;
683
335
    b->prev_bio = NULL;
684
335
    return ret;
685
335
}
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
62.8k
{
742
62.8k
    if (b == NULL)
743
0
        return NULL;
744
62.8k
    return b->next_bio;
745
62.8k
}
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
0
{
754
0
    BIO *b;
755
0
    int ref;
756
757
0
    while (bio != NULL) {
758
0
        b = bio;
759
0
        ref = b->references;
760
0
        bio = bio->next_bio;
761
0
        BIO_free(b);
762
        /* Since ref count > 1, don't free anyone else. */
763
0
        if (ref > 1)
764
0
            break;
765
0
    }
766
0
}
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
0
{
816
0
    BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
817
0
    b->retry_reason = b->next_bio->retry_reason;
818
0
}
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
2
{
851
2
#ifndef OPENSSL_NO_SOCK
852
2
    bio_sock_cleanup_int();
853
2
    CRYPTO_THREAD_lock_free(bio_lookup_lock);
854
2
    bio_lookup_lock = NULL;
855
2
#endif
856
2
    CRYPTO_THREAD_lock_free(bio_type_lock);
857
2
    bio_type_lock = NULL;
858
2
}
859
860
/* Internal variant of the below BIO_wait() not calling BIOerr() */
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 && fd < FD_SETSIZE)
873
0
        return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
874
0
#endif
875
    /* fall back to polling since no sockets are available */
876
877
0
    sec_diff = (long)(max_time - time(NULL)); /* might overflow */
878
0
    if (sec_diff < 0)
879
0
        return 0; /* clearly timeout */
880
881
    /* now take a nap at most the given number of milliseconds */
882
0
    if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
883
0
        if (nap_milliseconds > 1000)
884
0
            nap_milliseconds = 1000;
885
0
    } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
886
0
        if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
887
0
            nap_milliseconds = (unsigned int)sec_diff * 1000;
888
0
    }
889
0
    ossl_sleep(nap_milliseconds);
890
0
    return 1;
891
0
}
892
893
/*-
894
 * Wait on (typically socket-based) BIO at most until max_time.
895
 * Succeed immediately if max_time == 0.
896
 * If sockets are not available support polling: succeed after waiting at most
897
 * the number of nap_milliseconds in order to avoid a tight busy loop.
898
 * Call BIOerr(...) on timeout or error.
899
 * Returns -1 on error, 0 on timeout, and 1 on success.
900
 */
901
int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
902
0
{
903
0
    int rv = bio_wait(bio, max_time, nap_milliseconds);
904
905
0
    if (rv <= 0)
906
0
        ERR_raise(ERR_LIB_BIO,
907
0
                  rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
908
0
    return rv;
909
0
}
910
911
/*
912
 * Connect via given BIO using BIO_do_connect() until success/timeout/error.
913
 * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
914
 * For non-blocking and potentially even non-socket BIOs perform polling with
915
 * the given density: between polls sleep nap_milliseconds using BIO_wait()
916
 * in order to avoid a tight busy loop.
917
 * Returns -1 on error, 0 on timeout, and 1 on success.
918
 */
919
int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
920
0
{
921
0
    int blocking = timeout <= 0;
922
0
    time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
923
0
    int rv;
924
925
0
    if (bio == NULL) {
926
0
        ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
927
0
        return -1;
928
0
    }
929
930
0
    if (nap_milliseconds < 0)
931
0
        nap_milliseconds = 100;
932
0
    BIO_set_nbio(bio, !blocking);
933
934
0
 retry:
935
0
    ERR_set_mark();
936
0
    rv = BIO_do_connect(bio);
937
938
0
    if (rv <= 0) { /* could be timeout or retryable error or fatal error */
939
0
        int err = ERR_peek_last_error();
940
0
        int reason = ERR_GET_REASON(err);
941
0
        int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
942
943
0
        if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
944
0
            switch (reason) {
945
0
            case ERR_R_SYS_LIB:
946
                /*
947
                 * likely retryable system error occurred, which may be
948
                 * EAGAIN (resource temporarily unavailable) some 40 secs after
949
                 * calling getaddrinfo(): Temporary failure in name resolution
950
                 * or a premature ETIMEDOUT, some 30 seconds after connect()
951
                 */
952
0
            case BIO_R_CONNECT_ERROR:
953
0
            case BIO_R_NBIO_CONNECT_ERROR:
954
                /* some likely retryable connection error occurred */
955
0
                (void)BIO_reset(bio); /* often needed to avoid retry failure */
956
0
                do_retry = 1;
957
0
                break;
958
0
            default:
959
0
                break;
960
0
            }
961
0
        }
962
0
        if (timeout >= 0 && do_retry) {
963
0
            ERR_pop_to_mark();
964
            /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
965
0
            rv = bio_wait(bio, max_time, nap_milliseconds);
966
0
            if (rv > 0)
967
0
                goto retry;
968
0
            ERR_raise(ERR_LIB_BIO,
969
0
                      rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
970
0
        } else {
971
0
            ERR_clear_last_mark();
972
0
            rv = -1;
973
0
            if (err == 0) /* missing error queue entry */
974
                /* workaround: general error */
975
0
                ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
976
0
        }
977
0
    } else {
978
0
        ERR_clear_last_mark();
979
0
    }
980
981
0
    return rv;
982
0
}