1 /* ====================================================================
2 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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.
63 /* BIO_DEBUG implies BIO_PAIR_DEBUG */
65 # ifndef BIO_PAIR_DEBUG
66 # define BIO_PAIR_DEBUG
70 /* disable assert() unless BIO_PAIR_DEBUG has been defined */
71 #ifndef BIO_PAIR_DEBUG
82 #include <openssl/bio.h>
83 #include <openssl/err.h>
84 #include <openssl/crypto.h>
88 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
89 #if defined(OPENSSL_SYS_VXWORKS)
93 # define SSIZE_MAX INT_MAX
96 static int bio_new(BIO *bio);
97 static int bio_free(BIO *bio);
98 static int bio_read(BIO *bio, char *buf, int size);
99 static int bio_write(BIO *bio, const char *buf, int num);
100 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
101 static int bio_puts(BIO *bio, const char *str);
103 static int bio_make_pair(BIO *bio1, BIO *bio2);
104 static void bio_destroy_pair(BIO *bio);
106 static BIO_METHOD methods_biop = {
112 NULL /* no bio_gets */ ,
116 NULL /* no bio_callback_ctrl */
119 BIO_METHOD *BIO_s_bio(void)
121 return &methods_biop;
125 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
126 * peer->ptr is also a bio_bio_st, and its
127 * "peer" member points back to us. peer !=
128 * NULL iff init != 0 in the BIO. */
129 /* This is for what we write (i.e. reading uses peer's struct): */
130 int closed; /* valid iff peer != NULL */
131 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
132 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
134 char *buf; /* "size" elements (if != NULL) */
135 size_t request; /* valid iff peer != NULL; 0 if len != 0,
136 * otherwise set by peer to number of bytes
137 * it (unsuccessfully) tried to read, never
138 * more than buffer space (size-len)
142 static int bio_new(BIO *bio)
144 struct bio_bio_st *b;
146 b = OPENSSL_malloc(sizeof(*b));
151 /* enough for one TLS record (just a default) */
159 static int bio_free(BIO *bio)
161 struct bio_bio_st *b;
170 bio_destroy_pair(bio);
172 OPENSSL_free(b->buf);
178 static int bio_read(BIO *bio, char *buf, int size_)
182 struct bio_bio_st *b, *peer_b;
184 BIO_clear_retry_flags(bio);
191 assert(b->peer != NULL);
192 peer_b = b->peer->ptr;
193 assert(peer_b != NULL);
194 assert(peer_b->buf != NULL);
196 peer_b->request = 0; /* will be set in "retry_read" situation */
198 if (buf == NULL || size == 0)
201 if (peer_b->len == 0) {
203 return 0; /* writer has closed, and no data is left */
205 BIO_set_retry_read(bio); /* buffer is empty */
206 if (size <= peer_b->size)
207 peer_b->request = size;
210 * don't ask for more than the peer can deliver in one write
212 peer_b->request = peer_b->size;
218 if (peer_b->len < size)
221 /* now read "size" bytes */
226 do { /* one or two iterations */
229 assert(rest <= peer_b->len);
230 if (peer_b->offset + rest <= peer_b->size)
233 /* wrap around ring buffer */
234 chunk = peer_b->size - peer_b->offset;
235 assert(peer_b->offset + chunk <= peer_b->size);
237 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
239 peer_b->len -= chunk;
241 peer_b->offset += chunk;
242 assert(peer_b->offset <= peer_b->size);
243 if (peer_b->offset == peer_b->size)
247 /* buffer now empty, no need to advance "buf" */
248 assert(chunk == rest);
259 * non-copying interface: provide pointer to available data in buffer
260 * bio_nread0: return number of available bytes
261 * bio_nread: also advance index
262 * (example usage: bio_nread0(), read from buffer, bio_nread()
263 * or just bio_nread(), read from buffer)
266 * WARNING: The non-copying interface is largely untested as of yet and may
269 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
271 struct bio_bio_st *b, *peer_b;
274 BIO_clear_retry_flags(bio);
281 assert(b->peer != NULL);
282 peer_b = b->peer->ptr;
283 assert(peer_b != NULL);
284 assert(peer_b->buf != NULL);
288 if (peer_b->len == 0) {
291 /* avoid code duplication -- nothing available for reading */
292 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
296 if (peer_b->size < peer_b->offset + num)
297 /* no ring buffer wrap-around for non-copying interface */
298 num = peer_b->size - peer_b->offset;
302 *buf = peer_b->buf + peer_b->offset;
306 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
308 struct bio_bio_st *b, *peer_b;
309 ossl_ssize_t num, available;
311 if (num_ > SSIZE_MAX)
314 num = (ossl_ssize_t) num_;
316 available = bio_nread0(bio, buf);
323 peer_b = b->peer->ptr;
327 peer_b->offset += num;
328 assert(peer_b->offset <= peer_b->size);
329 if (peer_b->offset == peer_b->size)
337 static int bio_write(BIO *bio, const char *buf, int num_)
341 struct bio_bio_st *b;
343 BIO_clear_retry_flags(bio);
345 if (!bio->init || buf == NULL || num == 0)
350 assert(b->peer != NULL);
351 assert(b->buf != NULL);
355 /* we already closed */
356 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
360 assert(b->len <= b->size);
362 if (b->len == b->size) {
363 BIO_set_retry_write(bio); /* buffer is full */
368 if (num > b->size - b->len)
369 num = b->size - b->len;
371 /* now write "num" bytes */
376 do { /* one or two iterations */
380 assert(b->len + rest <= b->size);
382 write_offset = b->offset + b->len;
383 if (write_offset >= b->size)
384 write_offset -= b->size;
385 /* b->buf[write_offset] is the first byte we can write to. */
387 if (write_offset + rest <= b->size)
390 /* wrap around ring buffer */
391 chunk = b->size - write_offset;
393 memcpy(b->buf + write_offset, buf, chunk);
397 assert(b->len <= b->size);
408 * non-copying interface: provide pointer to region to write to
409 * bio_nwrite0: check how much space is available
410 * bio_nwrite: also increase length
411 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
412 * or just bio_nwrite(), write to buffer)
414 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
416 struct bio_bio_st *b;
420 BIO_clear_retry_flags(bio);
427 assert(b->peer != NULL);
428 assert(b->buf != NULL);
432 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
436 assert(b->len <= b->size);
438 if (b->len == b->size) {
439 BIO_set_retry_write(bio);
443 num = b->size - b->len;
444 write_offset = b->offset + b->len;
445 if (write_offset >= b->size)
446 write_offset -= b->size;
447 if (write_offset + num > b->size)
449 * no ring buffer wrap-around for non-copying interface (to fulfil
450 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
451 * to be called twice)
453 num = b->size - write_offset;
456 *buf = b->buf + write_offset;
457 assert(write_offset + num <= b->size);
462 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
464 struct bio_bio_st *b;
465 ossl_ssize_t num, space;
467 if (num_ > SSIZE_MAX)
470 num = (ossl_ssize_t) num_;
472 space = bio_nwrite0(bio, buf);
480 assert(b->len <= b->size);
485 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
488 struct bio_bio_st *b = bio->ptr;
493 /* specific CTRL codes */
495 case BIO_C_SET_WRITE_BUF_SIZE:
497 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
499 } else if (num == 0) {
500 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
503 size_t new_size = num;
505 if (b->size != new_size) {
506 OPENSSL_free(b->buf);
514 case BIO_C_GET_WRITE_BUF_SIZE:
518 case BIO_C_MAKE_BIO_PAIR:
520 BIO *other_bio = ptr;
522 if (bio_make_pair(bio, other_bio))
529 case BIO_C_DESTROY_BIO_PAIR:
531 * Affects both BIOs in the pair -- call just once! Or let
532 * BIO_free(bio1); BIO_free(bio2); do the job.
534 bio_destroy_pair(bio);
538 case BIO_C_GET_WRITE_GUARANTEE:
540 * How many bytes can the caller feed to the next write without
541 * having to keep any?
543 if (b->peer == NULL || b->closed)
546 ret = (long)b->size - b->len;
549 case BIO_C_GET_READ_REQUEST:
551 * If the peer unsuccessfully tried to read, how many bytes were
552 * requested? (As with BIO_CTRL_PENDING, that number can usually be
553 * treated as boolean.)
555 ret = (long)b->request;
558 case BIO_C_RESET_READ_REQUEST:
560 * Reset request. (Can be useful after read attempts at the other
561 * side that are meant to be non-blocking, e.g. when probing SSL_read
562 * to see if any data is available.)
568 case BIO_C_SHUTDOWN_WR:
569 /* similar to shutdown(..., SHUT_WR) */
575 /* prepare for non-copying read */
576 ret = (long)bio_nread0(bio, ptr);
580 /* non-copying read */
581 ret = (long)bio_nread(bio, ptr, (size_t)num);
585 /* prepare for non-copying write */
586 ret = (long)bio_nwrite0(bio, ptr);
590 /* non-copying write */
591 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
594 /* standard CTRL codes follow */
597 if (b->buf != NULL) {
604 case BIO_CTRL_GET_CLOSE:
608 case BIO_CTRL_SET_CLOSE:
609 bio->shutdown = (int)num;
613 case BIO_CTRL_PENDING:
614 if (b->peer != NULL) {
615 struct bio_bio_st *peer_b = b->peer->ptr;
617 ret = (long)peer_b->len;
622 case BIO_CTRL_WPENDING:
630 /* See BIO_dup_chain for circumstances we have to expect. */
632 BIO *other_bio = ptr;
633 struct bio_bio_st *other_b;
635 assert(other_bio != NULL);
636 other_b = other_bio->ptr;
637 assert(other_b != NULL);
639 assert(other_b->buf == NULL); /* other_bio is always fresh */
641 other_b->size = b->size;
653 BIO *other_bio = ptr;
656 struct bio_bio_st *other_b = other_bio->ptr;
658 assert(other_b != NULL);
659 ret = other_b->len == 0 && other_b->closed;
671 static int bio_puts(BIO *bio, const char *str)
673 return bio_write(bio, str, strlen(str));
676 static int bio_make_pair(BIO *bio1, BIO *bio2)
678 struct bio_bio_st *b1, *b2;
680 assert(bio1 != NULL);
681 assert(bio2 != NULL);
686 if (b1->peer != NULL || b2->peer != NULL) {
687 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
691 if (b1->buf == NULL) {
692 b1->buf = OPENSSL_malloc(b1->size);
693 if (b1->buf == NULL) {
694 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
701 if (b2->buf == NULL) {
702 b2->buf = OPENSSL_malloc(b2->size);
703 if (b2->buf == NULL) {
704 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
724 static void bio_destroy_pair(BIO *bio)
726 struct bio_bio_st *b = bio->ptr;
729 BIO *peer_bio = b->peer;
731 if (peer_bio != NULL) {
732 struct bio_bio_st *peer_b = peer_bio->ptr;
734 assert(peer_b != NULL);
735 assert(peer_b->peer == bio);
739 assert(peer_b->buf != NULL);
745 assert(b->buf != NULL);
752 /* Exported convenience functions */
753 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
754 BIO **bio2_p, size_t writebuf2)
756 BIO *bio1 = NULL, *bio2 = NULL;
760 bio1 = BIO_new(BIO_s_bio());
763 bio2 = BIO_new(BIO_s_bio());
768 r = BIO_set_write_buf_size(bio1, writebuf1);
773 r = BIO_set_write_buf_size(bio2, writebuf2);
778 r = BIO_make_bio_pair(bio1, bio2);
796 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
798 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
801 size_t BIO_ctrl_get_read_request(BIO *bio)
803 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
806 int BIO_ctrl_reset_read_request(BIO *bio)
808 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
812 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
813 * (conceivably some other BIOs could allow non-copying reads and writes
816 int BIO_nread0(BIO *bio, char **buf)
821 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
825 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
832 int BIO_nread(BIO *bio, char **buf, int num)
837 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
841 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
843 bio->num_read += ret;
847 int BIO_nwrite0(BIO *bio, char **buf)
852 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
856 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
863 int BIO_nwrite(BIO *bio, char **buf, int num)
868 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
872 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
874 bio->num_write += ret;