]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
4333b89f | 2 | * Copyright 1995-2021 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 | ||
0800318a TM |
10 | #define OPENSSL_SUPPRESS_DEPRECATED |
11 | ||
d02b48c6 RE |
12 | #include <stdio.h> |
13 | #include <errno.h> | |
ec577822 | 14 | #include <openssl/crypto.h> |
c6be0aa8 | 15 | #include "internal/numbers.h" |
706457b7 | 16 | #include "bio_local.h" |
58964a49 | 17 | |
98e553d2 MC |
18 | /* |
19 | * Helper macro for the callback to determine whether an operator expects a | |
20 | * len parameter or not | |
21 | */ | |
dc88a039 DDO |
22 | #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \ |
23 | || (o) == BIO_CB_GETS) | |
98e553d2 | 24 | |
0800318a TM |
25 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
26 | # define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL) | |
27 | #else | |
28 | # define HAS_CALLBACK(b) ((b)->callback_ex != NULL) | |
29 | #endif | |
98e553d2 MC |
30 | /* |
31 | * Helper function to work out whether to call the new style callback or the old | |
32 | * one, and translate between the two. | |
33 | * | |
34 | * This has a long return type for consistency with the old callback. Similarly | |
35 | * for the "long" used for "inret" | |
36 | */ | |
37 | static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, | |
dc88a039 DDO |
38 | int argi, long argl, long inret, |
39 | size_t *processed) | |
98e553d2 | 40 | { |
0800318a TM |
41 | long ret = inret; |
42 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
98e553d2 MC |
43 | int bareoper; |
44 | ||
c911e5da | 45 | if (b->callback_ex != NULL) |
0800318a | 46 | #endif |
98e553d2 | 47 | return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); |
98e553d2 | 48 | |
0800318a | 49 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
98e553d2 MC |
50 | /* Strip off any BIO_CB_RETURN flag */ |
51 | bareoper = oper & ~BIO_CB_RETURN; | |
52 | ||
53 | /* | |
54 | * We have an old style callback, so we will have to do nasty casts and | |
55 | * check for overflows. | |
56 | */ | |
57 | if (HAS_LEN_OPER(bareoper)) { | |
58 | /* In this case |len| is set, and should be used instead of |argi| */ | |
59 | if (len > INT_MAX) | |
60 | return -1; | |
61 | ||
62 | argi = (int)len; | |
c911e5da | 63 | } |
98e553d2 | 64 | |
d97ce8d9 | 65 | if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
c911e5da BE |
66 | if (*processed > INT_MAX) |
67 | return -1; | |
68 | inret = *processed; | |
98e553d2 MC |
69 | } |
70 | ||
71 | ret = b->callback(b, oper, argp, argi, argl, inret); | |
72 | ||
d97ce8d9 | 73 | if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
98e553d2 MC |
74 | *processed = (size_t)ret; |
75 | ret = 1; | |
76 | } | |
0800318a | 77 | #endif |
98e553d2 MC |
78 | return ret; |
79 | } | |
80 | ||
b0ee1de9 | 81 | BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method) |
0f113f3e | 82 | { |
9d7bfb14 | 83 | BIO *bio = OPENSSL_zalloc(sizeof(*bio)); |
0f113f3e | 84 | |
9d7bfb14 | 85 | if (bio == NULL) { |
9311d0c4 | 86 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
26a7d938 | 87 | return NULL; |
0f113f3e | 88 | } |
d02b48c6 | 89 | |
b0ee1de9 | 90 | bio->libctx = libctx; |
0f113f3e | 91 | bio->method = method; |
0f113f3e | 92 | bio->shutdown = 1; |
0f113f3e | 93 | bio->references = 1; |
9d7bfb14 | 94 | |
25a807bc | 95 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) |
9d7bfb14 | 96 | goto err; |
fb46be03 AG |
97 | |
98 | bio->lock = CRYPTO_THREAD_lock_new(); | |
99 | if (bio->lock == NULL) { | |
9311d0c4 | 100 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
fb46be03 | 101 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
9d7bfb14 | 102 | goto err; |
fb46be03 AG |
103 | } |
104 | ||
9d7bfb14 | 105 | if (method->create != NULL && !method->create(bio)) { |
9311d0c4 | 106 | ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL); |
9d7bfb14 F |
107 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
108 | CRYPTO_THREAD_lock_free(bio->lock); | |
109 | goto err; | |
fb46be03 | 110 | } |
7f55808f RL |
111 | if (method->create == NULL) |
112 | bio->init = 1; | |
fb46be03 | 113 | |
9d7bfb14 F |
114 | return bio; |
115 | ||
116 | err: | |
117 | OPENSSL_free(bio); | |
118 | return NULL; | |
0f113f3e | 119 | } |
d02b48c6 | 120 | |
b0ee1de9 MC |
121 | BIO *BIO_new(const BIO_METHOD *method) |
122 | { | |
123 | return BIO_new_ex(NULL, method); | |
124 | } | |
125 | ||
6b691a5c | 126 | int BIO_free(BIO *a) |
0f113f3e | 127 | { |
98e553d2 | 128 | int ret; |
d02b48c6 | 129 | |
0f113f3e | 130 | if (a == NULL) |
fb46be03 AG |
131 | return 0; |
132 | ||
2f545ae4 | 133 | if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0) |
fb46be03 | 134 | return 0; |
d02b48c6 | 135 | |
f3f1cf84 | 136 | REF_PRINT_COUNT("BIO", a); |
98e553d2 | 137 | if (ret > 0) |
fb46be03 | 138 | return 1; |
98e553d2 MC |
139 | REF_ASSERT_ISNT(ret < 0); |
140 | ||
0800318a | 141 | if (HAS_CALLBACK(a)) { |
98e553d2 MC |
142 | ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); |
143 | if (ret <= 0) | |
d8f6c533 | 144 | return 0; |
98e553d2 | 145 | } |
d02b48c6 | 146 | |
a14a740d F |
147 | if ((a->method != NULL) && (a->method->destroy != NULL)) |
148 | a->method->destroy(a); | |
149 | ||
0f113f3e | 150 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); |
58964a49 | 151 | |
fb46be03 AG |
152 | CRYPTO_THREAD_lock_free(a->lock); |
153 | ||
0f113f3e | 154 | OPENSSL_free(a); |
fb46be03 AG |
155 | |
156 | return 1; | |
0f113f3e | 157 | } |
d02b48c6 | 158 | |
a146ae55 MC |
159 | void BIO_set_data(BIO *a, void *ptr) |
160 | { | |
161 | a->ptr = ptr; | |
162 | } | |
163 | ||
164 | void *BIO_get_data(BIO *a) | |
165 | { | |
166 | return a->ptr; | |
167 | } | |
168 | ||
169 | void BIO_set_init(BIO *a, int init) | |
170 | { | |
171 | a->init = init; | |
172 | } | |
173 | ||
174 | int BIO_get_init(BIO *a) | |
175 | { | |
176 | return a->init; | |
177 | } | |
178 | ||
179 | void BIO_set_shutdown(BIO *a, int shut) | |
180 | { | |
181 | a->shutdown = shut; | |
182 | } | |
183 | ||
184 | int BIO_get_shutdown(BIO *a) | |
185 | { | |
186 | return a->shutdown; | |
187 | } | |
188 | ||
371acb22 | 189 | void BIO_vfree(BIO *a) |
0f113f3e MC |
190 | { |
191 | BIO_free(a); | |
192 | } | |
371acb22 | 193 | |
fb46be03 AG |
194 | int BIO_up_ref(BIO *a) |
195 | { | |
196 | int i; | |
197 | ||
2f545ae4 | 198 | if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0) |
fb46be03 AG |
199 | return 0; |
200 | ||
201 | REF_PRINT_COUNT("BIO", a); | |
202 | REF_ASSERT_ISNT(i < 2); | |
dc88a039 | 203 | return i > 1; |
fb46be03 AG |
204 | } |
205 | ||
7806f3dd | 206 | void BIO_clear_flags(BIO *b, int flags) |
0f113f3e MC |
207 | { |
208 | b->flags &= ~flags; | |
209 | } | |
210 | ||
211 | int BIO_test_flags(const BIO *b, int flags) | |
212 | { | |
213 | return (b->flags & flags); | |
214 | } | |
215 | ||
216 | void BIO_set_flags(BIO *b, int flags) | |
217 | { | |
218 | b->flags |= flags; | |
219 | } | |
220 | ||
0800318a | 221 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
d07aee2c MC |
222 | BIO_callback_fn BIO_get_callback(const BIO *b) |
223 | { | |
0f113f3e MC |
224 | return b->callback; |
225 | } | |
226 | ||
d07aee2c | 227 | void BIO_set_callback(BIO *b, BIO_callback_fn cb) |
0f113f3e MC |
228 | { |
229 | b->callback = cb; | |
230 | } | |
0800318a | 231 | #endif |
7806f3dd | 232 | |
d07aee2c MC |
233 | BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) |
234 | { | |
235 | return b->callback_ex; | |
236 | } | |
237 | ||
238 | void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb) | |
239 | { | |
240 | b->callback_ex = cb; | |
241 | } | |
242 | ||
7806f3dd | 243 | void BIO_set_callback_arg(BIO *b, char *arg) |
0f113f3e MC |
244 | { |
245 | b->cb_arg = arg; | |
246 | } | |
7806f3dd | 247 | |
0f113f3e MC |
248 | char *BIO_get_callback_arg(const BIO *b) |
249 | { | |
250 | return b->cb_arg; | |
251 | } | |
7806f3dd | 252 | |
0f113f3e MC |
253 | const char *BIO_method_name(const BIO *b) |
254 | { | |
255 | return b->method->name; | |
256 | } | |
7806f3dd NL |
257 | |
258 | int BIO_method_type(const BIO *b) | |
0f113f3e MC |
259 | { |
260 | return b->method->type; | |
261 | } | |
7806f3dd | 262 | |
bb5310be MC |
263 | /* |
264 | * This is essentially the same as BIO_read_ex() except that it allows | |
7bf79e33 MC |
265 | * 0 or a negative value to indicate failure (retryable or not) in the return. |
266 | * This is for compatibility with the old style BIO_read(), where existing code | |
267 | * may make assumptions about the return value that it might get. | |
bb5310be | 268 | */ |
d62bf89c | 269 | static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) |
d07aee2c MC |
270 | { |
271 | int ret; | |
d02b48c6 | 272 | |
dc88a039 | 273 | if (b == NULL) { |
ed4a9b15 | 274 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
dc88a039 DDO |
275 | return -1; |
276 | } | |
277 | if (b->method == NULL || b->method->bread == NULL) { | |
9311d0c4 | 278 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
bb5310be | 279 | return -2; |
0f113f3e | 280 | } |
d02b48c6 | 281 | |
0800318a | 282 | if (HAS_CALLBACK(b) && |
7bf79e33 | 283 | ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, |
c911e5da | 284 | NULL)) <= 0)) |
d07aee2c | 285 | return ret; |
d02b48c6 | 286 | |
0f113f3e | 287 | if (!b->init) { |
9311d0c4 | 288 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
dc88a039 | 289 | return -1; |
0f113f3e | 290 | } |
d02b48c6 | 291 | |
d62bf89c | 292 | ret = b->method->bread(b, data, dlen, readbytes); |
dfeab068 | 293 | |
d07aee2c | 294 | if (ret > 0) |
82cb311f | 295 | b->num_read += (uint64_t)*readbytes; |
d07aee2c | 296 | |
0800318a | 297 | if (HAS_CALLBACK(b)) |
42c60460 | 298 | ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, |
d62bf89c | 299 | dlen, 0, 0L, ret, readbytes); |
d02b48c6 | 300 | |
fbba62f6 | 301 | /* Shouldn't happen */ |
d62bf89c | 302 | if (ret > 0 && *readbytes > dlen) { |
9311d0c4 | 303 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
fbba62f6 | 304 | return -1; |
7bf79e33 | 305 | } |
fbba62f6 | 306 | |
d07aee2c | 307 | return ret; |
0f113f3e | 308 | } |
d02b48c6 | 309 | |
7bf79e33 | 310 | int BIO_read(BIO *b, void *data, int dlen) |
0f113f3e | 311 | { |
d62bf89c | 312 | size_t readbytes; |
3befffa3 MC |
313 | int ret; |
314 | ||
7bf79e33 | 315 | if (dlen < 0) |
3befffa3 MC |
316 | return 0; |
317 | ||
d62bf89c | 318 | ret = bio_read_intern(b, data, (size_t)dlen, &readbytes); |
3befffa3 MC |
319 | |
320 | if (ret > 0) { | |
4e3973b4 | 321 | /* *readbytes should always be <= dlen */ |
d62bf89c | 322 | ret = (int)readbytes; |
3befffa3 MC |
323 | } |
324 | ||
325 | return ret; | |
326 | } | |
327 | ||
d62bf89c | 328 | int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) |
bb5310be | 329 | { |
dc88a039 | 330 | return bio_read_intern(b, data, dlen, readbytes) > 0; |
bb5310be MC |
331 | } |
332 | ||
7bf79e33 | 333 | static int bio_write_intern(BIO *b, const void *data, size_t dlen, |
42c60460 | 334 | size_t *written) |
3befffa3 | 335 | { |
5d43bfa7 | 336 | size_t local_written; |
3befffa3 | 337 | int ret; |
58964a49 | 338 | |
5d43bfa7 DDO |
339 | if (written != NULL) |
340 | *written = 0; | |
f505161e SL |
341 | /* |
342 | * b == NULL is not an error but just means that zero bytes are written. | |
343 | * Do not raise an error here. | |
344 | */ | |
345 | if (b == NULL) | |
346 | return 0; | |
347 | ||
dc88a039 | 348 | if (b->method == NULL || b->method->bwrite == NULL) { |
9311d0c4 | 349 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
bb5310be | 350 | return -2; |
0f113f3e | 351 | } |
d02b48c6 | 352 | |
0800318a | 353 | if (HAS_CALLBACK(b) && |
7bf79e33 | 354 | ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, |
c911e5da | 355 | NULL)) <= 0)) |
3befffa3 | 356 | return ret; |
d02b48c6 | 357 | |
0f113f3e | 358 | if (!b->init) { |
9311d0c4 | 359 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
dc88a039 | 360 | return -1; |
0f113f3e | 361 | } |
d02b48c6 | 362 | |
5d43bfa7 | 363 | ret = b->method->bwrite(b, data, dlen, &local_written); |
dfeab068 | 364 | |
3befffa3 | 365 | if (ret > 0) |
5d43bfa7 | 366 | b->num_write += (uint64_t)local_written; |
d02b48c6 | 367 | |
0800318a | 368 | if (HAS_CALLBACK(b)) |
42c60460 | 369 | ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, |
5d43bfa7 | 370 | dlen, 0, 0L, ret, &local_written); |
3befffa3 | 371 | |
5d43bfa7 DDO |
372 | if (written != NULL) |
373 | *written = local_written; | |
3befffa3 | 374 | return ret; |
0f113f3e | 375 | } |
d02b48c6 | 376 | |
7bf79e33 | 377 | int BIO_write(BIO *b, const void *data, int dlen) |
bb5310be MC |
378 | { |
379 | size_t written; | |
380 | int ret; | |
381 | ||
5d43bfa7 | 382 | if (dlen <= 0) |
bb5310be MC |
383 | return 0; |
384 | ||
7bf79e33 | 385 | ret = bio_write_intern(b, data, (size_t)dlen, &written); |
bb5310be MC |
386 | |
387 | if (ret > 0) { | |
5d43bfa7 | 388 | /* written should always be <= dlen */ |
bb5310be MC |
389 | ret = (int)written; |
390 | } | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
7bf79e33 | 395 | int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) |
bb5310be | 396 | { |
bb19b9d4 DDO |
397 | return bio_write_intern(b, data, dlen, written) > 0 |
398 | || (b != NULL && dlen == 0); /* order is important for *written */ | |
bb5310be MC |
399 | } |
400 | ||
e0c4e43e HL |
401 | int BIO_sendmmsg(BIO *b, BIO_MSG *msg, |
402 | size_t stride, size_t num_msg, uint64_t flags, | |
403 | size_t *msgs_processed) | |
404 | { | |
405 | size_t ret; | |
406 | BIO_MMSG_CB_ARGS args; | |
407 | ||
408 | if (b == NULL) { | |
409 | *msgs_processed = 0; | |
410 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
411 | return 0; | |
412 | } | |
413 | ||
414 | if (b->method == NULL || b->method->bsendmmsg == NULL) { | |
415 | *msgs_processed = 0; | |
416 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
417 | return 0; | |
418 | } | |
419 | ||
420 | if (HAS_CALLBACK(b)) { | |
421 | args.msg = msg; | |
422 | args.stride = stride; | |
423 | args.num_msg = num_msg; | |
424 | args.flags = flags; | |
425 | args.msgs_processed = msgs_processed; | |
426 | ||
427 | ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args, | |
428 | 0, 0, 0, 0, NULL); | |
429 | if (ret == 0) | |
430 | return 0; | |
431 | } | |
432 | ||
433 | if (!b->init) { | |
434 | *msgs_processed = 0; | |
435 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
436 | return 0; | |
437 | } | |
438 | ||
439 | ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed); | |
440 | ||
441 | if (HAS_CALLBACK(b)) | |
442 | ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN, | |
443 | (void *)&args, ret, 0, 0, 0, NULL); | |
444 | ||
445 | return ret; | |
446 | } | |
447 | ||
448 | int BIO_recvmmsg(BIO *b, BIO_MSG *msg, | |
449 | size_t stride, size_t num_msg, uint64_t flags, | |
450 | size_t *msgs_processed) | |
451 | { | |
452 | size_t ret; | |
453 | BIO_MMSG_CB_ARGS args; | |
454 | ||
455 | if (b == NULL) { | |
456 | *msgs_processed = 0; | |
457 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
458 | return 0; | |
459 | } | |
460 | ||
461 | if (b->method == NULL || b->method->brecvmmsg == NULL) { | |
462 | *msgs_processed = 0; | |
463 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); | |
464 | return 0; | |
465 | } | |
466 | ||
467 | if (HAS_CALLBACK(b)) { | |
468 | args.msg = msg; | |
469 | args.stride = stride; | |
470 | args.num_msg = num_msg; | |
471 | args.flags = flags; | |
472 | args.msgs_processed = msgs_processed; | |
473 | ||
474 | ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args, | |
475 | 0, 0, 0, 0, NULL); | |
476 | if (ret == 0) | |
477 | return 0; | |
478 | } | |
479 | ||
480 | if (!b->init) { | |
481 | *msgs_processed = 0; | |
482 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
483 | return 0; | |
484 | } | |
485 | ||
486 | ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed); | |
487 | ||
488 | if (HAS_CALLBACK(b)) | |
489 | ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN, | |
490 | (void *)&args, ret, 0, 0, 0, NULL); | |
491 | ||
492 | return ret; | |
493 | } | |
494 | ||
4e3973b4 | 495 | int BIO_puts(BIO *b, const char *buf) |
0f113f3e | 496 | { |
98e553d2 | 497 | int ret; |
47263ace | 498 | size_t written = 0; |
d02b48c6 | 499 | |
dc88a039 | 500 | if (b == NULL) { |
ed4a9b15 | 501 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
dc88a039 DDO |
502 | return -1; |
503 | } | |
504 | if (b->method == NULL || b->method->bputs == NULL) { | |
9311d0c4 | 505 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
98e553d2 | 506 | return -2; |
0f113f3e | 507 | } |
d02b48c6 | 508 | |
0800318a | 509 | if (HAS_CALLBACK(b)) { |
4e3973b4 | 510 | ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); |
98e553d2 MC |
511 | if (ret <= 0) |
512 | return ret; | |
513 | } | |
d02b48c6 | 514 | |
0f113f3e | 515 | if (!b->init) { |
9311d0c4 | 516 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
dc88a039 | 517 | return -1; |
0f113f3e | 518 | } |
d02b48c6 | 519 | |
4e3973b4 | 520 | ret = b->method->bputs(b, buf); |
d02b48c6 | 521 | |
98e553d2 MC |
522 | if (ret > 0) { |
523 | b->num_write += (uint64_t)ret; | |
524 | written = ret; | |
525 | ret = 1; | |
526 | } | |
527 | ||
0800318a | 528 | if (HAS_CALLBACK(b)) |
4e3973b4 | 529 | ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, |
98e553d2 MC |
530 | 0L, ret, &written); |
531 | ||
532 | if (ret > 0) { | |
7bf79e33 | 533 | if (written > INT_MAX) { |
9311d0c4 | 534 | ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG); |
98e553d2 | 535 | ret = -1; |
7bf79e33 | 536 | } else { |
98e553d2 | 537 | ret = (int)written; |
7bf79e33 | 538 | } |
98e553d2 | 539 | } |
7d95ff76 | 540 | |
98e553d2 | 541 | return ret; |
0f113f3e | 542 | } |
d02b48c6 | 543 | |
4e3973b4 | 544 | int BIO_gets(BIO *b, char *buf, int size) |
0f113f3e | 545 | { |
98e553d2 | 546 | int ret; |
d62bf89c | 547 | size_t readbytes = 0; |
0f113f3e | 548 | |
dc88a039 | 549 | if (b == NULL) { |
ed4a9b15 | 550 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
dc88a039 DDO |
551 | return -1; |
552 | } | |
553 | if (b->method == NULL || b->method->bgets == NULL) { | |
9311d0c4 | 554 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
26a7d938 | 555 | return -2; |
0f113f3e MC |
556 | } |
557 | ||
4e3973b4 | 558 | if (size < 0) { |
9311d0c4 | 559 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); |
dc88a039 | 560 | return -1; |
fbba62f6 MC |
561 | } |
562 | ||
0800318a | 563 | if (HAS_CALLBACK(b)) { |
4e3973b4 | 564 | ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); |
98e553d2 MC |
565 | if (ret <= 0) |
566 | return ret; | |
567 | } | |
0f113f3e MC |
568 | |
569 | if (!b->init) { | |
9311d0c4 | 570 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
dc88a039 | 571 | return -1; |
0f113f3e MC |
572 | } |
573 | ||
4e3973b4 | 574 | ret = b->method->bgets(b, buf, size); |
98e553d2 MC |
575 | |
576 | if (ret > 0) { | |
d62bf89c | 577 | readbytes = ret; |
98e553d2 MC |
578 | ret = 1; |
579 | } | |
580 | ||
0800318a | 581 | if (HAS_CALLBACK(b)) |
4e3973b4 | 582 | ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, |
d62bf89c | 583 | 0, 0L, ret, &readbytes); |
98e553d2 MC |
584 | |
585 | if (ret > 0) { | |
fbba62f6 | 586 | /* Shouldn't happen */ |
4e3973b4 | 587 | if (readbytes > (size_t)size) |
98e553d2 MC |
588 | ret = -1; |
589 | else | |
d62bf89c | 590 | ret = (int)readbytes; |
98e553d2 | 591 | } |
0f113f3e | 592 | |
98e553d2 | 593 | return ret; |
0f113f3e MC |
594 | } |
595 | ||
18423879 DDO |
596 | int BIO_get_line(BIO *bio, char *buf, int size) |
597 | { | |
598 | int ret = 0; | |
599 | char *ptr = buf; | |
600 | ||
601 | if (buf == NULL) { | |
602 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
603 | return -1; | |
604 | } | |
605 | if (size <= 0) { | |
606 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); | |
607 | return -1; | |
608 | } | |
609 | *buf = '\0'; | |
610 | ||
611 | if (bio == NULL) { | |
612 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); | |
613 | return -1; | |
614 | } | |
615 | if (!bio->init) { | |
616 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); | |
617 | return -1; | |
618 | } | |
619 | ||
620 | while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0) | |
621 | if (*ptr++ == '\n') | |
622 | break; | |
623 | *ptr = '\0'; | |
624 | return ret > 0 || BIO_eof(bio) ? ptr - buf : ret; | |
625 | } | |
626 | ||
0f113f3e MC |
627 | int BIO_indent(BIO *b, int indent, int max) |
628 | { | |
629 | if (indent < 0) | |
630 | indent = 0; | |
631 | if (indent > max) | |
632 | indent = max; | |
633 | while (indent--) | |
634 | if (BIO_puts(b, " ") != 1) | |
635 | return 0; | |
636 | return 1; | |
637 | } | |
54a656ef | 638 | |
6b691a5c | 639 | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) |
0f113f3e MC |
640 | { |
641 | int i; | |
d02b48c6 | 642 | |
0f113f3e | 643 | i = iarg; |
26a7d938 | 644 | return BIO_ctrl(b, cmd, larg, (char *)&i); |
0f113f3e | 645 | } |
d02b48c6 | 646 | |
417be660 | 647 | void *BIO_ptr_ctrl(BIO *b, int cmd, long larg) |
0f113f3e | 648 | { |
417be660 | 649 | void *p = NULL; |
58964a49 | 650 | |
0f113f3e | 651 | if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0) |
26a7d938 | 652 | return NULL; |
0f113f3e | 653 | else |
26a7d938 | 654 | return p; |
0f113f3e | 655 | } |
58964a49 | 656 | |
95d29597 | 657 | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) |
0f113f3e MC |
658 | { |
659 | long ret; | |
d02b48c6 | 660 | |
398ae823 | 661 | if (b == NULL) |
dc88a039 | 662 | return -1; |
dc88a039 | 663 | if (b->method == NULL || b->method->ctrl == NULL) { |
9311d0c4 | 664 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
98e553d2 | 665 | return -2; |
0f113f3e | 666 | } |
d02b48c6 | 667 | |
0800318a | 668 | if (HAS_CALLBACK(b)) { |
98e553d2 MC |
669 | ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); |
670 | if (ret <= 0) | |
671 | return ret; | |
672 | } | |
d02b48c6 | 673 | |
0f113f3e | 674 | ret = b->method->ctrl(b, cmd, larg, parg); |
d02b48c6 | 675 | |
0800318a | 676 | if (HAS_CALLBACK(b)) |
98e553d2 MC |
677 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, |
678 | larg, ret, NULL); | |
679 | ||
680 | return ret; | |
0f113f3e | 681 | } |
d02b48c6 | 682 | |
fce78bd4 | 683 | long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
0f113f3e MC |
684 | { |
685 | long ret; | |
d3442bc7 | 686 | |
398ae823 | 687 | if (b == NULL) |
ed4a9b15 | 688 | return -2; |
dc88a039 DDO |
689 | if (b->method == NULL || b->method->callback_ctrl == NULL |
690 | || cmd != BIO_CTRL_SET_CALLBACK) { | |
9311d0c4 | 691 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
26a7d938 | 692 | return -2; |
0f113f3e | 693 | } |
d3442bc7 | 694 | |
0800318a | 695 | if (HAS_CALLBACK(b)) { |
98e553d2 MC |
696 | ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, |
697 | NULL); | |
698 | if (ret <= 0) | |
699 | return ret; | |
700 | } | |
d3442bc7 | 701 | |
0f113f3e | 702 | ret = b->method->callback_ctrl(b, cmd, fp); |
d3442bc7 | 703 | |
0800318a | 704 | if (HAS_CALLBACK(b)) |
98e553d2 MC |
705 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, |
706 | cmd, 0, ret, NULL); | |
707 | ||
708 | return ret; | |
0f113f3e | 709 | } |
d3442bc7 | 710 | |
0f113f3e MC |
711 | /* |
712 | * It is unfortunate to duplicate in functions what the BIO_(w)pending macros | |
95d29597 | 713 | * do; but those macros have inappropriate return type, and for interfacing |
0f113f3e MC |
714 | * from other programming languages, C macros aren't much of a help anyway. |
715 | */ | |
95d29597 | 716 | size_t BIO_ctrl_pending(BIO *bio) |
0f113f3e | 717 | { |
e9809f8a TM |
718 | long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); |
719 | ||
720 | if (ret < 0) | |
721 | ret = 0; | |
c6be0aa8 TM |
722 | #if LONG_MAX > SIZE_MAX |
723 | if (ret > SIZE_MAX) | |
724 | ret = SIZE_MAX; | |
725 | #endif | |
e9809f8a | 726 | return (size_t)ret; |
0f113f3e | 727 | } |
95d29597 BM |
728 | |
729 | size_t BIO_ctrl_wpending(BIO *bio) | |
0f113f3e | 730 | { |
e9809f8a TM |
731 | long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); |
732 | ||
733 | if (ret < 0) | |
734 | ret = 0; | |
c6be0aa8 TM |
735 | #if LONG_MAX > SIZE_MAX |
736 | if (ret > SIZE_MAX) | |
737 | ret = SIZE_MAX; | |
738 | #endif | |
e9809f8a | 739 | return (size_t)ret; |
0f113f3e | 740 | } |
95d29597 | 741 | |
d02b48c6 | 742 | /* put the 'bio' on the end of b's list of operators */ |
6b691a5c | 743 | BIO *BIO_push(BIO *b, BIO *bio) |
0f113f3e MC |
744 | { |
745 | BIO *lb; | |
746 | ||
747 | if (b == NULL) | |
26a7d938 | 748 | return bio; |
0f113f3e MC |
749 | lb = b; |
750 | while (lb->next_bio != NULL) | |
751 | lb = lb->next_bio; | |
752 | lb->next_bio = bio; | |
753 | if (bio != NULL) | |
754 | bio->prev_bio = lb; | |
755 | /* called to do internal processing */ | |
756 | BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb); | |
26a7d938 | 757 | return b; |
0f113f3e | 758 | } |
d02b48c6 RE |
759 | |
760 | /* Remove the first and return the rest */ | |
6b691a5c | 761 | BIO *BIO_pop(BIO *b) |
0f113f3e MC |
762 | { |
763 | BIO *ret; | |
d02b48c6 | 764 | |
398ae823 | 765 | if (b == NULL) |
26a7d938 | 766 | return NULL; |
0f113f3e | 767 | ret = b->next_bio; |
d02b48c6 | 768 | |
0f113f3e | 769 | BIO_ctrl(b, BIO_CTRL_POP, 0, b); |
5d780bab | 770 | |
0f113f3e MC |
771 | if (b->prev_bio != NULL) |
772 | b->prev_bio->next_bio = b->next_bio; | |
773 | if (b->next_bio != NULL) | |
774 | b->next_bio->prev_bio = b->prev_bio; | |
d02b48c6 | 775 | |
0f113f3e MC |
776 | b->next_bio = NULL; |
777 | b->prev_bio = NULL; | |
26a7d938 | 778 | return ret; |
0f113f3e | 779 | } |
d02b48c6 | 780 | |
6b691a5c | 781 | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) |
0f113f3e MC |
782 | { |
783 | BIO *b, *last; | |
784 | ||
785 | b = last = bio; | |
786 | for (;;) { | |
787 | if (!BIO_should_retry(b)) | |
788 | break; | |
789 | last = b; | |
790 | b = b->next_bio; | |
791 | if (b == NULL) | |
792 | break; | |
793 | } | |
794 | if (reason != NULL) | |
795 | *reason = last->retry_reason; | |
26a7d938 | 796 | return last; |
0f113f3e | 797 | } |
d02b48c6 | 798 | |
6b691a5c | 799 | int BIO_get_retry_reason(BIO *bio) |
0f113f3e | 800 | { |
26a7d938 | 801 | return bio->retry_reason; |
0f113f3e | 802 | } |
d02b48c6 | 803 | |
a146ae55 MC |
804 | void BIO_set_retry_reason(BIO *bio, int reason) |
805 | { | |
806 | bio->retry_reason = reason; | |
807 | } | |
808 | ||
6b691a5c | 809 | BIO *BIO_find_type(BIO *bio, int type) |
0f113f3e MC |
810 | { |
811 | int mt, mask; | |
812 | ||
dc88a039 | 813 | if (bio == NULL) { |
ed4a9b15 | 814 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e | 815 | return NULL; |
dc88a039 | 816 | } |
0f113f3e MC |
817 | mask = type & 0xff; |
818 | do { | |
819 | if (bio->method != NULL) { | |
820 | mt = bio->method->type; | |
821 | ||
822 | if (!mask) { | |
823 | if (mt & type) | |
26a7d938 | 824 | return bio; |
dc88a039 | 825 | } else if (mt == type) { |
26a7d938 | 826 | return bio; |
dc88a039 | 827 | } |
0f113f3e MC |
828 | } |
829 | bio = bio->next_bio; | |
830 | } while (bio != NULL); | |
26a7d938 | 831 | return NULL; |
0f113f3e | 832 | } |
d02b48c6 | 833 | |
cfd3bb17 | 834 | BIO *BIO_next(BIO *b) |
0f113f3e | 835 | { |
398ae823 | 836 | if (b == NULL) |
0f113f3e MC |
837 | return NULL; |
838 | return b->next_bio; | |
839 | } | |
cfd3bb17 | 840 | |
a146ae55 MC |
841 | void BIO_set_next(BIO *b, BIO *next) |
842 | { | |
843 | b->next_bio = next; | |
844 | } | |
845 | ||
6b691a5c | 846 | void BIO_free_all(BIO *bio) |
0f113f3e MC |
847 | { |
848 | BIO *b; | |
849 | int ref; | |
850 | ||
851 | while (bio != NULL) { | |
852 | b = bio; | |
853 | ref = b->references; | |
854 | bio = bio->next_bio; | |
855 | BIO_free(b); | |
856 | /* Since ref count > 1, don't free anyone else. */ | |
857 | if (ref > 1) | |
858 | break; | |
859 | } | |
860 | } | |
d02b48c6 | 861 | |
6b691a5c | 862 | BIO *BIO_dup_chain(BIO *in) |
0f113f3e MC |
863 | { |
864 | BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; | |
865 | ||
866 | for (bio = in; bio != NULL; bio = bio->next_bio) { | |
867 | if ((new_bio = BIO_new(bio->method)) == NULL) | |
868 | goto err; | |
0800318a | 869 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
0f113f3e | 870 | new_bio->callback = bio->callback; |
0800318a | 871 | #endif |
98e553d2 | 872 | new_bio->callback_ex = bio->callback_ex; |
0f113f3e MC |
873 | new_bio->cb_arg = bio->cb_arg; |
874 | new_bio->init = bio->init; | |
875 | new_bio->shutdown = bio->shutdown; | |
876 | new_bio->flags = bio->flags; | |
877 | ||
878 | /* This will let SSL_s_sock() work with stdin/stdout */ | |
879 | new_bio->num = bio->num; | |
880 | ||
881 | if (!BIO_dup_state(bio, (char *)new_bio)) { | |
882 | BIO_free(new_bio); | |
883 | goto err; | |
884 | } | |
885 | ||
886 | /* copy app data */ | |
887 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data, | |
aec54108 MC |
888 | &bio->ex_data)) { |
889 | BIO_free(new_bio); | |
0f113f3e | 890 | goto err; |
aec54108 | 891 | } |
0f113f3e MC |
892 | |
893 | if (ret == NULL) { | |
894 | eoc = new_bio; | |
895 | ret = eoc; | |
896 | } else { | |
897 | BIO_push(eoc, new_bio); | |
898 | eoc = new_bio; | |
899 | } | |
900 | } | |
26a7d938 | 901 | return ret; |
0f113f3e | 902 | err: |
aec54108 MC |
903 | BIO_free_all(ret); |
904 | ||
26a7d938 | 905 | return NULL; |
0f113f3e | 906 | } |
d02b48c6 | 907 | |
6b691a5c | 908 | void BIO_copy_next_retry(BIO *b) |
0f113f3e MC |
909 | { |
910 | BIO_set_flags(b, BIO_get_retry_flags(b->next_bio)); | |
911 | b->retry_reason = b->next_bio->retry_reason; | |
912 | } | |
d02b48c6 | 913 | |
dd9d233e | 914 | int BIO_set_ex_data(BIO *bio, int idx, void *data) |
0f113f3e | 915 | { |
26a7d938 | 916 | return CRYPTO_set_ex_data(&(bio->ex_data), idx, data); |
0f113f3e | 917 | } |
58964a49 | 918 | |
8cc86b81 | 919 | void *BIO_get_ex_data(const BIO *bio, int idx) |
0f113f3e | 920 | { |
26a7d938 | 921 | return CRYPTO_get_ex_data(&(bio->ex_data), idx); |
0f113f3e | 922 | } |
58964a49 | 923 | |
b8b12aad | 924 | uint64_t BIO_number_read(BIO *bio) |
c3ed3b6e | 925 | { |
0f113f3e MC |
926 | if (bio) |
927 | return bio->num_read; | |
928 | return 0; | |
c3ed3b6e DSH |
929 | } |
930 | ||
b8b12aad | 931 | uint64_t BIO_number_written(BIO *bio) |
c3ed3b6e | 932 | { |
0f113f3e MC |
933 | if (bio) |
934 | return bio->num_write; | |
935 | return 0; | |
c3ed3b6e | 936 | } |
ff234405 | 937 | |
1ee7b8b9 MC |
938 | void bio_free_ex_data(BIO *bio) |
939 | { | |
940 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); | |
941 | } | |
ff234405 MC |
942 | |
943 | void bio_cleanup(void) | |
944 | { | |
945 | #ifndef OPENSSL_NO_SOCK | |
946 | bio_sock_cleanup_int(); | |
947 | CRYPTO_THREAD_lock_free(bio_lookup_lock); | |
948 | bio_lookup_lock = NULL; | |
5a7ad1f0 | 949 | #endif |
8b8d963d RS |
950 | CRYPTO_THREAD_lock_free(bio_type_lock); |
951 | bio_type_lock = NULL; | |
ff234405 | 952 | } |
e8d0819d DDO |
953 | |
954 | /* Internal variant of the below BIO_wait() not calling BIOerr() */ | |
e98c7350 | 955 | static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) |
e8d0819d | 956 | { |
d8c78e5f | 957 | #ifndef OPENSSL_NO_SOCK |
e8d0819d | 958 | int fd; |
d8c78e5f | 959 | #endif |
e98c7350 | 960 | long sec_diff; |
e8d0819d | 961 | |
e98c7350 | 962 | if (max_time == 0) /* no timeout */ |
e8d0819d DDO |
963 | return 1; |
964 | ||
965 | #ifndef OPENSSL_NO_SOCK | |
e98c7350 | 966 | if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE) |
e8d0819d DDO |
967 | return BIO_socket_wait(fd, BIO_should_read(bio), max_time); |
968 | #endif | |
e98c7350 | 969 | /* fall back to polling since no sockets are available */ |
e8d0819d | 970 | |
e98c7350 DDO |
971 | sec_diff = (long)(max_time - time(NULL)); /* might overflow */ |
972 | if (sec_diff < 0) | |
973 | return 0; /* clearly timeout */ | |
974 | ||
975 | /* now take a nap at most the given number of milliseconds */ | |
976 | if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */ | |
977 | if (nap_milliseconds > 1000) | |
978 | nap_milliseconds = 1000; | |
979 | } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */ | |
980 | if ((unsigned long)sec_diff * 1000 < nap_milliseconds) | |
981 | nap_milliseconds = (unsigned int)sec_diff * 1000; | |
e8d0819d | 982 | } |
e98c7350 | 983 | ossl_sleep(nap_milliseconds); |
e8d0819d DDO |
984 | return 1; |
985 | } | |
986 | ||
e98c7350 | 987 | /*- |
e8d0819d | 988 | * Wait on (typically socket-based) BIO at most until max_time. |
e98c7350 DDO |
989 | * Succeed immediately if max_time == 0. |
990 | * If sockets are not available support polling: succeed after waiting at most | |
991 | * the number of nap_milliseconds in order to avoid a tight busy loop. | |
992 | * Call BIOerr(...) on timeout or error. | |
e8d0819d DDO |
993 | * Returns -1 on error, 0 on timeout, and 1 on success. |
994 | */ | |
e98c7350 | 995 | int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) |
e8d0819d | 996 | { |
e98c7350 | 997 | int rv = bio_wait(bio, max_time, nap_milliseconds); |
e8d0819d DDO |
998 | |
999 | if (rv <= 0) | |
9311d0c4 RL |
1000 | ERR_raise(ERR_LIB_BIO, |
1001 | rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR); | |
e8d0819d DDO |
1002 | return rv; |
1003 | } | |
1004 | ||
1005 | /* | |
e98c7350 DDO |
1006 | * Connect via given BIO using BIO_do_connect() until success/timeout/error. |
1007 | * Parameter timeout == 0 means no timeout, < 0 means exactly one try. | |
1008 | * For non-blocking and potentially even non-socket BIOs perform polling with | |
1009 | * the given density: between polls sleep nap_milliseconds using BIO_wait() | |
1010 | * in order to avoid a tight busy loop. | |
e8d0819d DDO |
1011 | * Returns -1 on error, 0 on timeout, and 1 on success. |
1012 | */ | |
e98c7350 | 1013 | int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds) |
e8d0819d | 1014 | { |
e98c7350 | 1015 | int blocking = timeout <= 0; |
e8d0819d DDO |
1016 | time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; |
1017 | int rv; | |
1018 | ||
1019 | if (bio == NULL) { | |
ed4a9b15 | 1020 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
e8d0819d DDO |
1021 | return -1; |
1022 | } | |
1023 | ||
e98c7350 DDO |
1024 | if (nap_milliseconds < 0) |
1025 | nap_milliseconds = 100; | |
1026 | BIO_set_nbio(bio, !blocking); | |
1027 | ||
1028 | retry: | |
afecd85d TM |
1029 | ERR_set_mark(); |
1030 | rv = BIO_do_connect(bio); | |
e98c7350 DDO |
1031 | |
1032 | if (rv <= 0) { /* could be timeout or retryable error or fatal error */ | |
1033 | int err = ERR_peek_last_error(); | |
1034 | int reason = ERR_GET_REASON(err); | |
1035 | int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */ | |
1036 | ||
1037 | if (ERR_GET_LIB(err) == ERR_LIB_BIO) { | |
1038 | switch (reason) { | |
1039 | case ERR_R_SYS_LIB: | |
1040 | /* | |
1041 | * likely retryable system error occurred, which may be | |
1042 | * EAGAIN (resource temporarily unavailable) some 40 secs after | |
1043 | * calling getaddrinfo(): Temporary failure in name resolution | |
1044 | * or a premature ETIMEDOUT, some 30 seconds after connect() | |
1045 | */ | |
1046 | case BIO_R_CONNECT_ERROR: | |
1047 | case BIO_R_NBIO_CONNECT_ERROR: | |
1048 | /* some likely retryable connection error occurred */ | |
1049 | (void)BIO_reset(bio); /* often needed to avoid retry failure */ | |
1050 | do_retry = 1; | |
1051 | break; | |
1052 | default: | |
1053 | break; | |
1054 | } | |
1055 | } | |
1056 | if (timeout >= 0 && do_retry) { | |
afecd85d | 1057 | ERR_pop_to_mark(); |
e98c7350 DDO |
1058 | /* will not actually wait if timeout == 0 (i.e., blocking BIO): */ |
1059 | rv = bio_wait(bio, max_time, nap_milliseconds); | |
e8d0819d DDO |
1060 | if (rv > 0) |
1061 | goto retry; | |
9311d0c4 RL |
1062 | ERR_raise(ERR_LIB_BIO, |
1063 | rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR); | |
e8d0819d | 1064 | } else { |
afecd85d | 1065 | ERR_clear_last_mark(); |
e8d0819d | 1066 | rv = -1; |
e98c7350 | 1067 | if (err == 0) /* missing error queue entry */ |
dc88a039 DDO |
1068 | /* workaround: general error */ |
1069 | ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); | |
e8d0819d | 1070 | } |
afecd85d TM |
1071 | } else { |
1072 | ERR_clear_last_mark(); | |
e8d0819d DDO |
1073 | } |
1074 | ||
1075 | return rv; | |
1076 | } |