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.
 
 
 
 
 
 

550 line
13 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 <openssl/buf.h>
  55. #include <openssl/err.h>
  56. #include <openssl/mem.h>
  57. struct bio_bio_st {
  58. BIO *peer; /* NULL if buf == NULL.
  59. * If peer != NULL, then peer->ptr is also a bio_bio_st,
  60. * and its "peer" member points back to us.
  61. * peer != NULL iff init != 0 in the BIO. */
  62. /* This is for what we write (i.e. reading uses peer's struct): */
  63. int closed; /* valid iff peer != NULL */
  64. size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
  65. size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
  66. size_t size;
  67. char *buf; /* "size" elements (if != NULL) */
  68. size_t request; /* valid iff peer != NULL; 0 if len != 0,
  69. * otherwise set by peer to number of bytes
  70. * it (unsuccessfully) tried to read,
  71. * never more than buffer space (size-len) warrants. */
  72. };
  73. static int bio_new(BIO *bio) {
  74. struct bio_bio_st *b;
  75. b = OPENSSL_malloc(sizeof *b);
  76. if (b == NULL) {
  77. return 0;
  78. }
  79. b->peer = NULL;
  80. b->size = 17 * 1024; /* enough for one TLS record (just a default) */
  81. b->buf = NULL;
  82. bio->ptr = b;
  83. return 1;
  84. }
  85. static void bio_destroy_pair(BIO *bio) {
  86. struct bio_bio_st *b = bio->ptr;
  87. BIO *peer_bio;
  88. struct bio_bio_st *peer_b;
  89. if (b == NULL) {
  90. return;
  91. }
  92. peer_bio = b->peer;
  93. if (peer_bio == NULL) {
  94. return;
  95. }
  96. peer_b = peer_bio->ptr;
  97. assert(peer_b != NULL);
  98. assert(peer_b->peer == bio);
  99. peer_b->peer = NULL;
  100. peer_bio->init = 0;
  101. assert(peer_b->buf != NULL);
  102. peer_b->len = 0;
  103. peer_b->offset = 0;
  104. b->peer = NULL;
  105. bio->init = 0;
  106. assert(b->buf != NULL);
  107. b->len = 0;
  108. b->offset = 0;
  109. }
  110. static int bio_free(BIO *bio) {
  111. struct bio_bio_st *b;
  112. if (bio == NULL) {
  113. return 0;
  114. }
  115. b = bio->ptr;
  116. assert(b != NULL);
  117. if (b->peer) {
  118. bio_destroy_pair(bio);
  119. }
  120. if (b->buf != NULL) {
  121. OPENSSL_free(b->buf);
  122. }
  123. OPENSSL_free(b);
  124. return 1;
  125. }
  126. static int bio_read(BIO *bio, char *buf, int size_) {
  127. size_t size = size_;
  128. size_t rest;
  129. struct bio_bio_st *b, *peer_b;
  130. BIO_clear_retry_flags(bio);
  131. if (!bio->init) {
  132. return 0;
  133. }
  134. b = bio->ptr;
  135. assert(b != NULL);
  136. assert(b->peer != NULL);
  137. peer_b = b->peer->ptr;
  138. assert(peer_b != NULL);
  139. assert(peer_b->buf != NULL);
  140. peer_b->request = 0; /* will be set in "retry_read" situation */
  141. if (buf == NULL || size == 0) {
  142. return 0;
  143. }
  144. if (peer_b->len == 0) {
  145. if (peer_b->closed) {
  146. return 0; /* writer has closed, and no data is left */
  147. } else {
  148. BIO_set_retry_read(bio); /* buffer is empty */
  149. if (size <= peer_b->size) {
  150. peer_b->request = size;
  151. } else {
  152. /* don't ask for more than the peer can
  153. * deliver in one write */
  154. peer_b->request = peer_b->size;
  155. }
  156. return -1;
  157. }
  158. }
  159. /* we can read */
  160. if (peer_b->len < size) {
  161. size = peer_b->len;
  162. }
  163. /* now read "size" bytes */
  164. rest = size;
  165. assert(rest > 0);
  166. /* one or two iterations */
  167. do {
  168. size_t chunk;
  169. assert(rest <= peer_b->len);
  170. if (peer_b->offset + rest <= peer_b->size) {
  171. chunk = rest;
  172. } else {
  173. /* wrap around ring buffer */
  174. chunk = peer_b->size - peer_b->offset;
  175. }
  176. assert(peer_b->offset + chunk <= peer_b->size);
  177. memcpy(buf, peer_b->buf + peer_b->offset, chunk);
  178. peer_b->len -= chunk;
  179. if (peer_b->len) {
  180. peer_b->offset += chunk;
  181. assert(peer_b->offset <= peer_b->size);
  182. if (peer_b->offset == peer_b->size) {
  183. peer_b->offset = 0;
  184. }
  185. buf += chunk;
  186. } else {
  187. /* buffer now empty, no need to advance "buf" */
  188. assert(chunk == rest);
  189. peer_b->offset = 0;
  190. }
  191. rest -= chunk;
  192. } while (rest);
  193. return size;
  194. }
  195. static int bio_write(BIO *bio, const char *buf, int num_) {
  196. size_t num = num_;
  197. size_t rest;
  198. struct bio_bio_st *b;
  199. BIO_clear_retry_flags(bio);
  200. if (!bio->init || buf == NULL || num == 0) {
  201. return 0;
  202. }
  203. b = bio->ptr;
  204. assert(b != NULL);
  205. assert(b->peer != NULL);
  206. assert(b->buf != NULL);
  207. b->request = 0;
  208. if (b->closed) {
  209. /* we already closed */
  210. OPENSSL_PUT_ERROR(BIO, bio_write, BIO_R_BROKEN_PIPE);
  211. return -1;
  212. }
  213. assert(b->len <= b->size);
  214. if (b->len == b->size) {
  215. BIO_set_retry_write(bio); /* buffer is full */
  216. return -1;
  217. }
  218. /* we can write */
  219. if (num > b->size - b->len) {
  220. num = b->size - b->len;
  221. }
  222. /* now write "num" bytes */
  223. rest = num;
  224. assert(rest > 0);
  225. /* one or two iterations */
  226. do {
  227. size_t write_offset;
  228. size_t chunk;
  229. assert(b->len + rest <= b->size);
  230. write_offset = b->offset + b->len;
  231. if (write_offset >= b->size) {
  232. write_offset -= b->size;
  233. }
  234. /* b->buf[write_offset] is the first byte we can write to. */
  235. if (write_offset + rest <= b->size) {
  236. chunk = rest;
  237. } else {
  238. /* wrap around ring buffer */
  239. chunk = b->size - write_offset;
  240. }
  241. memcpy(b->buf + write_offset, buf, chunk);
  242. b->len += chunk;
  243. assert(b->len <= b->size);
  244. rest -= chunk;
  245. buf += chunk;
  246. } while (rest);
  247. return num;
  248. }
  249. static int bio_make_pair(BIO *bio1, BIO *bio2) {
  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_make_pair, BIO_R_IN_USE);
  257. return 0;
  258. }
  259. if (b1->buf == NULL) {
  260. b1->buf = OPENSSL_malloc(b1->size);
  261. if (b1->buf == NULL) {
  262. OPENSSL_PUT_ERROR(BIO, bio_make_pair, ERR_R_MALLOC_FAILURE);
  263. return 0;
  264. }
  265. b1->len = 0;
  266. b1->offset = 0;
  267. }
  268. if (b2->buf == NULL) {
  269. b2->buf = OPENSSL_malloc(b2->size);
  270. if (b2->buf == NULL) {
  271. OPENSSL_PUT_ERROR(BIO, bio_make_pair, ERR_R_MALLOC_FAILURE);
  272. return 0;
  273. }
  274. b2->len = 0;
  275. b2->offset = 0;
  276. }
  277. b1->peer = bio2;
  278. b1->closed = 0;
  279. b1->request = 0;
  280. b2->peer = bio1;
  281. b2->closed = 0;
  282. b2->request = 0;
  283. bio1->init = 1;
  284. bio2->init = 1;
  285. return 1;
  286. }
  287. static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  288. long ret;
  289. struct bio_bio_st *b = bio->ptr;
  290. assert(b != NULL);
  291. switch (cmd) {
  292. /* specific CTRL codes */
  293. case BIO_C_SET_BUFF_SIZE:
  294. if (b->peer) {
  295. OPENSSL_PUT_ERROR(BIO, bio_ctrl, BIO_R_IN_USE);
  296. ret = 0;
  297. } else if (num == 0) {
  298. OPENSSL_PUT_ERROR(BIO, bio_ctrl, BIO_R_INVALID_ARGUMENT);
  299. ret = 0;
  300. } else {
  301. size_t new_size = num;
  302. if (b->size != new_size) {
  303. if (b->buf) {
  304. OPENSSL_free(b->buf);
  305. b->buf = NULL;
  306. }
  307. b->size = new_size;
  308. }
  309. ret = 1;
  310. }
  311. break;
  312. case BIO_C_GET_WRITE_BUF_SIZE:
  313. ret = (long)b->size;
  314. break;
  315. case BIO_C_GET_WRITE_GUARANTEE:
  316. /* How many bytes can the caller feed to the next write
  317. * without having to keep any? */
  318. if (b->peer == NULL || b->closed) {
  319. ret = 0;
  320. } else {
  321. ret = (long)b->size - b->len;
  322. }
  323. break;
  324. case BIO_C_GET_READ_REQUEST:
  325. /* If the peer unsuccessfully tried to read, how many bytes
  326. * were requested? (As with BIO_CTRL_PENDING, that number
  327. * can usually be treated as boolean.) */
  328. ret = (long)b->request;
  329. break;
  330. case BIO_C_RESET_READ_REQUEST:
  331. /* Reset request. (Can be useful after read attempts
  332. * at the other side that are meant to be non-blocking,
  333. * e.g. when probing SSL_read to see if any data is
  334. * available.) */
  335. b->request = 0;
  336. ret = 1;
  337. break;
  338. case BIO_C_SHUTDOWN_WR:
  339. /* similar to shutdown(..., SHUT_WR) */
  340. b->closed = 1;
  341. ret = 1;
  342. break;
  343. /* standard CTRL codes follow */
  344. case BIO_CTRL_RESET:
  345. if (b->buf != NULL) {
  346. b->len = 0;
  347. b->offset = 0;
  348. }
  349. ret = 0;
  350. break;
  351. case BIO_CTRL_GET_CLOSE:
  352. ret = bio->shutdown;
  353. break;
  354. case BIO_CTRL_SET_CLOSE:
  355. bio->shutdown = (int)num;
  356. ret = 1;
  357. break;
  358. case BIO_CTRL_PENDING:
  359. if (b->peer != NULL) {
  360. struct bio_bio_st *peer_b = b->peer->ptr;
  361. ret = (long)peer_b->len;
  362. } else {
  363. ret = 0;
  364. }
  365. break;
  366. case BIO_CTRL_WPENDING:
  367. ret = 0;
  368. if (b->buf != NULL) {
  369. ret = (long)b->len;
  370. }
  371. break;
  372. case BIO_CTRL_FLUSH:
  373. ret = 1;
  374. break;
  375. case BIO_CTRL_EOF: {
  376. BIO *other_bio = ptr;
  377. if (other_bio) {
  378. struct bio_bio_st *other_b = other_bio->ptr;
  379. assert(other_b != NULL);
  380. ret = other_b->len == 0 && other_b->closed;
  381. } else {
  382. ret = 1;
  383. }
  384. } break;
  385. default:
  386. ret = 0;
  387. }
  388. return ret;
  389. }
  390. static int bio_puts(BIO *bio, const char *str) {
  391. return bio_write(bio, str, strlen(str));
  392. }
  393. int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1, BIO **bio2_p,
  394. size_t writebuf2) {
  395. BIO *bio1 = NULL, *bio2 = NULL;
  396. long r;
  397. int ret = 0;
  398. bio1 = BIO_new(BIO_s_bio());
  399. if (bio1 == NULL) {
  400. goto err;
  401. }
  402. bio2 = BIO_new(BIO_s_bio());
  403. if (bio2 == NULL) {
  404. goto err;
  405. }
  406. if (writebuf1) {
  407. r = BIO_set_write_buffer_size(bio1, writebuf1);
  408. if (!r) {
  409. goto err;
  410. }
  411. }
  412. if (writebuf2) {
  413. r = BIO_set_write_buffer_size(bio2, writebuf2);
  414. if (!r) {
  415. goto err;
  416. }
  417. }
  418. if (!bio_make_pair(bio1, bio2)) {
  419. goto err;
  420. }
  421. ret = 1;
  422. err:
  423. if (ret == 0) {
  424. if (bio1) {
  425. BIO_free(bio1);
  426. bio1 = NULL;
  427. }
  428. if (bio2) {
  429. BIO_free(bio2);
  430. bio2 = NULL;
  431. }
  432. }
  433. *bio1_p = bio1;
  434. *bio2_p = bio2;
  435. return ret;
  436. }
  437. static const BIO_METHOD methods_biop = {
  438. BIO_TYPE_BIO, "BIO pair", bio_write, bio_read,
  439. bio_puts, NULL /* no bio_gets */, bio_ctrl, bio_new,
  440. bio_free, NULL /* no bio_callback_ctrl */
  441. };
  442. const BIO_METHOD *BIO_s_bio(void) { return &methods_biop; }
  443. size_t BIO_ctrl_get_read_request(BIO *bio) {
  444. return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
  445. }
  446. size_t BIO_ctrl_get_write_guarantee(BIO *bio) {
  447. return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
  448. }
  449. int BIO_shutdown_wr(BIO *bio) {
  450. return BIO_ctrl(bio, BIO_C_SHUTDOWN_WR, 0, NULL);
  451. }