]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
6738bf14 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <errno.h> | |
ec577822 | 12 | #include <openssl/crypto.h> |
706457b7 | 13 | #include "bio_local.h" |
b39fc560 | 14 | #include "internal/cryptlib.h" |
58964a49 | 15 | |
98e553d2 MC |
16 | |
17 | /* | |
18 | * Helper macro for the callback to determine whether an operator expects a | |
19 | * len parameter or not | |
20 | */ | |
21 | #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE || \ | |
22 | (o) == BIO_CB_GETS) | |
23 | ||
24 | /* | |
25 | * Helper function to work out whether to call the new style callback or the old | |
26 | * one, and translate between the two. | |
27 | * | |
28 | * This has a long return type for consistency with the old callback. Similarly | |
29 | * for the "long" used for "inret" | |
30 | */ | |
31 | static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, | |
32 | int argi, long argl, long inret, size_t *processed) | |
33 | { | |
34 | long ret; | |
35 | int bareoper; | |
36 | ||
c911e5da | 37 | if (b->callback_ex != NULL) |
98e553d2 | 38 | return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); |
98e553d2 MC |
39 | |
40 | /* Strip off any BIO_CB_RETURN flag */ | |
41 | bareoper = oper & ~BIO_CB_RETURN; | |
42 | ||
43 | /* | |
44 | * We have an old style callback, so we will have to do nasty casts and | |
45 | * check for overflows. | |
46 | */ | |
47 | if (HAS_LEN_OPER(bareoper)) { | |
48 | /* In this case |len| is set, and should be used instead of |argi| */ | |
49 | if (len > INT_MAX) | |
50 | return -1; | |
51 | ||
52 | argi = (int)len; | |
c911e5da | 53 | } |
98e553d2 | 54 | |
d97ce8d9 | 55 | if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
c911e5da BE |
56 | if (*processed > INT_MAX) |
57 | return -1; | |
58 | inret = *processed; | |
98e553d2 MC |
59 | } |
60 | ||
61 | ret = b->callback(b, oper, argp, argi, argl, inret); | |
62 | ||
d97ce8d9 | 63 | if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
98e553d2 MC |
64 | *processed = (size_t)ret; |
65 | ret = 1; | |
66 | } | |
67 | ||
68 | return ret; | |
69 | } | |
70 | ||
04f6b0fd | 71 | BIO *BIO_new(const BIO_METHOD *method) |
0f113f3e | 72 | { |
9d7bfb14 | 73 | BIO *bio = OPENSSL_zalloc(sizeof(*bio)); |
0f113f3e | 74 | |
9d7bfb14 | 75 | if (bio == NULL) { |
0f113f3e | 76 | BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); |
26a7d938 | 77 | return NULL; |
0f113f3e | 78 | } |
d02b48c6 | 79 | |
0f113f3e | 80 | bio->method = method; |
0f113f3e | 81 | bio->shutdown = 1; |
0f113f3e | 82 | bio->references = 1; |
9d7bfb14 | 83 | |
25a807bc | 84 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) |
9d7bfb14 | 85 | goto err; |
fb46be03 AG |
86 | |
87 | bio->lock = CRYPTO_THREAD_lock_new(); | |
88 | if (bio->lock == NULL) { | |
9d7bfb14 | 89 | BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); |
fb46be03 | 90 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
9d7bfb14 | 91 | goto err; |
fb46be03 AG |
92 | } |
93 | ||
9d7bfb14 F |
94 | if (method->create != NULL && !method->create(bio)) { |
95 | BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL); | |
96 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | |
97 | CRYPTO_THREAD_lock_free(bio->lock); | |
98 | goto err; | |
fb46be03 | 99 | } |
7f55808f RL |
100 | if (method->create == NULL) |
101 | bio->init = 1; | |
fb46be03 | 102 | |
9d7bfb14 F |
103 | return bio; |
104 | ||
105 | err: | |
106 | OPENSSL_free(bio); | |
107 | return NULL; | |
0f113f3e | 108 | } |
d02b48c6 | 109 | |
6b691a5c | 110 | int BIO_free(BIO *a) |
0f113f3e | 111 | { |
98e553d2 | 112 | int ret; |
d02b48c6 | 113 | |
0f113f3e | 114 | if (a == NULL) |
fb46be03 AG |
115 | return 0; |
116 | ||
2f545ae4 | 117 | if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0) |
fb46be03 | 118 | return 0; |
d02b48c6 | 119 | |
f3f1cf84 | 120 | REF_PRINT_COUNT("BIO", a); |
98e553d2 | 121 | if (ret > 0) |
fb46be03 | 122 | return 1; |
98e553d2 MC |
123 | REF_ASSERT_ISNT(ret < 0); |
124 | ||
125 | if (a->callback != NULL || a->callback_ex != NULL) { | |
126 | ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); | |
127 | if (ret <= 0) | |
128 | return ret; | |
129 | } | |
d02b48c6 | 130 | |
a14a740d F |
131 | if ((a->method != NULL) && (a->method->destroy != NULL)) |
132 | a->method->destroy(a); | |
133 | ||
0f113f3e | 134 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); |
58964a49 | 135 | |
fb46be03 AG |
136 | CRYPTO_THREAD_lock_free(a->lock); |
137 | ||
0f113f3e | 138 | OPENSSL_free(a); |
fb46be03 AG |
139 | |
140 | return 1; | |
0f113f3e | 141 | } |
d02b48c6 | 142 | |
a146ae55 MC |
143 | void BIO_set_data(BIO *a, void *ptr) |
144 | { | |
145 | a->ptr = ptr; | |
146 | } | |
147 | ||
148 | void *BIO_get_data(BIO *a) | |
149 | { | |
150 | return a->ptr; | |
151 | } | |
152 | ||
153 | void BIO_set_init(BIO *a, int init) | |
154 | { | |
155 | a->init = init; | |
156 | } | |
157 | ||
158 | int BIO_get_init(BIO *a) | |
159 | { | |
160 | return a->init; | |
161 | } | |
162 | ||
163 | void BIO_set_shutdown(BIO *a, int shut) | |
164 | { | |
165 | a->shutdown = shut; | |
166 | } | |
167 | ||
168 | int BIO_get_shutdown(BIO *a) | |
169 | { | |
170 | return a->shutdown; | |
171 | } | |
172 | ||
371acb22 | 173 | void BIO_vfree(BIO *a) |
0f113f3e MC |
174 | { |
175 | BIO_free(a); | |
176 | } | |
371acb22 | 177 | |
fb46be03 AG |
178 | int BIO_up_ref(BIO *a) |
179 | { | |
180 | int i; | |
181 | ||
2f545ae4 | 182 | if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0) |
fb46be03 AG |
183 | return 0; |
184 | ||
185 | REF_PRINT_COUNT("BIO", a); | |
186 | REF_ASSERT_ISNT(i < 2); | |
187 | return ((i > 1) ? 1 : 0); | |
188 | } | |
189 | ||
7806f3dd | 190 | void BIO_clear_flags(BIO *b, int flags) |
0f113f3e MC |
191 | { |
192 | b->flags &= ~flags; | |
193 | } | |
194 | ||
195 | int BIO_test_flags(const BIO *b, int flags) | |
196 | { | |
197 | return (b->flags & flags); | |
198 | } | |
199 | ||
200 | void BIO_set_flags(BIO *b, int flags) | |
201 | { | |
202 | b->flags |= flags; | |
203 | } | |
204 | ||
d07aee2c MC |
205 | BIO_callback_fn BIO_get_callback(const BIO *b) |
206 | { | |
0f113f3e MC |
207 | return b->callback; |
208 | } | |
209 | ||
d07aee2c | 210 | void BIO_set_callback(BIO *b, BIO_callback_fn cb) |
0f113f3e MC |
211 | { |
212 | b->callback = cb; | |
213 | } | |
7806f3dd | 214 | |
d07aee2c MC |
215 | BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) |
216 | { | |
217 | return b->callback_ex; | |
218 | } | |
219 | ||
220 | void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb) | |
221 | { | |
222 | b->callback_ex = cb; | |
223 | } | |
224 | ||
7806f3dd | 225 | void BIO_set_callback_arg(BIO *b, char *arg) |
0f113f3e MC |
226 | { |
227 | b->cb_arg = arg; | |
228 | } | |
7806f3dd | 229 | |
0f113f3e MC |
230 | char *BIO_get_callback_arg(const BIO *b) |
231 | { | |
232 | return b->cb_arg; | |
233 | } | |
7806f3dd | 234 | |
0f113f3e MC |
235 | const char *BIO_method_name(const BIO *b) |
236 | { | |
237 | return b->method->name; | |
238 | } | |
7806f3dd NL |
239 | |
240 | int BIO_method_type(const BIO *b) | |
0f113f3e MC |
241 | { |
242 | return b->method->type; | |
243 | } | |
7806f3dd | 244 | |
bb5310be MC |
245 | /* |
246 | * This is essentially the same as BIO_read_ex() except that it allows | |
7bf79e33 MC |
247 | * 0 or a negative value to indicate failure (retryable or not) in the return. |
248 | * This is for compatibility with the old style BIO_read(), where existing code | |
249 | * may make assumptions about the return value that it might get. | |
bb5310be | 250 | */ |
d62bf89c | 251 | static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) |
d07aee2c MC |
252 | { |
253 | int ret; | |
d02b48c6 | 254 | |
0f113f3e | 255 | if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) { |
7bf79e33 | 256 | BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD); |
bb5310be | 257 | return -2; |
0f113f3e | 258 | } |
d02b48c6 | 259 | |
d07aee2c | 260 | if ((b->callback != NULL || b->callback_ex != NULL) && |
7bf79e33 | 261 | ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, |
c911e5da | 262 | NULL)) <= 0)) |
d07aee2c | 263 | return ret; |
d02b48c6 | 264 | |
0f113f3e | 265 | if (!b->init) { |
7bf79e33 | 266 | BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED); |
d07aee2c | 267 | return -2; |
0f113f3e | 268 | } |
d02b48c6 | 269 | |
d62bf89c | 270 | ret = b->method->bread(b, data, dlen, readbytes); |
dfeab068 | 271 | |
d07aee2c | 272 | if (ret > 0) |
82cb311f | 273 | b->num_read += (uint64_t)*readbytes; |
d07aee2c MC |
274 | |
275 | if (b->callback != NULL || b->callback_ex != NULL) | |
42c60460 | 276 | ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, |
d62bf89c | 277 | dlen, 0, 0L, ret, readbytes); |
d02b48c6 | 278 | |
fbba62f6 | 279 | /* Shouldn't happen */ |
d62bf89c | 280 | if (ret > 0 && *readbytes > dlen) { |
7bf79e33 | 281 | BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR); |
fbba62f6 | 282 | return -1; |
7bf79e33 | 283 | } |
fbba62f6 | 284 | |
d07aee2c | 285 | return ret; |
0f113f3e | 286 | } |
d02b48c6 | 287 | |
7bf79e33 | 288 | int BIO_read(BIO *b, void *data, int dlen) |
0f113f3e | 289 | { |
d62bf89c | 290 | size_t readbytes; |
3befffa3 MC |
291 | int ret; |
292 | ||
7bf79e33 | 293 | if (dlen < 0) |
3befffa3 MC |
294 | return 0; |
295 | ||
d62bf89c | 296 | ret = bio_read_intern(b, data, (size_t)dlen, &readbytes); |
3befffa3 MC |
297 | |
298 | if (ret > 0) { | |
4e3973b4 | 299 | /* *readbytes should always be <= dlen */ |
d62bf89c | 300 | ret = (int)readbytes; |
3befffa3 MC |
301 | } |
302 | ||
303 | return ret; | |
304 | } | |
305 | ||
d62bf89c | 306 | int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) |
bb5310be MC |
307 | { |
308 | int ret; | |
309 | ||
d62bf89c | 310 | ret = bio_read_intern(b, data, dlen, readbytes); |
bb5310be MC |
311 | |
312 | if (ret > 0) | |
313 | ret = 1; | |
314 | else | |
315 | ret = 0; | |
316 | ||
317 | return ret; | |
318 | } | |
319 | ||
7bf79e33 | 320 | static int bio_write_intern(BIO *b, const void *data, size_t dlen, |
42c60460 | 321 | size_t *written) |
3befffa3 MC |
322 | { |
323 | int ret; | |
58964a49 | 324 | |
0f113f3e | 325 | if (b == NULL) |
bb5310be | 326 | return 0; |
d02b48c6 | 327 | |
0f113f3e | 328 | if ((b->method == NULL) || (b->method->bwrite == NULL)) { |
7bf79e33 | 329 | BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD); |
bb5310be | 330 | return -2; |
0f113f3e | 331 | } |
d02b48c6 | 332 | |
3befffa3 | 333 | if ((b->callback != NULL || b->callback_ex != NULL) && |
7bf79e33 | 334 | ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, |
c911e5da | 335 | NULL)) <= 0)) |
3befffa3 | 336 | return ret; |
d02b48c6 | 337 | |
0f113f3e | 338 | if (!b->init) { |
7bf79e33 | 339 | BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED); |
3befffa3 | 340 | return -2; |
0f113f3e | 341 | } |
d02b48c6 | 342 | |
7bf79e33 | 343 | ret = b->method->bwrite(b, data, dlen, written); |
dfeab068 | 344 | |
3befffa3 MC |
345 | if (ret > 0) |
346 | b->num_write += (uint64_t)*written; | |
d02b48c6 | 347 | |
3befffa3 | 348 | if (b->callback != NULL || b->callback_ex != NULL) |
42c60460 | 349 | ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, |
7bf79e33 | 350 | dlen, 0, 0L, ret, written); |
3befffa3 MC |
351 | |
352 | return ret; | |
0f113f3e | 353 | } |
d02b48c6 | 354 | |
7bf79e33 | 355 | int BIO_write(BIO *b, const void *data, int dlen) |
bb5310be MC |
356 | { |
357 | size_t written; | |
358 | int ret; | |
359 | ||
7bf79e33 | 360 | if (dlen < 0) |
bb5310be MC |
361 | return 0; |
362 | ||
7bf79e33 | 363 | ret = bio_write_intern(b, data, (size_t)dlen, &written); |
bb5310be MC |
364 | |
365 | if (ret > 0) { | |
4e3973b4 | 366 | /* *written should always be <= dlen */ |
bb5310be MC |
367 | ret = (int)written; |
368 | } | |
369 | ||
370 | return ret; | |
371 | } | |
372 | ||
7bf79e33 | 373 | int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) |
bb5310be MC |
374 | { |
375 | int ret; | |
376 | ||
7bf79e33 | 377 | ret = bio_write_intern(b, data, dlen, written); |
bb5310be MC |
378 | |
379 | if (ret > 0) | |
380 | ret = 1; | |
381 | else | |
382 | ret = 0; | |
383 | ||
384 | return ret; | |
385 | } | |
386 | ||
4e3973b4 | 387 | int BIO_puts(BIO *b, const char *buf) |
0f113f3e | 388 | { |
98e553d2 | 389 | int ret; |
47263ace | 390 | size_t written = 0; |
d02b48c6 | 391 | |
0f113f3e MC |
392 | if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) { |
393 | BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD); | |
98e553d2 | 394 | return -2; |
0f113f3e | 395 | } |
d02b48c6 | 396 | |
98e553d2 | 397 | if (b->callback != NULL || b->callback_ex != NULL) { |
4e3973b4 | 398 | ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); |
98e553d2 MC |
399 | if (ret <= 0) |
400 | return ret; | |
401 | } | |
d02b48c6 | 402 | |
0f113f3e MC |
403 | if (!b->init) { |
404 | BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED); | |
98e553d2 | 405 | return -2; |
0f113f3e | 406 | } |
d02b48c6 | 407 | |
4e3973b4 | 408 | ret = b->method->bputs(b, buf); |
d02b48c6 | 409 | |
98e553d2 MC |
410 | if (ret > 0) { |
411 | b->num_write += (uint64_t)ret; | |
412 | written = ret; | |
413 | ret = 1; | |
414 | } | |
415 | ||
416 | if (b->callback != NULL || b->callback_ex != NULL) | |
4e3973b4 | 417 | ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, |
98e553d2 MC |
418 | 0L, ret, &written); |
419 | ||
420 | if (ret > 0) { | |
7bf79e33 MC |
421 | if (written > INT_MAX) { |
422 | BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG); | |
98e553d2 | 423 | ret = -1; |
7bf79e33 | 424 | } else { |
98e553d2 | 425 | ret = (int)written; |
7bf79e33 | 426 | } |
98e553d2 | 427 | } |
7d95ff76 | 428 | |
98e553d2 | 429 | return ret; |
0f113f3e | 430 | } |
d02b48c6 | 431 | |
4e3973b4 | 432 | int BIO_gets(BIO *b, char *buf, int size) |
0f113f3e | 433 | { |
98e553d2 | 434 | int ret; |
d62bf89c | 435 | size_t readbytes = 0; |
0f113f3e MC |
436 | |
437 | if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) { | |
438 | BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD); | |
26a7d938 | 439 | return -2; |
0f113f3e MC |
440 | } |
441 | ||
4e3973b4 | 442 | if (size < 0) { |
fbba62f6 MC |
443 | BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT); |
444 | return 0; | |
445 | } | |
446 | ||
98e553d2 | 447 | if (b->callback != NULL || b->callback_ex != NULL) { |
4e3973b4 | 448 | ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); |
98e553d2 MC |
449 | if (ret <= 0) |
450 | return ret; | |
451 | } | |
0f113f3e MC |
452 | |
453 | if (!b->init) { | |
454 | BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED); | |
26a7d938 | 455 | return -2; |
0f113f3e MC |
456 | } |
457 | ||
4e3973b4 | 458 | ret = b->method->bgets(b, buf, size); |
98e553d2 MC |
459 | |
460 | if (ret > 0) { | |
d62bf89c | 461 | readbytes = ret; |
98e553d2 MC |
462 | ret = 1; |
463 | } | |
464 | ||
465 | if (b->callback != NULL || b->callback_ex != NULL) | |
4e3973b4 | 466 | ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, |
d62bf89c | 467 | 0, 0L, ret, &readbytes); |
98e553d2 MC |
468 | |
469 | if (ret > 0) { | |
fbba62f6 | 470 | /* Shouldn't happen */ |
4e3973b4 | 471 | if (readbytes > (size_t)size) |
98e553d2 MC |
472 | ret = -1; |
473 | else | |
d62bf89c | 474 | ret = (int)readbytes; |
98e553d2 | 475 | } |
0f113f3e | 476 | |
98e553d2 | 477 | return ret; |
0f113f3e MC |
478 | } |
479 | ||
480 | int BIO_indent(BIO *b, int indent, int max) | |
481 | { | |
482 | if (indent < 0) | |
483 | indent = 0; | |
484 | if (indent > max) | |
485 | indent = max; | |
486 | while (indent--) | |
487 | if (BIO_puts(b, " ") != 1) | |
488 | return 0; | |
489 | return 1; | |
490 | } | |
54a656ef | 491 | |
6b691a5c | 492 | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) |
0f113f3e MC |
493 | { |
494 | int i; | |
d02b48c6 | 495 | |
0f113f3e | 496 | i = iarg; |
26a7d938 | 497 | return BIO_ctrl(b, cmd, larg, (char *)&i); |
0f113f3e | 498 | } |
d02b48c6 | 499 | |
417be660 | 500 | void *BIO_ptr_ctrl(BIO *b, int cmd, long larg) |
0f113f3e | 501 | { |
417be660 | 502 | void *p = NULL; |
58964a49 | 503 | |
0f113f3e | 504 | if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0) |
26a7d938 | 505 | return NULL; |
0f113f3e | 506 | else |
26a7d938 | 507 | return p; |
0f113f3e | 508 | } |
58964a49 | 509 | |
95d29597 | 510 | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) |
0f113f3e MC |
511 | { |
512 | long ret; | |
d02b48c6 | 513 | |
0f113f3e | 514 | if (b == NULL) |
98e553d2 | 515 | return 0; |
d02b48c6 | 516 | |
0f113f3e MC |
517 | if ((b->method == NULL) || (b->method->ctrl == NULL)) { |
518 | BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD); | |
98e553d2 | 519 | return -2; |
0f113f3e | 520 | } |
d02b48c6 | 521 | |
98e553d2 MC |
522 | if (b->callback != NULL || b->callback_ex != NULL) { |
523 | ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); | |
524 | if (ret <= 0) | |
525 | return ret; | |
526 | } | |
d02b48c6 | 527 | |
0f113f3e | 528 | ret = b->method->ctrl(b, cmd, larg, parg); |
d02b48c6 | 529 | |
98e553d2 MC |
530 | if (b->callback != NULL || b->callback_ex != NULL) |
531 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, | |
532 | larg, ret, NULL); | |
533 | ||
534 | return ret; | |
0f113f3e | 535 | } |
d02b48c6 | 536 | |
fce78bd4 | 537 | long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
0f113f3e MC |
538 | { |
539 | long ret; | |
d3442bc7 | 540 | |
0f113f3e | 541 | if (b == NULL) |
26a7d938 | 542 | return 0; |
d3442bc7 | 543 | |
c911e5da BE |
544 | if ((b->method == NULL) || (b->method->callback_ctrl == NULL) |
545 | || (cmd != BIO_CTRL_SET_CALLBACK)) { | |
0f113f3e | 546 | BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD); |
26a7d938 | 547 | return -2; |
0f113f3e | 548 | } |
d3442bc7 | 549 | |
98e553d2 MC |
550 | if (b->callback != NULL || b->callback_ex != NULL) { |
551 | ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, | |
552 | NULL); | |
553 | if (ret <= 0) | |
554 | return ret; | |
555 | } | |
d3442bc7 | 556 | |
0f113f3e | 557 | ret = b->method->callback_ctrl(b, cmd, fp); |
d3442bc7 | 558 | |
98e553d2 MC |
559 | if (b->callback != NULL || b->callback_ex != NULL) |
560 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, | |
561 | cmd, 0, ret, NULL); | |
562 | ||
563 | return ret; | |
0f113f3e | 564 | } |
d3442bc7 | 565 | |
0f113f3e MC |
566 | /* |
567 | * It is unfortunate to duplicate in functions what the BIO_(w)pending macros | |
95d29597 | 568 | * do; but those macros have inappropriate return type, and for interfacing |
0f113f3e MC |
569 | * from other programming languages, C macros aren't much of a help anyway. |
570 | */ | |
95d29597 | 571 | size_t BIO_ctrl_pending(BIO *bio) |
0f113f3e MC |
572 | { |
573 | return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); | |
574 | } | |
95d29597 BM |
575 | |
576 | size_t BIO_ctrl_wpending(BIO *bio) | |
0f113f3e MC |
577 | { |
578 | return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); | |
579 | } | |
95d29597 | 580 | |
d02b48c6 | 581 | /* put the 'bio' on the end of b's list of operators */ |
6b691a5c | 582 | BIO *BIO_push(BIO *b, BIO *bio) |
0f113f3e MC |
583 | { |
584 | BIO *lb; | |
585 | ||
586 | if (b == NULL) | |
26a7d938 | 587 | return bio; |
0f113f3e MC |
588 | lb = b; |
589 | while (lb->next_bio != NULL) | |
590 | lb = lb->next_bio; | |
591 | lb->next_bio = bio; | |
592 | if (bio != NULL) | |
593 | bio->prev_bio = lb; | |
594 | /* called to do internal processing */ | |
595 | BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb); | |
26a7d938 | 596 | return b; |
0f113f3e | 597 | } |
d02b48c6 RE |
598 | |
599 | /* Remove the first and return the rest */ | |
6b691a5c | 600 | BIO *BIO_pop(BIO *b) |
0f113f3e MC |
601 | { |
602 | BIO *ret; | |
d02b48c6 | 603 | |
0f113f3e | 604 | if (b == NULL) |
26a7d938 | 605 | return NULL; |
0f113f3e | 606 | ret = b->next_bio; |
d02b48c6 | 607 | |
0f113f3e | 608 | BIO_ctrl(b, BIO_CTRL_POP, 0, b); |
5d780bab | 609 | |
0f113f3e MC |
610 | if (b->prev_bio != NULL) |
611 | b->prev_bio->next_bio = b->next_bio; | |
612 | if (b->next_bio != NULL) | |
613 | b->next_bio->prev_bio = b->prev_bio; | |
d02b48c6 | 614 | |
0f113f3e MC |
615 | b->next_bio = NULL; |
616 | b->prev_bio = NULL; | |
26a7d938 | 617 | return ret; |
0f113f3e | 618 | } |
d02b48c6 | 619 | |
6b691a5c | 620 | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) |
0f113f3e MC |
621 | { |
622 | BIO *b, *last; | |
623 | ||
624 | b = last = bio; | |
625 | for (;;) { | |
626 | if (!BIO_should_retry(b)) | |
627 | break; | |
628 | last = b; | |
629 | b = b->next_bio; | |
630 | if (b == NULL) | |
631 | break; | |
632 | } | |
633 | if (reason != NULL) | |
634 | *reason = last->retry_reason; | |
26a7d938 | 635 | return last; |
0f113f3e | 636 | } |
d02b48c6 | 637 | |
6b691a5c | 638 | int BIO_get_retry_reason(BIO *bio) |
0f113f3e | 639 | { |
26a7d938 | 640 | return bio->retry_reason; |
0f113f3e | 641 | } |
d02b48c6 | 642 | |
a146ae55 MC |
643 | void BIO_set_retry_reason(BIO *bio, int reason) |
644 | { | |
645 | bio->retry_reason = reason; | |
646 | } | |
647 | ||
6b691a5c | 648 | BIO *BIO_find_type(BIO *bio, int type) |
0f113f3e MC |
649 | { |
650 | int mt, mask; | |
651 | ||
cc99bfa7 | 652 | if (bio == NULL) |
0f113f3e MC |
653 | return NULL; |
654 | mask = type & 0xff; | |
655 | do { | |
656 | if (bio->method != NULL) { | |
657 | mt = bio->method->type; | |
658 | ||
659 | if (!mask) { | |
660 | if (mt & type) | |
26a7d938 | 661 | return bio; |
0f113f3e | 662 | } else if (mt == type) |
26a7d938 | 663 | return bio; |
0f113f3e MC |
664 | } |
665 | bio = bio->next_bio; | |
666 | } while (bio != NULL); | |
26a7d938 | 667 | return NULL; |
0f113f3e | 668 | } |
d02b48c6 | 669 | |
cfd3bb17 | 670 | BIO *BIO_next(BIO *b) |
0f113f3e | 671 | { |
cc99bfa7 | 672 | if (b == NULL) |
0f113f3e MC |
673 | return NULL; |
674 | return b->next_bio; | |
675 | } | |
cfd3bb17 | 676 | |
a146ae55 MC |
677 | void BIO_set_next(BIO *b, BIO *next) |
678 | { | |
679 | b->next_bio = next; | |
680 | } | |
681 | ||
6b691a5c | 682 | void BIO_free_all(BIO *bio) |
0f113f3e MC |
683 | { |
684 | BIO *b; | |
685 | int ref; | |
686 | ||
687 | while (bio != NULL) { | |
688 | b = bio; | |
689 | ref = b->references; | |
690 | bio = bio->next_bio; | |
691 | BIO_free(b); | |
692 | /* Since ref count > 1, don't free anyone else. */ | |
693 | if (ref > 1) | |
694 | break; | |
695 | } | |
696 | } | |
d02b48c6 | 697 | |
6b691a5c | 698 | BIO *BIO_dup_chain(BIO *in) |
0f113f3e MC |
699 | { |
700 | BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; | |
701 | ||
702 | for (bio = in; bio != NULL; bio = bio->next_bio) { | |
703 | if ((new_bio = BIO_new(bio->method)) == NULL) | |
704 | goto err; | |
705 | new_bio->callback = bio->callback; | |
98e553d2 | 706 | new_bio->callback_ex = bio->callback_ex; |
0f113f3e MC |
707 | new_bio->cb_arg = bio->cb_arg; |
708 | new_bio->init = bio->init; | |
709 | new_bio->shutdown = bio->shutdown; | |
710 | new_bio->flags = bio->flags; | |
711 | ||
712 | /* This will let SSL_s_sock() work with stdin/stdout */ | |
713 | new_bio->num = bio->num; | |
714 | ||
715 | if (!BIO_dup_state(bio, (char *)new_bio)) { | |
716 | BIO_free(new_bio); | |
717 | goto err; | |
718 | } | |
719 | ||
720 | /* copy app data */ | |
721 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data, | |
aec54108 MC |
722 | &bio->ex_data)) { |
723 | BIO_free(new_bio); | |
0f113f3e | 724 | goto err; |
aec54108 | 725 | } |
0f113f3e MC |
726 | |
727 | if (ret == NULL) { | |
728 | eoc = new_bio; | |
729 | ret = eoc; | |
730 | } else { | |
731 | BIO_push(eoc, new_bio); | |
732 | eoc = new_bio; | |
733 | } | |
734 | } | |
26a7d938 | 735 | return ret; |
0f113f3e | 736 | err: |
aec54108 MC |
737 | BIO_free_all(ret); |
738 | ||
26a7d938 | 739 | return NULL; |
0f113f3e | 740 | } |
d02b48c6 | 741 | |
6b691a5c | 742 | void BIO_copy_next_retry(BIO *b) |
0f113f3e MC |
743 | { |
744 | BIO_set_flags(b, BIO_get_retry_flags(b->next_bio)); | |
745 | b->retry_reason = b->next_bio->retry_reason; | |
746 | } | |
d02b48c6 | 747 | |
dd9d233e | 748 | int BIO_set_ex_data(BIO *bio, int idx, void *data) |
0f113f3e | 749 | { |
26a7d938 | 750 | return CRYPTO_set_ex_data(&(bio->ex_data), idx, data); |
0f113f3e | 751 | } |
58964a49 | 752 | |
dd9d233e | 753 | void *BIO_get_ex_data(BIO *bio, int idx) |
0f113f3e | 754 | { |
26a7d938 | 755 | return CRYPTO_get_ex_data(&(bio->ex_data), idx); |
0f113f3e | 756 | } |
58964a49 | 757 | |
b8b12aad | 758 | uint64_t BIO_number_read(BIO *bio) |
c3ed3b6e | 759 | { |
0f113f3e MC |
760 | if (bio) |
761 | return bio->num_read; | |
762 | return 0; | |
c3ed3b6e DSH |
763 | } |
764 | ||
b8b12aad | 765 | uint64_t BIO_number_written(BIO *bio) |
c3ed3b6e | 766 | { |
0f113f3e MC |
767 | if (bio) |
768 | return bio->num_write; | |
769 | return 0; | |
c3ed3b6e | 770 | } |
ff234405 | 771 | |
1ee7b8b9 MC |
772 | void bio_free_ex_data(BIO *bio) |
773 | { | |
774 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | |
775 | } | |
ff234405 MC |
776 | |
777 | void bio_cleanup(void) | |
778 | { | |
779 | #ifndef OPENSSL_NO_SOCK | |
780 | bio_sock_cleanup_int(); | |
781 | CRYPTO_THREAD_lock_free(bio_lookup_lock); | |
782 | bio_lookup_lock = NULL; | |
5a7ad1f0 | 783 | #endif |
8b8d963d RS |
784 | CRYPTO_THREAD_lock_free(bio_type_lock); |
785 | bio_type_lock = NULL; | |
ff234405 | 786 | } |
e8d0819d DDO |
787 | |
788 | /* Internal variant of the below BIO_wait() not calling BIOerr() */ | |
789 | static int bio_wait(BIO *bio, time_t max_time, unsigned int milliseconds) | |
790 | { | |
791 | int fd; | |
792 | ||
793 | if (max_time == 0) | |
794 | return 1; | |
795 | ||
796 | #ifndef OPENSSL_NO_SOCK | |
797 | if (BIO_get_fd(bio, &fd) > 0) | |
798 | return BIO_socket_wait(fd, BIO_should_read(bio), max_time); | |
799 | #endif | |
800 | if (milliseconds > 1000) { | |
801 | long sec_diff = (long)(max_time - time(NULL)); /* might overflow */ | |
802 | ||
803 | if (sec_diff <= 0) | |
804 | return 0; /* timeout */ | |
805 | if ((unsigned long)sec_diff < milliseconds / 1000) | |
806 | milliseconds = (unsigned long)sec_diff * 1000; | |
807 | } | |
808 | ossl_sleep(milliseconds); | |
809 | return 1; | |
810 | } | |
811 | ||
812 | /* | |
813 | * Wait on (typically socket-based) BIO at most until max_time. | |
814 | * Succeed immediately if max_time == 0. If sockets are not available succeed | |
815 | * after waiting at most given milliseconds in order to avoid a tight busy loop. | |
816 | * Call BIOerr(...) unless success. | |
817 | * Returns -1 on error, 0 on timeout, and 1 on success. | |
818 | */ | |
819 | int BIO_wait(BIO *bio, time_t max_time, unsigned int milliseconds) | |
820 | { | |
821 | int rv = bio_wait(bio, max_time, milliseconds); | |
822 | ||
823 | if (rv <= 0) | |
824 | BIOerr(0, rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR); | |
825 | return rv; | |
826 | } | |
827 | ||
828 | /* | |
829 | * Connect via given BIO using BIO_do_handshake() until success/timeout/error. | |
830 | * Parameter timeout == 0 means infinite, < 0 leads to immediate timeout error. | |
831 | * Returns -1 on error, 0 on timeout, and 1 on success. | |
832 | */ | |
833 | int BIO_connect_retry(BIO *bio, int timeout) | |
834 | { | |
835 | int blocking = timeout == 0; | |
836 | time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; | |
837 | int rv; | |
838 | ||
839 | if (bio == NULL) { | |
840 | BIOerr(0, ERR_R_PASSED_NULL_PARAMETER); | |
841 | return -1; | |
842 | } | |
843 | ||
844 | if (timeout < 0) { | |
845 | BIOerr(0, BIO_R_CONNECT_TIMEOUT); | |
846 | return 0; | |
847 | } | |
848 | ||
849 | if (!blocking) | |
850 | BIO_set_nbio(bio, 1); | |
851 | ||
852 | retry: /* it does not help here to set SSL_MODE_AUTO_RETRY */ | |
853 | rv = BIO_do_handshake(bio); /* This indirectly calls ERR_clear_error(); */ | |
854 | ||
855 | if (rv <= 0) { | |
856 | if (get_last_sys_error() == ETIMEDOUT) { | |
857 | /* | |
858 | * if blocking, despite blocking BIO, BIO_do_handshake() timed out | |
859 | * when non-blocking, BIO_do_handshake() timed out early | |
860 | * with rv == -1 and get_last_sys_error() == 0 | |
861 | */ | |
862 | ERR_clear_error(); | |
863 | (void)BIO_reset(bio); | |
864 | /* | |
865 | * unless using BIO_reset(), blocking next connect() may crash and | |
866 | * non-blocking next BIO_do_handshake() will fail | |
867 | */ | |
868 | goto retry; | |
869 | } else if (BIO_should_retry(bio)) { | |
870 | /* will not actually wait if timeout == 0 (i.e., blocking BIO) */ | |
871 | rv = bio_wait(bio, max_time, 100 /* milliseconds */); | |
872 | if (rv > 0) | |
873 | goto retry; | |
874 | BIOerr(0, rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR); | |
875 | } else { | |
876 | rv = -1; | |
877 | if (ERR_peek_error() == 0) /* missing error queue entry */ | |
878 | BIOerr(0, BIO_R_CONNECT_ERROR); /* workaround: general error */ | |
879 | } | |
880 | } | |
881 | ||
882 | return rv; | |
883 | } |