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 if (b->buf != NULL) {
174 OPENSSL_free(b->buf);
182 static int bio_read(BIO *bio, char *buf, int size_)
186 struct bio_bio_st *b, *peer_b;
188 BIO_clear_retry_flags(bio);
195 assert(b->peer != NULL);
196 peer_b = b->peer->ptr;
197 assert(peer_b != NULL);
198 assert(peer_b->buf != NULL);
200 peer_b->request = 0; /* will be set in "retry_read" situation */
202 if (buf == NULL || size == 0)
205 if (peer_b->len == 0) {
207 return 0; /* writer has closed, and no data is left */
209 BIO_set_retry_read(bio); /* buffer is empty */
210 if (size <= peer_b->size)
211 peer_b->request = size;
214 * don't ask for more than the peer can deliver in one write
216 peer_b->request = peer_b->size;
222 if (peer_b->len < size)
225 /* now read "size" bytes */
230 do { /* one or two iterations */
233 assert(rest <= peer_b->len);
234 if (peer_b->offset + rest <= peer_b->size)
237 /* wrap around ring buffer */
238 chunk = peer_b->size - peer_b->offset;
239 assert(peer_b->offset + chunk <= peer_b->size);
241 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
243 peer_b->len -= chunk;
245 peer_b->offset += chunk;
246 assert(peer_b->offset <= peer_b->size);
247 if (peer_b->offset == peer_b->size)
251 /* buffer now empty, no need to advance "buf" */
252 assert(chunk == rest);
263 * non-copying interface: provide pointer to available data in buffer
264 * bio_nread0: return number of available bytes
265 * bio_nread: also advance index
266 * (example usage: bio_nread0(), read from buffer, bio_nread()
267 * or just bio_nread(), read from buffer)
270 * WARNING: The non-copying interface is largely untested as of yet and may
273 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
275 struct bio_bio_st *b, *peer_b;
278 BIO_clear_retry_flags(bio);
285 assert(b->peer != NULL);
286 peer_b = b->peer->ptr;
287 assert(peer_b != NULL);
288 assert(peer_b->buf != NULL);
292 if (peer_b->len == 0) {
295 /* avoid code duplication -- nothing available for reading */
296 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
300 if (peer_b->size < peer_b->offset + num)
301 /* no ring buffer wrap-around for non-copying interface */
302 num = peer_b->size - peer_b->offset;
306 *buf = peer_b->buf + peer_b->offset;
310 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
312 struct bio_bio_st *b, *peer_b;
313 ossl_ssize_t num, available;
315 if (num_ > SSIZE_MAX)
318 num = (ossl_ssize_t) num_;
320 available = bio_nread0(bio, buf);
327 peer_b = b->peer->ptr;
331 peer_b->offset += num;
332 assert(peer_b->offset <= peer_b->size);
333 if (peer_b->offset == peer_b->size)
341 static int bio_write(BIO *bio, const char *buf, int num_)
345 struct bio_bio_st *b;
347 BIO_clear_retry_flags(bio);
349 if (!bio->init || buf == NULL || num == 0)
354 assert(b->peer != NULL);
355 assert(b->buf != NULL);
359 /* we already closed */
360 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
364 assert(b->len <= b->size);
366 if (b->len == b->size) {
367 BIO_set_retry_write(bio); /* buffer is full */
372 if (num > b->size - b->len)
373 num = b->size - b->len;
375 /* now write "num" bytes */
380 do { /* one or two iterations */
384 assert(b->len + rest <= b->size);
386 write_offset = b->offset + b->len;
387 if (write_offset >= b->size)
388 write_offset -= b->size;
389 /* b->buf[write_offset] is the first byte we can write to. */
391 if (write_offset + rest <= b->size)
394 /* wrap around ring buffer */
395 chunk = b->size - write_offset;
397 memcpy(b->buf + write_offset, buf, chunk);
401 assert(b->len <= b->size);
412 * non-copying interface: provide pointer to region to write to
413 * bio_nwrite0: check how much space is available
414 * bio_nwrite: also increase length
415 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
416 * or just bio_nwrite(), write to buffer)
418 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
420 struct bio_bio_st *b;
424 BIO_clear_retry_flags(bio);
431 assert(b->peer != NULL);
432 assert(b->buf != NULL);
436 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
440 assert(b->len <= b->size);
442 if (b->len == b->size) {
443 BIO_set_retry_write(bio);
447 num = b->size - b->len;
448 write_offset = b->offset + b->len;
449 if (write_offset >= b->size)
450 write_offset -= b->size;
451 if (write_offset + num > b->size)
453 * no ring buffer wrap-around for non-copying interface (to fulfil
454 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
455 * to be called twice)
457 num = b->size - write_offset;
460 *buf = b->buf + write_offset;
461 assert(write_offset + num <= b->size);
466 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
468 struct bio_bio_st *b;
469 ossl_ssize_t num, space;
471 if (num_ > SSIZE_MAX)
474 num = (ossl_ssize_t) num_;
476 space = bio_nwrite0(bio, buf);
484 assert(b->len <= b->size);
489 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
492 struct bio_bio_st *b = bio->ptr;
497 /* specific CTRL codes */
499 case BIO_C_SET_WRITE_BUF_SIZE:
501 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
503 } else if (num == 0) {
504 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
507 size_t new_size = num;
509 if (b->size != new_size) {
511 OPENSSL_free(b->buf);
520 case BIO_C_GET_WRITE_BUF_SIZE:
524 case BIO_C_MAKE_BIO_PAIR:
526 BIO *other_bio = ptr;
528 if (bio_make_pair(bio, other_bio))
535 case BIO_C_DESTROY_BIO_PAIR:
537 * Affects both BIOs in the pair -- call just once! Or let
538 * BIO_free(bio1); BIO_free(bio2); do the job.
540 bio_destroy_pair(bio);
544 case BIO_C_GET_WRITE_GUARANTEE:
546 * How many bytes can the caller feed to the next write without
547 * having to keep any?
549 if (b->peer == NULL || b->closed)
552 ret = (long)b->size - b->len;
555 case BIO_C_GET_READ_REQUEST:
557 * If the peer unsuccessfully tried to read, how many bytes were
558 * requested? (As with BIO_CTRL_PENDING, that number can usually be
559 * treated as boolean.)
561 ret = (long)b->request;
564 case BIO_C_RESET_READ_REQUEST:
566 * Reset request. (Can be useful after read attempts at the other
567 * side that are meant to be non-blocking, e.g. when probing SSL_read
568 * to see if any data is available.)
574 case BIO_C_SHUTDOWN_WR:
575 /* similar to shutdown(..., SHUT_WR) */
581 /* prepare for non-copying read */
582 ret = (long)bio_nread0(bio, ptr);
586 /* non-copying read */
587 ret = (long)bio_nread(bio, ptr, (size_t)num);
591 /* prepare for non-copying write */
592 ret = (long)bio_nwrite0(bio, ptr);
596 /* non-copying write */
597 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
600 /* standard CTRL codes follow */
603 if (b->buf != NULL) {
610 case BIO_CTRL_GET_CLOSE:
614 case BIO_CTRL_SET_CLOSE:
615 bio->shutdown = (int)num;
619 case BIO_CTRL_PENDING:
620 if (b->peer != NULL) {
621 struct bio_bio_st *peer_b = b->peer->ptr;
623 ret = (long)peer_b->len;
628 case BIO_CTRL_WPENDING:
636 /* See BIO_dup_chain for circumstances we have to expect. */
638 BIO *other_bio = ptr;
639 struct bio_bio_st *other_b;
641 assert(other_bio != NULL);
642 other_b = other_bio->ptr;
643 assert(other_b != NULL);
645 assert(other_b->buf == NULL); /* other_bio is always fresh */
647 other_b->size = b->size;
659 BIO *other_bio = ptr;
662 struct bio_bio_st *other_b = other_bio->ptr;
664 assert(other_b != NULL);
665 ret = other_b->len == 0 && other_b->closed;
677 static int bio_puts(BIO *bio, const char *str)
679 return bio_write(bio, str, strlen(str));
682 static int bio_make_pair(BIO *bio1, BIO *bio2)
684 struct bio_bio_st *b1, *b2;
686 assert(bio1 != NULL);
687 assert(bio2 != NULL);
692 if (b1->peer != NULL || b2->peer != NULL) {
693 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
697 if (b1->buf == NULL) {
698 b1->buf = OPENSSL_malloc(b1->size);
699 if (b1->buf == NULL) {
700 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
707 if (b2->buf == NULL) {
708 b2->buf = OPENSSL_malloc(b2->size);
709 if (b2->buf == NULL) {
710 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
730 static void bio_destroy_pair(BIO *bio)
732 struct bio_bio_st *b = bio->ptr;
735 BIO *peer_bio = b->peer;
737 if (peer_bio != NULL) {
738 struct bio_bio_st *peer_b = peer_bio->ptr;
740 assert(peer_b != NULL);
741 assert(peer_b->peer == bio);
745 assert(peer_b->buf != NULL);
751 assert(b->buf != NULL);
758 /* Exported convenience functions */
759 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
760 BIO **bio2_p, size_t writebuf2)
762 BIO *bio1 = NULL, *bio2 = NULL;
766 bio1 = BIO_new(BIO_s_bio());
769 bio2 = BIO_new(BIO_s_bio());
774 r = BIO_set_write_buf_size(bio1, writebuf1);
779 r = BIO_set_write_buf_size(bio2, writebuf2);
784 r = BIO_make_bio_pair(bio1, bio2);
802 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
804 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
807 size_t BIO_ctrl_get_read_request(BIO *bio)
809 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
812 int BIO_ctrl_reset_read_request(BIO *bio)
814 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
818 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
819 * (conceivably some other BIOs could allow non-copying reads and writes
822 int BIO_nread0(BIO *bio, char **buf)
827 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
831 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
838 int BIO_nread(BIO *bio, char **buf, int num)
843 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
847 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
849 bio->num_read += ret;
853 int BIO_nwrite0(BIO *bio, char **buf)
858 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
862 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
869 int BIO_nwrite(BIO *bio, char **buf, int num)
874 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
878 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
880 bio->num_write += ret;