Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/bio/bio_meth.c
Line
Count
Source
1
/*
2
 * Copyright 2016-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
#include "bio_local.h"
11
#include "internal/thread_once.h"
12
13
CRYPTO_REF_COUNT bio_type_count;
14
static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
15
16
DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
17
0
{
18
0
    return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START);
19
0
}
20
21
int BIO_get_new_index(void)
22
0
{
23
0
    int newval;
24
25
0
    if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
26
        /* Perhaps the error should be raised in do_bio_type_init()? */
27
0
        ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB);
28
0
        return -1;
29
0
    }
30
0
    if (!CRYPTO_UP_REF(&bio_type_count, &newval))
31
0
        return -1;
32
0
    if (newval > BIO_TYPE_MASK)
33
0
        return -1;
34
0
    return newval;
35
0
}
36
37
BIO_METHOD *BIO_meth_new(int type, const char *name)
38
4
{
39
4
    BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
40
41
4
    if (biom == NULL
42
4
        || (biom->name = OPENSSL_strdup(name)) == NULL) {
43
0
        OPENSSL_free(biom);
44
0
        return NULL;
45
0
    }
46
4
    biom->type = type;
47
4
    return biom;
48
4
}
49
50
void BIO_meth_free(BIO_METHOD *biom)
51
0
{
52
0
    if (biom != NULL) {
53
0
        OPENSSL_free(biom->name);
54
0
        OPENSSL_free(biom);
55
0
    }
56
0
}
57
58
#ifndef OPENSSL_NO_DEPRECATED_3_5
59
int (*BIO_meth_get_write(const BIO_METHOD *biom))(BIO *, const char *, int)
60
0
{
61
0
    return biom->bwrite_old;
62
0
}
63
64
int (*BIO_meth_get_write_ex(const BIO_METHOD *biom))(BIO *, const char *, size_t,
65
    size_t *)
66
0
{
67
0
    return biom->bwrite;
68
0
}
69
#endif
70
71
/* Conversion for old style bwrite to new style */
72
int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
73
0
{
74
0
    int ret;
75
76
0
    if (datal > INT_MAX)
77
0
        datal = INT_MAX;
78
79
0
    ret = bio->method->bwrite_old(bio, data, (int)datal);
80
81
0
    if (ret <= 0) {
82
0
        *written = 0;
83
0
        return ret;
84
0
    }
85
86
0
    *written = (size_t)ret;
87
88
0
    return 1;
89
0
}
90
91
int BIO_meth_set_write(BIO_METHOD *biom,
92
    int (*bwrite)(BIO *, const char *, int))
93
0
{
94
0
    biom->bwrite_old = bwrite;
95
0
    biom->bwrite = bwrite_conv;
96
0
    return 1;
97
0
}
98
99
int BIO_meth_set_write_ex(BIO_METHOD *biom,
100
    int (*bwrite)(BIO *, const char *, size_t, size_t *))
101
4
{
102
4
    biom->bwrite_old = NULL;
103
4
    biom->bwrite = bwrite;
104
4
    return 1;
105
4
}
106
107
#ifndef OPENSSL_NO_DEPRECATED_3_5
108
int (*BIO_meth_get_read(const BIO_METHOD *biom))(BIO *, char *, int)
109
0
{
110
0
    return biom->bread_old;
111
0
}
112
113
int (*BIO_meth_get_read_ex(const BIO_METHOD *biom))(BIO *, char *, size_t, size_t *)
114
0
{
115
0
    return biom->bread;
116
0
}
117
#endif
118
119
/* Conversion for old style bread to new style */
120
int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
121
0
{
122
0
    int ret;
123
124
0
    if (datal == 0) {
125
0
        *readbytes = 0;
126
0
        return 1;
127
0
    }
128
129
0
    if (datal > INT_MAX)
130
0
        datal = INT_MAX;
131
132
0
    ret = bio->method->bread_old(bio, data, (int)datal);
133
134
0
    bio->flags &= ~BIO_FLAGS_AUTO_EOF;
135
0
    if (ret == 0) {
136
0
        if (BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL) == 0)
137
0
            bio->flags |= BIO_FLAGS_AUTO_EOF;
138
0
        *readbytes = 0;
139
0
        return 0;
140
0
    }
141
142
0
    if (ret < 0) {
143
0
        *readbytes = 0;
144
0
        return ret;
145
0
    }
