]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
da1c088f | 2 | * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. |
f334461f | 3 | * |
09abbca1 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
f334461f MC |
8 | */ |
9 | ||
706457b7 | 10 | #include "bio_local.h" |
176db6dc | 11 | #include "internal/thread_once.h" |
f334461f | 12 | |
a22d1966 | 13 | CRYPTO_REF_COUNT bio_type_count; |
5a7ad1f0 RS |
14 | static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT; |
15 | ||
16 | DEFINE_RUN_ONCE_STATIC(do_bio_type_init) | |
17 | { | |
a22d1966 | 18 | return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START); |
5a7ad1f0 | 19 | } |
8b8d963d | 20 | |
3cb7c5cf | 21 | int BIO_get_new_index(void) |
8b8d963d RS |
22 | { |
23 | int newval; | |
24 | ||
5a7ad1f0 | 25 | if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) { |
e077455e RL |
26 | /* Perhaps the error should be raised in do_bio_type_init()? */ |
27 | ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); | |
5a7ad1f0 RS |
28 | return -1; |
29 | } | |
a22d1966 | 30 | if (!CRYPTO_UP_REF(&bio_type_count, &newval)) |
8b8d963d RS |
31 | return -1; |
32 | return newval; | |
33 | } | |
34 | ||
f334461f MC |
35 | BIO_METHOD *BIO_meth_new(int type, const char *name) |
36 | { | |
37 | BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD)); | |
38 | ||
6dbe4dc4 RS |
39 | if (biom == NULL |
40 | || (biom->name = OPENSSL_strdup(name)) == NULL) { | |
41 | OPENSSL_free(biom); | |
6dbe4dc4 | 42 | return NULL; |
f334461f | 43 | } |
6dbe4dc4 | 44 | biom->type = type; |
f334461f MC |
45 | return biom; |
46 | } | |
47 | ||
48 | void BIO_meth_free(BIO_METHOD *biom) | |
49 | { | |
6dbe4dc4 RS |
50 | if (biom != NULL) { |
51 | OPENSSL_free(biom->name); | |
52 | OPENSSL_free(biom); | |
53 | } | |
f334461f MC |
54 | } |
55 | ||
693be9a2 | 56 | int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int) |
3befffa3 MC |
57 | { |
58 | return biom->bwrite_old; | |
59 | } | |
60 | ||
693be9a2 | 61 | int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t, |
3befffa3 | 62 | size_t *) |
f334461f MC |
63 | { |
64 | return biom->bwrite; | |
65 | } | |
66 | ||
3befffa3 | 67 | /* Conversion for old style bwrite to new style */ |
42c60460 | 68 | int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written) |
3befffa3 MC |
69 | { |
70 | int ret; | |
71 | ||
42c60460 MC |
72 | if (datal > INT_MAX) |
73 | datal = INT_MAX; | |
3befffa3 | 74 | |
42c60460 | 75 | ret = bio->method->bwrite_old(bio, data, (int)datal); |
3befffa3 MC |
76 | |
77 | if (ret <= 0) { | |
78 | *written = 0; | |
79 | return ret; | |
80 | } | |
81 | ||
82 | *written = (size_t)ret; | |
83 | ||
84 | return 1; | |
85 | } | |
86 | ||
f334461f | 87 | int BIO_meth_set_write(BIO_METHOD *biom, |
adb4076a | 88 | int (*bwrite) (BIO *, const char *, int)) |
f334461f | 89 | { |
3befffa3 MC |
90 | biom->bwrite_old = bwrite; |
91 | biom->bwrite = bwrite_conv; | |
92 | return 1; | |
93 | } | |
94 | ||
95 | int BIO_meth_set_write_ex(BIO_METHOD *biom, | |
96 | int (*bwrite) (BIO *, const char *, size_t, size_t *)) | |
97 | { | |
98 | biom->bwrite_old = NULL; | |
adb4076a | 99 | biom->bwrite = bwrite; |
f334461f MC |
100 | return 1; |
101 | } | |
102 | ||
693be9a2 | 103 | int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int) |
d07aee2c MC |
104 | { |
105 | return biom->bread_old; | |
106 | } | |
107 | ||
693be9a2 | 108 | int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *) |
f334461f MC |
109 | { |
110 | return biom->bread; | |
111 | } | |
112 | ||
d07aee2c | 113 | /* Conversion for old style bread to new style */ |
d62bf89c | 114 | int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes) |
d07aee2c MC |
115 | { |
116 | int ret; | |
117 | ||
42c60460 MC |
118 | if (datal > INT_MAX) |
119 | datal = INT_MAX; | |
d07aee2c | 120 | |
42c60460 | 121 | ret = bio->method->bread_old(bio, data, (int)datal); |
d07aee2c MC |
122 | |
123 | if (ret <= 0) { | |
d62bf89c | 124 | *readbytes = 0; |
d07aee2c MC |
125 | return ret; |
126 | } | |
127 | ||
d62bf89c | 128 | *readbytes = (size_t)ret; |
d07aee2c MC |
129 | |
130 | return 1; | |
131 | } | |
132 | ||
f334461f | 133 | int BIO_meth_set_read(BIO_METHOD *biom, |
adb4076a | 134 | int (*bread) (BIO *, char *, int)) |
d07aee2c MC |
135 | { |
136 | biom->bread_old = bread; | |
137 | biom->bread = bread_conv; | |
138 | return 1; | |
139 | } | |
140 | ||
141 | int BIO_meth_set_read_ex(BIO_METHOD *biom, | |
142 | int (*bread) (BIO *, char *, size_t, size_t *)) | |
f334461f | 143 | { |
3befffa3 | 144 | biom->bread_old = NULL; |
adb4076a | 145 | biom->bread = bread; |
f334461f MC |
146 | return 1; |
147 | } | |
148 | ||
693be9a2 | 149 | int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *) |
f334461f MC |
150 | { |
151 | return biom->bputs; | |
152 | } | |
153 | ||
154 | int BIO_meth_set_puts(BIO_METHOD *biom, | |
adb4076a | 155 | int (*bputs) (BIO *, const char *)) |
f334461f | 156 | { |
adb4076a | 157 | biom->bputs = bputs; |
f334461f MC |
158 | return 1; |
159 | } | |
160 | ||
693be9a2 | 161 | int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int) |
f334461f | 162 | { |
a146ae55 | 163 | return biom->bgets; |
f334461f MC |
164 | } |
165 | ||
166 | int BIO_meth_set_gets(BIO_METHOD *biom, | |
adb4076a | 167 | int (*bgets) (BIO *, char *, int)) |
f334461f | 168 | { |
adb4076a | 169 | biom->bgets = bgets; |
f334461f MC |
170 | return 1; |
171 | } | |
172 | ||
693be9a2 | 173 | long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *) |
f334461f MC |
174 | { |
175 | return biom->ctrl; | |
176 | } | |
177 | ||
178 | int BIO_meth_set_ctrl(BIO_METHOD *biom, | |
179 | long (*ctrl) (BIO *, int, long, void *)) | |
180 | { | |
181 | biom->ctrl = ctrl; | |
182 | return 1; | |
183 | } | |
184 | ||
693be9a2 | 185 | int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *) |
f334461f MC |
186 | { |
187 | return biom->create; | |
188 | } | |
189 | ||
190 | int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)) | |
191 | { | |
192 | biom->create = create; | |
193 | return 1; | |
194 | } | |
195 | ||
693be9a2 | 196 | int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *) |
f334461f MC |
197 | { |
198 | return biom->destroy; | |
199 | } | |
200 | ||
201 | int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)) | |
202 | { | |
203 | biom->destroy = destroy; | |
204 | return 1; | |
205 | } | |
206 | ||
693be9a2 | 207 | long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *) |
f334461f MC |
208 | { |
209 | return biom->callback_ctrl; | |
210 | } | |
211 | ||
212 | int BIO_meth_set_callback_ctrl(BIO_METHOD *biom, | |
213 | long (*callback_ctrl) (BIO *, int, | |
fce78bd4 | 214 | BIO_info_cb *)) |
f334461f MC |
215 | { |
216 | biom->callback_ctrl = callback_ctrl; | |
217 | return 1; | |
218 | } | |
e0c4e43e HL |
219 | |
220 | int BIO_meth_set_sendmmsg(BIO_METHOD *biom, | |
221 | int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) | |
222 | { | |
223 | biom->bsendmmsg = bsendmmsg; | |
224 | return 1; | |
225 | } | |
226 | ||
227 | int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { | |
228 | return biom->bsendmmsg; | |
229 | } | |
230 | ||
231 | int BIO_meth_set_recvmmsg(BIO_METHOD *biom, | |
232 | int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) | |
233 | { | |
234 | biom->brecvmmsg = brecvmmsg; | |
235 | return 1; | |
236 | } | |
237 | ||
238 | int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { | |
239 | return biom->brecvmmsg; | |
240 | } |