1 /* $OpenBSD: bss_bio.c,v 1.23 2017/01/29 17:49:22 beck Exp $ */
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
81 #include <sys/types.h>
83 #include <openssl/bio.h>
84 #include <openssl/err.h>
85 #include <openssl/crypto.h>
87 static int bio_new(BIO
*bio
);
88 static int bio_free(BIO
*bio
);
89 static int bio_read(BIO
*bio
, char *buf
, int size
);
90 static int bio_write(BIO
*bio
, const char *buf
, int num
);
91 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
);
92 static int bio_puts(BIO
*bio
, const char *str
);
94 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
);
95 static void bio_destroy_pair(BIO
*bio
);
97 static BIO_METHOD methods_biop
= {
111 return &methods_biop
;
115 BIO
*peer
; /* NULL if buf == NULL.
116 * If peer != NULL, then peer->ptr is also a bio_bio_st,
117 * and its "peer" member points back to us.
118 * peer != NULL iff init != 0 in the BIO. */
120 /* This is for what we write (i.e. reading uses peer's struct): */
121 int closed
; /* valid iff peer != NULL */
122 size_t len
; /* valid iff buf != NULL; 0 if peer == NULL */
123 size_t offset
; /* valid iff buf != NULL; 0 if len == 0 */
125 char *buf
; /* "size" elements (if != NULL) */
127 size_t request
; /* valid iff peer != NULL; 0 if len != 0,
128 * otherwise set by peer to number of bytes
129 * it (unsuccessfully) tried to read,
130 * never more than buffer space (size-len) warrants. */
136 struct bio_bio_st
*b
;
138 b
= malloc(sizeof *b
);
143 b
->size
= 17 * 1024; /* enough for one TLS record (just a default) */
153 struct bio_bio_st
*b
;
162 bio_destroy_pair(bio
);
172 bio_read(BIO
*bio
, char *buf
, int size_
)
176 struct bio_bio_st
*b
, *peer_b
;
178 BIO_clear_retry_flags(bio
);
185 assert(b
->peer
!= NULL
);
186 peer_b
= b
->peer
->ptr
;
187 assert(peer_b
!= NULL
);
188 assert(peer_b
->buf
!= NULL
);
190 peer_b
->request
= 0; /* will be set in "retry_read" situation */
192 if (buf
== NULL
|| size
== 0)
195 if (peer_b
->len
== 0) {
197 return 0; /* writer has closed, and no data is left */
199 BIO_set_retry_read(bio
); /* buffer is empty */
200 if (size
<= peer_b
->size
)
201 peer_b
->request
= size
;
203 /* don't ask for more than the peer can
204 * deliver in one write */
205 peer_b
->request
= peer_b
->size
;
211 if (peer_b
->len
< size
)
214 /* now read "size" bytes */
219 do /* one or two iterations */
223 assert(rest
<= peer_b
->len
);
224 if (peer_b
->offset
+ rest
<= peer_b
->size
)
227 /* wrap around ring buffer */
228 chunk
= peer_b
->size
- peer_b
->offset
;
229 assert(peer_b
->offset
+ chunk
<= peer_b
->size
);
231 memcpy(buf
, peer_b
->buf
+ peer_b
->offset
, chunk
);
233 peer_b
->len
-= chunk
;
235 peer_b
->offset
+= chunk
;
236 assert(peer_b
->offset
<= peer_b
->size
);
237 if (peer_b
->offset
== peer_b
->size
)
241 /* buffer now empty, no need to advance "buf" */
242 assert(chunk
== rest
);
251 /* non-copying interface: provide pointer to available data in buffer
252 * bio_nread0: return number of available bytes
253 * bio_nread: also advance index
254 * (example usage: bio_nread0(), read from buffer, bio_nread()
255 * or just bio_nread(), read from buffer)
257 /* WARNING: The non-copying interface is largely untested as of yet
258 * and may contain bugs. */
260 bio_nread0(BIO
*bio
, char **buf
)
262 struct bio_bio_st
*b
, *peer_b
;
265 BIO_clear_retry_flags(bio
);
272 assert(b
->peer
!= NULL
);
273 peer_b
= b
->peer
->ptr
;
274 assert(peer_b
!= NULL
);
275 assert(peer_b
->buf
!= NULL
);
279 if (peer_b
->len
== 0) {
282 /* avoid code duplication -- nothing available for reading */
283 return bio_read(bio
, &dummy
, 1); /* returns 0 or -1 */
287 if (peer_b
->size
< peer_b
->offset
+ num
)
288 /* no ring buffer wrap-around for non-copying interface */
289 num
= peer_b
->size
- peer_b
->offset
;
293 *buf
= peer_b
->buf
+ peer_b
->offset
;
298 bio_nread(BIO
*bio
, char **buf
, size_t num_
)
300 struct bio_bio_st
*b
, *peer_b
;
301 ssize_t num
, available
;
303 if (num_
> SSIZE_MAX
)
308 available
= bio_nread0(bio
, buf
);
315 peer_b
= b
->peer
->ptr
;
319 peer_b
->offset
+= num
;
320 assert(peer_b
->offset
<= peer_b
->size
);
321 if (peer_b
->offset
== peer_b
->size
)
331 bio_write(BIO
*bio
, const char *buf
, int num_
)
335 struct bio_bio_st
*b
;
337 BIO_clear_retry_flags(bio
);
339 if (!bio
->init
|| buf
== NULL
|| num
== 0)
345 assert(b
->peer
!= NULL
);
346 assert(b
->buf
!= NULL
);
350 /* we already closed */
351 BIOerror(BIO_R_BROKEN_PIPE
);
355 assert(b
->len
<= b
->size
);
357 if (b
->len
== b
->size
) {
358 BIO_set_retry_write(bio
); /* buffer is full */
363 if (num
> b
->size
- b
->len
)
364 num
= b
->size
- b
->len
;
366 /* now write "num" bytes */
371 do /* one or two iterations */
376 assert(b
->len
+ rest
<= b
->size
);
378 write_offset
= b
->offset
+ b
->len
;
379 if (write_offset
>= b
->size
)
380 write_offset
-= b
->size
;
381 /* b->buf[write_offset] is the first byte we can write to. */
383 if (write_offset
+ rest
<= b
->size
)
386 /* wrap around ring buffer */
387 chunk
= b
->size
- write_offset
;
389 memcpy(b
->buf
+ write_offset
, buf
, chunk
);
393 assert(b
->len
<= b
->size
);
402 /* non-copying interface: provide pointer to region to write to
403 * bio_nwrite0: check how much space is available
404 * bio_nwrite: also increase length
405 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
406 * or just bio_nwrite(), write to buffer)
409 bio_nwrite0(BIO
*bio
, char **buf
)
411 struct bio_bio_st
*b
;
415 BIO_clear_retry_flags(bio
);
423 assert(b
->peer
!= NULL
);
424 assert(b
->buf
!= NULL
);
428 BIOerror(BIO_R_BROKEN_PIPE
);
432 assert(b
->len
<= b
->size
);
434 if (b
->len
== b
->size
) {
435 BIO_set_retry_write(bio
);
439 num
= b
->size
- b
->len
;
440 write_offset
= b
->offset
+ b
->len
;
441 if (write_offset
>= b
->size
)
442 write_offset
-= b
->size
;
443 if (write_offset
+ num
> b
->size
)
444 /* no ring buffer wrap-around for non-copying interface
445 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
446 * BIO_nwrite may have to be called twice) */
447 num
= b
->size
- write_offset
;
450 *buf
= b
->buf
+ write_offset
;
451 assert(write_offset
+ num
<= b
->size
);
457 bio_nwrite(BIO
*bio
, char **buf
, size_t num_
)
459 struct bio_bio_st
*b
;
462 if (num_
> SSIZE_MAX
)
467 space
= bio_nwrite0(bio
, buf
);
475 assert(b
->len
<= b
->size
);
482 bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
485 struct bio_bio_st
*b
= bio
->ptr
;
490 /* specific CTRL codes */
492 case BIO_C_SET_WRITE_BUF_SIZE
:
494 BIOerror(BIO_R_IN_USE
);
496 } else if (num
== 0) {
497 BIOerror(BIO_R_INVALID_ARGUMENT
);
500 size_t new_size
= num
;
502 if (b
->size
!= new_size
) {
511 case BIO_C_GET_WRITE_BUF_SIZE
:
512 ret
= (long) b
->size
;
515 case BIO_C_MAKE_BIO_PAIR
:
517 BIO
*other_bio
= ptr
;
519 if (bio_make_pair(bio
, other_bio
))
526 case BIO_C_DESTROY_BIO_PAIR
:
527 /* Affects both BIOs in the pair -- call just once!
528 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
529 bio_destroy_pair(bio
);
533 case BIO_C_GET_WRITE_GUARANTEE
:
534 /* How many bytes can the caller feed to the next write
535 * without having to keep any? */
536 if (b
->peer
== NULL
|| b
->closed
)
539 ret
= (long) b
->size
- b
->len
;
542 case BIO_C_GET_READ_REQUEST
:
543 /* If the peer unsuccessfully tried to read, how many bytes
544 * were requested? (As with BIO_CTRL_PENDING, that number
545 * can usually be treated as boolean.) */
546 ret
= (long) b
->request
;
549 case BIO_C_RESET_READ_REQUEST
:
550 /* Reset request. (Can be useful after read attempts
551 * at the other side that are meant to be non-blocking,
552 * e.g. when probing SSL_read to see if any data is
558 case BIO_C_SHUTDOWN_WR
:
559 /* similar to shutdown(..., SHUT_WR) */
565 /* prepare for non-copying read */
566 ret
= (long) bio_nread0(bio
, ptr
);
570 /* non-copying read */
571 ret
= (long) bio_nread(bio
, ptr
, (size_t) num
);
575 /* prepare for non-copying write */
576 ret
= (long) bio_nwrite0(bio
, ptr
);
580 /* non-copying write */
581 ret
= (long) bio_nwrite(bio
, ptr
, (size_t) num
);
585 /* standard CTRL codes follow */
588 if (b
->buf
!= NULL
) {
596 case BIO_CTRL_GET_CLOSE
:
600 case BIO_CTRL_SET_CLOSE
:
601 bio
->shutdown
= (int) num
;
605 case BIO_CTRL_PENDING
:
606 if (b
->peer
!= NULL
) {
607 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
609 ret
= (long) peer_b
->len
;
614 case BIO_CTRL_WPENDING
:
622 /* See BIO_dup_chain for circumstances we have to expect. */
624 BIO
*other_bio
= ptr
;
625 struct bio_bio_st
*other_b
;
627 assert(other_bio
!= NULL
);
628 other_b
= other_bio
->ptr
;
629 assert(other_b
!= NULL
);
631 assert(other_b
->buf
== NULL
); /* other_bio is always fresh */
633 other_b
->size
= b
->size
;
645 BIO
*other_bio
= ptr
;
648 struct bio_bio_st
*other_b
= other_bio
->ptr
;
650 assert(other_b
!= NULL
);
651 ret
= other_b
->len
== 0 && other_b
->closed
;
664 bio_puts(BIO
*bio
, const char *str
)
666 return bio_write(bio
, str
, strlen(str
));
671 bio_make_pair(BIO
*bio1
, BIO
*bio2
)
673 struct bio_bio_st
*b1
, *b2
;
675 assert(bio1
!= NULL
);
676 assert(bio2
!= NULL
);
681 if (b1
->peer
!= NULL
|| b2
->peer
!= NULL
) {
682 BIOerror(BIO_R_IN_USE
);
686 if (b1
->buf
== NULL
) {
687 b1
->buf
= malloc(b1
->size
);
688 if (b1
->buf
== NULL
) {
689 BIOerror(ERR_R_MALLOC_FAILURE
);
696 if (b2
->buf
== NULL
) {
697 b2
->buf
= malloc(b2
->size
);
698 if (b2
->buf
== NULL
) {
699 BIOerror(ERR_R_MALLOC_FAILURE
);
720 bio_destroy_pair(BIO
*bio
)
722 struct bio_bio_st
*b
= bio
->ptr
;
725 BIO
*peer_bio
= b
->peer
;
727 if (peer_bio
!= NULL
) {
728 struct bio_bio_st
*peer_b
= peer_bio
->ptr
;
730 assert(peer_b
!= NULL
);
731 assert(peer_b
->peer
== bio
);
735 assert(peer_b
->buf
!= NULL
);
741 assert(b
->buf
!= NULL
);
749 /* Exported convenience functions */
751 BIO_new_bio_pair(BIO
**bio1_p
, size_t writebuf1
, BIO
**bio2_p
, size_t writebuf2
)
753 BIO
*bio1
= NULL
, *bio2
= NULL
;
757 bio1
= BIO_new(BIO_s_bio());
760 bio2
= BIO_new(BIO_s_bio());
765 r
= BIO_set_write_buf_size(bio1
, writebuf1
);
770 r
= BIO_set_write_buf_size(bio2
, writebuf2
);
775 r
= BIO_make_bio_pair(bio1
, bio2
);
798 BIO_ctrl_get_write_guarantee(BIO
*bio
)
800 return BIO_ctrl(bio
, BIO_C_GET_WRITE_GUARANTEE
, 0, NULL
);
804 BIO_ctrl_get_read_request(BIO
*bio
)
806 return BIO_ctrl(bio
, BIO_C_GET_READ_REQUEST
, 0, NULL
);
810 BIO_ctrl_reset_read_request(BIO
*bio
)
812 return (BIO_ctrl(bio
, BIO_C_RESET_READ_REQUEST
, 0, NULL
) != 0);
816 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
817 * (conceivably some other BIOs could allow non-copying reads and writes too.)
820 BIO_nread0(BIO
*bio
, char **buf
)
825 BIOerror(BIO_R_UNINITIALIZED
);
829 ret
= BIO_ctrl(bio
, BIO_C_NREAD0
, 0, buf
);
837 BIO_nread(BIO
*bio
, char **buf
, int num
)
842 BIOerror(BIO_R_UNINITIALIZED
);
846 ret
= (int) BIO_ctrl(bio
, BIO_C_NREAD
, num
, buf
);
848 bio
->num_read
+= ret
;
853 BIO_nwrite0(BIO
*bio
, char **buf
)
858 BIOerror(BIO_R_UNINITIALIZED
);
862 ret
= BIO_ctrl(bio
, BIO_C_NWRITE0
, 0, buf
);
870 BIO_nwrite(BIO
*bio
, char **buf
, int num
)
875 BIOerror(BIO_R_UNINITIALIZED
);
879 ret
= BIO_ctrl(bio
, BIO_C_NWRITE
, num
, buf
);
881 bio
->num_write
+= ret
;