You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

494 lines
12 KiB

  1. /* ====================================================================
  2. * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <openssl/bio.h>
  53. #include <assert.h>
  54. #include <string.h>
  55. #include <openssl/buf.h>
  56. #include <openssl/err.h>
  57. #include <openssl/mem.h>
  58. #include "../internal.h"
  59. struct bio_bio_st {
  60. BIO *peer; /* NULL if buf == NULL.
  61. * If peer != NULL, then peer->ptr is also a bio_bio_st,
  62. * and its "peer" member points back to us.
  63. * peer != NULL iff init != 0 in the BIO. */
  64. /* This is for what we write (i.e. reading uses peer's struct): */
  65. int closed; /* valid iff peer != NULL */
  66. size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
  67. size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
  68. size_t size;
  69. uint8_t *buf; /* "size" elements (if != NULL) */
  70. size_t request; /* valid iff peer != NULL; 0 if len != 0,
  71. * otherwise set by peer to number of bytes
  72. * it (unsuccessfully) tried to read,
  73. * never more than buffer space (size-len) warrants. */
  74. };
  75. static int bio_new(BIO *bio) {
  76. struct bio_bio_st *b;
  77. b = OPENSSL_malloc(sizeof *b);
  78. if (b == NULL) {
  79. return 0;
  80. }
  81. OPENSSL_memset(b, 0, sizeof(struct bio_bio_st));
  82. b->size = 17 * 1024; /* enough for one TLS record (just a default) */
  83. bio->ptr = b;
  84. return 1;
  85. }
  86. static void bio_destroy_pair(BIO *bio) {
  87. struct bio_bio_st *b = bio->ptr;
  88. BIO *peer_bio;
  89. struct bio_bio_st *peer_b;
  90. if (b == NULL) {
  91. return;
  92. }
  93. peer_bio = b->peer;
  94. if (peer_bio == NULL) {
  95. return;
  96. }
  97. peer_b = peer_bio->ptr;
  98. assert(peer_b != NULL);
  99. assert(peer_b->peer == bio);
  100. peer_b->peer = NULL;
  101. peer_bio->init = 0;
  102. assert(peer_b->buf != NULL);
  103. peer_b->len = 0;
  104. peer_b->offset = 0;
  105. b->peer = NULL;
  106. bio->init = 0;
  107. assert(b->buf != NULL);
  108. b->len = 0;
  109. b->offset = 0;
  110. }
  111. static int bio_free(BIO *bio) {
  112. struct bio_bio_st *b;
  113. if (bio == NULL) {
  114. return 0;
  115. }
  116. b = bio->ptr;
  117. assert(b != NULL);
  118. if (b->peer) {
  119. bio_destroy_pair(bio);
  120. }
  121. OPENSSL_free(b->buf);
  122. OPENSSL_free(b);
  123. return 1;
  124. }
  125. static int bio_read(BIO *bio, char *buf, int size_) {
  126. size_t size = size_;
  127. size_t rest;
  128. struct bio_bio_st *b, *peer_b;
  129. BIO_clear_retry_flags(bio);
  130. if (!bio->init) {
  131. return 0;
  132. }
  133. b = bio->ptr;
  134. assert(b != NULL);
  135. assert(b->peer != NULL);
  136. peer_b = b->peer->ptr;
  137. assert(peer_b != NULL);
  138. assert(peer_b->buf != NULL);
  139. peer_b->request = 0; /* will be set in "retry_read" situation */
  140. if (buf == NULL || size == 0) {
  141. return 0;
  142. }
  143. if (peer_b->len == 0) {
  144. if (peer_b->closed) {
  145. return 0; /* writer has closed, and no data is left */
  146. } else {
  147. BIO_set_retry_read(bio); /* buffer is empty */
  148. if (size <= peer_b->size) {
  149. peer_b->request = size;
  150. } else {
  151. /* don't ask for more than the peer can
  152. * deliver in one write */
  153. peer_b->request = peer_b->size;
  154. }
  155. return -1;
  156. }
  157. }
  158. /* we can read */
  159. if (peer_b->len < size) {
  160. size = peer_b->len;
  161. }
  162. /* now read "size" bytes */
  163. rest = size;
  164. assert(rest > 0);
  165. /* one or two iterations */
  166. do {
  167. size_t chunk;
  168. assert(rest <= peer_b->len);
  169. if (peer_b->offset + rest <= peer_b->size) {
  170. chunk = rest;
  171. } else {
  172. /* wrap around ring buffer */
  173. chunk = peer_b->size - peer_b->offset;
  174. }
  175. assert(peer_b->offset + chunk <= peer_b->size);
  176. OPENSSL_memcpy(buf, peer_b->buf + peer_b->offset, chunk);
  177. peer_b->len -= chunk;
  178. if (peer_b->len) {
  179. peer_b->offset += chunk;
  180. assert(peer_b->offset <= peer_b->size);
  181. if (peer_b->offset == peer_b->size) {
  182. peer_b->offset = 0;
  183. }
  184. buf += chunk;
  185. } else {
  186. /* buffer now empty, no need to advance "buf" */
  187. assert(chunk == rest);
  188. peer_b->offset = 0;
  189. }
  190. rest -= chunk;
  191. } while (rest);
  192. return size;
  193. }
  194. static int bio_write(BIO *bio, const char *buf, int num_) {
  195. size_t num = num_;
  196. size_t rest;
  197. struct bio_bio_st *b;
  198. BIO_clear_retry_flags(bio);
  199. if (!bio->init || buf == NULL || num == 0) {
  200. return 0;
  201. }
  202. b = bio->ptr;
  203. assert(b != NULL);
  204. assert(b->peer != NULL);
  205. assert(b->buf != NULL);
  206. b->request = 0;
  207. if (b->closed) {
  208. /* we already closed */
  209. OPENSSL_PUT_ERROR(BIO, BIO_R_BROKEN_PIPE);
  210. return -1;
  211. }
  212. assert(b->len <= b->size);
  213. if (b->len == b->size) {
  214. BIO_set_retry_write(bio); /* buffer is full */
  215. return -1;
  216. }
  217. /* we can write */
  218. if (num > b->size - b->len) {
  219. num = b->size - b->len;
  220. }
  221. /* now write "num" bytes */
  222. rest = num;
  223. assert(rest > 0);
  224. /* one or two iterations */
  225. do {
  226. size_t write_offset;
  227. size_t chunk;
  228. assert(b->len + rest <= b->size);
  229. write_offset = b->offset + b->len;
  230. if (write_offset >= b->size) {
  231. write_offset -= b->size;
  232. }
  233. /* b->buf[write_offset] is the first byte we can write to. */
  234. if (write_offset + rest <= b->size) {
  235. chunk = rest;
  236. } else {
  237. /* wrap around ring buffer */
  238. chunk = b->size - write_offset;
  239. }
  240. OPENSSL_memcpy(b->buf + write_offset, buf, chunk);
  241. b->len += chunk;
  242. assert(b->len <= b->size);
  243. rest -= chunk;
  244. buf += chunk;
  245. } while (rest);
  246. return num;
  247. }
  248. static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len,
  249. size_t writebuf2_len) {
  250. struct bio_bio_st *b1, *b2;
  251. assert(bio1 != NULL);
  252. assert(bio2 != NULL);
  253. b1 = bio1->ptr;
  254. b2 = bio2->ptr;
  255. if (b1->peer != NULL || b2->peer != NULL) {
  256. OPENSSL_PUT_ERROR(BIO, BIO_R_IN_USE);
  257. return 0;
  258. }
  259. if (b1->buf == NULL) {
  260. if (writebuf1_len) {
  261. b1->size = writebuf1_len;
  262. }
  263. b1->buf = OPENSSL_malloc(b1->size);
  264. if (b1->buf == NULL) {
  265. OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
  266. return 0;
  267. }
  268. b1->len = 0;
  269. b1->offset = 0;
  270. }
  271. if (b2->buf == NULL) {
  272. if (writebuf2_len) {
  273. b2->size = writebuf2_len;
  274. }
  275. b2->buf = OPENSSL_malloc(b2->size);
  276. if (b2->buf == NULL) {
  277. OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
  278. return 0;
  279. }
  280. b2->len = 0;
  281. b2->offset = 0;
  282. }
  283. b1->peer = bio2;
  284. b1->closed = 0;
  285. b1->request = 0;
  286. b2->peer = bio1;
  287. b2->closed = 0;
  288. b2->request = 0;
  289. bio1->init = 1;
  290. bio2->init = 1;
  291. return 1;
  292. }
  293. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  294. long ret;
  295. struct bio_bio_st *b = bio->ptr;
  296. assert(b != NULL);
  297. switch (cmd) {
  298. /* specific CTRL codes */
  299. case BIO_C_GET_WRITE_BUF_SIZE:
  300. ret = (long)b->size;
  301. break;
  302. case BIO_C_GET_WRITE_GUARANTEE:
  303. /* How many bytes can the caller feed to the next write
  304. * without having to keep any? */
  305. if (b->peer == NULL || b->closed) {
  306. ret = 0;
  307. } else {
  308. ret = (long)b->size - b->len;
  309. }
  310. break;
  311. case BIO_C_GET_READ_REQUEST:
  312. /* If the peer unsuccessfully tried to read, how many bytes
  313. * were requested? (As with BIO_CTRL_PENDING, that number
  314. * can usually be treated as boolean.) */
  315. ret = (long)b->request;
  316. break;
  317. case BIO_C_RESET_READ_REQUEST:
  318. /* Reset request. (Can be useful after read attempts
  319. * at the other side that are meant to be non-blocking,
  320. * e.g. when probing SSL_read to see if any data is
  321. * available.) */
  322. b->request = 0;
  323. ret = 1;
  324. break;
  325. case BIO_C_SHUTDOWN_WR:
  326. /* similar to shutdown(..., SHUT_WR) */
  327. b->closed = 1;
  328. ret = 1;
  329. break;
  330. /* standard CTRL codes follow */
  331. case BIO_CTRL_GET_CLOSE:
  332. ret = bio->shutdown;
  333. break;
  334. case BIO_CTRL_SET_CLOSE:
  335. bio->shutdown = (int)num;
  336. ret = 1;
  337. break;
  338. case BIO_CTRL_PENDING:
  339. if (b->peer != NULL) {
  340. struct bio_bio_st *peer_b = b->peer->ptr;
  341. ret = (long)peer_b->len;
  342. } else {
  343. ret = 0;
  344. }
  345. break;
  346. case BIO_CTRL_WPENDING:
  347. ret = 0;
  348. if (b->buf != NULL) {
  349. ret = (long)b->len;
  350. }
  351. break;
  352. case BIO_CTRL_FLUSH:
  353. ret = 1;
  354. break;
  355. case BIO_CTRL_EOF: {
  356. BIO *other_bio = ptr;
  357. if (other_bio) {
  358. struct bio_bio_st *other_b = other_bio->ptr;
  359. assert(other_b != NULL);
  360. ret = other_b->len == 0 && other_b->closed;
  361. } else {
  362. ret = 1;
  363. }
  364. } break;
  365. default:
  366. ret = 0;
  367. }
  368. return ret;
  369. }
  370. static int bio_puts(BIO *bio, const char *str) {
  371. return bio_write(bio, str, strlen(str));
  372. }
  373. static const BIO_METHOD methods_biop = {
  374. BIO_TYPE_BIO, "BIO pair", bio_write, bio_read,
  375. bio_puts, NULL /* no bio_gets */, bio_ctrl, bio_new,
  376. bio_free, NULL /* no bio_callback_ctrl */
  377. };
  378. static const BIO_METHOD *bio_s_bio(void) { return &methods_biop; }
  379. int BIO_new_bio_pair(BIO** bio1_p, size_t writebuf1_len,
  380. BIO** bio2_p, size_t writebuf2_len) {
  381. BIO *bio1 = BIO_new(bio_s_bio());
  382. BIO *bio2 = BIO_new(bio_s_bio());
  383. if (bio1 == NULL || bio2 == NULL ||
  384. !bio_make_pair(bio1, bio2, writebuf1_len, writebuf2_len)) {
  385. BIO_free(bio1);
  386. BIO_free(bio2);
  387. *bio1_p = NULL;
  388. *bio2_p = NULL;
  389. return 0;
  390. }
  391. *bio1_p = bio1;
  392. *bio2_p = bio2;
  393. return 1;
  394. }
  395. size_t BIO_ctrl_get_read_request(BIO *bio) {
  396. return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
  397. }
  398. size_t BIO_ctrl_get_write_guarantee(BIO *bio) {
  399. return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
  400. }
  401. int BIO_shutdown_wr(BIO *bio) {
  402. return BIO_ctrl(bio, BIO_C_SHUTDOWN_WR, 0, NULL);
  403. }