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>
27 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
28 #if defined(OPENSSL_SYS_VSWORKS)
32 # define SSIZE_MAX INT_MAX
35 static int bio_new(BIO *bio);
36 static int bio_free(BIO *bio);
37 static int bio_read(BIO *bio, char *buf, int size);
38 static int bio_write(BIO *bio, const char *buf, int num);
39 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
40 static int bio_puts(BIO *bio, const char *str);
42 static int bio_make_pair(BIO *bio1, BIO *bio2);
43 static void bio_destroy_pair(BIO *bio);
45 static BIO_METHOD methods_biop =
52 NULL /* no bio_gets */,
56 NULL /* no bio_callback_ctrl */
59 BIO_METHOD *BIO_s_bio(void)
66 BIO *peer; /* NULL if buf == NULL.
67 * If peer != NULL, then peer->ptr is also a bio_bio_st,
68 * and its "peer" member points back to us.
69 * peer != NULL iff init != 0 in the BIO. */
71 /* This is for what we write (i.e. reading uses peer's struct): */
72 int closed; /* valid iff peer != NULL */
73 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
74 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
76 char *buf; /* "size" elements (if != NULL) */
78 size_t request; /* valid iff peer != NULL; 0 if len != 0,
79 * otherwise set by peer to number of bytes
80 * it (unsuccessfully) tried to read,
81 * never more than buffer space (size-len) warrants. */
84 static int bio_new(BIO *bio)
88 b = OPENSSL_malloc(sizeof *b);
93 b->size = 17*1024; /* enough for one TLS record (just a default) */
101 static int bio_free(BIO *bio)
103 struct bio_bio_st *b;
112 bio_destroy_pair(bio);
116 OPENSSL_free(b->buf);
126 static int bio_read(BIO *bio, char *buf, int size_)
130 struct bio_bio_st *b, *peer_b;
132 BIO_clear_retry_flags(bio);
139 assert(b->peer != NULL);
140 peer_b = b->peer->ptr;
141 assert(peer_b != NULL);
142 assert(peer_b->buf != NULL);
144 peer_b->request = 0; /* will be set in "retry_read" situation */
146 if (buf == NULL || size == 0)
149 if (peer_b->len == 0)
152 return 0; /* writer has closed, and no data is left */
155 BIO_set_retry_read(bio); /* buffer is empty */
156 if (size <= peer_b->size)
157 peer_b->request = size;
159 /* don't ask for more than the peer can
160 * deliver in one write */
161 peer_b->request = peer_b->size;
167 if (peer_b->len < size)
170 /* now read "size" bytes */
175 do /* one or two iterations */
179 assert(rest <= peer_b->len);
180 if (peer_b->offset + rest <= peer_b->size)
183 /* wrap around ring buffer */
184 chunk = peer_b->size - peer_b->offset;
185 assert(peer_b->offset + chunk <= peer_b->size);
187 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
189 peer_b->len -= chunk;
192 peer_b->offset += chunk;
193 assert(peer_b->offset <= peer_b->size);
194 if (peer_b->offset == peer_b->size)
200 /* buffer now empty, no need to advance "buf" */
201 assert(chunk == rest);
211 /* non-copying interface: provide pointer to available data in buffer
212 * bio_nread0: return number of available bytes
213 * bio_nread: also advance index
214 * (example usage: bio_nread0(), read from buffer, bio_nread()
215 * or just bio_nread(), read from buffer)
217 /* WARNING: The non-copying interface is largely untested as of yet
218 * and may contain bugs. */
219 static ssize_t bio_nread0(BIO *bio, char **buf)
221 struct bio_bio_st *b, *peer_b;
224 BIO_clear_retry_flags(bio);
231 assert(b->peer != NULL);
232 peer_b = b->peer->ptr;
233 assert(peer_b != NULL);
234 assert(peer_b->buf != NULL);
238 if (peer_b->len == 0)
242 /* avoid code duplication -- nothing available for reading */
243 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
247 if (peer_b->size < peer_b->offset + num)
248 /* no ring buffer wrap-around for non-copying interface */
249 num = peer_b->size - peer_b->offset;
253 *buf = peer_b->buf + peer_b->offset;
257 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
259 struct bio_bio_st *b, *peer_b;
260 ssize_t num, available;
262 if (num_ > SSIZE_MAX)
267 available = bio_nread0(bio, buf);
274 peer_b = b->peer->ptr;
279 peer_b->offset += num;
280 assert(peer_b->offset <= peer_b->size);
281 if (peer_b->offset == peer_b->size)
291 static int bio_write(BIO *bio, const char *buf, int num_)
295 struct bio_bio_st *b;
297 BIO_clear_retry_flags(bio);
299 if (!bio->init || buf == NULL || num == 0)
304 assert(b->peer != NULL);
305 assert(b->buf != NULL);
310 /* we already closed */
311 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
315 assert(b->len <= b->size);
317 if (b->len == b->size)
319 BIO_set_retry_write(bio); /* buffer is full */
324 if (num > b->size - b->len)
325 num = b->size - b->len;
327 /* now write "num" bytes */
332 do /* one or two iterations */
337 assert(b->len + rest <= b->size);
339 write_offset = b->offset + b->len;
340 if (write_offset >= b->size)
341 write_offset -= b->size;
342 /* b->buf[write_offset] is the first byte we can write to. */
344 if (write_offset + rest <= b->size)
347 /* wrap around ring buffer */
348 chunk = b->size - write_offset;
350 memcpy(b->buf + write_offset, buf, chunk);
354 assert(b->len <= b->size);
364 /* non-copying interface: provide pointer to region to write to
365 * bio_nwrite0: check how much space is available
366 * bio_nwrite: also increase length
367 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
368 * or just bio_nwrite(), write to buffer)
370 static ssize_t bio_nwrite0(BIO *bio, char **buf)
372 struct bio_bio_st *b;
376 BIO_clear_retry_flags(bio);
383 assert(b->peer != NULL);
384 assert(b->buf != NULL);
389 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
393 assert(b->len <= b->size);
395 if (b->len == b->size)
397 BIO_set_retry_write(bio);
401 num = b->size - b->len;
402 write_offset = b->offset + b->len;
403 if (write_offset >= b->size)
404 write_offset -= b->size;
405 if (write_offset + num > b->size)
406 /* no ring buffer wrap-around for non-copying interface
407 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
408 * BIO_nwrite may have to be called twice) */
409 num = b->size - write_offset;
412 *buf = b->buf + write_offset;
413 assert(write_offset + num <= b->size);
418 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
420 struct bio_bio_st *b;
423 if (num_ > SSIZE_MAX)
428 space = bio_nwrite0(bio, buf);
436 assert(b->len <= b->size);
442 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
445 struct bio_bio_st *b = bio->ptr;
451 /* specific CTRL codes */
453 case BIO_C_SET_WRITE_BUF_SIZE:
456 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
461 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
466 size_t new_size = num;
468 if (b->size != new_size)
472 OPENSSL_free(b->buf);
481 case BIO_C_GET_WRITE_BUF_SIZE:
482 ret = (long) b->size;
485 case BIO_C_MAKE_BIO_PAIR:
487 BIO *other_bio = ptr;
489 if (bio_make_pair(bio, other_bio))
496 case BIO_C_DESTROY_BIO_PAIR:
497 /* Effects both BIOs in the pair -- call just once!
498 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
499 bio_destroy_pair(bio);
503 case BIO_C_GET_WRITE_GUARANTEE:
504 /* How many bytes can the caller feed to the next write
505 * without having to keep any? */
506 if (b->peer == NULL || b->closed)
509 ret = (long) b->size - b->len;
512 case BIO_C_GET_READ_REQUEST:
513 /* If the peer unsuccessfully tried to read, how many bytes
514 * were requested? (As with BIO_CTRL_PENDING, that number
515 * can usually be treated as boolean.) */
516 ret = (long) b->request;
519 case BIO_C_RESET_READ_REQUEST:
520 /* Reset request. (Can be useful after read attempts
521 * at the other side that are meant to be non-blocking,
522 * e.g. when probing SSL_read to see if any data is
528 case BIO_C_SHUTDOWN_WR:
529 /* similar to shutdown(..., SHUT_WR) */
535 /* prepare for non-copying read */
536 ret = (long) bio_nread0(bio, ptr);
540 /* non-copying read */
541 ret = (long) bio_nread(bio, ptr, (size_t) num);
545 /* prepare for non-copying write */
546 ret = (long) bio_nwrite0(bio, ptr);
550 /* non-copying write */
551 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
555 /* standard CTRL codes follow */
566 case BIO_CTRL_GET_CLOSE:
570 case BIO_CTRL_SET_CLOSE:
571 bio->shutdown = (int) num;
575 case BIO_CTRL_PENDING:
578 struct bio_bio_st *peer_b = b->peer->ptr;
580 ret = (long) peer_b->len;
586 case BIO_CTRL_WPENDING:
594 /* See BIO_dup_chain for circumstances we have to expect. */
596 BIO *other_bio = ptr;
597 struct bio_bio_st *other_b;
599 assert(other_bio != NULL);
600 other_b = other_bio->ptr;
601 assert(other_b != NULL);
603 assert(other_b->buf == NULL); /* other_bio is always fresh */
605 other_b->size = b->size;
617 BIO *other_bio = ptr;
621 struct bio_bio_st *other_b = other_bio->ptr;
623 assert(other_b != NULL);
624 ret = other_b->len == 0 && other_b->closed;
637 static int bio_puts(BIO *bio, const char *str)
639 return bio_write(bio, str, strlen(str));
643 static int bio_make_pair(BIO *bio1, BIO *bio2)
645 struct bio_bio_st *b1, *b2;
647 assert(bio1 != NULL);
648 assert(bio2 != NULL);
653 if (b1->peer != NULL || b2->peer != NULL)
655 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
661 b1->buf = OPENSSL_malloc(b1->size);
664 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
673 b2->buf = OPENSSL_malloc(b2->size);
676 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
696 static void bio_destroy_pair(BIO *bio)
698 struct bio_bio_st *b = bio->ptr;
702 BIO *peer_bio = b->peer;
704 if (peer_bio != NULL)
706 struct bio_bio_st *peer_b = peer_bio->ptr;
708 assert(peer_b != NULL);
709 assert(peer_b->peer == bio);
713 assert(peer_b->buf != NULL);
719 assert(b->buf != NULL);
727 /* Exported convenience functions */
728 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
729 BIO **bio2_p, size_t writebuf2)
731 BIO *bio1 = NULL, *bio2 = NULL;
735 bio1 = BIO_new(BIO_s_bio());
738 bio2 = BIO_new(BIO_s_bio());
744 r = BIO_set_write_buf_size(bio1, writebuf1);
750 r = BIO_set_write_buf_size(bio2, writebuf2);
755 r = BIO_make_bio_pair(bio1, bio2);
780 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
782 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
785 size_t BIO_ctrl_get_read_request(BIO *bio)
787 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
790 int BIO_ctrl_reset_read_request(BIO *bio)
792 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
796 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
797 * (conceivably some other BIOs could allow non-copying reads and writes too.)
799 int BIO_nread0(BIO *bio, char **buf)
805 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
809 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
816 int BIO_nread(BIO *bio, char **buf, int num)
822 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
826 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
828 bio->num_read += ret;
832 int BIO_nwrite0(BIO *bio, char **buf)
838 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
842 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
849 int BIO_nwrite(BIO *bio, char **buf, int num)
855 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
859 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
861 bio->num_read += ret;