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)
79 b = OPENSSL_malloc(sizeof(*b));
87 /* enough for one TLS record (just a default) */
96 static int bio_free(BIO *bio)
107 bio_destroy_pair(bio);
109 OPENSSL_free(b->buf);
115 static int bio_read(BIO *bio, char *buf, int size_)
119 struct bio_bio_st *b, *peer_b;
121 BIO_clear_retry_flags(bio);
128 assert(b->peer != NULL);
129 peer_b = b->peer->ptr;
130 assert(peer_b != NULL);
131 assert(peer_b->buf != NULL);
133 peer_b->request = 0; /* will be set in "retry_read" situation */
135 if (buf == NULL || size == 0)
138 if (peer_b->len == 0) {
140 return 0; /* writer has closed, and no data is left */
142 BIO_set_retry_read(bio); /* buffer is empty */
143 if (size <= peer_b->size)
144 peer_b->request = size;
147 * don't ask for more than the peer can deliver in one write
149 peer_b->request = peer_b->size;
155 if (peer_b->len < size)
158 /* now read "size" bytes */
163 do { /* one or two iterations */
166 assert(rest <= peer_b->len);
167 if (peer_b->offset + rest <= peer_b->size)
170 /* wrap around ring buffer */
171 chunk = peer_b->size - peer_b->offset;
172 assert(peer_b->offset + chunk <= peer_b->size);
174 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
176 peer_b->len -= chunk;
178 peer_b->offset += chunk;
179 assert(peer_b->offset <= peer_b->size);
180 if (peer_b->offset == peer_b->size)
184 /* buffer now empty, no need to advance "buf" */
185 assert(chunk == rest);
196 * non-copying interface: provide pointer to available data in buffer
197 * bio_nread0: return number of available bytes
198 * bio_nread: also advance index
199 * (example usage: bio_nread0(), read from buffer, bio_nread()
200 * or just bio_nread(), read from buffer)
203 * WARNING: The non-copying interface is largely untested as of yet and may
206 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
208 struct bio_bio_st *b, *peer_b;
211 BIO_clear_retry_flags(bio);
218 assert(b->peer != NULL);
219 peer_b = b->peer->ptr;
220 assert(peer_b != NULL);
221 assert(peer_b->buf != NULL);
225 if (peer_b->len == 0) {
228 /* avoid code duplication -- nothing available for reading */
229 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
233 if (peer_b->size < peer_b->offset + num)
234 /* no ring buffer wrap-around for non-copying interface */
235 num = peer_b->size - peer_b->offset;
239 *buf = peer_b->buf + peer_b->offset;
243 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
245 struct bio_bio_st *b, *peer_b;
246 ossl_ssize_t num, available;
248 if (num_ > OSSL_SSIZE_MAX)
249 num = OSSL_SSIZE_MAX;
251 num = (ossl_ssize_t) num_;
253 available = bio_nread0(bio, buf);
260 peer_b = b->peer->ptr;
264 peer_b->offset += num;
265 assert(peer_b->offset <= peer_b->size);
266 if (peer_b->offset == peer_b->size)
274 static int bio_write(BIO *bio, const char *buf, int num_)
278 struct bio_bio_st *b;
280 BIO_clear_retry_flags(bio);
282 if (!bio->init || buf == NULL || num == 0)
287 assert(b->peer != NULL);
288 assert(b->buf != NULL);
292 /* we already closed */
293 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
297 assert(b->len <= b->size);
299 if (b->len == b->size) {
300 BIO_set_retry_write(bio); /* buffer is full */
305 if (num > b->size - b->len)
306 num = b->size - b->len;
308 /* now write "num" bytes */
313 do { /* one or two iterations */
317 assert(b->len + rest <= b->size);
319 write_offset = b->offset + b->len;
320 if (write_offset >= b->size)
321 write_offset -= b->size;
322 /* b->buf[write_offset] is the first byte we can write to. */
324 if (write_offset + rest <= b->size)
327 /* wrap around ring buffer */
328 chunk = b->size - write_offset;
330 memcpy(b->buf + write_offset, buf, chunk);
334 assert(b->len <= b->size);
345 * non-copying interface: provide pointer to region to write to
346 * bio_nwrite0: check how much space is available
347 * bio_nwrite: also increase length
348 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
349 * or just bio_nwrite(), write to buffer)
351 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
353 struct bio_bio_st *b;
357 BIO_clear_retry_flags(bio);
364 assert(b->peer != NULL);
365 assert(b->buf != NULL);
369 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
373 assert(b->len <= b->size);
375 if (b->len == b->size) {
376 BIO_set_retry_write(bio);
380 num = b->size - b->len;
381 write_offset = b->offset + b->len;
382 if (write_offset >= b->size)
383 write_offset -= b->size;
384 if (write_offset + num > b->size)
386 * no ring buffer wrap-around for non-copying interface (to fulfil
387 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
388 * to be called twice)
390 num = b->size - write_offset;
393 *buf = b->buf + write_offset;
394 assert(write_offset + num <= b->size);
399 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
401 struct bio_bio_st *b;
402 ossl_ssize_t num, space;
404 if (num_ > OSSL_SSIZE_MAX)
405 num = OSSL_SSIZE_MAX;
407 num = (ossl_ssize_t) num_;
409 space = bio_nwrite0(bio, buf);
417 assert(b->len <= b->size);
422 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
425 struct bio_bio_st *b = bio->ptr;
430 /* specific CTRL codes */
432 case BIO_C_SET_WRITE_BUF_SIZE:
434 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
436 } else if (num == 0) {
437 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
440 size_t new_size = num;
442 if (b->size != new_size) {
443 OPENSSL_free(b->buf);
451 case BIO_C_GET_WRITE_BUF_SIZE:
455 case BIO_C_MAKE_BIO_PAIR:
457 BIO *other_bio = ptr;
459 if (bio_make_pair(bio, other_bio))
466 case BIO_C_DESTROY_BIO_PAIR:
468 * Affects both BIOs in the pair -- call just once! Or let
469 * BIO_free(bio1); BIO_free(bio2); do the job.
471 bio_destroy_pair(bio);
475 case BIO_C_GET_WRITE_GUARANTEE:
477 * How many bytes can the caller feed to the next write without
478 * having to keep any?
480 if (b->peer == NULL || b->closed)
483 ret = (long)b->size - b->len;
486 case BIO_C_GET_READ_REQUEST:
488 * If the peer unsuccessfully tried to read, how many bytes were
489 * requested? (As with BIO_CTRL_PENDING, that number can usually be
490 * treated as boolean.)
492 ret = (long)b->request;
495 case BIO_C_RESET_READ_REQUEST:
497 * Reset request. (Can be useful after read attempts at the other
498 * side that are meant to be non-blocking, e.g. when probing SSL_read
499 * to see if any data is available.)
505 case BIO_C_SHUTDOWN_WR:
506 /* similar to shutdown(..., SHUT_WR) */
512 /* prepare for non-copying read */
513 ret = (long)bio_nread0(bio, ptr);
517 /* non-copying read */
518 ret = (long)bio_nread(bio, ptr, (size_t)num);
522 /* prepare for non-copying write */
523 ret = (long)bio_nwrite0(bio, ptr);
527 /* non-copying write */
528 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
531 /* standard CTRL codes follow */
534 if (b->buf != NULL) {
541 case BIO_CTRL_GET_CLOSE:
545 case BIO_CTRL_SET_CLOSE:
546 bio->shutdown = (int)num;
550 case BIO_CTRL_PENDING:
551 if (b->peer != NULL) {
552 struct bio_bio_st *peer_b = b->peer->ptr;
554 ret = (long)peer_b->len;
559 case BIO_CTRL_WPENDING:
567 /* See BIO_dup_chain for circumstances we have to expect. */
569 BIO *other_bio = ptr;
570 struct bio_bio_st *other_b;
572 assert(other_bio != NULL);
573 other_b = other_bio->ptr;
574 assert(other_b != NULL);
576 assert(other_b->buf == NULL); /* other_bio is always fresh */
578 other_b->size = b->size;
589 if (b->peer != NULL) {
590 struct bio_bio_st *peer_b = b->peer->ptr;
592 if (peer_b->len == 0 && peer_b->closed)
607 static int bio_puts(BIO *bio, const char *str)
609 return bio_write(bio, str, strlen(str));
612 static int bio_make_pair(BIO *bio1, BIO *bio2)
614 struct bio_bio_st *b1, *b2;
616 assert(bio1 != NULL);
617 assert(bio2 != NULL);
622 if (b1->peer != NULL || b2->peer != NULL) {
623 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
627 if (b1->buf == NULL) {
628 b1->buf = OPENSSL_malloc(b1->size);
629 if (b1->buf == NULL) {
630 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
637 if (b2->buf == NULL) {
638 b2->buf = OPENSSL_malloc(b2->size);
639 if (b2->buf == NULL) {
640 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
660 static void bio_destroy_pair(BIO *bio)
662 struct bio_bio_st *b = bio->ptr;
665 BIO *peer_bio = b->peer;
667 if (peer_bio != NULL) {
668 struct bio_bio_st *peer_b = peer_bio->ptr;
670 assert(peer_b != NULL);
671 assert(peer_b->peer == bio);
675 assert(peer_b->buf != NULL);
681 assert(b->buf != NULL);
688 /* Exported convenience functions */
689 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
690 BIO **bio2_p, size_t writebuf2)
692 BIO *bio1 = NULL, *bio2 = NULL;
696 bio1 = BIO_new(BIO_s_bio());
699 bio2 = BIO_new(BIO_s_bio());
704 r = BIO_set_write_buf_size(bio1, writebuf1);
709 r = BIO_set_write_buf_size(bio2, writebuf2);
714 r = BIO_make_bio_pair(bio1, bio2);
732 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
734 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
737 size_t BIO_ctrl_get_read_request(BIO *bio)
739 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
742 int BIO_ctrl_reset_read_request(BIO *bio)
744 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
748 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
749 * (conceivably some other BIOs could allow non-copying reads and writes
752 int BIO_nread0(BIO *bio, char **buf)
757 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
761 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
768 int BIO_nread(BIO *bio, char **buf, int num)
773 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
777 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
779 bio->num_read += ret;
783 int BIO_nwrite0(BIO *bio, char **buf)
788 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
792 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
799 int BIO_nwrite(BIO *bio, char **buf, int num)
804 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
808 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
810 bio->num_write += ret;