Coverage Report

Created: 2026-07-16 06:59

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