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.
68 #include <openssl/bio.h>
69 #include <openssl/err.h>
70 #include <openssl/crypto.h>
74 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
75 #if defined(OPENSSL_SYS_VXWORKS)
79 # define SSIZE_MAX INT_MAX
82 static int bio_new(BIO *bio);
83 static int bio_free(BIO *bio);
84 static int bio_read(BIO *bio, char *buf, int size);
85 static int bio_write(BIO *bio, const char *buf, int num);
86 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
87 static int bio_puts(BIO *bio, const char *str);
89 static int bio_make_pair(BIO *bio1, BIO *bio2);
90 static void bio_destroy_pair(BIO *bio);
92 static BIO_METHOD methods_biop = {
98 NULL /* no bio_gets */ ,
102 NULL /* no bio_callback_ctrl */
105 BIO_METHOD *BIO_s_bio(void)
107 return &methods_biop;
111 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
112 * peer->ptr is also a bio_bio_st, and its
113 * "peer" member points back to us. peer !=
114 * NULL iff init != 0 in the BIO. */
115 /* This is for what we write (i.e. reading uses peer's struct): */
116 int closed; /* valid iff peer != NULL */
117 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
118 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
120 char *buf; /* "size" elements (if != NULL) */
121 size_t request; /* valid iff peer != NULL; 0 if len != 0,
122 * otherwise set by peer to number of bytes
123 * it (unsuccessfully) tried to read, never
124 * more than buffer space (size-len)
128 static int bio_new(BIO *bio)
130 struct bio_bio_st *b;
132 b = OPENSSL_malloc(sizeof(*b));
137 /* enough for one TLS record (just a default) */
145 static int bio_free(BIO *bio)
147 struct bio_bio_st *b;
156 bio_destroy_pair(bio);
158 OPENSSL_free(b->buf);
164 static int bio_read(BIO *bio, char *buf, int size_)
168 struct bio_bio_st *b, *peer_b;
170 BIO_clear_retry_flags(bio);
177 assert(b->peer != NULL);
178 peer_b = b->peer->ptr;
179 assert(peer_b != NULL);
180 assert(peer_b->buf != NULL);
182 peer_b->request = 0; /* will be set in "retry_read" situation */
184 if (buf == NULL || size == 0)
187 if (peer_b->len == 0) {
189 return 0; /* writer has closed, and no data is left */
191 BIO_set_retry_read(bio); /* buffer is empty */
192 if (size <= peer_b->size)
193 peer_b->request = size;
196 * don't ask for more than the peer can deliver in one write
198 peer_b->request = peer_b->size;
204 if (peer_b->len < size)
207 /* now read "size" bytes */
212 do { /* one or two iterations */
215 assert(rest <= peer_b->len);
216 if (peer_b->offset + rest <= peer_b->size)
219 /* wrap around ring buffer */
220 chunk = peer_b->size - peer_b->offset;
221 assert(peer_b->offset + chunk <= peer_b->size);
223 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
225 peer_b->len -= chunk;
227 peer_b->offset += chunk;
228 assert(peer_b->offset <= peer_b->size);
229 if (peer_b->offset == peer_b->size)
233 /* buffer now empty, no need to advance "buf" */
234 assert(chunk == rest);
245 * non-copying interface: provide pointer to available data in buffer
246 * bio_nread0: return number of available bytes
247 * bio_nread: also advance index
248 * (example usage: bio_nread0(), read from buffer, bio_nread()
249 * or just bio_nread(), read from buffer)
252 * WARNING: The non-copying interface is largely untested as of yet and may
255 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
257 struct bio_bio_st *b, *peer_b;
260 BIO_clear_retry_flags(bio);
267 assert(b->peer != NULL);
268 peer_b = b->peer->ptr;
269 assert(peer_b != NULL);
270 assert(peer_b->buf != NULL);
274 if (peer_b->len == 0) {
277 /* avoid code duplication -- nothing available for reading */
278 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
282 if (peer_b->size < peer_b->offset + num)
283 /* no ring buffer wrap-around for non-copying interface */
284 num = peer_b->size - peer_b->offset;
288 *buf = peer_b->buf + peer_b->offset;
292 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
294 struct bio_bio_st *b, *peer_b;
295 ossl_ssize_t num, available;
297 if (num_ > SSIZE_MAX)
300 num = (ossl_ssize_t) num_;
302 available = bio_nread0(bio, buf);
309 peer_b = b->peer->ptr;
313 peer_b->offset += num;
314 assert(peer_b->offset <= peer_b->size);
315 if (peer_b->offset == peer_b->size)
323 static int bio_write(BIO *bio, const char *buf, int num_)
327 struct bio_bio_st *b;
329 BIO_clear_retry_flags(bio);
331 if (!bio->init || buf == NULL || num == 0)
336 assert(b->peer != NULL);
337 assert(b->buf != NULL);
341 /* we already closed */
342 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
346 assert(b->len <= b->size);
348 if (b->len == b->size) {
349 BIO_set_retry_write(bio); /* buffer is full */
354 if (num > b->size - b->len)
355 num = b->size - b->len;
357 /* now write "num" bytes */
362 do { /* one or two iterations */
366 assert(b->len + rest <= b->size);
368 write_offset = b->offset + b->len;
369 if (write_offset >= b->size)
370 write_offset -= b->size;
371 /* b->buf[write_offset] is the first byte we can write to. */
373 if (write_offset + rest <= b->size)
376 /* wrap around ring buffer */
377 chunk = b->size - write_offset;
379 memcpy(b->buf + write_offset, buf, chunk);
383 assert(b->len <= b->size);
394 * non-copying interface: provide pointer to region to write to
395 * bio_nwrite0: check how much space is available
396 * bio_nwrite: also increase length
397 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
398 * or just bio_nwrite(), write to buffer)
400 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
402 struct bio_bio_st *b;
406 BIO_clear_retry_flags(bio);
413 assert(b->peer != NULL);
414 assert(b->buf != NULL);
418 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
422 assert(b->len <= b->size);
424 if (b->len == b->size) {
425 BIO_set_retry_write(bio);
429 num = b->size - b->len;
430 write_offset = b->offset + b->len;
431 if (write_offset >= b->size)
432 write_offset -= b->size;
433 if (write_offset + num > b->size)
435 * no ring buffer wrap-around for non-copying interface (to fulfil
436 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
437 * to be called twice)
439 num = b->size - write_offset;
442 *buf = b->buf + write_offset;
443 assert(write_offset + num <= b->size);
448 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
450 struct bio_bio_st *b;
451 ossl_ssize_t num, space;
453 if (num_ > SSIZE_MAX)
456 num = (ossl_ssize_t) num_;
458 space = bio_nwrite0(bio, buf);
466 assert(b->len <= b->size);
471 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
474 struct bio_bio_st *b = bio->ptr;
479 /* specific CTRL codes */
481 case BIO_C_SET_WRITE_BUF_SIZE:
483 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
485 } else if (num == 0) {
486 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
489 size_t new_size = num;
491 if (b->size != new_size) {
492 OPENSSL_free(b->buf);
500 case BIO_C_GET_WRITE_BUF_SIZE:
504 case BIO_C_MAKE_BIO_PAIR:
506 BIO *other_bio = ptr;
508 if (bio_make_pair(bio, other_bio))
515 case BIO_C_DESTROY_BIO_PAIR:
517 * Affects both BIOs in the pair -- call just once! Or let
518 * BIO_free(bio1); BIO_free(bio2); do the job.
520 bio_destroy_pair(bio);
524 case BIO_C_GET_WRITE_GUARANTEE:
526 * How many bytes can the caller feed to the next write without
527 * having to keep any?
529 if (b->peer == NULL || b->closed)
532 ret = (long)b->size - b->len;
535 case BIO_C_GET_READ_REQUEST:
537 * If the peer unsuccessfully tried to read, how many bytes were
538 * requested? (As with BIO_CTRL_PENDING, that number can usually be
539 * treated as boolean.)
541 ret = (long)b->request;
544 case BIO_C_RESET_READ_REQUEST:
546 * Reset request. (Can be useful after read attempts at the other
547 * side that are meant to be non-blocking, e.g. when probing SSL_read
548 * to see if any data is available.)
554 case BIO_C_SHUTDOWN_WR:
555 /* similar to shutdown(..., SHUT_WR) */
561 /* prepare for non-copying read */
562 ret = (long)bio_nread0(bio, ptr);
566 /* non-copying read */
567 ret = (long)bio_nread(bio, ptr, (size_t)num);
571 /* prepare for non-copying write */
572 ret = (long)bio_nwrite0(bio, ptr);
576 /* non-copying write */
577 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
580 /* standard CTRL codes follow */
583 if (b->buf != NULL) {
590 case BIO_CTRL_GET_CLOSE:
594 case BIO_CTRL_SET_CLOSE:
595 bio->shutdown = (int)num;
599 case BIO_CTRL_PENDING:
600 if (b->peer != NULL) {
601 struct bio_bio_st *peer_b = b->peer->ptr;
603 ret = (long)peer_b->len;
608 case BIO_CTRL_WPENDING:
616 /* See BIO_dup_chain for circumstances we have to expect. */
618 BIO *other_bio = ptr;
619 struct bio_bio_st *other_b;
621 assert(other_bio != NULL);
622 other_b = other_bio->ptr;
623 assert(other_b != NULL);
625 assert(other_b->buf == NULL); /* other_bio is always fresh */
627 other_b->size = b->size;
639 BIO *other_bio = ptr;
642 struct bio_bio_st *other_b = other_bio->ptr;
644 assert(other_b != NULL);
645 ret = other_b->len == 0 && other_b->closed;
657 static int bio_puts(BIO *bio, const char *str)
659 return bio_write(bio, str, strlen(str));
662 static int bio_make_pair(BIO *bio1, BIO *bio2)
664 struct bio_bio_st *b1, *b2;
666 assert(bio1 != NULL);
667 assert(bio2 != NULL);
672 if (b1->peer != NULL || b2->peer != NULL) {
673 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
677 if (b1->buf == NULL) {
678 b1->buf = OPENSSL_malloc(b1->size);
679 if (b1->buf == NULL) {
680 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
687 if (b2->buf == NULL) {
688 b2->buf = OPENSSL_malloc(b2->size);
689 if (b2->buf == NULL) {
690 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
710 static void bio_destroy_pair(BIO *bio)
712 struct bio_bio_st *b = bio->ptr;
715 BIO *peer_bio = b->peer;
717 if (peer_bio != NULL) {
718 struct bio_bio_st *peer_b = peer_bio->ptr;
720 assert(peer_b != NULL);
721 assert(peer_b->peer == bio);
725 assert(peer_b->buf != NULL);
731 assert(b->buf != NULL);
738 /* Exported convenience functions */
739 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
740 BIO **bio2_p, size_t writebuf2)
742 BIO *bio1 = NULL, *bio2 = NULL;
746 bio1 = BIO_new(BIO_s_bio());
749 bio2 = BIO_new(BIO_s_bio());
754 r = BIO_set_write_buf_size(bio1, writebuf1);
759 r = BIO_set_write_buf_size(bio2, writebuf2);
764 r = BIO_make_bio_pair(bio1, bio2);
782 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
784 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
787 size_t BIO_ctrl_get_read_request(BIO *bio)
789 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
792 int BIO_ctrl_reset_read_request(BIO *bio)
794 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
798 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
799 * (conceivably some other BIOs could allow non-copying reads and writes
802 int BIO_nread0(BIO *bio, char **buf)
807 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
811 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
818 int BIO_nread(BIO *bio, char **buf, int num)
823 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
827 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
829 bio->num_read += ret;
833 int BIO_nwrite0(BIO *bio, char **buf)
838 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
842 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
849 int BIO_nwrite(BIO *bio, char **buf, int num)
854 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
858 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
860 bio->num_write += ret;