Coverage Report

Created: 2025-06-13 06:58

/src/openssl/crypto/bio/bio_meth.c
Line
Count
Source (jump to first uncovered line)
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
45
{
39
45
    BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
40
41
45
    if (biom == NULL
42
45
            || (biom->name = OPENSSL_strdup(name)) == NULL) {
43
0
        OPENSSL_free(biom);
44
0
        return NULL;
45
0
    }
46
45
    biom->type = type;
47
45
    return biom;
48
45
}
49
50
void BIO_meth_free(BIO_METHOD *biom)
51
43
{
52
43
    if (biom != NULL) {
53
43
        OPENSSL_free(biom->name);
54
43
        OPENSSL_free(biom);
55
43
    }
56
43
}
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
336M
{
74
336M
    int ret;
75
76
336M
    if (datal > INT_MAX)
77
0
        datal = INT_MAX;
78
79
336M
    ret = bio->method->bwrite_old(bio, data, (int)datal);
80
81
336M
    if (ret <= 0) {
82
0
        *written = 0;
83
0
        return ret;
84
0
    }
85
86
336M
    *written = (size_t)ret;
87
88
336M
    return 1;
89
336M
}
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
45
{
102
45
    biom->bwrite_old = NULL;
103
45
    biom->bwrite = bwrite;
104
45
    return 1;
105
45
}
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
1.42G
{
122
1.42G
    int ret;
123
124
1.42G
    if (datal > INT_MAX)
125
0
        datal = INT_MAX;
126
127
1.42G
    ret = bio->method->bread_old(bio, data, (int)datal);
128
129
1.42G
    if (ret <= 0) {
130
3.24M
        *readbytes = 0;
131
3.24M
        return ret;
132
3.24M
    }
133
134
1.41G
    *readbytes = (size_t)ret;
135
136
1.41G
    return 1;
137
1.42G
}
138
139
int BIO_meth_set_read(BIO_METHOD *biom,
140
                      int (*bread) (BIO *, char *, int))
141
0
{
142
0
    biom->bread_old = bread;
143
0
    biom->bread = bread_conv;
144
0
    return 1;
145
0
}
146
147
int BIO_meth_set_read_ex(BIO_METHOD *biom,
148
                         int (*bread) (BIO *, char *, size_t, size_t *))
149
45
{
150
45
    biom->bread_old = NULL;
151
45
    biom->bread = bread;
152
45
    return 1;
153
45
}
154
155
#ifndef OPENSSL_NO_DEPRECATED_3_5
156
int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
157
0
{
158
0
    return biom->bputs;
159
0
}
160
#endif
161
162
int BIO_meth_set_puts(BIO_METHOD *biom,
163
                      int (*bputs) (BIO *, const char *))
164
45
{
165
45
    biom->bputs = bputs;
166
45
    return 1;
167
45
}
168
169
#ifndef OPENSSL_NO_DEPRECATED_3_5
170
int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
171
0
{
172
0
    return biom->bgets;
173
0
}
174
#endif
175
176
int BIO_meth_set_gets(BIO_METHOD *biom,
177
                      int (*bgets) (BIO *, char *, int))
178
45
{
179
45
    biom->bgets = bgets;
180
45
    return 1;
181
45
}
182
183
#ifndef OPENSSL_NO_DEPRECATED_3_5
184
long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
185
0
{
186
0
    return biom->ctrl;
187
0
}
188
#endif
189
190
int BIO_meth_set_ctrl(BIO_METHOD *biom,
191
                      long (*ctrl) (BIO *, int, long, void *))
192
45
{
193
45
    biom->ctrl = ctrl;
194
45
    return 1;
195
45
}
196
197
#ifndef OPENSSL_NO_DEPRECATED_3_5
198
int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
199
0
{
200
0
    return biom->create;
201
0
}
202
#endif
203
204
int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
205
45
{
206
45
    biom->create = create;
207
45
    return 1;
208
45
}
209
210
#ifndef OPENSSL_NO_DEPRECATED_3_5
211
int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
212
0
{
213
0
    return biom->destroy;
214
0
}
215
#endif
216
217
int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
218
45
{
219
45
    biom->destroy = destroy;
220
45
    return 1;
221
45
}
222
223
#ifndef OPENSSL_NO_DEPRECATED_3_5
224
long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
225
0
{
226
0
    return biom->callback_ctrl;
227
0
}
228
#endif
229
230
int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
231
                               long (*callback_ctrl) (BIO *, int,
232
                                                      BIO_info_cb *))
233
0
{
234
0
    biom->callback_ctrl = callback_ctrl;
235
0
    return 1;
236
0
}
237
238
int BIO_meth_set_sendmmsg(BIO_METHOD *biom,
239
                          int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
240
0
{
241
0
    biom->bsendmmsg = bsendmmsg;
242
0
    return 1;
243
0
}
244
245
#ifndef OPENSSL_NO_DEPRECATED_3_5
246
0
int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
247
0
    return biom->bsendmmsg;
248
0
}
249
#endif
250
251
int BIO_meth_set_recvmmsg(BIO_METHOD *biom,
252
                          int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
253
0
{
254
0
    biom->brecvmmsg = brecvmmsg;
255
0
    return 1;
256
0
}
257
258
#ifndef OPENSSL_NO_DEPRECATED_3_5
259
0
int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
260
0
    return biom->brecvmmsg;
261
0
}
262
#endif