2
0

bio_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_lcl.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, 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 && (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(const BIO_METHOD *method)
  59. {
  60. BIO *bio = OPENSSL_zalloc(sizeof(*bio));
  61. if (bio == NULL) {
  62. BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
  63. return NULL;
  64. }
  65. bio->method = method;
  66. bio->shutdown = 1;
  67. bio->references = 1;
  68. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
  69. goto err;
  70. bio->lock = CRYPTO_THREAD_lock_new();
  71. if (bio->lock == NULL) {
  72. BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
  73. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  74. goto err;
  75. }
  76. if (method->create != NULL && !method->create(bio)) {
  77. BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
  78. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  79. CRYPTO_THREAD_lock_free(bio->lock);
  80. goto err;
  81. }
  82. if (method->create == NULL)
  83. bio->init = 1;
  84. return bio;
  85. err:
  86. OPENSSL_free(bio);
  87. return NULL;
  88. }
  89. int BIO_free(BIO *a)
  90. {
  91. int ret;
  92. if (a == NULL)
  93. return 0;
  94. if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
  95. return 0;
  96. REF_PRINT_COUNT("BIO", a);
  97. if (ret > 0)
  98. return 1;
  99. REF_ASSERT_ISNT(ret < 0);
  100. if (a->callback != NULL || a->callback_ex != NULL) {
  101. ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
  102. if (ret <= 0)
  103. return ret;
  104. }
  105. if ((a->method != NULL) && (a->method->destroy != NULL))
  106. a->method->destroy(a);
  107. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
  108. CRYPTO_THREAD_lock_free(a->lock);
  109. OPENSSL_free(a);
  110. return 1;
  111. }
  112. void BIO_set_data(BIO *a, void *ptr)
  113. {
  114. a->ptr = ptr;
  115. }
  116. void *BIO_get_data(BIO *a)
  117. {
  118. return a->ptr;
  119. }
  120. void BIO_set_init(BIO *a, int init)
  121. {
  122. a->init = init;
  123. }
  124. int BIO_get_init(BIO *a)
  125. {
  126. return a->init;
  127. }
  128. void BIO_set_shutdown(BIO *a, int shut)
  129. {
  130. a->shutdown = shut;
  131. }
  132. int BIO_get_shutdown(BIO *a)
  133. {
  134. return a->shutdown;
  135. }
  136. void BIO_vfree(BIO *a)
  137. {
  138. BIO_free(a);
  139. }
  140. int BIO_up_ref(BIO *a)
  141. {
  142. int i;
  143. if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
  144. return 0;
  145. REF_PRINT_COUNT("BIO", a);
  146. REF_ASSERT_ISNT(i < 2);
  147. return ((i > 1) ? 1 : 0);
  148. }
  149. void BIO_clear_flags(BIO *b, int flags)
  150. {
  151. b->flags &= ~flags;
  152. }
  153. int BIO_test_flags(const BIO *b, int flags)
  154. {
  155. return (b->flags & flags);
  156. }
  157. void BIO_set_flags(BIO *b, int flags)
  158. {
  159. b->flags |= flags;
  160. }
  161. BIO_callback_fn BIO_get_callback(const BIO *b)
  162. {
  163. return b->callback;
  164. }
  165. void BIO_set_callback(BIO *b, BIO_callback_fn cb)
  166. {
  167. b->callback = cb;
  168. }
  169. BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
  170. {
  171. return b->callback_ex;
  172. }
  173. void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
  174. {
  175. b->callback_ex = cb;
  176. }
  177. void BIO_set_callback_arg(BIO *b, char *arg)
  178. {
  179. b->cb_arg = arg;
  180. }
  181. char *BIO_get_callback_arg(const BIO *b)
  182. {
  183. return b->cb_arg;
  184. }
  185. const char *BIO_method_name(const BIO *b)
  186. {
  187. return b->method->name;
  188. }
  189. int BIO_method_type(const BIO *b)
  190. {
  191. return b->method->type;
  192. }
  193. /*
  194. * This is essentially the same as BIO_read_ex() except that it allows
  195. * 0 or a negative value to indicate failure (retryable or not) in the return.
  196. * This is for compatibility with the old style BIO_read(), where existing code
  197. * may make assumptions about the return value that it might get.
  198. */
  199. static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
  200. {
  201. int ret;
  202. if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
  203. BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
  204. return -2;
  205. }
  206. if ((b->callback != NULL || b->callback_ex != NULL) &&
  207. ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
  208. NULL)) <= 0))
  209. return ret;
  210. if (!b->init) {
  211. BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
  212. return -2;
  213. }
  214. ret = b->method->bread(b, data, dlen, readbytes);
  215. if (ret > 0)
  216. b->num_read += (uint64_t)*readbytes;
  217. if (b->callback != NULL || b->callback_ex != NULL)
  218. ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
  219. dlen, 0, 0L, ret, readbytes);
  220. /* Shouldn't happen */
  221. if (ret > 0 && *readbytes > dlen) {
  222. BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
  223. return -1;
  224. }
  225. return ret;
  226. }
  227. int BIO_read(BIO *b, void *data, int dlen)
  228. {
  229. size_t readbytes;
  230. int ret;
  231. if (dlen < 0)
  232. return 0;
  233. ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
  234. if (ret > 0) {
  235. /* *readbytes should always be <= dlen */
  236. ret = (int)readbytes;
  237. }
  238. return ret;
  239. }
  240. int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
  241. {
  242. int ret;
  243. ret = bio_read_intern(b, data, dlen, readbytes);
  244. if (ret > 0)
  245. ret = 1;
  246. else
  247. ret = 0;
  248. return ret;
  249. }
  250. static int bio_write_intern(BIO *b, const void *data, size_t dlen,
  251. size_t *written)
  252. {
  253. int ret;
  254. if (b == NULL)
  255. return 0;
  256. if ((b->method == NULL) || (b->method->bwrite == NULL)) {
  257. BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
  258. return -2;
  259. }
  260. if ((b->callback != NULL || b->callback_ex != NULL) &&
  261. ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
  262. NULL)) <= 0))
  263. return ret;
  264. if (!b->init) {
  265. BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
  266. return -2;
  267. }
  268. ret = b->method->bwrite(b, data, dlen, written);
  269. if (ret > 0)
  270. b->num_write += (uint64_t)*written;
  271. if (b->callback != NULL || b->callback_ex != NULL)
  272. ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
  273. dlen, 0, 0L, ret, written);
  274. return ret;
  275. }
  276. int BIO_write(BIO *b, const void *data, int dlen)
  277. {
  278. size_t written;
  279. int ret;
  280. if (dlen < 0)
  281. return 0;
  282. ret = bio_write_intern(b, data, (size_t)dlen, &written);
  283. if (ret > 0) {
  284. /* *written should always be <= dlen */
  285. ret = (int)written;
  286. }
  287. return ret;
  288. }
  289. int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
  290. {
  291. int ret;
  292. ret = bio_write_intern(b, data, dlen, written);
  293. if (ret > 0)
  294. ret = 1;
  295. else
  296. ret = 0;
  297. return ret;
  298. }
  299. int BIO_puts(BIO *b, const char *buf)
  300. {
  301. int ret;
  302. size_t written = 0;
  303. if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
  304. BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
  305. return -2;
  306. }
  307. if (b->callback != NULL || b->callback_ex != NULL) {
  308. ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
  309. if (ret <= 0)
  310. return ret;
  311. }
  312. if (!b->init) {
  313. BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
  314. return -2;
  315. }
  316. ret = b->method->bputs(b, buf);
  317. if (ret > 0) {
  318. b->num_write += (uint64_t)ret;
  319. written = ret;
  320. ret = 1;
  321. }
  322. if (b->callback != NULL || b->callback_ex != NULL)
  323. ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
  324. 0L, ret, &written);
  325. if (ret > 0) {
  326. if (written > INT_MAX) {
  327. BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
  328. ret = -1;
  329. } else {
  330. ret = (int)written;
  331. }
  332. }
  333. return ret;
  334. }
  335. int BIO_gets(BIO *b, char *buf, int size)
  336. {
  337. int ret;
  338. size_t readbytes = 0;
  339. if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
  340. BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
  341. return -2;
  342. }
  343. if (size < 0) {
  344. BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
  345. return 0;
  346. }
  347. if (b->callback != NULL || b->callback_ex != NULL) {
  348. ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
  349. if (ret <= 0)
  350. return ret;
  351. }
  352. if (!b->init) {
  353. BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
  354. return -2;
  355. }
  356. ret = b->method->bgets(b, buf, size);
  357. if (ret > 0) {
  358. readbytes = ret;
  359. ret = 1;
  360. }
  361. if (b->callback != NULL || b->callback_ex != NULL)
  362. ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
  363. 0, 0L, ret, &readbytes);
  364. if (ret > 0) {
  365. /* Shouldn't happen */
  366. if (readbytes > (size_t)size)
  367. ret = -1;
  368. else
  369. ret = (int)readbytes;
  370. }
  371. return ret;
  372. }
  373. int BIO_indent(BIO *b, int indent, int max)
  374. {
  375. if (indent < 0)
  376. indent = 0;
  377. if (indent > max)
  378. indent = max;
  379. while (indent--)
  380. if (BIO_puts(b, " ") != 1)
  381. return 0;
  382. return 1;
  383. }
  384. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  385. {
  386. int i;
  387. i = iarg;
  388. return BIO_ctrl(b, cmd, larg, (char *)&i);
  389. }
  390. void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  391. {
  392. void *p = NULL;
  393. if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
  394. return NULL;
  395. else
  396. return p;
  397. }
  398. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  399. {
  400. long ret;
  401. if (b == NULL)
  402. return 0;
  403. if ((b->method == NULL) || (b->method->ctrl == NULL)) {
  404. BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
  405. return -2;
  406. }
  407. if (b->callback != NULL || b->callback_ex != NULL) {
  408. ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
  409. if (ret <= 0)
  410. return ret;
  411. }
  412. ret = b->method->ctrl(b, cmd, larg, parg);
  413. if (b->callback != NULL || b->callback_ex != NULL)
  414. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
  415. larg, ret, NULL);
  416. return ret;
  417. }
  418. long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  419. {
  420. long ret;
  421. if (b == NULL)
  422. return 0;
  423. if ((b->method == NULL) || (b->method->callback_ctrl == NULL)
  424. || (cmd != BIO_CTRL_SET_CALLBACK)) {
  425. BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
  426. return -2;
  427. }
  428. if (b->callback != NULL || b->callback_ex != NULL) {
  429. ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
  430. NULL);
  431. if (ret <= 0)
  432. return ret;
  433. }
  434. ret = b->method->callback_ctrl(b, cmd, fp);
  435. if (b->callback != NULL || b->callback_ex != NULL)
  436. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
  437. cmd, 0, ret, NULL);
  438. return ret;
  439. }
  440. /*
  441. * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  442. * do; but those macros have inappropriate return type, and for interfacing
  443. * from other programming languages, C macros aren't much of a help anyway.
  444. */
  445. size_t BIO_ctrl_pending(BIO *bio)
  446. {
  447. return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  448. }
  449. size_t BIO_ctrl_wpending(BIO *bio)
  450. {
  451. return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  452. }
  453. /* put the 'bio' on the end of b's list of operators */
  454. BIO *BIO_push(BIO *b, BIO *bio)
  455. {
  456. BIO *lb;
  457. if (b == NULL)
  458. return bio;
  459. lb = b;
  460. while (lb->next_bio != NULL)
  461. lb = lb->next_bio;
  462. lb->next_bio = bio;
  463. if (bio != NULL)
  464. bio->prev_bio = lb;
  465. /* called to do internal processing */
  466. BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
  467. return b;
  468. }
  469. /* Remove the first and return the rest */
  470. BIO *BIO_pop(BIO *b)
  471. {
  472. BIO *ret;
  473. if (b == NULL)
  474. return NULL;
  475. ret = b->next_bio;
  476. BIO_ctrl(b, BIO_CTRL_POP, 0, b);
  477. if (b->prev_bio != NULL)
  478. b->prev_bio->next_bio = b->next_bio;
  479. if (b->next_bio != NULL)
  480. b->next_bio->prev_bio = b->prev_bio;
  481. b->next_bio = NULL;
  482. b->prev_bio = NULL;
  483. return ret;
  484. }
  485. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  486. {
  487. BIO *b, *last;
  488. b = last = bio;
  489. for (;;) {
  490. if (!BIO_should_retry(b))
  491. break;
  492. last = b;
  493. b = b->next_bio;
  494. if (b == NULL)
  495. break;
  496. }
  497. if (reason != NULL)
  498. *reason = last->retry_reason;
  499. return last;
  500. }
  501. int BIO_get_retry_reason(BIO *bio)
  502. {
  503. return bio->retry_reason;
  504. }
  505. void BIO_set_retry_reason(BIO *bio, int reason)
  506. {
  507. bio->retry_reason = reason;
  508. }
  509. BIO *BIO_find_type(BIO *bio, int type)
  510. {
  511. int mt, mask;
  512. if (bio == NULL)
  513. return NULL;
  514. mask = type & 0xff;
  515. do {
  516. if (bio->method != NULL) {
  517. mt = bio->method->type;
  518. if (!mask) {
  519. if (mt & type)
  520. return bio;
  521. } else if (mt == type)
  522. return bio;
  523. }
  524. bio = bio->next_bio;
  525. } while (bio != NULL);
  526. return NULL;
  527. }
  528. BIO *BIO_next(BIO *b)
  529. {
  530. if (b == NULL)
  531. return NULL;
  532. return b->next_bio;
  533. }
  534. void BIO_set_next(BIO *b, BIO *next)
  535. {
  536. b->next_bio = next;
  537. }
  538. void BIO_free_all(BIO *bio)
  539. {
  540. BIO *b;
  541. int ref;
  542. while (bio != NULL) {
  543. b = bio;
  544. ref = b->references;
  545. bio = bio->next_bio;
  546. BIO_free(b);
  547. /* Since ref count > 1, don't free anyone else. */
  548. if (ref > 1)
  549. break;
  550. }
  551. }
  552. BIO *BIO_dup_chain(BIO *in)
  553. {
  554. BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
  555. for (bio = in; bio != NULL; bio = bio->next_bio) {
  556. if ((new_bio = BIO_new(bio->method)) == NULL)
  557. goto err;
  558. new_bio->callback = bio->callback;
  559. new_bio->callback_ex = bio->callback_ex;
  560. new_bio->cb_arg = bio->cb_arg;
  561. new_bio->init = bio->init;
  562. new_bio->shutdown = bio->shutdown;
  563. new_bio->flags = bio->flags;
  564. /* This will let SSL_s_sock() work with stdin/stdout */
  565. new_bio->num = bio->num;
  566. if (!BIO_dup_state(bio, (char *)new_bio)) {
  567. BIO_free(new_bio);
  568. goto err;
  569. }
  570. /* copy app data */
  571. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
  572. &bio->ex_data)) {
  573. BIO_free(new_bio);
  574. goto err;
  575. }
  576. if (ret == NULL) {
  577. eoc = new_bio;
  578. ret = eoc;
  579. } else {
  580. BIO_push(eoc, new_bio);
  581. eoc = new_bio;
  582. }
  583. }
  584. return ret;
  585. err:
  586. BIO_free_all(ret);
  587. return NULL;
  588. }
  589. void BIO_copy_next_retry(BIO *b)
  590. {
  591. BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
  592. b->retry_reason = b->next_bio->retry_reason;
  593. }
  594. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  595. {
  596. return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
  597. }
  598. void *BIO_get_ex_data(BIO *bio, int idx)
  599. {
  600. return CRYPTO_get_ex_data(&(bio->ex_data), idx);
  601. }
  602. uint64_t BIO_number_read(BIO *bio)
  603. {
  604. if (bio)
  605. return bio->num_read;
  606. return 0;
  607. }
  608. uint64_t BIO_number_written(BIO *bio)
  609. {
  610. if (bio)
  611. return bio->num_write;
  612. return 0;
  613. }
  614. void bio_free_ex_data(BIO *bio)
  615. {
  616. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  617. }
  618. void bio_cleanup(void)
  619. {
  620. #ifndef OPENSSL_NO_SOCK
  621. bio_sock_cleanup_int();
  622. CRYPTO_THREAD_lock_free(bio_lookup_lock);
  623. bio_lookup_lock = NULL;
  624. #endif
  625. CRYPTO_THREAD_lock_free(bio_type_lock);
  626. bio_type_lock = NULL;
  627. }