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).
56 /* Special method for a BIO where the other endpoint is also a BIO
57 * of this kind, handled by the same thread (i.e. the "peer" is actually
58 * ourselves, wearing a different hat).
59 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
60 * for which no specific BIO method is available.
61 * 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 =
113 NULL /* no bio_gets */,
117 NULL /* no bio_callback_ctrl */
120 BIO_METHOD *BIO_s_bio(void)
122 return &methods_biop;
127 BIO *peer; /* NULL if buf == NULL.
128 * If peer != NULL, then peer->ptr is also a bio_bio_st,
129 * and its "peer" member points back to us.
130 * peer != NULL iff init != 0 in the BIO. */
132 /* This is for what we write (i.e. reading uses peer's struct): */
133 int closed; /* valid iff peer != NULL */
134 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
135 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
137 char *buf; /* "size" elements (if != NULL) */
139 size_t request; /* valid iff peer != NULL; 0 if len != 0,
140 * otherwise set by peer to number of bytes
141 * it (unsuccessfully) tried to read,
142 * never more than buffer space (size-len) warrants. */
145 static int bio_new(BIO *bio)
147 struct bio_bio_st *b;
149 b = OPENSSL_malloc(sizeof *b);
154 b->size = 17*1024; /* enough for one TLS record (just a default) */
162 static int bio_free(BIO *bio)
164 struct bio_bio_st *b;
173 bio_destroy_pair(bio);
177 OPENSSL_free(b->buf);
187 static int bio_read(BIO *bio, char *buf, int size_)
191 struct bio_bio_st *b, *peer_b;
193 BIO_clear_retry_flags(bio);
200 assert(b->peer != NULL);
201 peer_b = b->peer->ptr;
202 assert(peer_b != NULL);
203 assert(peer_b->buf != NULL);
205 peer_b->request = 0; /* will be set in "retry_read" situation */
207 if (buf == NULL || size == 0)
210 if (peer_b->len == 0)
213 return 0; /* writer has closed, and no data is left */
216 BIO_set_retry_read(bio); /* buffer is empty */
217 if (size <= peer_b->size)
218 peer_b->request = size;
220 /* don't ask for more than the peer can
221 * deliver in one write */
222 peer_b->request = peer_b->size;
228 if (peer_b->len < size)
231 /* now read "size" bytes */
236 do /* one or two iterations */
240 assert(rest <= peer_b->len);
241 if (peer_b->offset + rest <= peer_b->size)
244 /* wrap around ring buffer */
245 chunk = peer_b->size - peer_b->offset;
246 assert(peer_b->offset + chunk <= peer_b->size);
248 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
250 peer_b->len -= chunk;
253 peer_b->offset += chunk;
254 assert(peer_b->offset <= peer_b->size);
255 if (peer_b->offset == peer_b->size)
261 /* buffer now empty, no need to advance "buf" */
262 assert(chunk == rest);
273 * non-copying interface: provide pointer to available data in buffer
274 * bio_nread0: return number of available bytes
275 * bio_nread: also advance index
276 * (example usage: bio_nread0(), read from buffer, bio_nread()
277 * or just bio_nread(), read from buffer)
279 /* WARNING: The non-copying interface is largely untested as of yet
280 * and may contain bugs. */
281 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
283 struct bio_bio_st *b, *peer_b;
286 BIO_clear_retry_flags(bio);
293 assert(b->peer != NULL);
294 peer_b = b->peer->ptr;
295 assert(peer_b != NULL);
296 assert(peer_b->buf != NULL);
300 if (peer_b->len == 0)
304 /* avoid code duplication -- nothing available for reading */
305 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
309 if (peer_b->size < peer_b->offset + num)
310 /* no ring buffer wrap-around for non-copying interface */
311 num = peer_b->size - peer_b->offset;
315 *buf = peer_b->buf + peer_b->offset;
319 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
321 struct bio_bio_st *b, *peer_b;
322 ossl_ssize_t num, available;
324 if (num_ > SSIZE_MAX)
327 num = (ossl_ssize_t)num_;
329 available = bio_nread0(bio, buf);
336 peer_b = b->peer->ptr;
341 peer_b->offset += num;
342 assert(peer_b->offset <= peer_b->size);
343 if (peer_b->offset == peer_b->size)
353 static int bio_write(BIO *bio, const char *buf, int num_)
357 struct bio_bio_st *b;
359 BIO_clear_retry_flags(bio);
361 if (!bio->init || buf == NULL || num == 0)
366 assert(b->peer != NULL);
367 assert(b->buf != NULL);
372 /* we already closed */
373 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
377 assert(b->len <= b->size);
379 if (b->len == b->size)
381 BIO_set_retry_write(bio); /* buffer is full */
386 if (num > b->size - b->len)
387 num = b->size - b->len;
389 /* now write "num" bytes */
394 do /* one or two iterations */
399 assert(b->len + rest <= b->size);
401 write_offset = b->offset + b->len;
402 if (write_offset >= b->size)
403 write_offset -= b->size;
404 /* b->buf[write_offset] is the first byte we can write to. */
406 if (write_offset + rest <= b->size)
409 /* wrap around ring buffer */
410 chunk = b->size - write_offset;
412 memcpy(b->buf + write_offset, buf, chunk);
416 assert(b->len <= b->size);
427 * non-copying interface: provide pointer to region to write to
428 * bio_nwrite0: check how much space is available
429 * bio_nwrite: also increase length
430 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
431 * or just bio_nwrite(), write to buffer)
433 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
435 struct bio_bio_st *b;
439 BIO_clear_retry_flags(bio);
446 assert(b->peer != NULL);
447 assert(b->buf != NULL);
452 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
456 assert(b->len <= b->size);
458 if (b->len == b->size)
460 BIO_set_retry_write(bio);
464 num = b->size - b->len;
465 write_offset = b->offset + b->len;
466 if (write_offset >= b->size)
467 write_offset -= b->size;
468 if (write_offset + num > b->size)
469 /* no ring buffer wrap-around for non-copying interface
470 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
471 * BIO_nwrite may have to be called twice) */
472 num = b->size - write_offset;
475 *buf = b->buf + write_offset;
476 assert(write_offset + num <= b->size);
481 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
483 struct bio_bio_st *b;
484 ossl_ssize_t num, space;
486 if (num_ > SSIZE_MAX)
489 num = (ossl_ssize_t)num_;
491 space = bio_nwrite0(bio, buf);
499 assert(b->len <= b->size);
505 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
508 struct bio_bio_st *b = bio->ptr;
514 /* specific CTRL codes */
516 case BIO_C_SET_WRITE_BUF_SIZE:
519 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
524 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
529 size_t new_size = num;
531 if (b->size != new_size)
535 OPENSSL_free(b->buf);
544 case BIO_C_GET_WRITE_BUF_SIZE:
545 ret = (long) b->size;
548 case BIO_C_MAKE_BIO_PAIR:
550 BIO *other_bio = ptr;
552 if (bio_make_pair(bio, other_bio))
559 case BIO_C_DESTROY_BIO_PAIR:
560 /* Affects both BIOs in the pair -- call just once!
561 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
562 bio_destroy_pair(bio);
566 case BIO_C_GET_WRITE_GUARANTEE:
567 /* How many bytes can the caller feed to the next write
568 * without having to keep any? */
569 if (b->peer == NULL || b->closed)
572 ret = (long) b->size - b->len;
575 case BIO_C_GET_READ_REQUEST:
576 /* If the peer unsuccessfully tried to read, how many bytes
577 * were requested? (As with BIO_CTRL_PENDING, that number
578 * can usually be treated as boolean.) */
579 ret = (long) b->request;
582 case BIO_C_RESET_READ_REQUEST:
583 /* Reset request. (Can be useful after read attempts
584 * at the other side that are meant to be non-blocking,
585 * e.g. when probing SSL_read to see if any data is
591 case BIO_C_SHUTDOWN_WR:
592 /* similar to shutdown(..., SHUT_WR) */
598 /* prepare for non-copying read */
599 ret = (long) bio_nread0(bio, ptr);
603 /* non-copying read */
604 ret = (long) bio_nread(bio, ptr, (size_t) num);
608 /* prepare for non-copying write */
609 ret = (long) bio_nwrite0(bio, ptr);
613 /* non-copying write */
614 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
618 /* standard CTRL codes follow */
629 case BIO_CTRL_GET_CLOSE:
633 case BIO_CTRL_SET_CLOSE:
634 bio->shutdown = (int) num;
638 case BIO_CTRL_PENDING:
641 struct bio_bio_st *peer_b = b->peer->ptr;
643 ret = (long) peer_b->len;
649 case BIO_CTRL_WPENDING:
657 /* See BIO_dup_chain for circumstances we have to expect. */
659 BIO *other_bio = ptr;
660 struct bio_bio_st *other_b;
662 assert(other_bio != NULL);
663 other_b = other_bio->ptr;
664 assert(other_b != NULL);
666 assert(other_b->buf == NULL); /* other_bio is always fresh */
668 other_b->size = b->size;
680 BIO *other_bio = ptr;
684 struct bio_bio_st *other_b = other_bio->ptr;
686 assert(other_b != NULL);
687 ret = other_b->len == 0 && other_b->closed;
700 static int bio_puts(BIO *bio, const char *str)
702 return bio_write(bio, str, strlen(str));
706 static int bio_make_pair(BIO *bio1, BIO *bio2)
708 struct bio_bio_st *b1, *b2;
710 assert(bio1 != NULL);
711 assert(bio2 != NULL);
716 if (b1->peer != NULL || b2->peer != NULL)
718 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
724 b1->buf = OPENSSL_malloc(b1->size);
727 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
736 b2->buf = OPENSSL_malloc(b2->size);
739 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
759 static void bio_destroy_pair(BIO *bio)
761 struct bio_bio_st *b = bio->ptr;
765 BIO *peer_bio = b->peer;
767 if (peer_bio != NULL)
769 struct bio_bio_st *peer_b = peer_bio->ptr;
771 assert(peer_b != NULL);
772 assert(peer_b->peer == bio);
776 assert(peer_b->buf != NULL);
782 assert(b->buf != NULL);
790 /* Exported convenience functions */
791 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
792 BIO **bio2_p, size_t writebuf2)
794 BIO *bio1 = NULL, *bio2 = NULL;
798 bio1 = BIO_new(BIO_s_bio());
801 bio2 = BIO_new(BIO_s_bio());
807 r = BIO_set_write_buf_size(bio1, writebuf1);
813 r = BIO_set_write_buf_size(bio2, writebuf2);
818 r = BIO_make_bio_pair(bio1, bio2);
843 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
845 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
848 size_t BIO_ctrl_get_read_request(BIO *bio)
850 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
853 int BIO_ctrl_reset_read_request(BIO *bio)
855 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
859 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
860 * (conceivably some other BIOs could allow non-copying reads and writes too.)
862 int BIO_nread0(BIO *bio, char **buf)
868 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
872 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
879 int BIO_nread(BIO *bio, char **buf, int num)
885 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
889 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
891 bio->num_read += ret;
895 int BIO_nwrite0(BIO *bio, char **buf)
901 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
905 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
912 int BIO_nwrite(BIO *bio, char **buf, int num)
918 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
922 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
924 bio->num_write += ret;