2 * Copyright 1999-2020 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.
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
= {
41 /* TODO: Convert to new style write function */
44 /* TODO: Convert to new style read function */
48 NULL
/* no bio_gets */ ,
52 NULL
/* no bio_callback_ctrl */
55 const BIO_METHOD
*BIO_s_bio(void)
61 BIO
*peer
; /* NULL if buf == NULL. If peer != NULL, then
62 * peer->ptr is also a bio_bio_st, and its
63 * "peer" member points back to us. peer !=
64 * NULL iff init != 0 in the BIO. */
65 /* This is for what we write (i.e. reading uses peer's struct): */
66 int closed
; /* valid iff peer != NULL */
67 size_t len
; /* valid iff buf != NULL; 0 if peer == NULL */
68 size_t offset
; /* valid iff buf != NULL; 0 if len == 0 */
70 char *buf
; /* "size" elements (if != NULL) */
71 size_t request
; /* valid iff peer != NULL; 0 if len != 0,
72 * otherwise set by peer to number of bytes
73 * it (unsuccessfully) tried to read, never
74 * more than buffer space (size-len)
78 static int bio_new(BIO
*bio
)
80 struct bio_bio_st
*b
= OPENSSL_zalloc(sizeof(*b
));
85 /* enough for one TLS record (just a default) */
92 static int bio_free(BIO
*bio
)
103 bio_destroy_pair(bio
);
105 OPENSSL_free(b
->buf
);
111 static int bio_read(BIO
*bio
, char *buf
, int size_
)
115 struct bio_bio_st
*b
, *peer_b
;
117 BIO_clear_retry_flags(bio
);
124 assert(b
->peer
!= NULL
);
125 peer_b
= b
->peer
->ptr
;
126 assert(peer_b
!= NULL
);
127 assert(peer_b
->buf
!= NULL
);
129 peer_b
->request
= 0; /* will be set in "retry_read" situation */
131 if (buf
== NULL
|| size
== 0)
134 if (peer_b
->len
== 0) {
136 return 0; /* writer has closed, and no data is left */
138 BIO_set_retry_read(bio
); /* buffer is empty */
139 if (size
<= peer_b
->size
)
140 peer_b
->request
= size
;
143 * don't ask for more than the peer can deliver in one write
145 peer_b
->request
= peer_b
->size
;
151 if (peer_b
->len
< size
)
154 /* now read "size" bytes */
159 do { /* one or two iterations */
162 assert(rest
<= peer_b
->len
);
163 if (peer_b
->offset
+ rest
<= peer_b
->size
)
166 /* wrap around ring buffer */
167 chunk
= peer_b
->size
- peer_b
->offset
;
168 assert(peer_b
->offset
+ chunk
<= peer_b
->size
);
170 memcpy(buf
, peer_b
->buf
+ peer_b
->offset
, chunk
);
172 peer_b
->len
-= chunk
;
174 peer_b
->offset
+= chunk
;
175 assert(peer_b
->offset
<= peer_b
->size
);
176 if (peer_b
->offset
== peer_b
->size
)
180 /* buffer now empty, no need to advance "buf" */
181 assert(chunk
== rest
);
192 * non-copying interface: provide pointer to available data in buffer
193 * bio_nread0: return number of available bytes
194 * bio_nread: also advance index
195 * (example usage: bio_nread0(), read from buffer, bio_nread()
196 * or just bio_nread(), read from buffer)
199 * WARNING: The non-copying interface is largely untested as of yet and may
202 static ossl_ssize_t
bio_nread0(BIO
*bio
, char **buf
)
204 struct bio_bio_st
*b
, *peer_b
;
207 BIO_clear_retry_flags(bio
);
214 assert(b
->peer
!= NULL
);
215 peer_b
= b
->peer
->ptr
;
216 assert(peer_b
!= NULL
);
217 assert(peer_b
->buf
!= NULL
);
221 if (peer_b
->len
== 0) {
224 /* avoid code duplication -- nothing available for reading */
225 return bio_read(bio
, &dummy
, 1); /* returns 0 or -1 */
229 if (peer_b
->size
< peer_b
->offset
+ num
)
230 /* no ring buffer wrap-around for non-copying interface */
231 num
= peer_b
->size
- peer_b
->offset
;
235 *buf
= peer_b
->buf
+ peer_b
->offset
;
239 static ossl_ssize_t
bio_nread(BIO
*bio
, char **buf
, size_t num_
)
241 struct bio_bio_st
*b
, *peer_b
;
242 ossl_ssize_t num
, available
;
244 if (num_
> OSSL_SSIZE_MAX
)
245 num
= OSSL_SSIZE_MAX
;
247 num
= (ossl_ssize_t
) num_
;
249 available
= bio_nread0(bio
, buf
);
256 peer_b
= b
->peer
->ptr
;
260 peer_b
->offset
+= num
;
261 assert(peer_b
->offset
<= peer_b
->size
);
262 if (peer_b
->offset
== peer_b
->size
)
270 static int bio_write(BIO
*bio
, const char *buf
, int num_
)
274 struct bio_bio_st
*b
;
276 BIO_clear_retry_flags(bio
);
278 if (!bio
->init
|| buf
== NULL
|| num
== 0)
283 assert(b
->peer
!= NULL
);
284 assert(b
->buf
!= NULL
);
288 /* we already closed */
289 ERR_raise(ERR_LIB_BIO
, BIO_R_BROKEN_PIPE
);
293 assert(b
->len
<= b
->size
);
295 if (b
->len
== b
->size
) {
296 BIO_set_retry_write(bio
); /* buffer is full */
301 if (num
> b
->size
- b
->len
)
302 num
= b
->size
- b
->len
;
304 /* now write "num" bytes */
309 do { /* one or two iterations */
313 assert(b
->len
+ rest
<= b
->size
);
315 write_offset
= b
->offset
+ b
->len
;
316 if (write_offset
>= b
->size
)
317 write_offset
-= b
->size
;
318 /* b->buf[write_offset] is the first byte we can write to. */
320 if (write_offset
+ rest
<= b
->size
)
323 /* wrap around ring buffer */
324 chunk
= b
->size
- write_offset
;
326 memcpy(b
->buf
+ write_offset
, buf
, chunk
);
330 assert(b
->len
<= b
->size
);
341 * non-copying interface: provide pointer to region to write to
342 * bio_nwrite0: check how much space is available
343 * bio_nwrite: also increase length
344 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
345 * or just bio_nwrite(), write to buffer)
347 static ossl_ssize_t
bio_nwrite0(BIO
*bio
, char **buf
)
349 struct bio_bio_st
*b
;
353 BIO_clear_retry_flags(bio
);
360 assert(b
->peer
!= NULL
);
361 assert(b
->buf
!= NULL
);
365 ERR_raise(ERR_LIB_BIO
, BIO_R_BROKEN_PIPE
);
369 assert(b
->len
<= b
->size
);
371 if (b
->len
== b
->size
) {
372 BIO_set_retry_write(bio
);
376 num
= b
->size
- b
->len
;
377 write_offset
= b
->offset
+ b
->len
;
378 if (write_offset
>= b
->size
)
379 write_offset
-= b
->size
;
380 if (write_offset
+ num
> b
->size
)
382 * no ring buffer wrap-around for non-copying interface (to fulfil
383 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
384 * to be called twice)
386 num
= b
->size
- write_offset
;
389 *buf
= b
->buf
+ write_offset
;
390 assert(write_offset
+ num
<= b
->size
);
395 static ossl_ssize_t
bio_nwrite(BIO
*bio
, char **buf
, size_t num_
)
397 struct bio_bio_st
*b
;
398 ossl_ssize_t num
, space
;
400 if (num_
> OSSL_SSIZE_MAX
)
401 num
= OSSL_SSIZE_MAX
;
403 num
= (ossl_ssize_t
) num_
;
405 space
= bio_nwrite0(bio
, buf
);
413 assert(b
->len
<= b
->size
);
418 static long bio_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
421 struct bio_bio_st
*b
= bio
->ptr
;
426 /* specific CTRL codes */
428 case BIO_C_SET_WRITE_BUF_SIZE
:
430 ERR_raise(ERR_LIB_BIO
, BIO_R_IN_USE
);
432 } else if (num
== 0) {
433 ERR_raise(ERR_LIB_BIO
, BIO_R_INVALID_ARGUMENT
);
436 size_t new_size
= num
;
438 if (b
->size
!= new_size
) {
439 OPENSSL_free(b
->buf
);
447 case BIO_C_GET_WRITE_BUF_SIZE
:
451 case BIO_C_MAKE_BIO_PAIR
:
453 BIO
*other_bio
= ptr
;
455 if (bio_make_pair(bio
, other_bio
))
462 case BIO_C_DESTROY_BIO_PAIR
:
464 * Affects both BIOs in the pair -- call just once! Or let
465 * BIO_free(bio1); BIO_free(bio2); do the job.
467 bio_destroy_pair(bio
);
471 case BIO_C_GET_WRITE_GUARANTEE
:
473 * How many bytes can the caller feed to the next write without
474 * having to keep any?
476 if (b
->peer
== NULL
|| b
->closed
)
479 ret
= (long)b
->size
- b
->len
;
482 case BIO_C_GET_READ_REQUEST
:
484 * If the peer unsuccessfully tried to read, how many bytes were
485 * requested? (As with BIO_CTRL_PENDING, that number can usually be
486 * treated as boolean.)
488 ret
= (long)b
->request
;
491 case BIO_C_RESET_READ_REQUEST
:
493 * Reset request. (Can be useful after read attempts at the other
494 * side that are meant to be non-blocking, e.g. when probing SSL_read
495 * to see if any data is available.)
501 case BIO_C_SHUTDOWN_WR
:
502 /* similar to shutdown(..., SHUT_WR) */
508 /* prepare for non-copying read */
509 ret
= (long)bio_nread0(bio
, ptr
);
513 /* non-copying read */
514 ret
= (long)bio_nread(bio
, ptr
, (size_t)num
);
518 /* prepare for non-copying write */
519 ret
= (long)bio_nwrite0(bio
, ptr
);
523 /* non-copying write */
524 ret
= (long)bio_nwrite(bio
, ptr
, (size_t)num
);
527 /* standard CTRL codes follow */
530 if (b
->buf
!= NULL
) {
537 case BIO_CTRL_GET_CLOSE
:
541 case BIO_CTRL_SET_CLOSE
:
542 bio
->shutdown
= (int)num
;
546 case BIO_CTRL_PENDING
:
547 if (b
->peer
!= NULL
) {
548 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
550 ret
= (long)peer_b
->len
;
555 case BIO_CTRL_WPENDING
:
563 /* See BIO_dup_chain for circumstances we have to expect. */
565 BIO
*other_bio
= ptr
;
566 struct bio_bio_st
*other_b
;
568 assert(other_bio
!= NULL
);
569 other_b
= other_bio
->ptr
;
570 assert(other_b
!= NULL
);
572 assert(other_b
->buf
== NULL
); /* other_bio is always fresh */
574 other_b
->size
= b
->size
;
585 if (b
->peer
!= NULL
) {
586 struct bio_bio_st
*peer_b
= b
->peer
->ptr
;
588 if (peer_b
->len
== 0 && peer_b
->closed
)
603 static int bio_puts(BIO
*bio
, const char *str
)
605 return bio_write(bio
, str
, strlen(str
));
608 static int bio_make_pair(BIO
*bio1
, BIO
*bio2
)
610 struct bio_bio_st
*b1
, *b2
;
612 assert(bio1
!= NULL
);
613 assert(bio2
!= NULL
);
618 if (b1
->peer
!= NULL
|| b2
->peer
!= NULL
) {
619 ERR_raise(ERR_LIB_BIO
, BIO_R_IN_USE
);
623 if (b1
->buf
== NULL
) {
624 b1
->buf
= OPENSSL_malloc(b1
->size
);
625 if (b1
->buf
== NULL
) {
626 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
633 if (b2
->buf
== NULL
) {
634 b2
->buf
= OPENSSL_malloc(b2
->size
);
635 if (b2
->buf
== NULL
) {
636 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
656 static void bio_destroy_pair(BIO
*bio
)
658 struct bio_bio_st
*b
= bio
->ptr
;
661 BIO
*peer_bio
= b
->peer
;
663 if (peer_bio
!= NULL
) {
664 struct bio_bio_st
*peer_b
= peer_bio
->ptr
;
666 assert(peer_b
!= NULL
);
667 assert(peer_b
->peer
== bio
);
671 assert(peer_b
->buf
!= NULL
);
677 assert(b
->buf
!= NULL
);
684 /* Exported convenience functions */
685 int BIO_new_bio_pair(BIO
**bio1_p
, size_t writebuf1
,
686 BIO
**bio2_p
, size_t writebuf2
)
688 BIO
*bio1
= NULL
, *bio2
= NULL
;
692 bio1
= BIO_new(BIO_s_bio());
695 bio2
= BIO_new(BIO_s_bio());
700 r
= BIO_set_write_buf_size(bio1
, writebuf1
);
705 r
= BIO_set_write_buf_size(bio2
, writebuf2
);
710 r
= BIO_make_bio_pair(bio1
, bio2
);
728 size_t BIO_ctrl_get_write_guarantee(BIO
*bio
)
730 return BIO_ctrl(bio
, BIO_C_GET_WRITE_GUARANTEE
, 0, NULL
);
733 size_t BIO_ctrl_get_read_request(BIO
*bio
)
735 return BIO_ctrl(bio
, BIO_C_GET_READ_REQUEST
, 0, NULL
);
738 int BIO_ctrl_reset_read_request(BIO
*bio
)
740 return (BIO_ctrl(bio
, BIO_C_RESET_READ_REQUEST
, 0, NULL
) != 0);
744 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
745 * (conceivably some other BIOs could allow non-copying reads and writes
748 int BIO_nread0(BIO
*bio
, char **buf
)
753 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
757 ret
= BIO_ctrl(bio
, BIO_C_NREAD0
, 0, buf
);
764 int BIO_nread(BIO
*bio
, char **buf
, int num
)
769 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
773 ret
= (int)BIO_ctrl(bio
, BIO_C_NREAD
, num
, buf
);
775 bio
->num_read
+= ret
;
779 int BIO_nwrite0(BIO
*bio
, char **buf
)
784 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
788 ret
= BIO_ctrl(bio
, BIO_C_NWRITE0
, 0, buf
);
795 int BIO_nwrite(BIO
*bio
, char **buf
, int num
)
800 ERR_raise(ERR_LIB_BIO
, BIO_R_UNINITIALIZED
);
804 ret
= BIO_ctrl(bio
, BIO_C_NWRITE
, num
, buf
);
806 bio
->num_write
+= ret
;