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.
69 #include <openssl/err.h>
70 #include <openssl/crypto.h>
74 static int bio_new(BIO
*bio
);
75 static int bio_free(BIO
*bio
);
76 static int bio_read(BIO
*bio
, char *buf
, int size
);
77 static int bio_write(BIO
*bio
, const char *buf
, int num
);
78 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
);
79 static int bio_puts(BIO
*bio
, const char *str
);
81 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
);
82 static void bio_destroy_pair(BIO
*bio
);
84 static const BIO_METHOD methods_biop
= {
90 NULL
/* no bio_gets */ ,
94 NULL
/* no bio_callback_ctrl */
97 const BIO_METHOD
*BIO_s_bio(void)
103 BIO
*peer
; /* NULL if buf == NULL. If peer != NULL, then
104 * peer->ptr is also a bio_bio_st, and its
105 * "peer" member points back to us. peer !=
106 * NULL iff init != 0 in the BIO. */
107 /* This is for what we write (i.e. reading uses peer's struct): */
108 int closed
; /* valid iff peer != NULL */
109 size_t len
; /* valid iff buf != NULL; 0 if peer == NULL */
110 size_t offset
; /* valid iff buf != NULL; 0 if len == 0 */
112 char *buf
; /* "size" elements (if != NULL) */
113 size_t request
; /* valid iff peer != NULL; 0 if len != 0,
114 * otherwise set by peer to number of bytes
115 * it (unsuccessfully) tried to read, never
116 * more than buffer space (size-len)
120 static int bio_new(BIO
*bio
)
122 struct bio_bio_st
*b
;
124 b
= OPENSSL_malloc(sizeof(*b
));
129 /* enough for one TLS record (just a default) */
137 static int bio_free(BIO
*bio
)
139 struct bio_bio_st
*b
;
148 bio_destroy_pair(bio
);
150 OPENSSL_free(b
->buf
);
156 static int bio_read(BIO
*bio
, char *buf
, int size_
)
160 struct bio_bio_st
*b
, *peer_b
;
162 BIO_clear_retry_flags(bio
);
169 assert(b
->peer
!= NULL
);
170 peer_b
= b
->peer
->ptr
;
171 assert(peer_b
!= NULL
);
172 assert(peer_b
->buf
!= NULL
);
174 peer_b
->request
= 0; /* will be set in "retry_read" situation */
176 if (buf
== NULL
|| size
== 0)
179 if (peer_b
->len
== 0) {
181 return 0; /* writer has closed, and no data is left */
183 BIO_set_retry_read(bio
); /* buffer is empty */
184 if (size
<= peer_b
->size
)
185 peer_b
->request
= size
;
188 * don't ask for more than the peer can deliver in one write
190 peer_b
->request
= peer_b
->size
;
196 if (peer_b
->len
< size
)
199 /* now read "size" bytes */
204 do { /* one or two iterations */
207 assert(rest
<= peer_b
->len
);
208 if (peer_b
->offset
+ rest
<= peer_b
->size
)
211 /* wrap around ring buffer */
212 chunk
= peer_b
->size
- peer_b
->offset
;
213 assert(peer_b
->offset
+ chunk
<= peer_b
->size
);
215 memcpy(buf
, peer_b
->buf
+ peer_b
->offset
, chunk
);
217 peer_b
->len
-= chunk
;
219 peer_b
->offset
+= chunk
;
220 assert(peer_b
->offset
<= peer_b
->size
);
221 if (peer_b
->offset
== peer_b
->size
)
225 /* buffer now empty, no need to advance "buf" */
226 assert(chunk
== rest
);
237 * non-copying interface: provide pointer to available data in buffer
238 * bio_nread0: return number of available bytes
239 * bio_nread: also advance index
240 * (example usage: bio_nread0(), read from buffer, bio_nread()
241 * or just bio_nread(), read from buffer)
244 * WARNING: The non-copying interface is largely untested as of yet and may
247 static ossl_ssize_t
bio_nread0(BIO
*bio
, char **buf
)
249 struct bio_bio_st
*b
, *peer_b
;
252 BIO_clear_retry_flags(bio
);
259 assert(b
->peer
!= NULL
);
260 peer_b
= b
->peer
->ptr
;
261 assert(peer_b
!= NULL
);
262 assert(peer_b
->buf
!= NULL
);
266 if (peer_b
->len
== 0) {
269 /* avoid code duplication -- nothing available for reading */
270 return bio_read(bio
, &dummy
, 1); /* returns 0 or -1 */
274 if (peer_b
->size
< peer_b
->offset
+ num
)
275 /* no ring buffer wrap-around for non-copying interface */
276 num
= peer_b
->size
- peer_b
->offset
;
280 *buf
= peer_b
->buf
+ peer_b
->offset
;
284 static ossl_ssize_t
bio_nread(BIO
*bio
, char **buf
, size_t num_
)
286 struct bio_bio_st
*b
, *peer_b
;
287 ossl_ssize_t num
, available
;
289 if (num_
> OSSL_SSIZE_MAX
)
290 num
= OSSL_SSIZE_MAX
;
292 num
= (ossl_ssize_t
) num_
;
294 available
= bio_nread0(bio
, buf
);
301 peer_b
= b
->peer
->ptr
;
305 peer_b
->offset
+= num
;
306 assert(peer_b
->offset
<= peer_b
->size
);
307 if (peer_b
->offset
== peer_b
->size
)
315 static int bio_write(BIO
*bio
, const char *buf
, int num_
)
319 struct bio_bio_st
*b
;
321 BIO_clear_retry_flags(bio
);
323 if (!bio
->init
|| buf
== NULL
|| num
== 0)
328 assert(b
->peer
!= NULL
);
329 assert(b
->buf
!= NULL
);
333 /* we already closed */
334 BIOerr(BIO_F_BIO_WRITE
, BIO_R_BROKEN_PIPE
);
338 assert(b
->len
<= b
->size
);
340 if (b
->len
== b
->size
) {
341 BIO_set_retry_write(bio
); /* buffer is full */
346 if (num
> b
->size
- b
->len
)
347 num
= b
->size
- b
->len
;
349 /* now write "num" bytes */
354 do { /* one or two iterations */
358 assert(b
->len
+ rest
<= b
->size
);
360 write_offset
= b
->offset
+ b
->len
;
361 if (write_offset
>= b
->size
)
362 write_offset
-= b
->size
;
363 /* b->buf[write_offset] is the first byte we can write to. */
365 if (write_offset
+ rest
<= b
->size
)
368 /* wrap around ring buffer */
369 chunk
= b
->size
- write_offset
;
371 memcpy(b
->buf
+ write_offset
, buf
, chunk
);
375 assert(b
->len
<= b
->size
);
386 * non-copying interface: provide pointer to region to write to
387 * bio_nwrite0: check how much space is available
388 * bio_nwrite: also increase length
389 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
390 * or just bio_nwrite(), write to buffer)
392 static ossl_ssize_t
bio_nwrite0(BIO
*bio
, char **buf
)
394 struct bio_bio_st
*b
;
398 BIO_clear_retry_flags(bio
);
405 assert(b
->peer
!= NULL
);
406 assert(b
->buf
!= NULL
);
410 BIOerr(BIO_F_BIO_NWRITE0
, BIO_R_BROKEN_PIPE
);
414 assert(b
->len
<= b
->size
);
416 if (b
->len
== b
->size
) {
417 BIO_set_retry_write(bio
);
421 num
= b
->size
- b
->len
;
422 write_offset
= b
->offset
+ b
->len
;
423 if (write_offset
>= b
->size
)
424 write_offset
-= b
->size
;
425 if (write_offset
+ num
> b
->size
)
427 * no ring buffer wrap-around for non-copying interface (to fulfil
428 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
429 * to be called twice)
431 num
= b
->size
- write_offset
;
434 *buf
= b
->buf
+ write_offset
;
435 assert(write_offset
+ num
<= b
->size
);
440 static ossl_ssize_t
bio_nwrite(BIO
*bio
, char **buf
, size_t num_
)
442 struct bio_bio_st
*b
;
443 ossl_ssize_t num
, space
;
445 if (num_
> OSSL_SSIZE_MAX
)
446 num
= OSSL_SSIZE_MAX
;
448 num
= (ossl_ssize_t
) num_
;
450 space
= bio_nwrite0(bio
, buf
);
458 assert(b
->len
<= b
->size
);
463 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
466 struct bio_bio_st
*b
= bio
->ptr
;
471 /* specific CTRL codes */
473 case BIO_C_SET_WRITE_BUF_SIZE
:
475 BIOerr(BIO_F_BIO_CTRL
, BIO_R_IN_USE
);
477 } else if (num
== 0) {
478 BIOerr(BIO_F_BIO_CTRL
, BIO_R_INVALID_ARGUMENT
);
481 size_t new_size
= num
;
483 if (b
->size
!= new_size
) {
484 OPENSSL_free(b
->buf
);
492 case BIO_C_GET_WRITE_BUF_SIZE
:
496 case BIO_C_MAKE_BIO_PAIR
:
498 BIO
*other_bio
= ptr
;
500 if (bio_make_pair(bio
, other_bio
))
507 case BIO_C_DESTROY_BIO_PAIR
:
509 * Affects both BIOs in the pair -- call just once! Or let
510 * BIO_free(bio1); BIO_free(bio2); do the job.
512 bio_destroy_pair(bio
);
516 case BIO_C_GET_WRITE_GUARANTEE
:
518 * How many bytes can the caller feed to the next write without
519 * having to keep any?
521 if (b
->peer
== NULL
|| b
->closed
)
524 ret
= (long)b
->size
- b
->len
;
527 case BIO_C_GET_READ_REQUEST
:
529 * If the peer unsuccessfully tried to read, how many bytes were
530 * requested? (As with BIO_CTRL_PENDING, that number can usually be
531 * treated as boolean.)
533 ret
= (long)b
->request
;
536 case BIO_C_RESET_READ_REQUEST
:
538 * Reset request. (Can be useful after read attempts at the other
539 * side that are meant to be non-blocking, e.g. when probing SSL_read
540 * to see if any data is available.)
546 case BIO_C_SHUTDOWN_WR
:
547 /* similar to shutdown(..., SHUT_WR) */
553 /* prepare for non-copying read */
554 ret
= (long)bio_nread0(bio
, ptr
);
558 /* non-copying read */
559 ret
= (long)bio_nread(bio
, ptr
, (size_t)num
);
563 /* prepare for non-copying write */
564 ret
= (long)bio_nwrite0(bio
, ptr
);
568 /* non-copying write */
569 ret
= (long)bio_nwrite(bio
, ptr
, (size_t)num
);
572 /* standard CTRL codes follow */
575 if (b
->buf
!= NULL
) {
582 case BIO_CTRL_GET_CLOSE
:
586 case BIO_CTRL_SET_CLOSE
:
587 bio
->shutdown
= (int)num
;
591 case BIO_CTRL_PENDING
:
592 if (b
->peer
!= NULL
) {
593 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
595 ret
= (long)peer_b
->len
;
600 case BIO_CTRL_WPENDING
:
608 /* See BIO_dup_chain for circumstances we have to expect. */
610 BIO
*other_bio
= ptr
;
611 struct bio_bio_st
*other_b
;
613 assert(other_bio
!= NULL
);
614 other_b
= other_bio
->ptr
;
615 assert(other_b
!= NULL
);
617 assert(other_b
->buf
== NULL
); /* other_bio is always fresh */
619 other_b
->size
= b
->size
;
631 BIO
*other_bio
= ptr
;
634 struct bio_bio_st
*other_b
= other_bio
->ptr
;
636 assert(other_b
!= NULL
);
637 ret
= other_b
->len
== 0 && other_b
->closed
;
649 static int bio_puts(BIO
*bio
, const char *str
)
651 return bio_write(bio
, str
, strlen(str
));
654 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
)
656 struct bio_bio_st
*b1
, *b2
;
658 assert(bio1
!= NULL
);
659 assert(bio2
!= NULL
);
664 if (b1
->peer
!= NULL
|| b2
->peer
!= NULL
) {
665 BIOerr(BIO_F_BIO_MAKE_PAIR
, BIO_R_IN_USE
);
669 if (b1
->buf
== NULL
) {
670 b1
->buf
= OPENSSL_malloc(b1
->size
);
671 if (b1
->buf
== NULL
) {
672 BIOerr(BIO_F_BIO_MAKE_PAIR
, ERR_R_MALLOC_FAILURE
);
679 if (b2
->buf
== NULL
) {
680 b2
->buf
= OPENSSL_malloc(b2
->size
);
681 if (b2
->buf
== NULL
) {
682 BIOerr(BIO_F_BIO_MAKE_PAIR
, ERR_R_MALLOC_FAILURE
);
702 static void bio_destroy_pair(BIO
*bio
)
704 struct bio_bio_st
*b
= bio
->ptr
;
707 BIO
*peer_bio
= b
->peer
;
709 if (peer_bio
!= NULL
) {
710 struct bio_bio_st
*peer_b
= peer_bio
->ptr
;
712 assert(peer_b
!= NULL
);
713 assert(peer_b
->peer
== bio
);
717 assert(peer_b
->buf
!= NULL
);
723 assert(b
->buf
!= NULL
);
730 /* Exported convenience functions */
731 int BIO_new_bio_pair(BIO
**bio1_p
, size_t writebuf1
,
732 BIO
**bio2_p
, size_t writebuf2
)
734 BIO
*bio1
= NULL
, *bio2
= NULL
;
738 bio1
= BIO_new(BIO_s_bio());
741 bio2
= BIO_new(BIO_s_bio());
746 r
= BIO_set_write_buf_size(bio1
, writebuf1
);
751 r
= BIO_set_write_buf_size(bio2
, writebuf2
);
756 r
= BIO_make_bio_pair(bio1
, bio2
);
774 size_t BIO_ctrl_get_write_guarantee(BIO
*bio
)
776 return BIO_ctrl(bio
, BIO_C_GET_WRITE_GUARANTEE
, 0, NULL
);
779 size_t BIO_ctrl_get_read_request(BIO
*bio
)
781 return BIO_ctrl(bio
, BIO_C_GET_READ_REQUEST
, 0, NULL
);
784 int BIO_ctrl_reset_read_request(BIO
*bio
)
786 return (BIO_ctrl(bio
, BIO_C_RESET_READ_REQUEST
, 0, NULL
) != 0);
790 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
791 * (conceivably some other BIOs could allow non-copying reads and writes
794 int BIO_nread0(BIO
*bio
, char **buf
)
799 BIOerr(BIO_F_BIO_NREAD0
, BIO_R_UNINITIALIZED
);
803 ret
= BIO_ctrl(bio
, BIO_C_NREAD0
, 0, buf
);
810 int BIO_nread(BIO
*bio
, char **buf
, int num
)
815 BIOerr(BIO_F_BIO_NREAD
, BIO_R_UNINITIALIZED
);
819 ret
= (int)BIO_ctrl(bio
, BIO_C_NREAD
, num
, buf
);
821 bio
->num_read
+= ret
;
825 int BIO_nwrite0(BIO
*bio
, char **buf
)
830 BIOerr(BIO_F_BIO_NWRITE0
, BIO_R_UNINITIALIZED
);
834 ret
= BIO_ctrl(bio
, BIO_C_NWRITE0
, 0, buf
);
841 int BIO_nwrite(BIO
*bio
, char **buf
, int num
)
846 BIOerr(BIO_F_BIO_NWRITE
, BIO_R_UNINITIALIZED
);
850 ret
= BIO_ctrl(bio
, BIO_C_NWRITE
, num
, buf
);
852 bio
->num_write
+= ret
;