Coverage Report

Created: 2026-09-12 06:55

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