Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/bio/bio_meth.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016-2020 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_RWLOCK *bio_type_lock = NULL;
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
    bio_type_lock = CRYPTO_THREAD_lock_new();
19
0
    return bio_type_lock != NULL;
20
0
}
21
22
int BIO_get_new_index(void)
23
0
{
24
0
    static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
25
0
    int newval;
26
27
0
    if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
28
0
        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
29
0
        return -1;
30
0
    }
31
0
    if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
32
0
        return -1;
33
0
    return newval;
34
0
}
35
36
BIO_METHOD *BIO_meth_new(int type, const char *name)
37
45
{
38
45
    BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
39
40
45
    if (biom == NULL
41
45
            || (biom->name = OPENSSL_strdup(name)) == NULL) {
42
0
        OPENSSL_free(biom);
43
0
        ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
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
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
336M
{
72
336M
    int ret;
73
74
336M
    if (datal > INT_MAX)
75
0
        datal = INT_MAX;
76
77
336M
    ret = bio->method->bwrite_old(bio, data, (int)datal);
78
79
336M
    if (ret <= 0) {
80
0
        *written = 0;
81
0
        return ret;
82
0
    }
83
84
336M
    *written = (size_t)ret;
85
86
336M
    return 1;
87
336M
}
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
45
{
100
45
    biom->bwrite_old = NULL;
101
45
    biom->bwrite = bwrite;
102
45
    return 1;
103
45
}
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
1.42G
{
118
1.42G
    int ret;
119
120
1.42G
    if (datal > INT_MAX)
121
0
        datal = INT_MAX;
122
123
1.42G
    ret = bio->method->bread_old(bio, data, (int)datal);
124
125
1.42G
    if (ret <= 0) {
126
3.24M
        *readbytes = 0;
127
3.24M
        return ret;
128
3.24M
    }
129
130
1.41G
    *readbytes = (size_t)ret;
131
132
1.41G
    return 1;
133
1.42G
}
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
45
{
146
45
    biom->bread_old = NULL;
147
45
    biom->bread = bread;
148
45
    return 1;
149
45
}
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
45
{
159
45
    biom->bputs = bputs;
160
45
    return 1;
161
45
}
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
45
{
171
45
    biom->bgets = bgets;
172
45
    return 1;
173
45
}
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
45
{
183
45
    biom->ctrl = ctrl;
184
45
    return 1;
185
45
}
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
45
{
194
45
    biom->create = create;
195
45
    return 1;
196
45
}
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
45
{
205
45
    biom->destroy = destroy;
206
45
    return 1;
207
45
}
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
}