2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * Special method for a BIO where the other endpoint is also a BIO of this
12 * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
13 * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
14 * library with I/O interfaces for which no specific BIO method is available.
15 * See ssl/ssltest.c for some hints on how this can be used.
18 #include "internal/e_os.h"
24 #include "bio_local.h"
25 #include <openssl/err.h>
26 #include <openssl/crypto.h>
28 static int bio_new(BIO
*bio
);
29 static int bio_free(BIO
*bio
);
30 static int bio_read(BIO
*bio
, char *buf
, int size
);
31 static int bio_write(BIO
*bio
, const char *buf
, int num
);
32 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
);
33 static int bio_puts(BIO
*bio
, const char *str
);
35 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
);
36 static void bio_destroy_pair(BIO
*bio
);
38 static const BIO_METHOD methods_biop
= {
46 NULL
/* no bio_gets */ ,
50 NULL
/* no bio_callback_ctrl */
53 const BIO_METHOD
*BIO_s_bio(void)
59 BIO
*peer
; /* NULL if buf == NULL. If peer != NULL, then
60 * peer->ptr is also a bio_bio_st, and its
61 * "peer" member points back to us. peer !=
62 * NULL iff init != 0 in the BIO. */
63 /* This is for what we write (i.e. reading uses peer's struct): */
64 int closed
; /* valid iff peer != NULL */
65 size_t len
; /* valid iff buf != NULL; 0 if peer == NULL */
66 size_t offset
; /* valid iff buf != NULL; 0 if len == 0 */
68 char *buf
; /* "size" elements (if != NULL) */
69 size_t request
; /* valid iff peer != NULL; 0 if len != 0,
70 * otherwise set by peer to number of bytes
71 * it (unsuccessfully) tried to read, never
72 * more than buffer space (size-len)
76 static int bio_new(BIO
*bio
)
78 struct bio_bio_st
*b
= OPENSSL_zalloc(sizeof(*b
));
83 /* enough for one TLS record (just a default) */
90 static int bio_free(BIO
*bio
)
101 bio_destroy_pair(bio
);
103 OPENSSL_free(b
->buf
);
109 static int bio_read(BIO
*bio
, char *buf
, int size_
)
113 struct bio_bio_st
*b
, *peer_b
;
115 BIO_clear_retry_flags(bio
);
122 assert(b
->peer
!= NULL
);
123 peer_b
= b
->peer
->ptr
;
124 assert(peer_b
!= NULL
);
125 assert(peer_b
->buf
!= NULL
);
127 peer_b
->request
= 0; /* will be set in "retry_read" situation */
129 if (buf
== NULL
|| size
== 0)
132 if (peer_b
->len
== 0) {
134 return 0; /* writer has closed, and no data is left */
136 BIO_set_retry_read(bio
); /* buffer is empty */
137 if (size
<= peer_b
->size
)
138 peer_b
->request
= size
;
141 * don't ask for more than the peer can deliver in one write
143 peer_b
->request
= peer_b
->size
;
149 if (peer_b
->len
< size
)
152 /* now read "size" bytes */
157 do { /* one or two iterations */
160 assert(rest
<= peer_b
->len
);
161 if (peer_b
->offset
+ rest
<= peer_b
->size
)
164 /* wrap around ring buffer */
165 chunk
= peer_b
->size
- peer_b
->offset
;
166 assert(peer_b
->offset
+ chunk
<= peer_b
->size
);
168 memcpy(buf
, peer_b
->buf
+ peer_b
->offset
, chunk
);
170 peer_b
->len
-= chunk
;
172 peer_b
->offset
+= chunk
;
173 assert(peer_b
->offset
<= peer_b
->size
);
174 if (peer_b
->offset
== peer_b
->size
)
178 /* buffer now empty, no need to advance "buf" */
179 assert(chunk
== rest
);
190 * non-copying interface: provide pointer to available data in buffer
191 * bio_nread0: return number of available bytes
192 * bio_nread: also advance index
193 * (example usage: bio_nread0(), read from buffer, bio_nread()
194 * or just bio_nread(), read from buffer)
197 * WARNING: The non-copying interface is largely untested as of yet and may
200 static ossl_ssize_t
bio_nread0(BIO
*bio
, char **buf
)
202 struct bio_bio_st
*b
, *peer_b
;
205 BIO_clear_retry_flags(bio
);
212 assert(b
->peer
!= NULL
);
213 peer_b
= b
->peer
->ptr
;
214 assert(peer_b
!= NULL
);
215 assert(peer_b
->buf
!= NULL
);
219 if (peer_b
->len
== 0) {
222 /* avoid code duplication -- nothing available for reading */
223 return bio_read(bio
, &dummy
, 1); /* returns 0 or -1 */
227 if (peer_b
->size
< peer_b
->offset
+ num
)
228 /* no ring buffer wrap-around for non-copying interface */
229 num
= peer_b
->size
- peer_b
->offset
;
233 *buf
= peer_b
->buf
+ peer_b
->offset
;
237 static ossl_ssize_t
bio_nread(BIO
*bio
, char **buf
, size_t num_
)
239 struct bio_bio_st
*b
, *peer_b
;
240 ossl_ssize_t num
, available
;
242 if (num_
> OSSL_SSIZE_MAX
)
243 num
= OSSL_SSIZE_MAX
;
245 num
= (ossl_ssize_t
) num_
;
247 available
= bio_nread0(bio
, buf
);
254 peer_b
= b
->peer
->ptr
;
258 peer_b
->offset
+= num
;
259 assert(peer_b
->offset
<= peer_b
->size
);
260 if (peer_b
->offset
== peer_b
->size
)
268 static int bio_write(BIO
*bio
, const char *buf
, int num_
)
272 struct bio_bio_st
*b
;
274 BIO_clear_retry_flags(bio
);
276 if (!bio
->init
|| buf
== NULL
|| num_
<= 0)
281 assert(b
->peer
!= NULL
);
282 assert(b
->buf
!= NULL
);
286 /* we already closed */
287 ERR_raise(ERR_LIB_BIO
, BIO_R_BROKEN_PIPE
);
291 assert(b
->len
<= b
->size
);
293 if (b
->len
== b
->size
) {
294 BIO_set_retry_write(bio
); /* buffer is full */
299 if (num
> b
->size
- b
->len
)
300 num
= b
->size
- b
->len
;
302 /* now write "num" bytes */
307 do { /* one or two iterations */
311 assert(b
->len
+ rest
<= b
->size
);
313 write_offset
= b
->offset
+ b
->len
;
314 if (write_offset
>= b
->size
)
315 write_offset
-= b
->size
;
316 /* b->buf[write_offset] is the first byte we can write to. */
318 if (write_offset
+ rest
<= b
->size
)
321 /* wrap around ring buffer */
322 chunk
= b
->size
- write_offset
;
324 memcpy(b
->buf
+ write_offset
, buf
, chunk
);
328 assert(b
->len
<= b
->size
);
339 * non-copying interface: provide pointer to region to write to
340 * bio_nwrite0: check how much space is available
341 * bio_nwrite: also increase length
342 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
343 * or just bio_nwrite(), write to buffer)
345 static ossl_ssize_t
bio_nwrite0(BIO
*bio
, char **buf
)
347 struct bio_bio_st
*b
;
351 BIO_clear_retry_flags(bio
);
358 assert(b
->peer
!= NULL
);
359 assert(b
->buf
!= NULL
);
363 ERR_raise(ERR_LIB_BIO
, BIO_R_BROKEN_PIPE
);
367 assert(b
->len
<= b
->size
);
369 if (b
->len
== b
->size
) {
370 BIO_set_retry_write(bio
);
374 num
= b
->size
- b
->len
;
375 write_offset
= b
->offset
+ b
->len
;
376 if (write_offset
>= b
->size
)
377 write_offset
-= b
->size
;
378 if (write_offset
+ num
> b
->size
)
380 * no ring buffer wrap-around for non-copying interface (to fulfil
381 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
382 * to be called twice)
384 num
= b
->size
- write_offset
;
387 *buf
= b
->buf
+ write_offset
;
388 assert(write_offset
+ num
<= b
->size
);
393 static ossl_ssize_t
bio_nwrite(BIO
*bio
, char **buf
, size_t num_
)
395 struct bio_bio_st
*b
;
396 ossl_ssize_t num
, space
;
398 if (num_
> OSSL_SSIZE_MAX
)
399 num
= OSSL_SSIZE_MAX
;
401 num
= (ossl_ssize_t
) num_
;
403 space
= bio_nwrite0(bio
, buf
);
411 assert(b
->len
<= b
->size
);
416 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
419 struct bio_bio_st
*b
= bio
->ptr
;
424 /* specific CTRL codes */
426 case BIO_C_SET_WRITE_BUF_SIZE
:
428 ERR_raise(ERR_LIB_BIO
, BIO_R_IN_USE
);
430 } else if (num
== 0) {
431 ERR_raise(ERR_LIB_BIO
, BIO_R_INVALID_ARGUMENT
);
434 size_t new_size
= num
;
436 if (b
->size
!= new_size
) {
437 OPENSSL_free(b
->buf
);
445 case BIO_C_GET_WRITE_BUF_SIZE
:
449 case BIO_C_MAKE_BIO_PAIR
:
451 BIO
*other_bio
= ptr
;
453 if (bio_make_pair(bio
, other_bio
))
460 case BIO_C_DESTROY_BIO_PAIR
:
462 * Affects both BIOs in the pair -- call just once! Or let
463 * BIO_free(bio1); BIO_free(bio2); do the job.
465 bio_destroy_pair(bio
);
469 case BIO_C_GET_WRITE_GUARANTEE
:
471 * How many bytes can the caller feed to the next write without
472 * having to keep any?
474 if (b
->peer
== NULL
|| b
->closed
)
477 ret
= (long)b
->size
- b
->len
;
480 case BIO_C_GET_READ_REQUEST
:
482 * If the peer unsuccessfully tried to read, how many bytes were
483 * requested? (As with BIO_CTRL_PENDING, that number can usually be
484 * treated as boolean.)
486 ret
= (long)b
->request
;
489 case BIO_C_RESET_READ_REQUEST
:
491 * Reset request. (Can be useful after read attempts at the other
492 * side that are meant to be non-blocking, e.g. when probing SSL_read
493 * to see if any data is available.)
499 case BIO_C_SHUTDOWN_WR
:
500 /* similar to shutdown(..., SHUT_WR) */
506 /* prepare for non-copying read */
507 ret
= (long)bio_nread0(bio
, ptr
);
511 /* non-copying read */
512 ret
= (long)bio_nread(bio
, ptr
, (size_t)num
);
516 /* prepare for non-copying write */
517 ret
= (long)bio_nwrite0(bio
, ptr
);
521 /* non-copying write */
522 ret
= (long)bio_nwrite(bio
, ptr
, (size_t)num
);
525 /* standard CTRL codes follow */
528 if (b
->buf
!= NULL
) {
535 case BIO_CTRL_GET_CLOSE
:
539 case BIO_CTRL_SET_CLOSE
:
540 bio
->shutdown
= (int)num
;
544 case BIO_CTRL_PENDING
:
545 if (b
->peer
!= NULL
) {
546 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
548 ret
= (long)peer_b
->len
;
553 case BIO_CTRL_WPENDING
:
561 /* See BIO_dup_chain for circumstances we have to expect. */
563 BIO
*other_bio
= ptr
;
564 struct bio_bio_st
*other_b
;
566 assert(other_bio
!= NULL
);
567 other_b
= other_bio
->ptr
;
568 assert(other_b
!= NULL
);
570 assert(other_b
->buf
== NULL
); /* other_bio is always fresh */
572 other_b
->size
= b
->size
;
583 if (b
->peer
!= NULL
) {
584 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
586 if (peer_b
->len
== 0 && peer_b
->closed
)
601 static int bio_puts(BIO
*bio
, const char *str
)
603 return bio_write(bio
, str
, strlen(str
));
606 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
)
608 struct bio_bio_st
*b1
, *b2
;
610 assert(bio1
!= NULL
);
611 assert(bio2
!= NULL
);
616 if (b1
->peer
!= NULL
|| b2
->peer
!= NULL
) {
617 ERR_raise(ERR_LIB_BIO
, BIO_R_IN_USE
);
621 if (b1
->buf
== NULL
) {
622 b1
->buf
= OPENSSL_malloc(b1
->size
);
629 if (b2
->buf
== NULL
) {
630 b2
->buf
= OPENSSL_malloc(b2
->size
);
650 static void bio_destroy_pair(BIO
*bio
)
652 struct bio_bio_st
*b
= bio
->ptr
;
655 BIO
*peer_bio
= b
->peer
;
657 if (peer_bio
!= NULL
) {
658 struct bio_bio_st
*peer_b
= peer_bio
->ptr
;
660 assert(peer_b
!= NULL
);
661 assert(peer_b
->peer
== bio
);
665 assert(peer_b
->buf
!= NULL
);
671 assert(b
->buf
!= NULL
);
678 /* Exported convenience functions */
679 int BIO_new_bio_pair(BIO
**bio1_p
, size_t writebuf1
,
680 BIO
**bio2_p
, size_t writebuf2
)
682 BIO
*bio1
= NULL
, *bio2
= NULL
;
686 bio1
= BIO_new(BIO_s_bio());
689 bio2
= BIO_new(BIO_s_bio());
694 r
= BIO_set_write_buf_size(bio1
, writebuf1
);
699 r
= BIO_set_write_buf_size(bio2
, writebuf2
);
704 r
= BIO_make_bio_pair(bio1
, bio2
);
722 size_t BIO_ctrl_get_write_guarantee(BIO
*bio
)
724 return BIO_ctrl(bio
, BIO_C_GET_WRITE_GUARANTEE
, 0, NULL
);
727 size_t BIO_ctrl_get_read_request(BIO
*bio
)
729 return BIO_ctrl(bio
, BIO_C_GET_READ_REQUEST
, 0, NULL
);
732 int BIO_ctrl_reset_read_request(BIO
*bio
)
734 return (BIO_ctrl(bio
, BIO_C_RESET_READ_REQUEST
, 0, NULL
) != 0);
738 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
739 * (conceivably some other BIOs could allow non-copying reads and writes
742 int BIO_nread0(BIO
*bio
, char **buf
)
747 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
751 ret
= BIO_ctrl(bio
, BIO_C_NREAD0
, 0, buf
);
758 int BIO_nread(BIO
*bio
, char **buf
, int num
)
763 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
767 ret
= (int)BIO_ctrl(bio
, BIO_C_NREAD
, num
, buf
);
769 bio
->num_read
+= ret
;
773 int BIO_nwrite0(BIO
*bio
, char **buf
)
778 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
782 ret
= BIO_ctrl(bio
, BIO_C_NWRITE0
, 0, buf
);
789 int BIO_nwrite(BIO
*bio
, char **buf
, int num
)
794 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
798 ret
= BIO_ctrl(bio
, BIO_C_NWRITE
, num
, buf
);
800 bio
->num_write
+= ret
;