Coverage Report

Created: 2025-12-31 06:58

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