2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 <openssl/err.h>
25 #include <openssl/crypto.h>
29 static int bio_new(BIO *bio);
30 static int bio_free(BIO *bio);
31 static int bio_read(BIO *bio, char *buf, int size);
32 static int bio_write(BIO *bio, const char *buf, int num);
33 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
34 static int bio_puts(BIO *bio, const char *str);
36 static int bio_make_pair(BIO *bio1, BIO *bio2);
37 static void bio_destroy_pair(BIO *bio);
39 static const BIO_METHOD methods_biop = {
43 /* TODO: Convert to new style read function */
47 NULL /* no bio_gets */ ,
51 NULL /* no bio_callback_ctrl */
54 const BIO_METHOD *BIO_s_bio(void)
60 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
61 * peer->ptr is also a bio_bio_st, and its
62 * "peer" member points back to us. peer !=
63 * NULL iff init != 0 in the BIO. */
64 /* This is for what we write (i.e. reading uses peer's struct): */
65 int closed; /* valid iff peer != NULL */
66 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
67 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
69 char *buf; /* "size" elements (if != NULL) */
70 size_t request; /* valid iff peer != NULL; 0 if len != 0,
71 * otherwise set by peer to number of bytes
72 * it (unsuccessfully) tried to read, never
73 * more than buffer space (size-len)
77 static int bio_new(BIO *bio)
79 struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
84 /* enough for one TLS record (just a default) */
91 static int bio_free(BIO *bio)
102 bio_destroy_pair(bio);
104 OPENSSL_free(b->buf);
110 static int bio_read(BIO *bio, char *buf, int size_)
114 struct bio_bio_st *b, *peer_b;
116 BIO_clear_retry_flags(bio);
123 assert(b->peer != NULL);
124 peer_b = b->peer->ptr;
125 assert(peer_b != NULL);
126 assert(peer_b->buf != NULL);
128 peer_b->request = 0; /* will be set in "retry_read" situation */
130 if (buf == NULL || size == 0)
133 if (peer_b->len == 0) {
135 return 0; /* writer has closed, and no data is left */
137 BIO_set_retry_read(bio); /* buffer is empty */
138 if (size <= peer_b->size)
139 peer_b->request = size;
142 * don't ask for more than the peer can deliver in one write
144 peer_b->request = peer_b->size;
150 if (peer_b->len < size)
153 /* now read "size" bytes */
158 do { /* one or two iterations */
161 assert(rest <= peer_b->len);
162 if (peer_b->offset + rest <= peer_b->size)
165 /* wrap around ring buffer */
166 chunk = peer_b->size - peer_b->offset;
167 assert(peer_b->offset + chunk <= peer_b->size);
169 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
171 peer_b->len -= chunk;
173 peer_b->offset += chunk;
174 assert(peer_b->offset <= peer_b->size);
175 if (peer_b->offset == peer_b->size)
179 /* buffer now empty, no need to advance "buf" */
180 assert(chunk == rest);
191 * non-copying interface: provide pointer to available data in buffer
192 * bio_nread0: return number of available bytes
193 * bio_nread: also advance index
194 * (example usage: bio_nread0(), read from buffer, bio_nread()
195 * or just bio_nread(), read from buffer)
198 * WARNING: The non-copying interface is largely untested as of yet and may
201 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
203 struct bio_bio_st *b, *peer_b;
206 BIO_clear_retry_flags(bio);
213 assert(b->peer != NULL);
214 peer_b = b->peer->ptr;
215 assert(peer_b != NULL);
216 assert(peer_b->buf != NULL);
220 if (peer_b->len == 0) {
223 /* avoid code duplication -- nothing available for reading */
224 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
228 if (peer_b->size < peer_b->offset + num)
229 /* no ring buffer wrap-around for non-copying interface */
230 num = peer_b->size - peer_b->offset;
234 *buf = peer_b->buf + peer_b->offset;
238 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
240 struct bio_bio_st *b, *peer_b;
241 ossl_ssize_t num, available;
243 if (num_ > OSSL_SSIZE_MAX)
244 num = OSSL_SSIZE_MAX;
246 num = (ossl_ssize_t) num_;
248 available = bio_nread0(bio, buf);
255 peer_b = b->peer->ptr;
259 peer_b->offset += num;
260 assert(peer_b->offset <= peer_b->size);
261 if (peer_b->offset == peer_b->size)
269 static int bio_write(BIO *bio, const char *buf, int num_)
273 struct bio_bio_st *b;
275 BIO_clear_retry_flags(bio);
277 if (!bio->init || buf == NULL || num == 0)
282 assert(b->peer != NULL);
283 assert(b->buf != NULL);
287 /* we already closed */
288 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
292 assert(b->len <= b->size);
294 if (b->len == b->size) {
295 BIO_set_retry_write(bio); /* buffer is full */
300 if (num > b->size - b->len)
301 num = b->size - b->len;
303 /* now write "num" bytes */
308 do { /* one or two iterations */
312 assert(b->len + rest <= b->size);
314 write_offset = b->offset + b->len;
315 if (write_offset >= b->size)
316 write_offset -= b->size;
317 /* b->buf[write_offset] is the first byte we can write to. */
319 if (write_offset + rest <= b->size)
322 /* wrap around ring buffer */
323 chunk = b->size - write_offset;
325 memcpy(b->buf + write_offset, buf, chunk);
329 assert(b->len <= b->size);
340 * non-copying interface: provide pointer to region to write to
341 * bio_nwrite0: check how much space is available
342 * bio_nwrite: also increase length
343 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
344 * or just bio_nwrite(), write to buffer)
346 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
348 struct bio_bio_st *b;
352 BIO_clear_retry_flags(bio);
359 assert(b->peer != NULL);
360 assert(b->buf != NULL);
364 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
368 assert(b->len <= b->size);
370 if (b->len == b->size) {
371 BIO_set_retry_write(bio);
375 num = b->size - b->len;
376 write_offset = b->offset + b->len;
377 if (write_offset >= b->size)
378 write_offset -= b->size;
379 if (write_offset + num > b->size)
381 * no ring buffer wrap-around for non-copying interface (to fulfil
382 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
383 * to be called twice)
385 num = b->size - write_offset;
388 *buf = b->buf + write_offset;
389 assert(write_offset + num <= b->size);
394 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
396 struct bio_bio_st *b;
397 ossl_ssize_t num, space;
399 if (num_ > OSSL_SSIZE_MAX)
400 num = OSSL_SSIZE_MAX;
402 num = (ossl_ssize_t) num_;
404 space = bio_nwrite0(bio, buf);
412 assert(b->len <= b->size);
417 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
420 struct bio_bio_st *b = bio->ptr;
425 /* specific CTRL codes */
427 case BIO_C_SET_WRITE_BUF_SIZE:
429 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
431 } else if (num == 0) {
432 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
435 size_t new_size = num;
437 if (b->size != new_size) {
438 OPENSSL_free(b->buf);
446 case BIO_C_GET_WRITE_BUF_SIZE:
450 case BIO_C_MAKE_BIO_PAIR:
452 BIO *other_bio = ptr;
454 if (bio_make_pair(bio, other_bio))
461 case BIO_C_DESTROY_BIO_PAIR:
463 * Affects both BIOs in the pair -- call just once! Or let
464 * BIO_free(bio1); BIO_free(bio2); do the job.
466 bio_destroy_pair(bio);
470 case BIO_C_GET_WRITE_GUARANTEE:
472 * How many bytes can the caller feed to the next write without
473 * having to keep any?
475 if (b->peer == NULL || b->closed)
478 ret = (long)b->size - b->len;
481 case BIO_C_GET_READ_REQUEST:
483 * If the peer unsuccessfully tried to read, how many bytes were
484 * requested? (As with BIO_CTRL_PENDING, that number can usually be
485 * treated as boolean.)
487 ret = (long)b->request;
490 case BIO_C_RESET_READ_REQUEST:
492 * Reset request. (Can be useful after read attempts at the other
493 * side that are meant to be non-blocking, e.g. when probing SSL_read
494 * to see if any data is available.)
500 case BIO_C_SHUTDOWN_WR:
501 /* similar to shutdown(..., SHUT_WR) */
507 /* prepare for non-copying read */
508 ret = (long)bio_nread0(bio, ptr);
512 /* non-copying read */
513 ret = (long)bio_nread(bio, ptr, (size_t)num);
517 /* prepare for non-copying write */
518 ret = (long)bio_nwrite0(bio, ptr);
522 /* non-copying write */
523 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
526 /* standard CTRL codes follow */
529 if (b->buf != NULL) {
536 case BIO_CTRL_GET_CLOSE:
540 case BIO_CTRL_SET_CLOSE:
541 bio->shutdown = (int)num;
545 case BIO_CTRL_PENDING:
546 if (b->peer != NULL) {
547 struct bio_bio_st *peer_b = b->peer->ptr;
549 ret = (long)peer_b->len;
554 case BIO_CTRL_WPENDING:
562 /* See BIO_dup_chain for circumstances we have to expect. */
564 BIO *other_bio = ptr;
565 struct bio_bio_st *other_b;
567 assert(other_bio != NULL);
568 other_b = other_bio->ptr;
569 assert(other_b != NULL);
571 assert(other_b->buf == NULL); /* other_bio is always fresh */
573 other_b->size = b->size;
584 if (b->peer != NULL) {
585 struct bio_bio_st *peer_b = b->peer->ptr;
587 if (peer_b->len == 0 && peer_b->closed)
602 static int bio_puts(BIO *bio, const char *str)
604 return bio_write(bio, str, strlen(str));
607 static int bio_make_pair(BIO *bio1, BIO *bio2)
609 struct bio_bio_st *b1, *b2;
611 assert(bio1 != NULL);
612 assert(bio2 != NULL);
617 if (b1->peer != NULL || b2->peer != NULL) {
618 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
622 if (b1->buf == NULL) {
623 b1->buf = OPENSSL_malloc(b1->size);
624 if (b1->buf == NULL) {
625 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
632 if (b2->buf == NULL) {
633 b2->buf = OPENSSL_malloc(b2->size);
634 if (b2->buf == NULL) {
635 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
655 static void bio_destroy_pair(BIO *bio)
657 struct bio_bio_st *b = bio->ptr;
660 BIO *peer_bio = b->peer;
662 if (peer_bio != NULL) {
663 struct bio_bio_st *peer_b = peer_bio->ptr;
665 assert(peer_b != NULL);
666 assert(peer_b->peer == bio);
670 assert(peer_b->buf != NULL);
676 assert(b->buf != NULL);
683 /* Exported convenience functions */
684 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
685 BIO **bio2_p, size_t writebuf2)
687 BIO *bio1 = NULL, *bio2 = NULL;
691 bio1 = BIO_new(BIO_s_bio());
694 bio2 = BIO_new(BIO_s_bio());
699 r = BIO_set_write_buf_size(bio1, writebuf1);
704 r = BIO_set_write_buf_size(bio2, writebuf2);
709 r = BIO_make_bio_pair(bio1, bio2);
727 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
729 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
732 size_t BIO_ctrl_get_read_request(BIO *bio)
734 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
737 int BIO_ctrl_reset_read_request(BIO *bio)
739 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
743 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
744 * (conceivably some other BIOs could allow non-copying reads and writes
747 int BIO_nread0(BIO *bio, char **buf)
752 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
756 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
763 int BIO_nread(BIO *bio, char **buf, int num)
768 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
772 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
774 bio->num_read += ret;
778 int BIO_nwrite0(BIO *bio, char **buf)
783 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
787 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
794 int BIO_nwrite(BIO *bio, char **buf, int num)
799 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
803 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
805 bio->num_write += ret;