bio_lib.c 22 KB

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