1 /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
3 /* Special method for a BIO where the other endpoint is also a BIO
4 * of this kind, handled by the same thread (i.e. the "peer" is actually
5 * ourselves, wearing a different hat).
6 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
7 * for which no specific BIO method is available.
8 * See ssl/ssltest.c for some hints on how this can be used. */
10 #ifndef BIO_PAIR_DEBUG
11 # undef NDEBUG /* avoid conflicting definitions */
20 #include <openssl/bio.h>
21 #include <openssl/err.h>
22 #include <openssl/err.h>
23 #include <openssl/crypto.h>
25 #include "openssl/e_os.h"
27 # define SSIZE_MAX INT_MAX
30 static int bio_new(BIO *bio);
31 static int bio_free(BIO *bio);
32 static int bio_read(BIO *bio, char *buf, int size);
33 static int bio_write(BIO *bio, char *buf, int num);
34 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
35 static int bio_puts(BIO *bio, char *str);
37 static int bio_make_pair(BIO *bio1, BIO *bio2);
38 static void bio_destroy_pair(BIO *bio);
40 static BIO_METHOD methods_biop =
47 NULL /* no bio_gets */,
51 NULL /* no bio_callback_ctrl */
54 BIO_METHOD *BIO_s_bio(void)
61 BIO *peer; /* NULL if buf == NULL.
62 * If peer != NULL, then peer->ptr is also a bio_bio_st,
63 * and its "peer" member points back to us.
64 * peer != 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) */
73 size_t request; /* valid iff peer != NULL; 0 if len != 0,
74 * otherwise set by peer to number of bytes
75 * it (unsuccessfully) tried to read,
76 * never more than buffer space (size-len) warrants. */
79 static int bio_new(BIO *bio)
83 b = Malloc(sizeof *b);
88 b->size = 17*1024; /* enough for one TLS record (just a default) */
96 static int bio_free(BIO *bio)
107 bio_destroy_pair(bio);
121 static int bio_read(BIO *bio, char *buf, int size_)
125 struct bio_bio_st *b, *peer_b;
127 BIO_clear_retry_flags(bio);
134 assert(b->peer != NULL);
135 peer_b = b->peer->ptr;
136 assert(peer_b != NULL);
137 assert(peer_b->buf != NULL);
139 peer_b->request = 0; /* will be set in "retry_read" situation */
141 if (buf == NULL || size == 0)
144 if (peer_b->len == 0)
147 return 0; /* writer has closed, and no data is left */
150 BIO_set_retry_read(bio); /* buffer is empty */
151 if (size <= peer_b->size)
152 peer_b->request = size;
154 /* don't ask for more than the peer can
155 * deliver in one write */
156 peer_b->request = peer_b->size;
162 if (peer_b->len < size)
165 /* now read "size" bytes */
170 do /* one or two iterations */
174 assert(rest <= peer_b->len);
175 if (peer_b->offset + rest <= peer_b->size)
178 /* wrap around ring buffer */
179 chunk = peer_b->size - peer_b->offset;
180 assert(peer_b->offset + chunk <= peer_b->size);
182 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
184 peer_b->len -= chunk;
187 peer_b->offset += chunk;
188 assert(peer_b->offset <= peer_b->size);
189 if (peer_b->offset == peer_b->size)
195 /* buffer now empty, no need to advance "buf" */
196 assert(chunk == rest);
206 /* non-copying interface: provide pointer to available data in buffer
207 * bio_nread0: return number of available bytes
208 * bio_nread: also advance index
209 * (example usage: bio_nread0(), read from buffer, bio_nread()
210 * or just bio_nread(), read from buffer)
212 /* WARNING: The non-copying interface is largely untested as of yet
213 * and may contain bugs. */
214 static ssize_t bio_nread0(BIO *bio, char **buf)
216 struct bio_bio_st *b, *peer_b;
219 BIO_clear_retry_flags(bio);
226 assert(b->peer != NULL);
227 peer_b = b->peer->ptr;
228 assert(peer_b != NULL);
229 assert(peer_b->buf != NULL);
233 if (peer_b->len == 0)
237 /* avoid code duplication -- nothing available for reading */
238 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
242 if (peer_b->size < peer_b->offset + num)
243 /* no ring buffer wrap-around for non-copying interface */
244 num = peer_b->size - peer_b->offset;
248 *buf = peer_b->buf + peer_b->offset;
252 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
254 struct bio_bio_st *b, *peer_b;
255 ssize_t num, available;
257 if (num_ > SSIZE_MAX)
262 available = bio_nread0(bio, buf);
269 peer_b = b->peer->ptr;
274 peer_b->offset += num;
275 assert(peer_b->offset <= peer_b->size);
276 if (peer_b->offset == peer_b->size)
286 static int bio_write(BIO *bio, char *buf, int num_)
290 struct bio_bio_st *b;
292 BIO_clear_retry_flags(bio);
294 if (!bio->init || buf == NULL || num == 0)
299 assert(b->peer != NULL);
300 assert(b->buf != NULL);
305 /* we already closed */
306 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
310 assert(b->len <= b->size);
312 if (b->len == b->size)
314 BIO_set_retry_write(bio); /* buffer is full */
319 if (num > b->size - b->len)
320 num = b->size - b->len;
322 /* now write "num" bytes */
327 do /* one or two iterations */
332 assert(b->len + rest <= b->size);
334 write_offset = b->offset + b->len;
335 if (write_offset >= b->size)
336 write_offset -= b->size;
337 /* b->buf[write_offset] is the first byte we can write to. */
339 if (write_offset + rest <= b->size)
342 /* wrap around ring buffer */
343 chunk = b->size - write_offset;
345 memcpy(b->buf + write_offset, buf, chunk);
349 assert(b->len <= b->size);
359 /* non-copying interface: provide pointer to region to write to
360 * bio_nwrite0: check how much space is available
361 * bio_nwrite: also increase length
362 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
363 * or just bio_nwrite(), write to buffer)
365 static ssize_t bio_nwrite0(BIO *bio, char **buf)
367 struct bio_bio_st *b;
371 BIO_clear_retry_flags(bio);
378 assert(b->peer != NULL);
379 assert(b->buf != NULL);
384 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
388 assert(b->len <= b->size);
390 if (b->len == b->size)
392 BIO_set_retry_write(bio);
396 num = b->size - b->len;
397 write_offset = b->offset + b->len;
398 if (write_offset >= b->size)
399 write_offset -= b->size;
400 if (write_offset + num > b->size)
401 /* no ring buffer wrap-around for non-copying interface
402 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
403 * BIO_nwrite may have to be called twice) */
404 num = b->size - write_offset;
407 *buf = b->buf + write_offset;
408 assert(write_offset + num <= b->size);
413 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
415 struct bio_bio_st *b;
418 if (num_ > SSIZE_MAX)
423 space = bio_nwrite0(bio, buf);
431 assert(b->len <= b->size);
437 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
440 struct bio_bio_st *b = bio->ptr;
446 /* specific CTRL codes */
448 case BIO_C_SET_WRITE_BUF_SIZE:
451 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
456 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
461 size_t new_size = num;
463 if (b->size != new_size)
476 case BIO_C_GET_WRITE_BUF_SIZE:
477 num = (long) b->size;
479 case BIO_C_MAKE_BIO_PAIR:
481 BIO *other_bio = ptr;
483 if (bio_make_pair(bio, other_bio))
490 case BIO_C_DESTROY_BIO_PAIR:
491 /* Effects both BIOs in the pair -- call just once!
492 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
493 bio_destroy_pair(bio);
497 case BIO_C_GET_WRITE_GUARANTEE:
498 /* How many bytes can the caller feed to the next write
499 * without having to keep any? */
500 if (b->peer == NULL || b->closed)
503 ret = (long) b->size - b->len;
506 case BIO_C_GET_READ_REQUEST:
507 /* If the peer unsuccessfully tried to read, how many bytes
508 * were requested? (As with BIO_CTRL_PENDING, that number
509 * can usually be treated as boolean.) */
510 ret = (long) b->request;
513 case BIO_C_RESET_READ_REQUEST:
514 /* Reset request. (Can be useful after read attempts
515 * at the other side that are meant to be non-blocking,
516 * e.g. when probing SSL_read to see if any data is
522 case BIO_C_SHUTDOWN_WR:
523 /* similar to shutdown(..., SHUT_WR) */
529 /* prepare for non-copying read */
530 ret = (long) bio_nread0(bio, ptr);
534 /* non-copying read */
535 ret = (long) bio_nread(bio, ptr, (size_t) num);
539 /* prepare for non-copying write */
540 ret = (long) bio_nwrite0(bio, ptr);
544 /* non-copying write */
545 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
549 /* standard CTRL codes follow */
560 case BIO_CTRL_GET_CLOSE:
564 case BIO_CTRL_SET_CLOSE:
565 bio->shutdown = (int) num;
569 case BIO_CTRL_PENDING:
572 struct bio_bio_st *peer_b = b->peer->ptr;
574 ret = (long) peer_b->len;
580 case BIO_CTRL_WPENDING:
588 /* See BIO_dup_chain for circumstances we have to expect. */
590 BIO *other_bio = ptr;
591 struct bio_bio_st *other_b;
593 assert(other_bio != NULL);
594 other_b = other_bio->ptr;
595 assert(other_b != NULL);
597 assert(other_b->buf == NULL); /* other_bio is always fresh */
599 other_b->size = b->size;
611 BIO *other_bio = ptr;
615 struct bio_bio_st *other_b = other_bio->ptr;
617 assert(other_b != NULL);
618 ret = other_b->len == 0 && other_b->closed;
631 static int bio_puts(BIO *bio, char *str)
633 return bio_write(bio, str, strlen(str));
637 static int bio_make_pair(BIO *bio1, BIO *bio2)
639 struct bio_bio_st *b1, *b2;
641 assert(bio1 != NULL);
642 assert(bio2 != NULL);
647 if (b1->peer != NULL || b2->peer != NULL)
649 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
655 b1->buf = Malloc(b1->size);
658 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
667 b2->buf = Malloc(b2->size);
670 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
690 static void bio_destroy_pair(BIO *bio)
692 struct bio_bio_st *b = bio->ptr;
696 BIO *peer_bio = b->peer;
698 if (peer_bio != NULL)
700 struct bio_bio_st *peer_b = peer_bio->ptr;
702 assert(peer_b != NULL);
703 assert(peer_b->peer == bio);
707 assert(peer_b->buf != NULL);
713 assert(b->buf != NULL);
721 /* Exported convenience functions */
722 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
723 BIO **bio2_p, size_t writebuf2)
725 BIO *bio1 = NULL, *bio2 = NULL;
729 bio1 = BIO_new(BIO_s_bio());
732 bio2 = BIO_new(BIO_s_bio());
738 r = BIO_set_write_buf_size(bio1, writebuf1);
744 r = BIO_set_write_buf_size(bio2, writebuf2);
749 r = BIO_make_bio_pair(bio1, bio2);
774 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
776 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
779 size_t BIO_ctrl_get_read_request(BIO *bio)
781 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
784 int BIO_ctrl_reset_read_request(BIO *bio)
786 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
790 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
791 * (conceivably some other BIOs could allow non-copying reads and writes too.)
793 int BIO_nread0(BIO *bio, char **buf)
799 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
803 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
810 int BIO_nread(BIO *bio, char **buf, int num)
816 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
820 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
822 bio->num_read += ret;
826 int BIO_nwrite0(BIO *bio, char **buf)
832 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
836 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
843 int BIO_nwrite(BIO *bio, char **buf, int num)
849 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
853 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
855 bio->num_read += ret;