Coverage Report

Created: 2026-02-14 07:20

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