Coverage Report

Created: 2024-05-21 06:33

/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
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
1
{
52
1
    if (biom != NULL) {
53
1
        OPENSSL_free(biom->name);
54
1
        OPENSSL_free(biom);
55
1
    }
56
1
}
57
58
int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int)
59
0
{
60
0
    return biom->bwrite_old;
61
0
}
62
63
int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t,
64
                                                size_t *)
65
0
{
66
0
    return biom->bwrite;
67
0
}
68
69
/* Conversion for old style bwrite to new style */
70
int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
71
0
{
72
0
    int ret;
73
74
0
    if (datal > INT_MAX)
75
0
        datal = INT_MAX;
76
77
0
    ret = bio->method->bwrite_old(bio, data, (int)datal);
78
79
0
    if (ret <= 0) {
80
0
        *written = 0;
81
0
        return ret;
82
0
    }
83
84
0
    *written = (size_t)ret;
85
86
0
    return 1;
87
0
}
88
89
int BIO_meth_set_write(BIO_METHOD *biom,
90
                       int (*bwrite) (BIO *, const char *, int))
91
0
{
92
0
    biom->bwrite_old = bwrite;
93
0
    biom->bwrite = bwrite_conv;
94
0
    return 1;
95
0
}
96
97
int BIO_meth_set_write_ex(BIO_METHOD *biom,
98
                       int (*bwrite) (BIO *, const char *, size_t, size_t *))
99
4
{
100
4
    biom->bwrite_old = NULL;
101
4
    biom->bwrite = bwrite;
102
4
    return 1;
103
4
}
104
105
int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int)
106
0
{
107
0
    return biom->bread_old;
108
0
}
109
110
int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
111
0
{
112
0
    return biom->bread;
113
0
}
114
115
/* Conversion for old style bread to new style */
116
int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
117
0
{
118
0
    int ret;
119
120
0
    if (datal > INT_MAX)
121
0
        datal = INT_MAX;
122
123
0
    ret = bio->method->bread_old(bio, data, (int)datal);
124
125
0
    if (ret <= 0) {
126
0
        *readbytes = 0;
127
0
        return ret;
128
0
    }
129
130
0
    *readbytes = (size_t)ret;
131
132
0
    return 1;
133
0
}
134
135
int BIO_meth_set_read(BIO_METHOD *biom,
136
                      int (*bread) (BIO *, char *, int))
137
0
{
138
0
    biom->bread_old = bread;
139
0
    biom->bread = bread_conv;
140
0
    return 1;
141
0
}
142
143
int BIO_meth_set_read_ex(BIO_METHOD *biom,
144
                         int (*bread) (BIO *, char *, size_t, size_t *))
145
4
{
146
4
    biom->bread_old = NULL;
147
4
    biom->bread = bread;
148
4
    return 1;
149
4
}
150
151
int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
152
0
{
153
0
    return biom->bputs;
154
0
}
155
156
int BIO_meth_set_puts(BIO_METHOD *biom,
157
                      int (*bputs) (BIO *, const char *))
158
4
{
159
4
    biom->bputs = bputs;
160
4
    return 1;
161
4
}
162
163
int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
164
0
{
165
0
    return biom->bgets;
166
0
}
167
168
int BIO_meth_set_gets(BIO_METHOD *biom,
169
                      int (*bgets) (BIO *, char *, int))
170
4
{
171
4
    biom->bgets = bgets;
172
4
    return 1;
173
4
}
174
175
long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
176
0
{
177
0
    return biom->ctrl;
178
0
}
179
180
int BIO_meth_set_ctrl(BIO_METHOD *biom,
181
                      long (*ctrl) (BIO *, int, long, void *))
182
4
{
183
4
    biom->ctrl = ctrl;
184
4
    return 1;
185
4
}
186
187
int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
188
0
{
189
0
    return biom->create;
190
0
}
191
192
int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
193
4
{
194
4
    biom->create = create;
195
4
    return 1;
196
4
}
197
198
int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
199
0
{
200
0
    return biom->destroy;
201
0
}
202
203
int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
204
4
{
205
4
    biom->destroy = destroy;
206
4
    return 1;
207
4
}
208
209
long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
210
0
{
211
0
    return biom->callback_ctrl;
212
0
}
213
214
int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
215
                               long (*callback_ctrl) (BIO *, int,
216
                                                      BIO_info_cb *))
217
0
{
218
0
    biom->callback_ctrl = callback_ctrl;
219
0
    return 1;
220
0
}
221
222
int BIO_meth_set_sendmmsg(BIO_METHOD *biom,
223
                          int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
224
0
{
225
0
    biom->bsendmmsg = bsendmmsg;
226
0
    return 1;
227
0
}
228
229
0
int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
230
0
    return biom->bsendmmsg;
231
0
}
232
233
int BIO_meth_set_recvmmsg(BIO_METHOD *biom,
234
                          int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
235
0
{
236
0
    biom->brecvmmsg = brecvmmsg;
237
0
    return 1;
238
0
}
239
240
0
int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
241
0
    return biom->brecvmmsg;
242
0
}