/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 | 1 | { |
39 | 1 | BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD)); |
40 | | |
41 | 1 | if (biom == NULL |
42 | 1 | || (biom->name = OPENSSL_strdup(name)) == NULL) { |
43 | 0 | OPENSSL_free(biom); |
44 | 0 | return NULL; |
45 | 0 | } |
46 | 1 | biom->type = type; |
47 | 1 | return biom; |
48 | 1 | } |
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 | | #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 | 400k | { |
74 | 400k | int ret; |
75 | | |
76 | 400k | if (datal > INT_MAX) |
77 | 0 | datal = INT_MAX; |
78 | | |
79 | 400k | ret = bio->method->bwrite_old(bio, data, (int)datal); |
80 | | |
81 | 400k | if (ret <= 0) { |
82 | 0 | *written = 0; |
83 | 0 | return ret; |
84 | 0 | } |
85 | | |
86 | 400k | *written = (size_t)ret; |
87 | | |
88 | 400k | return 1; |
89 | 400k | } |
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 | 1 | { |
102 | 1 | biom->bwrite_old = NULL; |
103 | 1 | biom->bwrite = bwrite; |
104 | 1 | return 1; |
105 | 1 | } |
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 | 20.4M | { |
122 | 20.4M | int ret; |
123 | | |
124 | 20.4M | if (datal > INT_MAX) |
125 | 0 | datal = INT_MAX; |
126 | | |
127 | 20.4M | ret = bio->method->bread_old(bio, data, (int)datal); |
128 | | |
129 | 20.4M | if (ret <= 0) { |
130 | 4.94k | *readbytes = 0; |
131 | 4.94k | return ret; |
132 | 4.94k | } |
133 | | |
134 | 20.3M | *readbytes = (size_t)ret; |
135 | | |
136 | 20.3M | return 1; |
137 | 20.4M | } |
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 | 1 | { |
150 | 1 | biom->bread_old = NULL; |
151 | 1 | biom->bread = bread; |
152 | 1 | return 1; |
153 | 1 | } |
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 | 1 | { |
165 | 1 | biom->bputs = bputs; |
166 | 1 | return 1; |
167 | 1 | } |
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 | 1 | { |
179 | 1 | biom->bgets = bgets; |
180 | 1 | return 1; |
181 | 1 | } |
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 | 1 | { |
193 | 1 | biom->ctrl = ctrl; |
194 | 1 | return 1; |
195 | 1 | } |
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 | 1 | { |
206 | 1 | biom->create = create; |
207 | 1 | return 1; |
208 | 1 | } |
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 | 1 | { |
219 | 1 | biom->destroy = destroy; |
220 | 1 | return 1; |
221 | 1 | } |
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 |