bss_bio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. /*
  56. * Special method for a BIO where the other endpoint is also a BIO of this
  57. * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
  58. * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
  59. * library with I/O interfaces for which no specific BIO method is available.
  60. * See ssl/ssltest.c for some hints on how this can be used.
  61. */
  62. /* BIO_DEBUG implies BIO_PAIR_DEBUG */
  63. #ifdef BIO_DEBUG
  64. # ifndef BIO_PAIR_DEBUG
  65. # define BIO_PAIR_DEBUG
  66. # endif
  67. #endif
  68. /* disable assert() unless BIO_PAIR_DEBUG has been defined */
  69. #ifndef BIO_PAIR_DEBUG
  70. # ifndef NDEBUG
  71. # define NDEBUG
  72. # endif
  73. #endif
  74. #include <assert.h>
  75. #include <limits.h>
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include <openssl/bio.h>
  79. #include <openssl/err.h>
  80. #include <openssl/crypto.h>
  81. #include "e_os.h"
  82. /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
  83. #if defined(OPENSSL_SYS_VXWORKS)
  84. # undef SSIZE_MAX
  85. #endif
  86. #ifndef SSIZE_MAX
  87. # define SSIZE_MAX INT_MAX
  88. #endif
  89. static int bio_new(BIO *bio);
  90. static int bio_free(BIO *bio);
  91. static int bio_read(BIO *bio, char *buf, int size);
  92. static int bio_write(BIO *bio, const char *buf, int num);
  93. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
  94. static int bio_puts(BIO *bio, const char *str);
  95. static int bio_make_pair(BIO *bio1, BIO *bio2);
  96. static void bio_destroy_pair(BIO *bio);
  97. static BIO_METHOD methods_biop = {
  98. BIO_TYPE_BIO,
  99. "BIO pair",
  100. bio_write,
  101. bio_read,
  102. bio_puts,
  103. NULL /* no bio_gets */ ,
  104. bio_ctrl,
  105. bio_new,
  106. bio_free,
  107. NULL /* no bio_callback_ctrl */
  108. };
  109. BIO_METHOD *BIO_s_bio(void)
  110. {
  111. return &methods_biop;
  112. }
  113. struct bio_bio_st {
  114. BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
  115. * peer->ptr is also a bio_bio_st, and its
  116. * "peer" member points back to us. peer !=
  117. * NULL iff init != 0 in the BIO. */
  118. /* This is for what we write (i.e. reading uses peer's struct): */
  119. int closed; /* valid iff peer != NULL */
  120. size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
  121. size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
  122. size_t size;
  123. char *buf; /* "size" elements (if != NULL) */
  124. size_t request; /* valid iff peer != NULL; 0 if len != 0,
  125. * otherwise set by peer to number of bytes
  126. * it (unsuccessfully) tried to read, never
  127. * more than buffer space (size-len)
  128. * warrants. */
  129. };
  130. static int bio_new(BIO *bio)
  131. {
  132. struct bio_bio_st *b;
  133. b = OPENSSL_malloc(sizeof *b);
  134. if (b == NULL)
  135. return 0;
  136. b->peer = NULL;
  137. /* enough for one TLS record (just a default) */
  138. b->size = 17 * 1024;
  139. b->buf = NULL;
  140. bio->ptr = b;
  141. return 1;
  142. }
  143. static int bio_free(BIO *bio)
  144. {
  145. struct bio_bio_st *b;
  146. if (bio == NULL)
  147. return 0;
  148. b = bio->ptr;
  149. assert(b != NULL);
  150. if (b->peer)
  151. bio_destroy_pair(bio);
  152. if (b->buf != NULL) {
  153. OPENSSL_free(b->buf);
  154. }
  155. OPENSSL_free(b);
  156. return 1;
  157. }
  158. static int bio_read(BIO *bio, char *buf, int size_)
  159. {
  160. size_t size = size_;
  161. size_t rest;
  162. struct bio_bio_st *b, *peer_b;
  163. BIO_clear_retry_flags(bio);
  164. if (!bio->init)
  165. return 0;
  166. b = bio->ptr;
  167. assert(b != NULL);
  168. assert(b->peer != NULL);
  169. peer_b = b->peer->ptr;
  170. assert(peer_b != NULL);
  171. assert(peer_b->buf != NULL);
  172. peer_b->request = 0; /* will be set in "retry_read" situation */
  173. if (buf == NULL || size == 0)
  174. return 0;
  175. if (peer_b->len == 0) {
  176. if (peer_b->closed)
  177. return 0; /* writer has closed, and no data is left */
  178. else {
  179. BIO_set_retry_read(bio); /* buffer is empty */
  180. if (size <= peer_b->size)
  181. peer_b->request = size;
  182. else
  183. /*
  184. * don't ask for more than the peer can deliver in one write
  185. */
  186. peer_b->request = peer_b->size;
  187. return -1;
  188. }
  189. }
  190. /* we can read */
  191. if (peer_b->len < size)
  192. size = peer_b->len;
  193. /* now read "size" bytes */
  194. rest = size;
  195. assert(rest > 0);
  196. do { /* one or two iterations */
  197. size_t chunk;
  198. assert(rest <= peer_b->len);
  199. if (peer_b->offset + rest <= peer_b->size)
  200. chunk = rest;
  201. else
  202. /* wrap around ring buffer */
  203. chunk = peer_b->size - peer_b->offset;
  204. assert(peer_b->offset + chunk <= peer_b->size);
  205. memcpy(buf, peer_b->buf + peer_b->offset, chunk);
  206. peer_b->len -= chunk;
  207. if (peer_b->len) {
  208. peer_b->offset += chunk;
  209. assert(peer_b->offset <= peer_b->size);
  210. if (peer_b->offset == peer_b->size)
  211. peer_b->offset = 0;
  212. buf += chunk;
  213. } else {
  214. /* buffer now empty, no need to advance "buf" */
  215. assert(chunk == rest);
  216. peer_b->offset = 0;
  217. }
  218. rest -= chunk;
  219. }
  220. while (rest);
  221. return size;
  222. }
  223. /*-
  224. * non-copying interface: provide pointer to available data in buffer
  225. * bio_nread0: return number of available bytes
  226. * bio_nread: also advance index
  227. * (example usage: bio_nread0(), read from buffer, bio_nread()
  228. * or just bio_nread(), read from buffer)
  229. */
  230. /*
  231. * WARNING: The non-copying interface is largely untested as of yet and may
  232. * contain bugs.
  233. */
  234. static ssize_t bio_nread0(BIO *bio, char **buf)
  235. {
  236. struct bio_bio_st *b, *peer_b;
  237. ssize_t num;
  238. BIO_clear_retry_flags(bio);
  239. if (!bio->init)
  240. return 0;
  241. b = bio->ptr;
  242. assert(b != NULL);
  243. assert(b->peer != NULL);
  244. peer_b = b->peer->ptr;
  245. assert(peer_b != NULL);
  246. assert(peer_b->buf != NULL);
  247. peer_b->request = 0;
  248. if (peer_b->len == 0) {
  249. char dummy;
  250. /* avoid code duplication -- nothing available for reading */
  251. return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
  252. }
  253. num = peer_b->len;
  254. if (peer_b->size < peer_b->offset + num)
  255. /* no ring buffer wrap-around for non-copying interface */
  256. num = peer_b->size - peer_b->offset;
  257. assert(num > 0);
  258. if (buf != NULL)
  259. *buf = peer_b->buf + peer_b->offset;
  260. return num;
  261. }
  262. static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
  263. {
  264. struct bio_bio_st *b, *peer_b;
  265. ssize_t num, available;
  266. if (num_ > SSIZE_MAX)
  267. num = SSIZE_MAX;
  268. else
  269. num = (ssize_t) num_;
  270. available = bio_nread0(bio, buf);
  271. if (num > available)
  272. num = available;
  273. if (num <= 0)
  274. return num;
  275. b = bio->ptr;
  276. peer_b = b->peer->ptr;
  277. peer_b->len -= num;
  278. if (peer_b->len) {
  279. peer_b->offset += num;
  280. assert(peer_b->offset <= peer_b->size);
  281. if (peer_b->offset == peer_b->size)
  282. peer_b->offset = 0;
  283. } else
  284. peer_b->offset = 0;
  285. return num;
  286. }
  287. static int bio_write(BIO *bio, const char *buf, int num_)
  288. {
  289. size_t num = num_;
  290. size_t rest;
  291. struct bio_bio_st *b;
  292. BIO_clear_retry_flags(bio);
  293. if (!bio->init || buf == NULL || num == 0)
  294. return 0;
  295. b = bio->ptr;
  296. assert(b != NULL);
  297. assert(b->peer != NULL);
  298. assert(b->buf != NULL);
  299. b->request = 0;
  300. if (b->closed) {
  301. /* we already closed */
  302. BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
  303. return -1;
  304. }
  305. assert(b->len <= b->size);
  306. if (b->len == b->size) {
  307. BIO_set_retry_write(bio); /* buffer is full */
  308. return -1;
  309. }
  310. /* we can write */
  311. if (num > b->size - b->len)
  312. num = b->size - b->len;
  313. /* now write "num" bytes */
  314. rest = num;
  315. assert(rest > 0);
  316. do { /* one or two iterations */
  317. size_t write_offset;
  318. size_t chunk;
  319. assert(b->len + rest <= b->size);
  320. write_offset = b->offset + b->len;
  321. if (write_offset >= b->size)
  322. write_offset -= b->size;
  323. /* b->buf[write_offset] is the first byte we can write to. */
  324. if (write_offset + rest <= b->size)
  325. chunk = rest;
  326. else
  327. /* wrap around ring buffer */
  328. chunk = b->size - write_offset;
  329. memcpy(b->buf + write_offset, buf, chunk);
  330. b->len += chunk;
  331. assert(b->len <= b->size);
  332. rest -= chunk;
  333. buf += chunk;
  334. }
  335. while (rest);
  336. return num;
  337. }
  338. /*-
  339. * non-copying interface: provide pointer to region to write to
  340. * bio_nwrite0: check how much space is available
  341. * bio_nwrite: also increase length
  342. * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
  343. * or just bio_nwrite(), write to buffer)
  344. */
  345. static ssize_t bio_nwrite0(BIO *bio, char **buf)
  346. {
  347. struct bio_bio_st *b;
  348. size_t num;
  349. size_t write_offset;
  350. BIO_clear_retry_flags(bio);
  351. if (!bio->init)
  352. return 0;
  353. b = bio->ptr;
  354. assert(b != NULL);
  355. assert(b->peer != NULL);
  356. assert(b->buf != NULL);
  357. b->request = 0;
  358. if (b->closed) {
  359. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
  360. return -1;
  361. }
  362. assert(b->len <= b->size);
  363. if (b->len == b->size) {
  364. BIO_set_retry_write(bio);
  365. return -1;
  366. }
  367. num = b->size - b->len;
  368. write_offset = b->offset + b->len;
  369. if (write_offset >= b->size)
  370. write_offset -= b->size;
  371. if (write_offset + num > b->size)
  372. /*
  373. * no ring buffer wrap-around for non-copying interface (to fulfil
  374. * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
  375. * to be called twice)
  376. */
  377. num = b->size - write_offset;
  378. if (buf != NULL)
  379. *buf = b->buf + write_offset;
  380. assert(write_offset + num <= b->size);
  381. return num;
  382. }
  383. static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
  384. {
  385. struct bio_bio_st *b;
  386. ssize_t num, space;
  387. if (num_ > SSIZE_MAX)
  388. num = SSIZE_MAX;
  389. else
  390. num = (ssize_t) num_;
  391. space = bio_nwrite0(bio, buf);
  392. if (num > space)
  393. num = space;
  394. if (num <= 0)
  395. return num;
  396. b = bio->ptr;
  397. assert(b != NULL);
  398. b->len += num;
  399. assert(b->len <= b->size);
  400. return num;
  401. }
  402. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
  403. {
  404. long ret;
  405. struct bio_bio_st *b = bio->ptr;
  406. assert(b != NULL);
  407. switch (cmd) {
  408. /* specific CTRL codes */
  409. case BIO_C_SET_WRITE_BUF_SIZE:
  410. if (b->peer) {
  411. BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
  412. ret = 0;
  413. } else if (num == 0) {
  414. BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
  415. ret = 0;
  416. } else {
  417. size_t new_size = num;
  418. if (b->size != new_size) {
  419. if (b->buf) {
  420. OPENSSL_free(b->buf);
  421. b->buf = NULL;
  422. }
  423. b->size = new_size;
  424. }
  425. ret = 1;
  426. }
  427. break;
  428. case BIO_C_GET_WRITE_BUF_SIZE:
  429. ret = (long)b->size;
  430. break;
  431. case BIO_C_MAKE_BIO_PAIR:
  432. {
  433. BIO *other_bio = ptr;
  434. if (bio_make_pair(bio, other_bio))
  435. ret = 1;
  436. else
  437. ret = 0;
  438. }
  439. break;
  440. case BIO_C_DESTROY_BIO_PAIR:
  441. /*
  442. * Affects both BIOs in the pair -- call just once! Or let
  443. * BIO_free(bio1); BIO_free(bio2); do the job.
  444. */
  445. bio_destroy_pair(bio);
  446. ret = 1;
  447. break;
  448. case BIO_C_GET_WRITE_GUARANTEE:
  449. /*
  450. * How many bytes can the caller feed to the next write without
  451. * having to keep any?
  452. */
  453. if (b->peer == NULL || b->closed)
  454. ret = 0;
  455. else
  456. ret = (long)b->size - b->len;
  457. break;
  458. case BIO_C_GET_READ_REQUEST:
  459. /*
  460. * If the peer unsuccessfully tried to read, how many bytes were
  461. * requested? (As with BIO_CTRL_PENDING, that number can usually be
  462. * treated as boolean.)
  463. */
  464. ret = (long)b->request;
  465. break;
  466. case BIO_C_RESET_READ_REQUEST:
  467. /*
  468. * Reset request. (Can be useful after read attempts at the other
  469. * side that are meant to be non-blocking, e.g. when probing SSL_read
  470. * to see if any data is available.)
  471. */
  472. b->request = 0;
  473. ret = 1;
  474. break;
  475. case BIO_C_SHUTDOWN_WR:
  476. /* similar to shutdown(..., SHUT_WR) */
  477. b->closed = 1;
  478. ret = 1;
  479. break;
  480. case BIO_C_NREAD0:
  481. /* prepare for non-copying read */
  482. ret = (long)bio_nread0(bio, ptr);
  483. break;
  484. case BIO_C_NREAD:
  485. /* non-copying read */
  486. ret = (long)bio_nread(bio, ptr, (size_t)num);
  487. break;
  488. case BIO_C_NWRITE0:
  489. /* prepare for non-copying write */
  490. ret = (long)bio_nwrite0(bio, ptr);
  491. break;
  492. case BIO_C_NWRITE:
  493. /* non-copying write */
  494. ret = (long)bio_nwrite(bio, ptr, (size_t)num);
  495. break;
  496. /* standard CTRL codes follow */
  497. case BIO_CTRL_RESET:
  498. if (b->buf != NULL) {
  499. b->len = 0;
  500. b->offset = 0;
  501. }
  502. ret = 0;
  503. break;
  504. case BIO_CTRL_GET_CLOSE:
  505. ret = bio->shutdown;
  506. break;
  507. case BIO_CTRL_SET_CLOSE:
  508. bio->shutdown = (int)num;
  509. ret = 1;
  510. break;
  511. case BIO_CTRL_PENDING:
  512. if (b->peer != NULL) {
  513. struct bio_bio_st *peer_b = b->peer->ptr;
  514. ret = (long)peer_b->len;
  515. } else
  516. ret = 0;
  517. break;
  518. case BIO_CTRL_WPENDING:
  519. if (b->buf != NULL)
  520. ret = (long)b->len;
  521. else
  522. ret = 0;
  523. break;
  524. case BIO_CTRL_DUP:
  525. /* See BIO_dup_chain for circumstances we have to expect. */
  526. {
  527. BIO *other_bio = ptr;
  528. struct bio_bio_st *other_b;
  529. assert(other_bio != NULL);
  530. other_b = other_bio->ptr;
  531. assert(other_b != NULL);
  532. assert(other_b->buf == NULL); /* other_bio is always fresh */
  533. other_b->size = b->size;
  534. }
  535. ret = 1;
  536. break;
  537. case BIO_CTRL_FLUSH:
  538. ret = 1;
  539. break;
  540. case BIO_CTRL_EOF:
  541. {
  542. BIO *other_bio = ptr;
  543. if (other_bio) {
  544. struct bio_bio_st *other_b = other_bio->ptr;
  545. assert(other_b != NULL);
  546. ret = other_b->len == 0 && other_b->closed;
  547. } else
  548. ret = 1;
  549. }
  550. break;
  551. default:
  552. ret = 0;
  553. }
  554. return ret;
  555. }
  556. static int bio_puts(BIO *bio, const char *str)
  557. {
  558. return bio_write(bio, str, strlen(str));
  559. }
  560. static int bio_make_pair(BIO *bio1, BIO *bio2)
  561. {
  562. struct bio_bio_st *b1, *b2;
  563. assert(bio1 != NULL);
  564. assert(bio2 != NULL);
  565. b1 = bio1->ptr;
  566. b2 = bio2->ptr;
  567. if (b1->peer != NULL || b2->peer != NULL) {
  568. BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
  569. return 0;
  570. }
  571. if (b1->buf == NULL) {
  572. b1->buf = OPENSSL_malloc(b1->size);
  573. if (b1->buf == NULL) {
  574. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  575. return 0;
  576. }
  577. b1->len = 0;
  578. b1->offset = 0;
  579. }
  580. if (b2->buf == NULL) {
  581. b2->buf = OPENSSL_malloc(b2->size);
  582. if (b2->buf == NULL) {
  583. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  584. return 0;
  585. }
  586. b2->len = 0;
  587. b2->offset = 0;
  588. }
  589. b1->peer = bio2;
  590. b1->closed = 0;
  591. b1->request = 0;
  592. b2->peer = bio1;
  593. b2->closed = 0;
  594. b2->request = 0;
  595. bio1->init = 1;
  596. bio2->init = 1;
  597. return 1;
  598. }
  599. static void bio_destroy_pair(BIO *bio)
  600. {
  601. struct bio_bio_st *b = bio->ptr;
  602. if (b != NULL) {
  603. BIO *peer_bio = b->peer;
  604. if (peer_bio != NULL) {
  605. struct bio_bio_st *peer_b = peer_bio->ptr;
  606. assert(peer_b != NULL);
  607. assert(peer_b->peer == bio);
  608. peer_b->peer = NULL;
  609. peer_bio->init = 0;
  610. assert(peer_b->buf != NULL);
  611. peer_b->len = 0;
  612. peer_b->offset = 0;
  613. b->peer = NULL;
  614. bio->init = 0;
  615. assert(b->buf != NULL);
  616. b->len = 0;
  617. b->offset = 0;
  618. }
  619. }
  620. }
  621. /* Exported convenience functions */
  622. int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
  623. BIO **bio2_p, size_t writebuf2)
  624. {
  625. BIO *bio1 = NULL, *bio2 = NULL;
  626. long r;
  627. int ret = 0;
  628. bio1 = BIO_new(BIO_s_bio());
  629. if (bio1 == NULL)
  630. goto err;
  631. bio2 = BIO_new(BIO_s_bio());
  632. if (bio2 == NULL)
  633. goto err;
  634. if (writebuf1) {
  635. r = BIO_set_write_buf_size(bio1, writebuf1);
  636. if (!r)
  637. goto err;
  638. }
  639. if (writebuf2) {
  640. r = BIO_set_write_buf_size(bio2, writebuf2);
  641. if (!r)
  642. goto err;
  643. }
  644. r = BIO_make_bio_pair(bio1, bio2);
  645. if (!r)
  646. goto err;
  647. ret = 1;
  648. err:
  649. if (ret == 0) {
  650. if (bio1) {
  651. BIO_free(bio1);
  652. bio1 = NULL;
  653. }
  654. if (bio2) {
  655. BIO_free(bio2);
  656. bio2 = NULL;
  657. }
  658. }
  659. *bio1_p = bio1;
  660. *bio2_p = bio2;
  661. return ret;
  662. }
  663. size_t BIO_ctrl_get_write_guarantee(BIO *bio)
  664. {
  665. return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
  666. }
  667. size_t BIO_ctrl_get_read_request(BIO *bio)
  668. {
  669. return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
  670. }
  671. int BIO_ctrl_reset_read_request(BIO *bio)
  672. {
  673. return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
  674. }
  675. /*
  676. * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
  677. * (conceivably some other BIOs could allow non-copying reads and writes
  678. * too.)
  679. */
  680. int BIO_nread0(BIO *bio, char **buf)
  681. {
  682. long ret;
  683. if (!bio->init) {
  684. BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
  685. return -2;
  686. }
  687. ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
  688. if (ret > INT_MAX)
  689. return INT_MAX;
  690. else
  691. return (int)ret;
  692. }
  693. int BIO_nread(BIO *bio, char **buf, int num)
  694. {
  695. int ret;
  696. if (!bio->init) {
  697. BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
  698. return -2;
  699. }
  700. ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
  701. if (ret > 0)
  702. bio->num_read += ret;
  703. return ret;
  704. }
  705. int BIO_nwrite0(BIO *bio, char **buf)
  706. {
  707. long ret;
  708. if (!bio->init) {
  709. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
  710. return -2;
  711. }
  712. ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
  713. if (ret > INT_MAX)
  714. return INT_MAX;
  715. else
  716. return (int)ret;
  717. }
  718. int BIO_nwrite(BIO *bio, char **buf, int num)
  719. {
  720. int ret;
  721. if (!bio->init) {
  722. BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
  723. return -2;
  724. }
  725. ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
  726. if (ret > 0)
  727. bio->num_write += ret;
  728. return ret;
  729. }