1 /* crypto/bio/bss_bio.c */
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);
155 /* enough for one TLS record (just a default) */
164 static int bio_free(BIO *bio)
166 struct bio_bio_st *b;
175 bio_destroy_pair(bio);
177 if (b->buf != NULL) {
178 OPENSSL_free(b->buf);
186 static int bio_read(BIO *bio, char *buf, int size_)
190 struct bio_bio_st *b, *peer_b;
192 BIO_clear_retry_flags(bio);
199 assert(b->peer != NULL);
200 peer_b = b->peer->ptr;
201 assert(peer_b != NULL);
202 assert(peer_b->buf != NULL);
204 peer_b->request = 0; /* will be set in "retry_read" situation */
206 if (buf == NULL || size == 0)
209 if (peer_b->len == 0) {
211 return 0; /* writer has closed, and no data is left */
213 BIO_set_retry_read(bio); /* buffer is empty */
214 if (size <= peer_b->size)
215 peer_b->request = size;
218 * don't ask for more than the peer can deliver in one write
220 peer_b->request = peer_b->size;
226 if (peer_b->len < size)
229 /* now read "size" bytes */
234 do { /* one or two iterations */
237 assert(rest <= peer_b->len);
238 if (peer_b->offset + rest <= peer_b->size)
241 /* wrap around ring buffer */
242 chunk = peer_b->size - peer_b->offset;
243 assert(peer_b->offset + chunk <= peer_b->size);
245 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
247 peer_b->len -= chunk;
249 peer_b->offset += chunk;
250 assert(peer_b->offset <= peer_b->size);
251 if (peer_b->offset == peer_b->size)
255 /* buffer now empty, no need to advance "buf" */
256 assert(chunk == rest);
267 * non-copying interface: provide pointer to available data in buffer
268 * bio_nread0: return number of available bytes
269 * bio_nread: also advance index
270 * (example usage: bio_nread0(), read from buffer, bio_nread()
271 * or just bio_nread(), read from buffer)
274 * WARNING: The non-copying interface is largely untested as of yet and may
277 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
279 struct bio_bio_st *b, *peer_b;
282 BIO_clear_retry_flags(bio);
289 assert(b->peer != NULL);
290 peer_b = b->peer->ptr;
291 assert(peer_b != NULL);
292 assert(peer_b->buf != NULL);
296 if (peer_b->len == 0) {
299 /* avoid code duplication -- nothing available for reading */
300 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
304 if (peer_b->size < peer_b->offset + num)
305 /* no ring buffer wrap-around for non-copying interface */
306 num = peer_b->size - peer_b->offset;
310 *buf = peer_b->buf + peer_b->offset;
314 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
316 struct bio_bio_st *b, *peer_b;
317 ossl_ssize_t num, available;
319 if (num_ > SSIZE_MAX)
322 num = (ossl_ssize_t) num_;
324 available = bio_nread0(bio, buf);
331 peer_b = b->peer->ptr;
335 peer_b->offset += num;
336 assert(peer_b->offset <= peer_b->size);
337 if (peer_b->offset == peer_b->size)
345 static int bio_write(BIO *bio, const char *buf, int num_)
349 struct bio_bio_st *b;
351 BIO_clear_retry_flags(bio);
353 if (!bio->init || buf == NULL || num == 0)
358 assert(b->peer != NULL);
359 assert(b->buf != NULL);
363 /* we already closed */
364 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
368 assert(b->len <= b->size);
370 if (b->len == b->size) {
371 BIO_set_retry_write(bio); /* buffer is full */
376 if (num > b->size - b->len)
377 num = b->size - b->len;
379 /* now write "num" bytes */
384 do { /* one or two iterations */
388 assert(b->len + rest <= b->size);
390 write_offset = b->offset + b->len;
391 if (write_offset >= b->size)
392 write_offset -= b->size;
393 /* b->buf[write_offset] is the first byte we can write to. */
395 if (write_offset + rest <= b->size)
398 /* wrap around ring buffer */
399 chunk = b->size - write_offset;
401 memcpy(b->buf + write_offset, buf, chunk);
405 assert(b->len <= b->size);
416 * non-copying interface: provide pointer to region to write to
417 * bio_nwrite0: check how much space is available
418 * bio_nwrite: also increase length
419 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
420 * or just bio_nwrite(), write to buffer)
422 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
424 struct bio_bio_st *b;
428 BIO_clear_retry_flags(bio);
435 assert(b->peer != NULL);
436 assert(b->buf != NULL);
440 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
444 assert(b->len <= b->size);
446 if (b->len == b->size) {
447 BIO_set_retry_write(bio);
451 num = b->size - b->len;
452 write_offset = b->offset + b->len;
453 if (write_offset >= b->size)
454 write_offset -= b->size;
455 if (write_offset + num > b->size)
457 * no ring buffer wrap-around for non-copying interface (to fulfil
458 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
459 * to be called twice)
461 num = b->size - write_offset;
464 *buf = b->buf + write_offset;
465 assert(write_offset + num <= b->size);
470 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
472 struct bio_bio_st *b;
473 ossl_ssize_t num, space;
475 if (num_ > SSIZE_MAX)
478 num = (ossl_ssize_t) num_;
480 space = bio_nwrite0(bio, buf);
488 assert(b->len <= b->size);
493 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
496 struct bio_bio_st *b = bio->ptr;
501 /* specific CTRL codes */
503 case BIO_C_SET_WRITE_BUF_SIZE:
505 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
507 } else if (num == 0) {
508 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
511 size_t new_size = num;
513 if (b->size != new_size) {
515 OPENSSL_free(b->buf);
524 case BIO_C_GET_WRITE_BUF_SIZE:
528 case BIO_C_MAKE_BIO_PAIR:
530 BIO *other_bio = ptr;
532 if (bio_make_pair(bio, other_bio))
539 case BIO_C_DESTROY_BIO_PAIR:
541 * Affects both BIOs in the pair -- call just once! Or let
542 * BIO_free(bio1); BIO_free(bio2); do the job.
544 bio_destroy_pair(bio);
548 case BIO_C_GET_WRITE_GUARANTEE:
550 * How many bytes can the caller feed to the next write without
551 * having to keep any?
553 if (b->peer == NULL || b->closed)
556 ret = (long)b->size - b->len;
559 case BIO_C_GET_READ_REQUEST:
561 * If the peer unsuccessfully tried to read, how many bytes were
562 * requested? (As with BIO_CTRL_PENDING, that number can usually be
563 * treated as boolean.)
565 ret = (long)b->request;
568 case BIO_C_RESET_READ_REQUEST:
570 * Reset request. (Can be useful after read attempts at the other
571 * side that are meant to be non-blocking, e.g. when probing SSL_read
572 * to see if any data is available.)
578 case BIO_C_SHUTDOWN_WR:
579 /* similar to shutdown(..., SHUT_WR) */
585 /* prepare for non-copying read */
586 ret = (long)bio_nread0(bio, ptr);
590 /* non-copying read */
591 ret = (long)bio_nread(bio, ptr, (size_t)num);
595 /* prepare for non-copying write */
596 ret = (long)bio_nwrite0(bio, ptr);
600 /* non-copying write */
601 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
604 /* standard CTRL codes follow */
607 if (b->buf != NULL) {
614 case BIO_CTRL_GET_CLOSE:
618 case BIO_CTRL_SET_CLOSE:
619 bio->shutdown = (int)num;
623 case BIO_CTRL_PENDING:
624 if (b->peer != NULL) {
625 struct bio_bio_st *peer_b = b->peer->ptr;
627 ret = (long)peer_b->len;
632 case BIO_CTRL_WPENDING:
640 /* See BIO_dup_chain for circumstances we have to expect. */
642 BIO *other_bio = ptr;
643 struct bio_bio_st *other_b;
645 assert(other_bio != NULL);
646 other_b = other_bio->ptr;
647 assert(other_b != NULL);
649 assert(other_b->buf == NULL); /* other_bio is always fresh */
651 other_b->size = b->size;
662 if (b->peer != NULL) {
663 struct bio_bio_st *peer_b = b->peer->ptr;
665 if (peer_b->len == 0 && peer_b->closed)
680 static int bio_puts(BIO *bio, const char *str)
682 return bio_write(bio, str, strlen(str));
685 static int bio_make_pair(BIO *bio1, BIO *bio2)
687 struct bio_bio_st *b1, *b2;
689 assert(bio1 != NULL);
690 assert(bio2 != NULL);
695 if (b1->peer != NULL || b2->peer != NULL) {
696 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
700 if (b1->buf == NULL) {
701 b1->buf = OPENSSL_malloc(b1->size);
702 if (b1->buf == NULL) {
703 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
710 if (b2->buf == NULL) {
711 b2->buf = OPENSSL_malloc(b2->size);
712 if (b2->buf == NULL) {
713 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
733 static void bio_destroy_pair(BIO *bio)
735 struct bio_bio_st *b = bio->ptr;
738 BIO *peer_bio = b->peer;
740 if (peer_bio != NULL) {
741 struct bio_bio_st *peer_b = peer_bio->ptr;
743 assert(peer_b != NULL);
744 assert(peer_b->peer == bio);
748 assert(peer_b->buf != NULL);
754 assert(b->buf != NULL);
761 /* Exported convenience functions */
762 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
763 BIO **bio2_p, size_t writebuf2)
765 BIO *bio1 = NULL, *bio2 = NULL;
769 bio1 = BIO_new(BIO_s_bio());
772 bio2 = BIO_new(BIO_s_bio());
777 r = BIO_set_write_buf_size(bio1, writebuf1);
782 r = BIO_set_write_buf_size(bio2, writebuf2);
787 r = BIO_make_bio_pair(bio1, bio2);
809 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
811 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
814 size_t BIO_ctrl_get_read_request(BIO *bio)
816 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
819 int BIO_ctrl_reset_read_request(BIO *bio)
821 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
825 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
826 * (conceivably some other BIOs could allow non-copying reads and writes
829 int BIO_nread0(BIO *bio, char **buf)
834 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
838 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
845 int BIO_nread(BIO *bio, char **buf, int num)
850 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
854 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
856 bio->num_read += ret;
860 int BIO_nwrite0(BIO *bio, char **buf)
865 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
869 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
876 int BIO_nwrite(BIO *bio, char **buf, int num)
881 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
885 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
887 bio->num_write += ret;