bss_bio.c 21 KB

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