bio_lib.c 22 KB

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