bio_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. size_t local_written;
  269. int ret;
  270. if (written != NULL)
  271. *written = 0;
  272. /*
  273. * b == NULL is not an error but just means that zero bytes are written.
  274. * Do not raise an error here.
  275. */
  276. if (b == NULL)
  277. return 0;
  278. if (b->method == NULL || b->method->bwrite == NULL) {
  279. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  280. return -2;
  281. }
  282. if (HAS_CALLBACK(b) &&
  283. ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
  284. NULL)) <= 0))
  285. return ret;
  286. if (!b->init) {
  287. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  288. return -1;
  289. }
  290. ret = b->method->bwrite(b, data, dlen, &local_written);
  291. if (ret > 0)
  292. b->num_write += (uint64_t)local_written;
  293. if (HAS_CALLBACK(b))
  294. ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
  295. dlen, 0, 0L, ret, &local_written);
  296. if (written != NULL)
  297. *written = local_written;
  298. return ret;
  299. }
  300. int BIO_write(BIO *b, const void *data, int dlen)
  301. {
  302. size_t written;
  303. int ret;
  304. if (dlen <= 0)
  305. return 0;
  306. ret = bio_write_intern(b, data, (size_t)dlen, &written);
  307. if (ret > 0) {
  308. /* written should always be <= dlen */
  309. ret = (int)written;
  310. }
  311. return ret;
  312. }
  313. int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
  314. {
  315. return bio_write_intern(b, data, dlen, written) > 0
  316. || (b != NULL && dlen == 0); /* order is important for *written */
  317. }
  318. int BIO_puts(BIO *b, const char *buf)
  319. {
  320. int ret;
  321. size_t written = 0;
  322. if (b == NULL) {
  323. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  324. return -1;
  325. }
  326. if (b->method == NULL || b->method->bputs == NULL) {
  327. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  328. return -2;
  329. }
  330. if (HAS_CALLBACK(b)) {
  331. ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
  332. if (ret <= 0)
  333. return ret;
  334. }
  335. if (!b->init) {
  336. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  337. return -1;
  338. }
  339. ret = b->method->bputs(b, buf);
  340. if (ret > 0) {
  341. b->num_write += (uint64_t)ret;
  342. written = ret;
  343. ret = 1;
  344. }
  345. if (HAS_CALLBACK(b))
  346. ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
  347. 0L, ret, &written);
  348. if (ret > 0) {
  349. if (written > INT_MAX) {
  350. ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
  351. ret = -1;
  352. } else {
  353. ret = (int)written;
  354. }
  355. }
  356. return ret;
  357. }
  358. int BIO_gets(BIO *b, char *buf, int size)
  359. {
  360. int ret;
  361. size_t readbytes = 0;
  362. if (b == NULL) {
  363. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  364. return -1;
  365. }
  366. if (b->method == NULL || b->method->bgets == NULL) {
  367. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  368. return -2;
  369. }
  370. if (size < 0) {
  371. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  372. return -1;
  373. }
  374. if (HAS_CALLBACK(b)) {
  375. ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
  376. if (ret <= 0)
  377. return ret;
  378. }
  379. if (!b->init) {
  380. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  381. return -1;
  382. }
  383. ret = b->method->bgets(b, buf, size);
  384. if (ret > 0) {
  385. readbytes = ret;
  386. ret = 1;
  387. }
  388. if (HAS_CALLBACK(b))
  389. ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
  390. 0, 0L, ret, &readbytes);
  391. if (ret > 0) {
  392. /* Shouldn't happen */
  393. if (readbytes > (size_t)size)
  394. ret = -1;
  395. else
  396. ret = (int)readbytes;
  397. }
  398. return ret;
  399. }
  400. int BIO_get_line(BIO *bio, char *buf, int size)
  401. {
  402. int ret = 0;
  403. char *ptr = buf;
  404. if (buf == NULL) {
  405. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  406. return -1;
  407. }
  408. if (size <= 0) {
  409. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  410. return -1;
  411. }
  412. *buf = '\0';
  413. if (bio == NULL) {
  414. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  415. return -1;
  416. }
  417. if (!bio->init) {
  418. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  419. return -1;
  420. }
  421. while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0)
  422. if (*ptr++ == '\n')
  423. break;
  424. *ptr = '\0';
  425. return ret > 0 || BIO_eof(bio) ? ptr - buf : ret;
  426. }
  427. int BIO_indent(BIO *b, int indent, int max)
  428. {
  429. if (indent < 0)
  430. indent = 0;
  431. if (indent > max)
  432. indent = max;
  433. while (indent--)
  434. if (BIO_puts(b, " ") != 1)
  435. return 0;
  436. return 1;
  437. }
  438. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  439. {
  440. int i;
  441. i = iarg;
  442. return BIO_ctrl(b, cmd, larg, (char *)&i);
  443. }
  444. void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  445. {
  446. void *p = NULL;
  447. if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
  448. return NULL;
  449. else
  450. return p;
  451. }
  452. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  453. {
  454. long ret;
  455. if (b == NULL) {
  456. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  457. return -1;
  458. }
  459. if (b->method == NULL || b->method->ctrl == NULL) {
  460. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  461. return -2;
  462. }
  463. if (HAS_CALLBACK(b)) {
  464. ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
  465. if (ret <= 0)
  466. return ret;
  467. }
  468. ret = b->method->ctrl(b, cmd, larg, parg);
  469. if (HAS_CALLBACK(b))
  470. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
  471. larg, ret, NULL);
  472. return ret;
  473. }
  474. long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  475. {
  476. long ret;
  477. if (b == NULL) {
  478. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  479. return -2;
  480. }
  481. if (b->method == NULL || b->method->callback_ctrl == NULL
  482. || cmd != BIO_CTRL_SET_CALLBACK) {
  483. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  484. return -2;
  485. }
  486. if (HAS_CALLBACK(b)) {
  487. ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
  488. NULL);
  489. if (ret <= 0)
  490. return ret;
  491. }
  492. ret = b->method->callback_ctrl(b, cmd, fp);
  493. if (HAS_CALLBACK(b))
  494. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
  495. cmd, 0, ret, NULL);
  496. return ret;
  497. }
  498. /*
  499. * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  500. * do; but those macros have inappropriate return type, and for interfacing
  501. * from other programming languages, C macros aren't much of a help anyway.
  502. */
  503. size_t BIO_ctrl_pending(BIO *bio)
  504. {
  505. return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  506. }
  507. size_t BIO_ctrl_wpending(BIO *bio)
  508. {
  509. return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  510. }
  511. /* put the 'bio' on the end of b's list of operators */
  512. BIO *BIO_push(BIO *b, BIO *bio)
  513. {
  514. BIO *lb;
  515. if (b == NULL)
  516. return bio;
  517. lb = b;
  518. while (lb->next_bio != NULL)
  519. lb = lb->next_bio;
  520. lb->next_bio = bio;
  521. if (bio != NULL)
  522. bio->prev_bio = lb;
  523. /* called to do internal processing */
  524. BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
  525. return b;
  526. }
  527. /* Remove the first and return the rest */
  528. BIO *BIO_pop(BIO *b)
  529. {
  530. BIO *ret;
  531. if (b == NULL) {
  532. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  533. return NULL;
  534. }
  535. ret = b->next_bio;
  536. BIO_ctrl(b, BIO_CTRL_POP, 0, b);
  537. if (b->prev_bio != NULL)
  538. b->prev_bio->next_bio = b->next_bio;
  539. if (b->next_bio != NULL)
  540. b->next_bio->prev_bio = b->prev_bio;
  541. b->next_bio = NULL;
  542. b->prev_bio = NULL;
  543. return ret;
  544. }
  545. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  546. {
  547. BIO *b, *last;
  548. b = last = bio;
  549. for (;;) {
  550. if (!BIO_should_retry(b))
  551. break;
  552. last = b;
  553. b = b->next_bio;
  554. if (b == NULL)
  555. break;
  556. }
  557. if (reason != NULL)
  558. *reason = last->retry_reason;
  559. return last;
  560. }
  561. int BIO_get_retry_reason(BIO *bio)
  562. {
  563. return bio->retry_reason;
  564. }
  565. void BIO_set_retry_reason(BIO *bio, int reason)
  566. {
  567. bio->retry_reason = reason;
  568. }
  569. BIO *BIO_find_type(BIO *bio, int type)
  570. {
  571. int mt, mask;
  572. if (bio == NULL) {
  573. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  574. return NULL;
  575. }
  576. mask = type & 0xff;
  577. do {
  578. if (bio->method != NULL) {
  579. mt = bio->method->type;
  580. if (!mask) {
  581. if (mt & type)
  582. return bio;
  583. } else if (mt == type) {
  584. return bio;
  585. }
  586. }
  587. bio = bio->next_bio;
  588. } while (bio != NULL);
  589. return NULL;
  590. }
  591. BIO *BIO_next(BIO *b)
  592. {
  593. if (b == NULL) {
  594. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  595. return NULL;
  596. }
  597. return b->next_bio;
  598. }
  599. void BIO_set_next(BIO *b, BIO *next)
  600. {
  601. b->next_bio = next;
  602. }
  603. void BIO_free_all(BIO *bio)
  604. {
  605. BIO *b;
  606. int ref;
  607. while (bio != NULL) {
  608. b = bio;
  609. ref = b->references;
  610. bio = bio->next_bio;
  611. BIO_free(b);
  612. /* Since ref count > 1, don't free anyone else. */
  613. if (ref > 1)
  614. break;
  615. }
  616. }
  617. BIO *BIO_dup_chain(BIO *in)
  618. {
  619. BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
  620. for (bio = in; bio != NULL; bio = bio->next_bio) {
  621. if ((new_bio = BIO_new(bio->method)) == NULL)
  622. goto err;
  623. #ifndef OPENSSL_NO_DEPRECATED_3_0
  624. new_bio->callback = bio->callback;
  625. #endif
  626. new_bio->callback_ex = bio->callback_ex;
  627. new_bio->cb_arg = bio->cb_arg;
  628. new_bio->init = bio->init;
  629. new_bio->shutdown = bio->shutdown;
  630. new_bio->flags = bio->flags;
  631. /* This will let SSL_s_sock() work with stdin/stdout */
  632. new_bio->num = bio->num;
  633. if (!BIO_dup_state(bio, (char *)new_bio)) {
  634. BIO_free(new_bio);
  635. goto err;
  636. }
  637. /* copy app data */
  638. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
  639. &bio->ex_data)) {
  640. BIO_free(new_bio);
  641. goto err;
  642. }
  643. if (ret == NULL) {
  644. eoc = new_bio;
  645. ret = eoc;
  646. } else {
  647. BIO_push(eoc, new_bio);
  648. eoc = new_bio;
  649. }
  650. }
  651. return ret;
  652. err:
  653. BIO_free_all(ret);
  654. return NULL;
  655. }
  656. void BIO_copy_next_retry(BIO *b)
  657. {
  658. BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
  659. b->retry_reason = b->next_bio->retry_reason;
  660. }
  661. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  662. {
  663. return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
  664. }
  665. void *BIO_get_ex_data(const BIO *bio, int idx)
  666. {
  667. return CRYPTO_get_ex_data(&(bio->ex_data), idx);
  668. }
  669. uint64_t BIO_number_read(BIO *bio)
  670. {
  671. if (bio)
  672. return bio->num_read;
  673. return 0;
  674. }
  675. uint64_t BIO_number_written(BIO *bio)
  676. {
  677. if (bio)
  678. return bio->num_write;
  679. return 0;
  680. }
  681. void bio_free_ex_data(BIO *bio)
  682. {
  683. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  684. }
  685. void bio_cleanup(void)
  686. {
  687. #ifndef OPENSSL_NO_SOCK
  688. bio_sock_cleanup_int();
  689. CRYPTO_THREAD_lock_free(bio_lookup_lock);
  690. bio_lookup_lock = NULL;
  691. #endif
  692. CRYPTO_THREAD_lock_free(bio_type_lock);
  693. bio_type_lock = NULL;
  694. }
  695. /* Internal variant of the below BIO_wait() not calling BIOerr() */
  696. static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  697. {
  698. #ifndef OPENSSL_NO_SOCK
  699. int fd;
  700. #endif
  701. long sec_diff;
  702. if (max_time == 0) /* no timeout */
  703. return 1;
  704. #ifndef OPENSSL_NO_SOCK
  705. if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
  706. return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
  707. #endif
  708. /* fall back to polling since no sockets are available */
  709. sec_diff = (long)(max_time - time(NULL)); /* might overflow */
  710. if (sec_diff < 0)
  711. return 0; /* clearly timeout */
  712. /* now take a nap at most the given number of milliseconds */
  713. if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
  714. if (nap_milliseconds > 1000)
  715. nap_milliseconds = 1000;
  716. } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
  717. if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
  718. nap_milliseconds = (unsigned int)sec_diff * 1000;
  719. }
  720. ossl_sleep(nap_milliseconds);
  721. return 1;
  722. }
  723. /*-
  724. * Wait on (typically socket-based) BIO at most until max_time.
  725. * Succeed immediately if max_time == 0.
  726. * If sockets are not available support polling: succeed after waiting at most
  727. * the number of nap_milliseconds in order to avoid a tight busy loop.
  728. * Call BIOerr(...) on timeout or error.
  729. * Returns -1 on error, 0 on timeout, and 1 on success.
  730. */
  731. int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  732. {
  733. int rv = bio_wait(bio, max_time, nap_milliseconds);
  734. if (rv <= 0)
  735. ERR_raise(ERR_LIB_BIO,
  736. rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
  737. return rv;
  738. }
  739. /*
  740. * Connect via given BIO using BIO_do_connect() until success/timeout/error.
  741. * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
  742. * For non-blocking and potentially even non-socket BIOs perform polling with
  743. * the given density: between polls sleep nap_milliseconds using BIO_wait()
  744. * in order to avoid a tight busy loop.
  745. * Returns -1 on error, 0 on timeout, and 1 on success.
  746. */
  747. int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
  748. {
  749. int blocking = timeout <= 0;
  750. time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
  751. int rv;
  752. if (bio == NULL) {
  753. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  754. return -1;
  755. }
  756. if (nap_milliseconds < 0)
  757. nap_milliseconds = 100;
  758. BIO_set_nbio(bio, !blocking);
  759. retry:
  760. ERR_set_mark();
  761. rv = BIO_do_connect(bio);
  762. if (rv <= 0) { /* could be timeout or retryable error or fatal error */
  763. int err = ERR_peek_last_error();
  764. int reason = ERR_GET_REASON(err);
  765. int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
  766. if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
  767. switch (reason) {
  768. case ERR_R_SYS_LIB:
  769. /*
  770. * likely retryable system error occurred, which may be
  771. * EAGAIN (resource temporarily unavailable) some 40 secs after
  772. * calling getaddrinfo(): Temporary failure in name resolution
  773. * or a premature ETIMEDOUT, some 30 seconds after connect()
  774. */
  775. case BIO_R_CONNECT_ERROR:
  776. case BIO_R_NBIO_CONNECT_ERROR:
  777. /* some likely retryable connection error occurred */
  778. (void)BIO_reset(bio); /* often needed to avoid retry failure */
  779. do_retry = 1;
  780. break;
  781. default:
  782. break;
  783. }
  784. }
  785. if (timeout >= 0 && do_retry) {
  786. ERR_pop_to_mark();
  787. /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
  788. rv = bio_wait(bio, max_time, nap_milliseconds);
  789. if (rv > 0)
  790. goto retry;
  791. ERR_raise(ERR_LIB_BIO,
  792. rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
  793. } else {
  794. ERR_clear_last_mark();
  795. rv = -1;
  796. if (err == 0) /* missing error queue entry */
  797. /* workaround: general error */
  798. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  799. }
  800. } else {
  801. ERR_clear_last_mark();
  802. }
  803. return rv;
  804. }