bss_bio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /* crypto/bio/bss_bio.c */
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  52. * Hudson ([email protected]).
  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. b->closed = 0;
  138. b->len = 0;
  139. b->offset = 0;
  140. /* enough for one TLS record (just a default) */
  141. b->size = 17 * 1024;
  142. b->buf = NULL;
  143. b->request = 0;
  144. bio->ptr = b;
  145. return 1;
  146. }
  147. static int bio_free(BIO *bio)
  148. {
  149. struct bio_bio_st *b;
  150. if (bio == NULL)
  151. return 0;
  152. b = bio->ptr;
  153. assert(b != NULL);
  154. if (b->peer)
  155. bio_destroy_pair(bio);
  156. if (b->buf != NULL) {
  157. OPENSSL_free(b->buf);
  158. }
  159. OPENSSL_free(b);
  160. return 1;
  161. }
  162. static int bio_read(BIO *bio, char *buf, int size_)
  163. {
  164. size_t size = size_;
  165. size_t rest;
  166. struct bio_bio_st *b, *peer_b;
  167. BIO_clear_retry_flags(bio);
  168. if (!bio->init)
  169. return 0;
  170. b = bio->ptr;
  171. assert(b != NULL);
  172. assert(b->peer != NULL);
  173. peer_b = b->peer->ptr;
  174. assert(peer_b != NULL);
  175. assert(peer_b->buf != NULL);
  176. peer_b->request = 0; /* will be set in "retry_read" situation */
  177. if (buf == NULL || size == 0)
  178. return 0;
  179. if (peer_b->len == 0) {
  180. if (peer_b->closed)
  181. return 0; /* writer has closed, and no data is left */
  182. else {
  183. BIO_set_retry_read(bio); /* buffer is empty */
  184. if (size <= peer_b->size)
  185. peer_b->request = size;
  186. else
  187. /*
  188. * don't ask for more than the peer can deliver in one write
  189. */
  190. peer_b->request = peer_b->size;
  191. return -1;
  192. }
  193. }
  194. /* we can read */
  195. if (peer_b->len < size)
  196. size = peer_b->len;
  197. /* now read "size" bytes */
  198. rest = size;
  199. assert(rest > 0);
  200. do { /* one or two iterations */
  201. size_t chunk;
  202. assert(rest <= peer_b->len);
  203. if (peer_b->offset + rest <= peer_b->size)
  204. chunk = rest;
  205. else
  206. /* wrap around ring buffer */
  207. chunk = peer_b->size - peer_b->offset;
  208. assert(peer_b->offset + chunk <= peer_b->size);
  209. memcpy(buf, peer_b->buf + peer_b->offset, chunk);
  210. peer_b->len -= chunk;
  211. if (peer_b->len) {
  212. peer_b->offset += chunk;
  213. assert(peer_b->offset <= peer_b->size);
  214. if (peer_b->offset == peer_b->size)
  215. peer_b->offset = 0;
  216. buf += chunk;
  217. } else {
  218. /* buffer now empty, no need to advance "buf" */
  219. assert(chunk == rest);
  220. peer_b->offset = 0;
  221. }
  222. rest -= chunk;
  223. }
  224. while (rest);
  225. return size;
  226. }
  227. /*-
  228. * non-copying interface: provide pointer to available data in buffer
  229. * bio_nread0: return number of available bytes
  230. * bio_nread: also advance index
  231. * (example usage: bio_nread0(), read from buffer, bio_nread()
  232. * or just bio_nread(), read from buffer)
  233. */
  234. /*
  235. * WARNING: The non-copying interface is largely untested as of yet and may
  236. * contain bugs.
  237. */
  238. static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
  239. {
  240. struct bio_bio_st *b, *peer_b;
  241. ossl_ssize_t num;
  242. BIO_clear_retry_flags(bio);
  243. if (!bio->init)
  244. return 0;
  245. b = bio->ptr;
  246. assert(b != NULL);
  247. assert(b->peer != NULL);
  248. peer_b = b->peer->ptr;
  249. assert(peer_b != NULL);
  250. assert(peer_b->buf != NULL);
  251. peer_b->request = 0;
  252. if (peer_b->len == 0) {
  253. char dummy;
  254. /* avoid code duplication -- nothing available for reading */
  255. return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
  256. }
  257. num = peer_b->len;
  258. if (peer_b->size < peer_b->offset + num)
  259. /* no ring buffer wrap-around for non-copying interface */
  260. num = peer_b->size - peer_b->offset;
  261. assert(num > 0);
  262. if (buf != NULL)
  263. *buf = peer_b->buf + peer_b->offset;
  264. return num;
  265. }
  266. static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
  267. {
  268. struct bio_bio_st *b, *peer_b;
  269. ossl_ssize_t num, available;
  270. if (num_ > SSIZE_MAX)
  271. num = SSIZE_MAX;
  272. else
  273. num = (ossl_ssize_t) num_;
  274. available = bio_nread0(bio, buf);
  275. if (num > available)
  276. num = available;
  277. if (num <= 0)
  278. return num;
  279. b = bio->ptr;
  280. peer_b = b->peer->ptr;
  281. peer_b->len -= num;
  282. if (peer_b->len) {
  283. peer_b->offset += num;
  284. assert(peer_b->offset <= peer_b->size);
  285. if (peer_b->offset == peer_b->size)
  286. peer_b->offset = 0;
  287. } else
  288. peer_b->offset = 0;
  289. return num;
  290. }
  291. static int bio_write(BIO *bio, const char *buf, int num_)
  292. {
  293. size_t num = num_;
  294. size_t rest;
  295. struct bio_bio_st *b;
  296. BIO_clear_retry_flags(bio);
  297. if (!bio->init || buf == NULL || num == 0)
  298. return 0;
  299. b = bio->ptr;
  300. assert(b != NULL);
  301. assert(b->peer != NULL);
  302. assert(b->buf != NULL);
  303. b->request = 0;
  304. if (b->closed) {
  305. /* we already closed */
  306. BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
  307. return -1;
  308. }
  309. assert(b->len <= b->size);
  310. if (b->len == b->size) {
  311. BIO_set_retry_write(bio); /* buffer is full */
  312. return -1;
  313. }
  314. /* we can write */
  315. if (num > b->size - b->len)
  316. num = b->size - b->len;
  317. /* now write "num" bytes */
  318. rest = num;
  319. assert(rest > 0);
  320. do { /* one or two iterations */
  321. size_t write_offset;
  322. size_t chunk;
  323. assert(b->len + rest <= b->size);
  324. write_offset = b->offset + b->len;
  325. if (write_offset >= b->size)
  326. write_offset -= b->size;
  327. /* b->buf[write_offset] is the first byte we can write to. */
  328. if (write_offset + rest <= b->size)
  329. chunk = rest;
  330. else
  331. /* wrap around ring buffer */
  332. chunk = b->size - write_offset;
  333. memcpy(b->buf + write_offset, buf, chunk);
  334. b->len += chunk;
  335. assert(b->len <= b->size);
  336. rest -= chunk;
  337. buf += chunk;
  338. }
  339. while (rest);
  340. return num;
  341. }
  342. /*-
  343. * non-copying interface: provide pointer to region to write to
  344. * bio_nwrite0: check how much space is available
  345. * bio_nwrite: also increase length
  346. * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
  347. * or just bio_nwrite(), write to buffer)
  348. */
  349. static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
  350. {
  351. struct bio_bio_st *b;
  352. size_t num;
  353. size_t write_offset;
  354. BIO_clear_retry_flags(bio);
  355. if (!bio->init)
  356. return 0;
  357. b = bio->ptr;
  358. assert(b != NULL);
  359. assert(b->peer != NULL);
  360. assert(b->buf != NULL);
  361. b->request = 0;
  362. if (b->closed) {
  363. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
  364. return -1;
  365. }
  366. assert(b->len <= b->size);
  367. if (b->len == b->size) {
  368. BIO_set_retry_write(bio);
  369. return -1;
  370. }
  371. num = b->size - b->len;
  372. write_offset = b->offset + b->len;
  373. if (write_offset >= b->size)
  374. write_offset -= b->size;
  375. if (write_offset + num > b->size)
  376. /*
  377. * no ring buffer wrap-around for non-copying interface (to fulfil
  378. * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
  379. * to be called twice)
  380. */
  381. num = b->size - write_offset;
  382. if (buf != NULL)
  383. *buf = b->buf + write_offset;
  384. assert(write_offset + num <= b->size);
  385. return num;
  386. }
  387. static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
  388. {
  389. struct bio_bio_st *b;
  390. ossl_ssize_t num, space;
  391. if (num_ > SSIZE_MAX)
  392. num = SSIZE_MAX;
  393. else
  394. num = (ossl_ssize_t) num_;
  395. space = bio_nwrite0(bio, buf);
  396. if (num > space)
  397. num = space;
  398. if (num <= 0)
  399. return num;
  400. b = bio->ptr;
  401. assert(b != NULL);
  402. b->len += num;
  403. assert(b->len <= b->size);
  404. return num;
  405. }
  406. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
  407. {
  408. long ret;
  409. struct bio_bio_st *b = bio->ptr;
  410. assert(b != NULL);
  411. switch (cmd) {
  412. /* specific CTRL codes */
  413. case BIO_C_SET_WRITE_BUF_SIZE:
  414. if (b->peer) {
  415. BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
  416. ret = 0;
  417. } else if (num == 0) {
  418. BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
  419. ret = 0;
  420. } else {
  421. size_t new_size = num;
  422. if (b->size != new_size) {
  423. if (b->buf) {
  424. OPENSSL_free(b->buf);
  425. b->buf = NULL;
  426. }
  427. b->size = new_size;
  428. }
  429. ret = 1;
  430. }
  431. break;
  432. case BIO_C_GET_WRITE_BUF_SIZE:
  433. ret = (long)b->size;
  434. break;
  435. case BIO_C_MAKE_BIO_PAIR:
  436. {
  437. BIO *other_bio = ptr;
  438. if (bio_make_pair(bio, other_bio))
  439. ret = 1;
  440. else
  441. ret = 0;
  442. }
  443. break;
  444. case BIO_C_DESTROY_BIO_PAIR:
  445. /*
  446. * Affects both BIOs in the pair -- call just once! Or let
  447. * BIO_free(bio1); BIO_free(bio2); do the job.
  448. */
  449. bio_destroy_pair(bio);
  450. ret = 1;
  451. break;
  452. case BIO_C_GET_WRITE_GUARANTEE:
  453. /*
  454. * How many bytes can the caller feed to the next write without
  455. * having to keep any?
  456. */
  457. if (b->peer == NULL || b->closed)
  458. ret = 0;
  459. else
  460. ret = (long)b->size - b->len;
  461. break;
  462. case BIO_C_GET_READ_REQUEST:
  463. /*
  464. * If the peer unsuccessfully tried to read, how many bytes were
  465. * requested? (As with BIO_CTRL_PENDING, that number can usually be
  466. * treated as boolean.)
  467. */
  468. ret = (long)b->request;
  469. break;
  470. case BIO_C_RESET_READ_REQUEST:
  471. /*
  472. * Reset request. (Can be useful after read attempts at the other
  473. * side that are meant to be non-blocking, e.g. when probing SSL_read
  474. * to see if any data is available.)
  475. */
  476. b->request = 0;
  477. ret = 1;
  478. break;
  479. case BIO_C_SHUTDOWN_WR:
  480. /* similar to shutdown(..., SHUT_WR) */
  481. b->closed = 1;
  482. ret = 1;
  483. break;
  484. case BIO_C_NREAD0:
  485. /* prepare for non-copying read */
  486. ret = (long)bio_nread0(bio, ptr);
  487. break;
  488. case BIO_C_NREAD:
  489. /* non-copying read */
  490. ret = (long)bio_nread(bio, ptr, (size_t)num);
  491. break;
  492. case BIO_C_NWRITE0:
  493. /* prepare for non-copying write */
  494. ret = (long)bio_nwrite0(bio, ptr);
  495. break;
  496. case BIO_C_NWRITE:
  497. /* non-copying write */
  498. ret = (long)bio_nwrite(bio, ptr, (size_t)num);
  499. break;
  500. /* standard CTRL codes follow */
  501. case BIO_CTRL_RESET:
  502. if (b->buf != NULL) {
  503. b->len = 0;
  504. b->offset = 0;
  505. }
  506. ret = 0;
  507. break;
  508. case BIO_CTRL_GET_CLOSE:
  509. ret = bio->shutdown;
  510. break;
  511. case BIO_CTRL_SET_CLOSE:
  512. bio->shutdown = (int)num;
  513. ret = 1;
  514. break;
  515. case BIO_CTRL_PENDING:
  516. if (b->peer != NULL) {
  517. struct bio_bio_st *peer_b = b->peer->ptr;
  518. ret = (long)peer_b->len;
  519. } else
  520. ret = 0;
  521. break;
  522. case BIO_CTRL_WPENDING:
  523. if (b->buf != NULL)
  524. ret = (long)b->len;
  525. else
  526. ret = 0;
  527. break;
  528. case BIO_CTRL_DUP:
  529. /* See BIO_dup_chain for circumstances we have to expect. */
  530. {
  531. BIO *other_bio = ptr;
  532. struct bio_bio_st *other_b;
  533. assert(other_bio != NULL);
  534. other_b = other_bio->ptr;
  535. assert(other_b != NULL);
  536. assert(other_b->buf == NULL); /* other_bio is always fresh */
  537. other_b->size = b->size;
  538. }
  539. ret = 1;
  540. break;
  541. case BIO_CTRL_FLUSH:
  542. ret = 1;
  543. break;
  544. case BIO_CTRL_EOF:
  545. if (b->peer != NULL) {
  546. struct bio_bio_st *peer_b = b->peer->ptr;
  547. if (peer_b->len == 0 && peer_b->closed)
  548. ret = 1;
  549. else
  550. ret = 0;
  551. } else {
  552. ret = 1;
  553. }
  554. break;
  555. default:
  556. ret = 0;
  557. }
  558. return ret;
  559. }
  560. static int bio_puts(BIO *bio, const char *str)
  561. {
  562. return bio_write(bio, str, strlen(str));
  563. }
  564. static int bio_make_pair(BIO *bio1, BIO *bio2)
  565. {
  566. struct bio_bio_st *b1, *b2;
  567. assert(bio1 != NULL);
  568. assert(bio2 != NULL);
  569. b1 = bio1->ptr;
  570. b2 = bio2->ptr;
  571. if (b1->peer != NULL || b2->peer != NULL) {
  572. BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
  573. return 0;
  574. }
  575. if (b1->buf == NULL) {
  576. b1->buf = OPENSSL_malloc(b1->size);
  577. if (b1->buf == NULL) {
  578. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  579. return 0;
  580. }
  581. b1->len = 0;
  582. b1->offset = 0;
  583. }
  584. if (b2->buf == NULL) {
  585. b2->buf = OPENSSL_malloc(b2->size);
  586. if (b2->buf == NULL) {
  587. BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
  588. return 0;
  589. }
  590. b2->len = 0;
  591. b2->offset = 0;
  592. }
  593. b1->peer = bio2;
  594. b1->closed = 0;
  595. b1->request = 0;
  596. b2->peer = bio1;
  597. b2->closed = 0;
  598. b2->request = 0;
  599. bio1->init = 1;
  600. bio2->init = 1;
  601. return 1;
  602. }
  603. static void bio_destroy_pair(BIO *bio)
  604. {
  605. struct bio_bio_st *b = bio->ptr;
  606. if (b != NULL) {
  607. BIO *peer_bio = b->peer;
  608. if (peer_bio != NULL) {
  609. struct bio_bio_st *peer_b = peer_bio->ptr;
  610. assert(peer_b != NULL);
  611. assert(peer_b->peer == bio);
  612. peer_b->peer = NULL;
  613. peer_bio->init = 0;
  614. assert(peer_b->buf != NULL);
  615. peer_b->len = 0;
  616. peer_b->offset = 0;
  617. b->peer = NULL;
  618. bio->init = 0;
  619. assert(b->buf != NULL);
  620. b->len = 0;
  621. b->offset = 0;
  622. }
  623. }
  624. }
  625. /* Exported convenience functions */
  626. int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
  627. BIO **bio2_p, size_t writebuf2)
  628. {
  629. BIO *bio1 = NULL, *bio2 = NULL;
  630. long r;
  631. int ret = 0;
  632. bio1 = BIO_new(BIO_s_bio());
  633. if (bio1 == NULL)
  634. goto err;
  635. bio2 = BIO_new(BIO_s_bio());
  636. if (bio2 == NULL)
  637. goto err;
  638. if (writebuf1) {
  639. r = BIO_set_write_buf_size(bio1, writebuf1);
  640. if (!r)
  641. goto err;
  642. }
  643. if (writebuf2) {
  644. r = BIO_set_write_buf_size(bio2, writebuf2);
  645. if (!r)
  646. goto err;
  647. }
  648. r = BIO_make_bio_pair(bio1, bio2);
  649. if (!r)
  650. goto err;
  651. ret = 1;
  652. err:
  653. if (ret == 0) {
  654. if (bio1) {
  655. BIO_free(bio1);
  656. bio1 = NULL;
  657. }
  658. if (bio2) {
  659. BIO_free(bio2);
  660. bio2 = NULL;
  661. }
  662. }
  663. *bio1_p = bio1;
  664. *bio2_p = bio2;
  665. return ret;
  666. }
  667. size_t BIO_ctrl_get_write_guarantee(BIO *bio)
  668. {
  669. return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
  670. }
  671. size_t BIO_ctrl_get_read_request(BIO *bio)
  672. {
  673. return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
  674. }
  675. int BIO_ctrl_reset_read_request(BIO *bio)
  676. {
  677. return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
  678. }
  679. /*
  680. * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
  681. * (conceivably some other BIOs could allow non-copying reads and writes
  682. * too.)
  683. */
  684. int BIO_nread0(BIO *bio, char **buf)
  685. {
  686. long ret;
  687. if (!bio->init) {
  688. BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
  689. return -2;
  690. }
  691. ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
  692. if (ret > INT_MAX)
  693. return INT_MAX;
  694. else
  695. return (int)ret;
  696. }
  697. int BIO_nread(BIO *bio, char **buf, int num)
  698. {
  699. int ret;
  700. if (!bio->init) {
  701. BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
  702. return -2;
  703. }
  704. ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
  705. if (ret > 0)
  706. bio->num_read += ret;
  707. return ret;
  708. }
  709. int BIO_nwrite0(BIO *bio, char **buf)
  710. {
  711. long ret;
  712. if (!bio->init) {
  713. BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
  714. return -2;
  715. }
  716. ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
  717. if (ret > INT_MAX)
  718. return INT_MAX;
  719. else
  720. return (int)ret;
  721. }
  722. int BIO_nwrite(BIO *bio, char **buf, int num)
  723. {
  724. int ret;
  725. if (!bio->init) {
  726. BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
  727. return -2;
  728. }
  729. ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
  730. if (ret > 0)
  731. bio->num_write += ret;
  732. return ret;
  733. }