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 = {
42 /* TODO: Convert to new style write function */
45 /* TODO: Convert to new style read function */
49 NULL /* no bio_gets */ ,
53 NULL /* no bio_callback_ctrl */
56 const BIO_METHOD *BIO_s_bio(void)
62 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
63 * peer->ptr is also a bio_bio_st, and its
64 * "peer" member points back to us. peer !=
65 * NULL iff init != 0 in the BIO. */
66 /* This is for what we write (i.e. reading uses peer's struct): */
67 int closed; /* valid iff peer != NULL */
68 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
69 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
71 char *buf; /* "size" elements (if != NULL) */
72 size_t request; /* valid iff peer != NULL; 0 if len != 0,
73 * otherwise set by peer to number of bytes
74 * it (unsuccessfully) tried to read, never
75 * more than buffer space (size-len)
79 static int bio_new(BIO *bio)
81 struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
86 /* enough for one TLS record (just a default) */
93 static int bio_free(BIO *bio)
104 bio_destroy_pair(bio);
106 OPENSSL_free(b->buf);
112 static int bio_read(BIO *bio, char *buf, int size_)
116 struct bio_bio_st *b, *peer_b;
118 BIO_clear_retry_flags(bio);
125 assert(b->peer != NULL);
126 peer_b = b->peer->ptr;
127 assert(peer_b != NULL);
128 assert(peer_b->buf != NULL);
130 peer_b->request = 0; /* will be set in "retry_read" situation */
132 if (buf == NULL || size == 0)
135 if (peer_b->len == 0) {
137 return 0; /* writer has closed, and no data is left */
139 BIO_set_retry_read(bio); /* buffer is empty */
140 if (size <= peer_b->size)
141 peer_b->request = size;
144 * don't ask for more than the peer can deliver in one write
146 peer_b->request = peer_b->size;
152 if (peer_b->len < size)
155 /* now read "size" bytes */
160 do { /* one or two iterations */
163 assert(rest <= peer_b->len);
164 if (peer_b->offset + rest <= peer_b->size)
167 /* wrap around ring buffer */
168 chunk = peer_b->size - peer_b->offset;
169 assert(peer_b->offset + chunk <= peer_b->size);
171 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
173 peer_b->len -= chunk;
175 peer_b->offset += chunk;
176 assert(peer_b->offset <= peer_b->size);
177 if (peer_b->offset == peer_b->size)
181 /* buffer now empty, no need to advance "buf" */
182 assert(chunk == rest);
193 * non-copying interface: provide pointer to available data in buffer
194 * bio_nread0: return number of available bytes
195 * bio_nread: also advance index
196 * (example usage: bio_nread0(), read from buffer, bio_nread()
197 * or just bio_nread(), read from buffer)
200 * WARNING: The non-copying interface is largely untested as of yet and may
203 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
205 struct bio_bio_st *b, *peer_b;
208 BIO_clear_retry_flags(bio);
215 assert(b->peer != NULL);
216 peer_b = b->peer->ptr;
217 assert(peer_b != NULL);
218 assert(peer_b->buf != NULL);
222 if (peer_b->len == 0) {
225 /* avoid code duplication -- nothing available for reading */
226 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
230 if (peer_b->size < peer_b->offset + num)
231 /* no ring buffer wrap-around for non-copying interface */
232 num = peer_b->size - peer_b->offset;
236 *buf = peer_b->buf + peer_b->offset;
240 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
242 struct bio_bio_st *b, *peer_b;
243 ossl_ssize_t num, available;
245 if (num_ > OSSL_SSIZE_MAX)
246 num = OSSL_SSIZE_MAX;
248 num = (ossl_ssize_t) num_;
250 available = bio_nread0(bio, buf);
257 peer_b = b->peer->ptr;
261 peer_b->offset += num;
262 assert(peer_b->offset <= peer_b->size);
263 if (peer_b->offset == peer_b->size)
271 static int bio_write(BIO *bio, const char *buf, int num_)
275 struct bio_bio_st *b;
277 BIO_clear_retry_flags(bio);
279 if (!bio->init || buf == NULL || num == 0)
284 assert(b->peer != NULL);
285 assert(b->buf != NULL);
289 /* we already closed */
290 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
294 assert(b->len <= b->size);
296 if (b->len == b->size) {
297 BIO_set_retry_write(bio); /* buffer is full */
302 if (num > b->size - b->len)
303 num = b->size - b->len;
305 /* now write "num" bytes */
310 do { /* one or two iterations */
314 assert(b->len + rest <= b->size);
316 write_offset = b->offset + b->len;
317 if (write_offset >= b->size)
318 write_offset -= b->size;
319 /* b->buf[write_offset] is the first byte we can write to. */
321 if (write_offset + rest <= b->size)
324 /* wrap around ring buffer */
325 chunk = b->size - write_offset;
327 memcpy(b->buf + write_offset, buf, chunk);
331 assert(b->len <= b->size);
342 * non-copying interface: provide pointer to region to write to
343 * bio_nwrite0: check how much space is available
344 * bio_nwrite: also increase length
345 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
346 * or just bio_nwrite(), write to buffer)
348 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
350 struct bio_bio_st *b;
354 BIO_clear_retry_flags(bio);
361 assert(b->peer != NULL);
362 assert(b->buf != NULL);
366 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
370 assert(b->len <= b->size);
372 if (b->len == b->size) {
373 BIO_set_retry_write(bio);
377 num = b->size - b->len;
378 write_offset = b->offset + b->len;
379 if (write_offset >= b->size)
380 write_offset -= b->size;
381 if (write_offset + num > b->size)
383 * no ring buffer wrap-around for non-copying interface (to fulfil
384 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
385 * to be called twice)
387 num = b->size - write_offset;
390 *buf = b->buf + write_offset;
391 assert(write_offset + num <= b->size);
396 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
398 struct bio_bio_st *b;
399 ossl_ssize_t num, space;
401 if (num_ > OSSL_SSIZE_MAX)
402 num = OSSL_SSIZE_MAX;
404 num = (ossl_ssize_t) num_;
406 space = bio_nwrite0(bio, buf);
414 assert(b->len <= b->size);
419 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
422 struct bio_bio_st *b = bio->ptr;
427 /* specific CTRL codes */
429 case BIO_C_SET_WRITE_BUF_SIZE:
431 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
433 } else if (num == 0) {
434 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
437 size_t new_size = num;
439 if (b->size != new_size) {
440 OPENSSL_free(b->buf);
448 case BIO_C_GET_WRITE_BUF_SIZE:
452 case BIO_C_MAKE_BIO_PAIR:
454 BIO *other_bio = ptr;
456 if (bio_make_pair(bio, other_bio))
463 case BIO_C_DESTROY_BIO_PAIR:
465 * Affects both BIOs in the pair -- call just once! Or let
466 * BIO_free(bio1); BIO_free(bio2); do the job.
468 bio_destroy_pair(bio);
472 case BIO_C_GET_WRITE_GUARANTEE:
474 * How many bytes can the caller feed to the next write without
475 * having to keep any?
477 if (b->peer == NULL || b->closed)
480 ret = (long)b->size - b->len;
483 case BIO_C_GET_READ_REQUEST:
485 * If the peer unsuccessfully tried to read, how many bytes were
486 * requested? (As with BIO_CTRL_PENDING, that number can usually be
487 * treated as boolean.)
489 ret = (long)b->request;
492 case BIO_C_RESET_READ_REQUEST:
494 * Reset request. (Can be useful after read attempts at the other
495 * side that are meant to be non-blocking, e.g. when probing SSL_read
496 * to see if any data is available.)
502 case BIO_C_SHUTDOWN_WR:
503 /* similar to shutdown(..., SHUT_WR) */
509 /* prepare for non-copying read */
510 ret = (long)bio_nread0(bio, ptr);
514 /* non-copying read */
515 ret = (long)bio_nread(bio, ptr, (size_t)num);
519 /* prepare for non-copying write */
520 ret = (long)bio_nwrite0(bio, ptr);
524 /* non-copying write */
525 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
528 /* standard CTRL codes follow */
531 if (b->buf != NULL) {
538 case BIO_CTRL_GET_CLOSE:
542 case BIO_CTRL_SET_CLOSE:
543 bio->shutdown = (int)num;
547 case BIO_CTRL_PENDING:
548 if (b->peer != NULL) {
549 struct bio_bio_st *peer_b = b->peer->ptr;
551 ret = (long)peer_b->len;
556 case BIO_CTRL_WPENDING:
564 /* See BIO_dup_chain for circumstances we have to expect. */
566 BIO *other_bio = ptr;
567 struct bio_bio_st *other_b;
569 assert(other_bio != NULL);
570 other_b = other_bio->ptr;
571 assert(other_b != NULL);
573 assert(other_b->buf == NULL); /* other_bio is always fresh */
575 other_b->size = b->size;
586 if (b->peer != NULL) {
587 struct bio_bio_st *peer_b = b->peer->ptr;
589 if (peer_b->len == 0 && peer_b->closed)
604 static int bio_puts(BIO *bio, const char *str)
606 return bio_write(bio, str, strlen(str));
609 static int bio_make_pair(BIO *bio1, BIO *bio2)
611 struct bio_bio_st *b1, *b2;
613 assert(bio1 != NULL);
614 assert(bio2 != NULL);
619 if (b1->peer != NULL || b2->peer != NULL) {
620 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
624 if (b1->buf == NULL) {
625 b1->buf = OPENSSL_malloc(b1->size);
626 if (b1->buf == NULL) {
627 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
634 if (b2->buf == NULL) {
635 b2->buf = OPENSSL_malloc(b2->size);
636 if (b2->buf == NULL) {
637 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
657 static void bio_destroy_pair(BIO *bio)
659 struct bio_bio_st *b = bio->ptr;
662 BIO *peer_bio = b->peer;
664 if (peer_bio != NULL) {
665 struct bio_bio_st *peer_b = peer_bio->ptr;
667 assert(peer_b != NULL);
668 assert(peer_b->peer == bio);
672 assert(peer_b->buf != NULL);
678 assert(b->buf != NULL);
685 /* Exported convenience functions */
686 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
687 BIO **bio2_p, size_t writebuf2)
689 BIO *bio1 = NULL, *bio2 = NULL;
693 bio1 = BIO_new(BIO_s_bio());
696 bio2 = BIO_new(BIO_s_bio());
701 r = BIO_set_write_buf_size(bio1, writebuf1);
706 r = BIO_set_write_buf_size(bio2, writebuf2);
711 r = BIO_make_bio_pair(bio1, bio2);
729 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
731 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
734 size_t BIO_ctrl_get_read_request(BIO *bio)
736 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
739 int BIO_ctrl_reset_read_request(BIO *bio)
741 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
745 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
746 * (conceivably some other BIOs could allow non-copying reads and writes
749 int BIO_nread0(BIO *bio, char **buf)
754 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
758 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
765 int BIO_nread(BIO *bio, char **buf, int num)
770 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
774 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
776 bio->num_read += ret;
780 int BIO_nwrite0(BIO *bio, char **buf)
785 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
789 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
796 int BIO_nwrite(BIO *bio, char **buf, int num)
801 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
805 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
807 bio->num_write += ret;