A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

919 lines
22 KiB

23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
  1. /*
  2. * Copyright 1995-2021 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. #include <stdio.h>
  10. #include <errno.h>
  11. #include <openssl/crypto.h>
  12. #include "bio_local.h"
  13. /*
  14. * Helper macro for the callback to determine whether an operator expects a
  15. * len parameter or not
  16. */
  17. #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
  18. || (o) == BIO_CB_GETS)
  19. /*
  20. * Helper function to work out whether to call the new style callback or the old
  21. * one, and translate between the two.
  22. *
  23. * This has a long return type for consistency with the old callback. Similarly
  24. * for the "long" used for "inret"
  25. */
  26. static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
  27. int argi, long argl, long inret,
  28. size_t *processed)
  29. {
  30. long ret;
  31. int bareoper;
  32. if (b->callback_ex != NULL)
  33. return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
  34. /* Strip off any BIO_CB_RETURN flag */
  35. bareoper = oper & ~BIO_CB_RETURN;
  36. /*
  37. * We have an old style callback, so we will have to do nasty casts and
  38. * check for overflows.
  39. */
  40. if (HAS_LEN_OPER(bareoper)) {
  41. /* In this case |len| is set, and should be used instead of |argi| */
  42. if (len > INT_MAX)
  43. return -1;
  44. argi = (int)len;
  45. }
  46. if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
  47. if (*processed > INT_MAX)
  48. return -1;
  49. inret = *processed;
  50. }
  51. ret = b->callback(b, oper, argp, argi, argl, inret);
  52. if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
  53. *processed = (size_t)ret;
  54. ret = 1;
  55. }
  56. return ret;
  57. }
  58. BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
  59. {
  60. BIO *bio = OPENSSL_zalloc(sizeof(*bio));
  61. if (bio == NULL) {
  62. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  63. return NULL;
  64. }
  65. bio->libctx = libctx;
  66. bio->method = method;
  67. bio->shutdown = 1;
  68. bio->references = 1;
  69. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
  70. goto err;
  71. bio->lock = CRYPTO_THREAD_lock_new();
  72. if (bio->lock == NULL) {
  73. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  74. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  75. goto err;
  76. }
  77. if (method->create != NULL && !method->create(bio)) {
  78. ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
  79. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  80. CRYPTO_THREAD_lock_free(bio->lock);
  81. goto err;
  82. }
  83. if (method->create == NULL)
  84. bio->init = 1;
  85. return bio;
  86. err:
  87. OPENSSL_free(bio);
  88. return NULL;
  89. }
  90. BIO *BIO_new(const BIO_METHOD *method)
  91. {
  92. return BIO_new_ex(NULL, method);
  93. }
  94. int BIO_free(BIO *a)
  95. {
  96. int ret;
  97. if (a == NULL)
  98. return 0;
  99. if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
  100. return 0;
  101. REF_PRINT_COUNT("BIO", a);
  102. if (ret > 0)
  103. return 1;
  104. REF_ASSERT_ISNT(ret < 0);
  105. if (a->callback != NULL || a->callback_ex != NULL) {
  106. ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
  107. if (ret <= 0)
  108. return ret;
  109. }
  110. if ((a->method != NULL) && (a->method->destroy != NULL))
  111. a->method->destroy(a);
  112. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
  113. CRYPTO_THREAD_lock_free(a->lock);
  114. OPENSSL_free(a);
  115. return 1;
  116. }
  117. void BIO_set_data(BIO *a, void *ptr)
  118. {
  119. a->ptr = ptr;
  120. }
  121. void *BIO_get_data(BIO *a)
  122. {
  123. return a->ptr;
  124. }
  125. void BIO_set_init(BIO *a, int init)
  126. {
  127. a->init = init;
  128. }
  129. int BIO_get_init(BIO *a)
  130. {
  131. return a->init;
  132. }
  133. void BIO_set_shutdown(BIO *a, int shut)
  134. {
  135. a->shutdown = shut;
  136. }
  137. int BIO_get_shutdown(BIO *a)
  138. {
  139. return a->shutdown;
  140. }
  141. void BIO_vfree(BIO *a)
  142. {
  143. BIO_free(a);
  144. }
  145. int BIO_up_ref(BIO *a)
  146. {
  147. int i;
  148. if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
  149. return 0;
  150. REF_PRINT_COUNT("BIO", a);
  151. REF_ASSERT_ISNT(i < 2);
  152. return i > 1;
  153. }
  154. void BIO_clear_flags(BIO *b, int flags)
  155. {
  156. b->flags &= ~flags;
  157. }
  158. int BIO_test_flags(const BIO *b, int flags)
  159. {
  160. return (b->flags & flags);
  161. }
  162. void BIO_set_flags(BIO *b, int flags)
  163. {
  164. b->flags |= flags;
  165. }
  166. BIO_callback_fn BIO_get_callback(const BIO *b)
  167. {
  168. return b->callback;
  169. }
  170. void BIO_set_callback(BIO *b, BIO_callback_fn cb)
  171. {
  172. b->callback = cb;
  173. }
  174. BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
  175. {
  176. return b->callback_ex;
  177. }
  178. void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
  179. {
  180. b->callback_ex = cb;
  181. }
  182. void BIO_set_callback_arg(BIO *b, char *arg)
  183. {
  184. b->cb_arg = arg;
  185. }
  186. char *BIO_get_callback_arg(const BIO *b)
  187. {
  188. return b->cb_arg;
  189. }
  190. const char *BIO_method_name(const BIO *b)
  191. {
  192. return b->method->name;
  193. }
  194. int BIO_method_type(const BIO *b)
  195. {
  196. return b->method->type;
  197. }
  198. /*
  199. * This is essentially the same as BIO_read_ex() except that it allows
  200. * 0 or a negative value to indicate failure (retryable or not) in the return.
  201. * This is for compatibility with the old style BIO_read(), where existing code
  202. * may make assumptions about the return value that it might get.
  203. */
  204. static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
  205. {
  206. int ret;
  207. if (b == NULL) {
  208. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  209. return -1;
  210. }
  211. if (b->method == NULL || b->method->bread == NULL) {
  212. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  213. return -2;
  214. }
  215. if ((b->callback != NULL || b->callback_ex != NULL) &&
  216. ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
  217. NULL)) <= 0))
  218. return ret;
  219. if (!b->init) {
  220. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  221. return -1;
  222. }
  223. ret = b->method->bread(b, data, dlen, readbytes);
  224. if (ret > 0)
  225. b->num_read += (uint64_t)*readbytes;
  226. if (b->callback != NULL || b->callback_ex != NULL)
  227. ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
  228. dlen, 0, 0L, ret, readbytes);
  229. /* Shouldn't happen */
  230. if (ret > 0 && *readbytes > dlen) {
  231. ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
  232. return -1;
  233. }
  234. return ret;
  235. }
  236. int BIO_read(BIO *b, void *data, int dlen)
  237. {
  238. size_t readbytes;
  239. int ret;
  240. if (dlen < 0)
  241. return 0;
  242. ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
  243. if (ret > 0) {
  244. /* *readbytes should always be <= dlen */
  245. ret = (int)readbytes;
  246. }
  247. return ret;
  248. }
  249. int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
  250. {
  251. return bio_read_intern(b, data, dlen, readbytes) > 0;
  252. }
  253. static int bio_write_intern(BIO *b, const void *data, size_t dlen,
  254. size_t *written)
  255. {
  256. int ret;
  257. if (b == NULL) {
  258. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  259. return -1;
  260. }
  261. if (b->method == NULL || b->method->bwrite == NULL) {
  262. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  263. return -2;
  264. }
  265. if ((b->callback != NULL || b->callback_ex != NULL) &&
  266. ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
  267. NULL)) <= 0))
  268. return ret;
  269. if (!b->init) {
  270. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  271. return -1;
  272. }
  273. ret = b->method->bwrite(b, data, dlen, written);
  274. if (ret > 0)
  275. b->num_write += (uint64_t)*written;
  276. if (b->callback != NULL || b->callback_ex != NULL)
  277. ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
  278. dlen, 0, 0L, ret, written);
  279. return ret;
  280. }
  281. int BIO_write(BIO *b, const void *data, int dlen)
  282. {
  283. size_t written;
  284. int ret;
  285. if (dlen < 0)
  286. return 0;
  287. ret = bio_write_intern(b, data, (size_t)dlen, &written);
  288. if (ret > 0) {
  289. /* *written should always be <= dlen */
  290. ret = (int)written;
  291. }
  292. return ret;
  293. }
  294. int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
  295. {
  296. return bio_write_intern(b, data, dlen, written) > 0;
  297. }
  298. int BIO_puts(BIO *b, const char *buf)
  299. {
  300. int ret;
  301. size_t written = 0;
  302. if (b == NULL) {
  303. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  304. return -1;
  305. }
  306. if (b->method == NULL || b->method->bputs == NULL) {
  307. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  308. return -2;
  309. }
  310. if (b->callback != NULL || b->callback_ex != NULL) {
  311. ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
  312. if (ret <= 0)
  313. return ret;
  314. }
  315. if (!b->init) {
  316. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  317. return -1;
  318. }
  319. ret = b->method->bputs(b, buf);
  320. if (ret > 0) {
  321. b->num_write += (uint64_t)ret;
  322. written = ret;
  323. ret = 1;
  324. }
  325. if (b->callback != NULL || b->callback_ex != NULL)
  326. ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
  327. 0L, ret, &written);
  328. if (ret > 0) {
  329. if (written > INT_MAX) {
  330. ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
  331. ret = -1;
  332. } else {
  333. ret = (int)written;
  334. }
  335. }
  336. return ret;
  337. }
  338. int BIO_gets(BIO *b, char *buf, int size)
  339. {
  340. int ret;
  341. size_t readbytes = 0;
  342. if (b == NULL) {
  343. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  344. return -1;
  345. }
  346. if (b->method == NULL || b->method->bgets == NULL) {
  347. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  348. return -2;
  349. }
  350. if (size < 0) {
  351. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  352. return -1;
  353. }
  354. if (b->callback != NULL || b->callback_ex != NULL) {
  355. ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
  356. if (ret <= 0)
  357. return ret;
  358. }
  359. if (!b->init) {
  360. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  361. return -1;
  362. }
  363. ret = b->method->bgets(b, buf, size);
  364. if (ret > 0) {
  365. readbytes = ret;
  366. ret = 1;
  367. }
  368. if (b->callback != NULL || b->callback_ex != NULL)
  369. ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
  370. 0, 0L, ret, &readbytes);
  371. if (ret > 0) {
  372. /* Shouldn't happen */
  373. if (readbytes > (size_t)size)
  374. ret = -1;
  375. else
  376. ret = (int)readbytes;
  377. }
  378. return ret;
  379. }
  380. int BIO_indent(BIO *b, int indent, int max)
  381. {
  382. if (indent < 0)
  383. indent = 0;
  384. if (indent > max)
  385. indent = max;
  386. while (indent--)
  387. if (BIO_puts(b, " ") != 1)
  388. return 0;
  389. return 1;
  390. }
  391. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  392. {
  393. int i;
  394. i = iarg;
  395. return BIO_ctrl(b, cmd, larg, (char *)&i);
  396. }
  397. void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  398. {
  399. void *p = NULL;
  400. if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
  401. return NULL;
  402. else
  403. return p;
  404. }
  405. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  406. {
  407. long ret;
  408. if (b == NULL) {
  409. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  410. return -1;
  411. }
  412. if (b->method == NULL || b->method->ctrl == NULL) {
  413. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  414. return -2;
  415. }
  416. if (b->callback != NULL || b->callback_ex != NULL) {
  417. ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
  418. if (ret <= 0)
  419. return ret;
  420. }
  421. ret = b->method->ctrl(b, cmd, larg, parg);
  422. if (b->callback != NULL || b->callback_ex != NULL)
  423. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
  424. larg, ret, NULL);
  425. return ret;
  426. }
  427. long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  428. {
  429. long ret;
  430. if (b == NULL) {
  431. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  432. return -2;
  433. }
  434. if (b->method == NULL || b->method->callback_ctrl == NULL
  435. || cmd != BIO_CTRL_SET_CALLBACK) {
  436. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  437. return -2;
  438. }
  439. if (b->callback != NULL || b->callback_ex != NULL) {
  440. ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
  441. NULL);
  442. if (ret <= 0)
  443. return ret;
  444. }
  445. ret = b->method->callback_ctrl(b, cmd, fp);
  446. if (b->callback != NULL || b->callback_ex != NULL)
  447. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
  448. cmd, 0, ret, NULL);
  449. return ret;
  450. }
  451. /*
  452. * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  453. * do; but those macros have inappropriate return type, and for interfacing
  454. * from other programming languages, C macros aren't much of a help anyway.
  455. */
  456. size_t BIO_ctrl_pending(BIO *bio)
  457. {
  458. return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  459. }
  460. size_t BIO_ctrl_wpending(BIO *bio)
  461. {
  462. return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  463. }
  464. /* put the 'bio' on the end of b's list of operators */
  465. BIO *BIO_push(BIO *b, BIO *bio)
  466. {
  467. BIO *lb;
  468. if (b == NULL)
  469. return bio;
  470. lb = b;
  471. while (lb->next_bio != NULL)
  472. lb = lb->next_bio;
  473. lb->next_bio = bio;
  474. if (bio != NULL)
  475. bio->prev_bio = lb;
  476. /* called to do internal processing */
  477. BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
  478. return b;
  479. }
  480. /* Remove the first and return the rest */
  481. BIO *BIO_pop(BIO *b)
  482. {
  483. BIO *ret;
  484. if (b == NULL) {
  485. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  486. return NULL;
  487. }
  488. ret = b->next_bio;
  489. BIO_ctrl(b, BIO_CTRL_POP, 0, b);
  490. if (b->prev_bio != NULL)
  491. b->prev_bio->next_bio = b->next_bio;
  492. if (b->next_bio != NULL)
  493. b->next_bio->prev_bio = b->prev_bio;
  494. b->next_bio = NULL;
  495. b->prev_bio = NULL;
  496. return ret;
  497. }
  498. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  499. {
  500. BIO *b, *last;
  501. b = last = bio;
  502. for (;;) {
  503. if (!BIO_should_retry(b))
  504. break;
  505. last = b;
  506. b = b->next_bio;
  507. if (b == NULL)
  508. break;
  509. }
  510. if (reason != NULL)
  511. *reason = last->retry_reason;
  512. return last;
  513. }
  514. int BIO_get_retry_reason(BIO *bio)
  515. {
  516. return bio->retry_reason;
  517. }
  518. void BIO_set_retry_reason(BIO *bio, int reason)
  519. {
  520. bio->retry_reason = reason;
  521. }
  522. BIO *BIO_find_type(BIO *bio, int type)
  523. {
  524. int mt, mask;
  525. if (bio == NULL) {
  526. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  527. return NULL;
  528. }
  529. mask = type & 0xff;
  530. do {
  531. if (bio->method != NULL) {
  532. mt = bio->method->type;
  533. if (!mask) {
  534. if (mt & type)
  535. return bio;
  536. } else if (mt == type) {
  537. return bio;
  538. }
  539. }
  540. bio = bio->next_bio;
  541. } while (bio != NULL);
  542. return NULL;
  543. }
  544. BIO *BIO_next(BIO *b)
  545. {
  546. if (b == NULL) {
  547. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  548. return NULL;
  549. }
  550. return b->next_bio;
  551. }
  552. void BIO_set_next(BIO *b, BIO *next)
  553. {
  554. b->next_bio = next;
  555. }
  556. void BIO_free_all(BIO *bio)
  557. {
  558. BIO *b;
  559. int ref;
  560. while (bio != NULL) {
  561. b = bio;
  562. ref = b->references;
  563. bio = bio->next_bio;
  564. BIO_free(b);
  565. /* Since ref count > 1, don't free anyone else. */
  566. if (ref > 1)
  567. break;
  568. }
  569. }
  570. BIO *BIO_dup_chain(BIO *in)
  571. {
  572. BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
  573. for (bio = in; bio != NULL; bio = bio->next_bio) {
  574. if ((new_bio = BIO_new(bio->method)) == NULL)
  575. goto err;
  576. new_bio->callback = bio->callback;
  577. new_bio->callback_ex = bio->callback_ex;
  578. new_bio->cb_arg = bio->cb_arg;
  579. new_bio->init = bio->init;
  580. new_bio->shutdown = bio->shutdown;
  581. new_bio->flags = bio->flags;
  582. /* This will let SSL_s_sock() work with stdin/stdout */
  583. new_bio->num = bio->num;
  584. if (!BIO_dup_state(bio, (char *)new_bio)) {
  585. BIO_free(new_bio);
  586. goto err;
  587. }
  588. /* copy app data */
  589. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
  590. &bio->ex_data)) {
  591. BIO_free(new_bio);
  592. goto err;
  593. }
  594. if (ret == NULL) {
  595. eoc = new_bio;
  596. ret = eoc;
  597. } else {
  598. BIO_push(eoc, new_bio);
  599. eoc = new_bio;
  600. }
  601. }
  602. return ret;
  603. err:
  604. BIO_free_all(ret);
  605. return NULL;
  606. }
  607. void BIO_copy_next_retry(BIO *b)
  608. {
  609. BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
  610. b->retry_reason = b->next_bio->retry_reason;
  611. }
  612. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  613. {
  614. return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
  615. }
  616. void *BIO_get_ex_data(const BIO *bio, int idx)
  617. {
  618. return CRYPTO_get_ex_data(&(bio->ex_data), idx);
  619. }
  620. uint64_t BIO_number_read(BIO *bio)
  621. {
  622. if (bio)
  623. return bio->num_read;
  624. return 0;
  625. }
  626. uint64_t BIO_number_written(BIO *bio)
  627. {
  628. if (bio)
  629. return bio->num_write;
  630. return 0;
  631. }
  632. void bio_free_ex_data(BIO *bio)
  633. {
  634. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  635. }
  636. void bio_cleanup(void)
  637. {
  638. #ifndef OPENSSL_NO_SOCK
  639. bio_sock_cleanup_int();
  640. CRYPTO_THREAD_lock_free(bio_lookup_lock);
  641. bio_lookup_lock = NULL;
  642. #endif
  643. CRYPTO_THREAD_lock_free(bio_type_lock);
  644. bio_type_lock = NULL;
  645. }
  646. /* Internal variant of the below BIO_wait() not calling BIOerr() */
  647. static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  648. {
  649. #ifndef OPENSSL_NO_SOCK
  650. int fd;
  651. #endif
  652. long sec_diff;
  653. if (max_time == 0) /* no timeout */
  654. return 1;
  655. #ifndef OPENSSL_NO_SOCK
  656. if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
  657. return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
  658. #endif
  659. /* fall back to polling since no sockets are available */
  660. sec_diff = (long)(max_time - time(NULL)); /* might overflow */
  661. if (sec_diff < 0)
  662. return 0; /* clearly timeout */
  663. /* now take a nap at most the given number of milliseconds */
  664. if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
  665. if (nap_milliseconds > 1000)
  666. nap_milliseconds = 1000;
  667. } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
  668. if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
  669. nap_milliseconds = (unsigned int)sec_diff * 1000;
  670. }
  671. ossl_sleep(nap_milliseconds);
  672. return 1;
  673. }
  674. /*-
  675. * Wait on (typically socket-based) BIO at most until max_time.
  676. * Succeed immediately if max_time == 0.
  677. * If sockets are not available support polling: succeed after waiting at most
  678. * the number of nap_milliseconds in order to avoid a tight busy loop.
  679. * Call BIOerr(...) on timeout or error.
  680. * Returns -1 on error, 0 on timeout, and 1 on success.
  681. */
  682. int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  683. {
  684. int rv = bio_wait(bio, max_time, nap_milliseconds);
  685. if (rv <= 0)
  686. ERR_raise(ERR_LIB_BIO,
  687. rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
  688. return rv;
  689. }
  690. /*
  691. * Connect via given BIO using BIO_do_connect() until success/timeout/error.
  692. * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
  693. * For non-blocking and potentially even non-socket BIOs perform polling with
  694. * the given density: between polls sleep nap_milliseconds using BIO_wait()
  695. * in order to avoid a tight busy loop.
  696. * Returns -1 on error, 0 on timeout, and 1 on success.
  697. */
  698. int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
  699. {
  700. int blocking = timeout <= 0;
  701. time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
  702. int rv;
  703. if (bio == NULL) {
  704. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  705. return -1;
  706. }
  707. if (nap_milliseconds < 0)
  708. nap_milliseconds = 100;
  709. BIO_set_nbio(bio, !blocking);
  710. retry:
  711. ERR_set_mark();
  712. rv = BIO_do_connect(bio);
  713. if (rv <= 0) { /* could be timeout or retryable error or fatal error */
  714. int err = ERR_peek_last_error();
  715. int reason = ERR_GET_REASON(err);
  716. int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
  717. if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
  718. switch (reason) {
  719. case ERR_R_SYS_LIB:
  720. /*
  721. * likely retryable system error occurred, which may be
  722. * EAGAIN (resource temporarily unavailable) some 40 secs after
  723. * calling getaddrinfo(): Temporary failure in name resolution
  724. * or a premature ETIMEDOUT, some 30 seconds after connect()
  725. */
  726. case BIO_R_CONNECT_ERROR:
  727. case BIO_R_NBIO_CONNECT_ERROR:
  728. /* some likely retryable connection error occurred */
  729. (void)BIO_reset(bio); /* often needed to avoid retry failure */
  730. do_retry = 1;
  731. break;
  732. default:
  733. break;
  734. }
  735. }
  736. if (timeout >= 0 && do_retry) {
  737. ERR_pop_to_mark();
  738. /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
  739. rv = bio_wait(bio, max_time, nap_milliseconds);
  740. if (rv > 0)
  741. goto retry;
  742. ERR_raise(ERR_LIB_BIO,
  743. rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
  744. } else {
  745. ERR_clear_last_mark();
  746. rv = -1;
  747. if (err == 0) /* missing error queue entry */
  748. /* workaround: general error */
  749. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  750. }
  751. } else {
  752. ERR_clear_last_mark();
  753. }
  754. return rv;
  755. }