2
0

bss_bio.c 19 KB

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