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 = {
45 NULL /* no bio_gets */ ,
49 NULL /* no bio_callback_ctrl */
52 const BIO_METHOD *BIO_s_bio(void)
58 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
59 * peer->ptr is also a bio_bio_st, and its
60 * "peer" member points back to us. peer !=
61 * NULL iff init != 0 in the BIO. */
62 /* This is for what we write (i.e. reading uses peer's struct): */
63 int closed; /* valid iff peer != NULL */
64 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
65 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
67 char *buf; /* "size" elements (if != NULL) */
68 size_t request; /* valid iff peer != NULL; 0 if len != 0,
69 * otherwise set by peer to number of bytes
70 * it (unsuccessfully) tried to read, never
71 * more than buffer space (size-len)
75 static int bio_new(BIO *bio)
77 struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
82 /* enough for one TLS record (just a default) */
89 static int bio_free(BIO *bio)
100 bio_destroy_pair(bio);
102 OPENSSL_free(b->buf);
108 static int bio_read(BIO *bio, char *buf, int size_)
112 struct bio_bio_st *b, *peer_b;
114 BIO_clear_retry_flags(bio);
121 assert(b->peer != NULL);
122 peer_b = b->peer->ptr;
123 assert(peer_b != NULL);
124 assert(peer_b->buf != NULL);
126 peer_b->request = 0; /* will be set in "retry_read" situation */
128 if (buf == NULL || size == 0)
131 if (peer_b->len == 0) {
133 return 0; /* writer has closed, and no data is left */
135 BIO_set_retry_read(bio); /* buffer is empty */
136 if (size <= peer_b->size)
137 peer_b->request = size;
140 * don't ask for more than the peer can deliver in one write
142 peer_b->request = peer_b->size;
148 if (peer_b->len < size)
151 /* now read "size" bytes */
156 do { /* one or two iterations */
159 assert(rest <= peer_b->len);
160 if (peer_b->offset + rest <= peer_b->size)
163 /* wrap around ring buffer */
164 chunk = peer_b->size - peer_b->offset;
165 assert(peer_b->offset + chunk <= peer_b->size);
167 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
169 peer_b->len -= chunk;
171 peer_b->offset += chunk;
172 assert(peer_b->offset <= peer_b->size);
173 if (peer_b->offset == peer_b->size)
177 /* buffer now empty, no need to advance "buf" */
178 assert(chunk == rest);
189 * non-copying interface: provide pointer to available data in buffer
190 * bio_nread0: return number of available bytes
191 * bio_nread: also advance index
192 * (example usage: bio_nread0(), read from buffer, bio_nread()
193 * or just bio_nread(), read from buffer)
196 * WARNING: The non-copying interface is largely untested as of yet and may
199 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
201 struct bio_bio_st *b, *peer_b;
204 BIO_clear_retry_flags(bio);
211 assert(b->peer != NULL);
212 peer_b = b->peer->ptr;
213 assert(peer_b != NULL);
214 assert(peer_b->buf != NULL);
218 if (peer_b->len == 0) {
221 /* avoid code duplication -- nothing available for reading */
222 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
226 if (peer_b->size < peer_b->offset + num)
227 /* no ring buffer wrap-around for non-copying interface */
228 num = peer_b->size - peer_b->offset;
232 *buf = peer_b->buf + peer_b->offset;
236 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
238 struct bio_bio_st *b, *peer_b;
239 ossl_ssize_t num, available;
241 if (num_ > OSSL_SSIZE_MAX)
242 num = OSSL_SSIZE_MAX;
244 num = (ossl_ssize_t) num_;
246 available = bio_nread0(bio, buf);
253 peer_b = b->peer->ptr;
257 peer_b->offset += num;
258 assert(peer_b->offset <= peer_b->size);
259 if (peer_b->offset == peer_b->size)
267 static int bio_write(BIO *bio, const char *buf, int num_)
271 struct bio_bio_st *b;
273 BIO_clear_retry_flags(bio);
275 if (!bio->init || buf == NULL || num == 0)
280 assert(b->peer != NULL);
281 assert(b->buf != NULL);
285 /* we already closed */
286 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
290 assert(b->len <= b->size);
292 if (b->len == b->size) {
293 BIO_set_retry_write(bio); /* buffer is full */
298 if (num > b->size - b->len)
299 num = b->size - b->len;
301 /* now write "num" bytes */
306 do { /* one or two iterations */
310 assert(b->len + rest <= b->size);
312 write_offset = b->offset + b->len;
313 if (write_offset >= b->size)
314 write_offset -= b->size;
315 /* b->buf[write_offset] is the first byte we can write to. */
317 if (write_offset + rest <= b->size)
320 /* wrap around ring buffer */
321 chunk = b->size - write_offset;
323 memcpy(b->buf + write_offset, buf, chunk);
327 assert(b->len <= b->size);
338 * non-copying interface: provide pointer to region to write to
339 * bio_nwrite0: check how much space is available
340 * bio_nwrite: also increase length
341 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
342 * or just bio_nwrite(), write to buffer)
344 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
346 struct bio_bio_st *b;
350 BIO_clear_retry_flags(bio);
357 assert(b->peer != NULL);
358 assert(b->buf != NULL);
362 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
366 assert(b->len <= b->size);
368 if (b->len == b->size) {
369 BIO_set_retry_write(bio);
373 num = b->size - b->len;
374 write_offset = b->offset + b->len;
375 if (write_offset >= b->size)
376 write_offset -= b->size;
377 if (write_offset + num > b->size)
379 * no ring buffer wrap-around for non-copying interface (to fulfil
380 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
381 * to be called twice)
383 num = b->size - write_offset;
386 *buf = b->buf + write_offset;
387 assert(write_offset + num <= b->size);
392 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
394 struct bio_bio_st *b;
395 ossl_ssize_t num, space;
397 if (num_ > OSSL_SSIZE_MAX)
398 num = OSSL_SSIZE_MAX;
400 num = (ossl_ssize_t) num_;
402 space = bio_nwrite0(bio, buf);
410 assert(b->len <= b->size);
415 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
418 struct bio_bio_st *b = bio->ptr;
423 /* specific CTRL codes */
425 case BIO_C_SET_WRITE_BUF_SIZE:
427 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
429 } else if (num == 0) {
430 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
433 size_t new_size = num;
435 if (b->size != new_size) {
436 OPENSSL_free(b->buf);
444 case BIO_C_GET_WRITE_BUF_SIZE:
448 case BIO_C_MAKE_BIO_PAIR:
450 BIO *other_bio = ptr;
452 if (bio_make_pair(bio, other_bio))
459 case BIO_C_DESTROY_BIO_PAIR:
461 * Affects both BIOs in the pair -- call just once! Or let
462 * BIO_free(bio1); BIO_free(bio2); do the job.
464 bio_destroy_pair(bio);
468 case BIO_C_GET_WRITE_GUARANTEE:
470 * How many bytes can the caller feed to the next write without
471 * having to keep any?
473 if (b->peer == NULL || b->closed)
476 ret = (long)b->size - b->len;
479 case BIO_C_GET_READ_REQUEST:
481 * If the peer unsuccessfully tried to read, how many bytes were
482 * requested? (As with BIO_CTRL_PENDING, that number can usually be
483 * treated as boolean.)
485 ret = (long)b->request;
488 case BIO_C_RESET_READ_REQUEST:
490 * Reset request. (Can be useful after read attempts at the other
491 * side that are meant to be non-blocking, e.g. when probing SSL_read
492 * to see if any data is available.)
498 case BIO_C_SHUTDOWN_WR:
499 /* similar to shutdown(..., SHUT_WR) */
505 /* prepare for non-copying read */
506 ret = (long)bio_nread0(bio, ptr);
510 /* non-copying read */
511 ret = (long)bio_nread(bio, ptr, (size_t)num);
515 /* prepare for non-copying write */
516 ret = (long)bio_nwrite0(bio, ptr);
520 /* non-copying write */
521 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
524 /* standard CTRL codes follow */
527 if (b->buf != NULL) {
534 case BIO_CTRL_GET_CLOSE:
538 case BIO_CTRL_SET_CLOSE:
539 bio->shutdown = (int)num;
543 case BIO_CTRL_PENDING:
544 if (b->peer != NULL) {
545 struct bio_bio_st *peer_b = b->peer->ptr;
547 ret = (long)peer_b->len;
552 case BIO_CTRL_WPENDING:
560 /* See BIO_dup_chain for circumstances we have to expect. */
562 BIO *other_bio = ptr;
563 struct bio_bio_st *other_b;
565 assert(other_bio != NULL);
566 other_b = other_bio->ptr;
567 assert(other_b != NULL);
569 assert(other_b->buf == NULL); /* other_bio is always fresh */
571 other_b->size = b->size;
582 if (b->peer != NULL) {
583 struct bio_bio_st *peer_b = b->peer->ptr;
585 if (peer_b->len == 0 && peer_b->closed)
600 static int bio_puts(BIO *bio, const char *str)
602 return bio_write(bio, str, strlen(str));
605 static int bio_make_pair(BIO *bio1, BIO *bio2)
607 struct bio_bio_st *b1, *b2;
609 assert(bio1 != NULL);
610 assert(bio2 != NULL);
615 if (b1->peer != NULL || b2->peer != NULL) {
616 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
620 if (b1->buf == NULL) {
621 b1->buf = OPENSSL_malloc(b1->size);
622 if (b1->buf == NULL) {
623 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
630 if (b2->buf == NULL) {
631 b2->buf = OPENSSL_malloc(b2->size);
632 if (b2->buf == NULL) {
633 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
653 static void bio_destroy_pair(BIO *bio)
655 struct bio_bio_st *b = bio->ptr;
658 BIO *peer_bio = b->peer;
660 if (peer_bio != NULL) {
661 struct bio_bio_st *peer_b = peer_bio->ptr;
663 assert(peer_b != NULL);
664 assert(peer_b->peer == bio);
668 assert(peer_b->buf != NULL);
674 assert(b->buf != NULL);
681 /* Exported convenience functions */
682 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
683 BIO **bio2_p, size_t writebuf2)
685 BIO *bio1 = NULL, *bio2 = NULL;
689 bio1 = BIO_new(BIO_s_bio());
692 bio2 = BIO_new(BIO_s_bio());
697 r = BIO_set_write_buf_size(bio1, writebuf1);
702 r = BIO_set_write_buf_size(bio2, writebuf2);
707 r = BIO_make_bio_pair(bio1, bio2);
725 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
727 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
730 size_t BIO_ctrl_get_read_request(BIO *bio)
732 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
735 int BIO_ctrl_reset_read_request(BIO *bio)
737 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
741 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
742 * (conceivably some other BIOs could allow non-copying reads and writes
745 int BIO_nread0(BIO *bio, char **buf)
750 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
754 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
761 int BIO_nread(BIO *bio, char **buf, int num)
766 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
770 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
772 bio->num_read += ret;
776 int BIO_nwrite0(BIO *bio, char **buf)
781 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
785 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
792 int BIO_nwrite(BIO *bio, char **buf, int num)
797 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
801 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
803 bio->num_write += ret;