146
147
0
    *readbytes = (size_t)ret;
148
149
0
    return 1;
150
0
}
151
152
int BIO_meth_set_read(BIO_METHOD *biom,
153
    int (*bread)(BIO *, char *, int))
154
0
{
155
0
    biom->bread_old = bread;
156
0
    biom->bread = bread_conv;
157
0
    return 1;
158
0
}
159
160
int BIO_meth_set_read_ex(BIO_METHOD *biom,
161
    int (*bread)(BIO *, char *, size_t, size_t *))
162
4
{
163
4
    biom->bread_old = NULL;
164
4
    biom->bread = bread;
165
4
    return 1;
166
4
}
167
168
#ifndef OPENSSL_NO_DEPRECATED_3_5
169
int (*BIO_meth_get_puts(const BIO_METHOD *biom))(BIO *, const char *)
170
0
{
171
0
    return biom->bputs;
172
0
}
173
#endif
174
175
int BIO_meth_set_puts(BIO_METHOD *biom,
176
    int (*bputs)(BIO *, const char *))
177
4
{
178
4
    biom->bputs = bputs;
179
4
    return 1;
180
4
}
181
182
#ifndef OPENSSL_NO_DEPRECATED_3_5
183
int (*BIO_meth_get_gets(const BIO_METHOD *biom))(BIO *, char *, int)
184
0
{
185
0
    return biom->bgets;
186
0
}
187
#endif
188
189
int BIO_meth_set_gets(BIO_METHOD *biom,
190
    int (*bgets)(BIO *, char *, int))
191
4
{
192
4
    biom->bgets = bgets;
193
4
    return 1;
194
4
}
195
196
#ifndef OPENSSL_NO_DEPRECATED_3_5
197
long (*BIO_meth_get_ctrl(const BIO_METHOD *biom))(BIO *, int, long, void *)
198
0
{
199
0
    return biom->ctrl;
200
0
}
201
#endif
202
203
int BIO_meth_set_ctrl(BIO_METHOD *biom,
204
    long (*ctrl)(BIO *, int, long, void *))
205
4
{
206
4
    biom->ctrl = ctrl;
207
4
    return 1;
208
4
}
209
210
#ifndef OPENSSL_NO_DEPRECATED_3_5
211
int (*BIO_meth_get_create(const BIO_METHOD *biom))(BIO *)
212
0
{
213
0
    return biom->create;
214
0
}
215
#endif
216
217
int BIO_meth_set_create(BIO_METHOD *biom, int (*create)(BIO *))
218
4
{
219
4
    biom->create = create;
220
4
    return 1;
221
4
}
222
223
#ifndef OPENSSL_NO_DEPRECATED_3_5
224
int (*BIO_meth_get_destroy(const BIO_METHOD *biom))(BIO *)
225
0
{
226
0
    return biom->destroy;
227
0
}
228
#endif
229
230
int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy)(BIO *))
231
4
{
232
4
    biom->destroy = destroy;
233
4
    return 1;
234
4
}
235
236
#ifndef OPENSSL_NO_DEPRECATED_3_5
237
long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom))(BIO *, int, BIO_info_cb *)
238
0
{
239
0
    return biom->callback_ctrl;
240
0
}
241
#endif
242
243
int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
244
    long (*callback_ctrl)(BIO *, int,
245
        BIO_info_cb *))
246
0
{
247
0
    biom->callback_ctrl = callback_ctrl;
248
0
    return 1;
249
0
}
250
251
int BIO_meth_set_sendmmsg(BIO_METHOD *biom,
252
    int (*bsendmmsg)(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
253
0
{
254
0
    biom->bsendmmsg = bsendmmsg;
255
0
    return 1;
256
0
}
257
258
#ifndef OPENSSL_NO_DEPRECATED_3_5
259
int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)
260
0
{
261
0
    return biom->bsendmmsg;
262
0
}
263
#endif
264
265
int BIO_meth_set_recvmmsg(BIO_METHOD *biom,
266
    int (*brecvmmsg)(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
267
0
{
268
0
    biom->brecvmmsg = brecvmmsg;
269
0
    return 1;
270
0
}
271
272
#ifndef OPENSSL_NO_DEPRECATED_3_5
273
int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)
274
0
{
275
0
    return biom->brecvmmsg;
276
0
}
277
#endif