1 /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
2 /* ====================================================================
3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
57 * Special method for a BIO where the other endpoint is also a BIO of this
58 * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
59 * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
60 * library with I/O interfaces for which no specific BIO method is available.
61 * See ssl/ssltest.c for some hints on how this can be used.
64 /* BIO_DEBUG implies BIO_PAIR_DEBUG */
66 # ifndef BIO_PAIR_DEBUG
67 # define BIO_PAIR_DEBUG
71 /* disable assert() unless BIO_PAIR_DEBUG has been defined */
72 #ifndef BIO_PAIR_DEBUG
83 #include <openssl/bio.h>
84 #include <openssl/err.h>
85 #include <openssl/crypto.h>
89 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
90 #if defined(OPENSSL_SYS_VXWORKS)
94 # define SSIZE_MAX INT_MAX
97 static int bio_new(BIO *bio);
98 static int bio_free(BIO *bio);
99 static int bio_read(BIO *bio, char *buf, int size);
100 static int bio_write(BIO *bio, const char *buf, int num);
101 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
102 static int bio_puts(BIO *bio, const char *str);
104 static int bio_make_pair(BIO *bio1, BIO *bio2);
105 static void bio_destroy_pair(BIO *bio);
107 static BIO_METHOD methods_biop = {
113 NULL /* no bio_gets */ ,
117 NULL /* no bio_callback_ctrl */
120 BIO_METHOD *BIO_s_bio(void)
122 return &methods_biop;
126 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
127 * peer->ptr is also a bio_bio_st, and its
128 * "peer" member points back to us. peer !=
129 * NULL iff init != 0 in the BIO. */
130 /* This is for what we write (i.e. reading uses peer's struct): */
131 int closed; /* valid iff peer != NULL */
132 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
133 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
135 char *buf; /* "size" elements (if != NULL) */
136 size_t request; /* valid iff peer != NULL; 0 if len != 0,
137 * otherwise set by peer to number of bytes
138 * it (unsuccessfully) tried to read, never
139 * more than buffer space (size-len)
143 static int bio_new(BIO *bio)
145 struct bio_bio_st *b;
147 b = OPENSSL_malloc(sizeof(*b));
152 /* enough for one TLS record (just a default) */
160 static int bio_free(BIO *bio)
162 struct bio_bio_st *b;
171 bio_destroy_pair(bio);
173 OPENSSL_free(b->buf);
179 static int bio_read(BIO *bio, char *buf, int size_)
183 struct bio_bio_st *b, *peer_b;
185 BIO_clear_retry_flags(bio);
192 assert(b->peer != NULL);
193 peer_b = b->peer->ptr;
194 assert(peer_b != NULL);
195 assert(peer_b->buf != NULL);
197 peer_b->request = 0; /* will be set in "retry_read" situation */
199 if (buf == NULL || size == 0)
202 if (peer_b->len == 0) {
204 return 0; /* writer has closed, and no data is left */
206 BIO_set_retry_read(bio); /* buffer is empty */
207 if (size <= peer_b->size)
208 peer_b->request = size;
211 * don't ask for more than the peer can deliver in one write
213 peer_b->request = peer_b->size;
219 if (peer_b->len < size)
222 /* now read "size" bytes */
227 do { /* one or two iterations */
230 assert(rest <= peer_b->len);
231 if (peer_b->offset + rest <= peer_b->size)
234 /* wrap around ring buffer */
235 chunk = peer_b->size - peer_b->offset;
236 assert(peer_b->offset + chunk <= peer_b->size);
238 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
240 peer_b->len -= chunk;
242 peer_b->offset += chunk;
243 assert(peer_b->offset <= peer_b->size);
244 if (peer_b->offset == peer_b->size)
248 /* buffer now empty, no need to advance "buf" */
249 assert(chunk == rest);
260 * non-copying interface: provide pointer to available data in buffer
261 * bio_nread0: return number of available bytes
262 * bio_nread: also advance index
263 * (example usage: bio_nread0(), read from buffer, bio_nread()
264 * or just bio_nread(), read from buffer)
267 * WARNING: The non-copying interface is largely untested as of yet and may
270 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
272 struct bio_bio_st *b, *peer_b;
275 BIO_clear_retry_flags(bio);
282 assert(b->peer != NULL);
283 peer_b = b->peer->ptr;
284 assert(peer_b != NULL);
285 assert(peer_b->buf != NULL);
289 if (peer_b->len == 0) {
292 /* avoid code duplication -- nothing available for reading */
293 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
297 if (peer_b->size < peer_b->offset + num)
298 /* no ring buffer wrap-around for non-copying interface */
299 num = peer_b->size - peer_b->offset;
303 *buf = peer_b->buf + peer_b->offset;
307 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
309 struct bio_bio_st *b, *peer_b;
310 ossl_ssize_t num, available;
312 if (num_ > SSIZE_MAX)
315 num = (ossl_ssize_t) num_;
317 available = bio_nread0(bio, buf);
324 peer_b = b->peer->ptr;
328 peer_b->offset += num;
329 assert(peer_b->offset <= peer_b->size);
330 if (peer_b->offset == peer_b->size)
338 static int bio_write(BIO *bio, const char *buf, int num_)
342 struct bio_bio_st *b;
344 BIO_clear_retry_flags(bio);
346 if (!bio->init || buf == NULL || num == 0)
351 assert(b->peer != NULL);
352 assert(b->buf != NULL);
356 /* we already closed */
357 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
361 assert(b->len <= b->size);
363 if (b->len == b->size) {
364 BIO_set_retry_write(bio); /* buffer is full */
369 if (num > b->size - b->len)
370 num = b->size - b->len;
372 /* now write "num" bytes */
377 do { /* one or two iterations */
381 assert(b->len + rest <= b->size);
383 write_offset = b->offset + b->len;
384 if (write_offset >= b->size)
385 write_offset -= b->size;
386 /* b->buf[write_offset] is the first byte we can write to. */
388 if (write_offset + rest <= b->size)
391 /* wrap around ring buffer */
392 chunk = b->size - write_offset;
394 memcpy(b->buf + write_offset, buf, chunk);
398 assert(b->len <= b->size);
409 * non-copying interface: provide pointer to region to write to
410 * bio_nwrite0: check how much space is available
411 * bio_nwrite: also increase length
412 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
413 * or just bio_nwrite(), write to buffer)
415 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
417 struct bio_bio_st *b;
421 BIO_clear_retry_flags(bio);
428 assert(b->peer != NULL);
429 assert(b->buf != NULL);
433 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
437 assert(b->len <= b->size);
439 if (b->len == b->size) {
440 BIO_set_retry_write(bio);
444 num = b->size - b->len;
445 write_offset = b->offset + b->len;
446 if (write_offset >= b->size)
447 write_offset -= b->size;
448 if (write_offset + num > b->size)
450 * no ring buffer wrap-around for non-copying interface (to fulfil
451 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
452 * to be called twice)
454 num = b->size - write_offset;
457 *buf = b->buf + write_offset;
458 assert(write_offset + num <= b->size);
463 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
465 struct bio_bio_st *b;
466 ossl_ssize_t num, space;
468 if (num_ > SSIZE_MAX)
471 num = (ossl_ssize_t) num_;
473 space = bio_nwrite0(bio, buf);
481 assert(b->len <= b->size);
486 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
489 struct bio_bio_st *b = bio->ptr;
494 /* specific CTRL codes */
496 case BIO_C_SET_WRITE_BUF_SIZE:
498 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
500 } else if (num == 0) {
501 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
504 size_t new_size = num;
506 if (b->size != new_size) {
507 OPENSSL_free(b->buf);
515 case BIO_C_GET_WRITE_BUF_SIZE:
519 case BIO_C_MAKE_BIO_PAIR:
521 BIO *other_bio = ptr;
523 if (bio_make_pair(bio, other_bio))
530 case BIO_C_DESTROY_BIO_PAIR:
532 * Affects both BIOs in the pair -- call just once! Or let
533 * BIO_free(bio1); BIO_free(bio2); do the job.
535 bio_destroy_pair(bio);
539 case BIO_C_GET_WRITE_GUARANTEE:
541 * How many bytes can the caller feed to the next write without
542 * having to keep any?
544 if (b->peer == NULL || b->closed)
547 ret = (long)b->size - b->len;
550 case BIO_C_GET_READ_REQUEST:
552 * If the peer unsuccessfully tried to read, how many bytes were
553 * requested? (As with BIO_CTRL_PENDING, that number can usually be
554 * treated as boolean.)
556 ret = (long)b->request;
559 case BIO_C_RESET_READ_REQUEST:
561 * Reset request. (Can be useful after read attempts at the other
562 * side that are meant to be non-blocking, e.g. when probing SSL_read
563 * to see if any data is available.)
569 case BIO_C_SHUTDOWN_WR:
570 /* similar to shutdown(..., SHUT_WR) */
576 /* prepare for non-copying read */
577 ret = (long)bio_nread0(bio, ptr);
581 /* non-copying read */
582 ret = (long)bio_nread(bio, ptr, (size_t)num);
586 /* prepare for non-copying write */
587 ret = (long)bio_nwrite0(bio, ptr);
591 /* non-copying write */
592 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
595 /* standard CTRL codes follow */
598 if (b->buf != NULL) {
605 case BIO_CTRL_GET_CLOSE:
609 case BIO_CTRL_SET_CLOSE:
610 bio->shutdown = (int)num;
614 case BIO_CTRL_PENDING:
615 if (b->peer != NULL) {
616 struct bio_bio_st *peer_b = b->peer->ptr;
618 ret = (long)peer_b->len;
623 case BIO_CTRL_WPENDING:
631 /* See BIO_dup_chain for circumstances we have to expect. */
633 BIO *other_bio = ptr;
634 struct bio_bio_st *other_b;
636 assert(other_bio != NULL);
637 other_b = other_bio->ptr;
638 assert(other_b != NULL);
640 assert(other_b->buf == NULL); /* other_bio is always fresh */
642 other_b->size = b->size;
654 BIO *other_bio = ptr;
657 struct bio_bio_st *other_b = other_bio->ptr;
659 assert(other_b != NULL);
660 ret = other_b->len == 0 && other_b->closed;
672 static int bio_puts(BIO *bio, const char *str)
674 return bio_write(bio, str, strlen(str));
677 static int bio_make_pair(BIO *bio1, BIO *bio2)
679 struct bio_bio_st *b1, *b2;
681 assert(bio1 != NULL);
682 assert(bio2 != NULL);
687 if (b1->peer != NULL || b2->peer != NULL) {
688 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
692 if (b1->buf == NULL) {
693 b1->buf = OPENSSL_malloc(b1->size);
694 if (b1->buf == NULL) {
695 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
702 if (b2->buf == NULL) {
703 b2->buf = OPENSSL_malloc(b2->size);
704 if (b2->buf == NULL) {
705 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
725 static void bio_destroy_pair(BIO *bio)
727 struct bio_bio_st *b = bio->ptr;
730 BIO *peer_bio = b->peer;
732 if (peer_bio != NULL) {
733 struct bio_bio_st *peer_b = peer_bio->ptr;
735 assert(peer_b != NULL);
736 assert(peer_b->peer == bio);
740 assert(peer_b->buf != NULL);
746 assert(b->buf != NULL);
753 /* Exported convenience functions */
754 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
755 BIO **bio2_p, size_t writebuf2)
757 BIO *bio1 = NULL, *bio2 = NULL;
761 bio1 = BIO_new(BIO_s_bio());
764 bio2 = BIO_new(BIO_s_bio());
769 r = BIO_set_write_buf_size(bio1, writebuf1);
774 r = BIO_set_write_buf_size(bio2, writebuf2);
779 r = BIO_make_bio_pair(bio1, bio2);
797 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
799 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
802 size_t BIO_ctrl_get_read_request(BIO *bio)
804 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
807 int BIO_ctrl_reset_read_request(BIO *bio)
809 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
813 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
814 * (conceivably some other BIOs could allow non-copying reads and writes
817 int BIO_nread0(BIO *bio, char **buf)
822 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
826 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
833 int BIO_nread(BIO *bio, char **buf, int num)
838 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
842 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
844 bio->num_read += ret;
848 int BIO_nwrite0(BIO *bio, char **buf)
853 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
857 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
864 int BIO_nwrite(BIO *bio, char **buf, int num)
869 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
873 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
875 bio->num_write += ret;