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 /* BIO_DEBUG implies BIO_PAIR_DEBUG */
12 # ifndef BIO_PAIR_DEBUG
13 # define BIO_PAIR_DEBUG
17 /* disable assert() unless BIO_PAIR_DEBUG has been defined */
18 #ifndef BIO_PAIR_DEBUG
29 #include <openssl/bio.h>
30 #include <openssl/err.h>
31 #include <openssl/crypto.h>
35 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
36 #if defined(OPENSSL_SYS_VXWORKS)
40 # define SSIZE_MAX INT_MAX
43 static int bio_new(BIO *bio);
44 static int bio_free(BIO *bio);
45 static int bio_read(BIO *bio, char *buf, int size);
46 static int bio_write(BIO *bio, const char *buf, int num);
47 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
48 static int bio_puts(BIO *bio, const char *str);
50 static int bio_make_pair(BIO *bio1, BIO *bio2);
51 static void bio_destroy_pair(BIO *bio);
53 static BIO_METHOD methods_biop =
60 NULL /* no bio_gets */,
64 NULL /* no bio_callback_ctrl */
67 BIO_METHOD *BIO_s_bio(void)
74 BIO *peer; /* NULL if buf == NULL.
75 * If peer != NULL, then peer->ptr is also a bio_bio_st,
76 * and its "peer" member points back to us.
77 * peer != NULL iff init != 0 in the BIO. */
79 /* This is for what we write (i.e. reading uses peer's struct): */
80 int closed; /* valid iff peer != NULL */
81 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
82 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
84 char *buf; /* "size" elements (if != NULL) */
86 size_t request; /* valid iff peer != NULL; 0 if len != 0,
87 * otherwise set by peer to number of bytes
88 * it (unsuccessfully) tried to read,
89 * never more than buffer space (size-len) warrants. */
92 static int bio_new(BIO *bio)
96 b = OPENSSL_malloc(sizeof *b);
101 b->size = 17*1024; /* enough for one TLS record (just a default) */
109 static int bio_free(BIO *bio)
111 struct bio_bio_st *b;
120 bio_destroy_pair(bio);
124 OPENSSL_free(b->buf);
134 static int bio_read(BIO *bio, char *buf, int size_)
138 struct bio_bio_st *b, *peer_b;
140 BIO_clear_retry_flags(bio);
147 assert(b->peer != NULL);
148 peer_b = b->peer->ptr;
149 assert(peer_b != NULL);
150 assert(peer_b->buf != NULL);
152 peer_b->request = 0; /* will be set in "retry_read" situation */
154 if (buf == NULL || size == 0)
157 if (peer_b->len == 0)
160 return 0; /* writer has closed, and no data is left */
163 BIO_set_retry_read(bio); /* buffer is empty */
164 if (size <= peer_b->size)
165 peer_b->request = size;
167 /* don't ask for more than the peer can
168 * deliver in one write */
169 peer_b->request = peer_b->size;
175 if (peer_b->len < size)
178 /* now read "size" bytes */
183 do /* one or two iterations */
187 assert(rest <= peer_b->len);
188 if (peer_b->offset + rest <= peer_b->size)
191 /* wrap around ring buffer */
192 chunk = peer_b->size - peer_b->offset;
193 assert(peer_b->offset + chunk <= peer_b->size);
195 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
197 peer_b->len -= chunk;
200 peer_b->offset += chunk;
201 assert(peer_b->offset <= peer_b->size);
202 if (peer_b->offset == peer_b->size)
208 /* buffer now empty, no need to advance "buf" */
209 assert(chunk == rest);
219 /* non-copying interface: provide pointer to available data in buffer
220 * bio_nread0: return number of available bytes
221 * bio_nread: also advance index
222 * (example usage: bio_nread0(), read from buffer, bio_nread()
223 * or just bio_nread(), read from buffer)
225 /* WARNING: The non-copying interface is largely untested as of yet
226 * and may contain bugs. */
227 static ssize_t bio_nread0(BIO *bio, char **buf)
229 struct bio_bio_st *b, *peer_b;
232 BIO_clear_retry_flags(bio);
239 assert(b->peer != NULL);
240 peer_b = b->peer->ptr;
241 assert(peer_b != NULL);
242 assert(peer_b->buf != NULL);
246 if (peer_b->len == 0)
250 /* avoid code duplication -- nothing available for reading */
251 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
255 if (peer_b->size < peer_b->offset + num)
256 /* no ring buffer wrap-around for non-copying interface */
257 num = peer_b->size - peer_b->offset;
261 *buf = peer_b->buf + peer_b->offset;
265 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
267 struct bio_bio_st *b, *peer_b;
268 ssize_t num, available;
270 if (num_ > SSIZE_MAX)
275 available = bio_nread0(bio, buf);
282 peer_b = b->peer->ptr;
287 peer_b->offset += num;
288 assert(peer_b->offset <= peer_b->size);
289 if (peer_b->offset == peer_b->size)
299 static int bio_write(BIO *bio, const char *buf, int num_)
303 struct bio_bio_st *b;
305 BIO_clear_retry_flags(bio);
307 if (!bio->init || buf == NULL || num == 0)
312 assert(b->peer != NULL);
313 assert(b->buf != NULL);
318 /* we already closed */
319 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
323 assert(b->len <= b->size);
325 if (b->len == b->size)
327 BIO_set_retry_write(bio); /* buffer is full */
332 if (num > b->size - b->len)
333 num = b->size - b->len;
335 /* now write "num" bytes */
340 do /* one or two iterations */
345 assert(b->len + rest <= b->size);
347 write_offset = b->offset + b->len;
348 if (write_offset >= b->size)
349 write_offset -= b->size;
350 /* b->buf[write_offset] is the first byte we can write to. */
352 if (write_offset + rest <= b->size)
355 /* wrap around ring buffer */
356 chunk = b->size - write_offset;
358 memcpy(b->buf + write_offset, buf, chunk);
362 assert(b->len <= b->size);
372 /* non-copying interface: provide pointer to region to write to
373 * bio_nwrite0: check how much space is available
374 * bio_nwrite: also increase length
375 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
376 * or just bio_nwrite(), write to buffer)
378 static ssize_t bio_nwrite0(BIO *bio, char **buf)
380 struct bio_bio_st *b;
384 BIO_clear_retry_flags(bio);
391 assert(b->peer != NULL);
392 assert(b->buf != NULL);
397 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
401 assert(b->len <= b->size);
403 if (b->len == b->size)
405 BIO_set_retry_write(bio);
409 num = b->size - b->len;
410 write_offset = b->offset + b->len;
411 if (write_offset >= b->size)
412 write_offset -= b->size;
413 if (write_offset + num > b->size)
414 /* no ring buffer wrap-around for non-copying interface
415 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
416 * BIO_nwrite may have to be called twice) */
417 num = b->size - write_offset;
420 *buf = b->buf + write_offset;
421 assert(write_offset + num <= b->size);
426 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
428 struct bio_bio_st *b;
431 if (num_ > SSIZE_MAX)
436 space = bio_nwrite0(bio, buf);
444 assert(b->len <= b->size);
450 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
453 struct bio_bio_st *b = bio->ptr;
459 /* specific CTRL codes */
461 case BIO_C_SET_WRITE_BUF_SIZE:
464 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
469 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
474 size_t new_size = num;
476 if (b->size != new_size)
480 OPENSSL_free(b->buf);
489 case BIO_C_GET_WRITE_BUF_SIZE:
490 ret = (long) b->size;
493 case BIO_C_MAKE_BIO_PAIR:
495 BIO *other_bio = ptr;
497 if (bio_make_pair(bio, other_bio))
504 case BIO_C_DESTROY_BIO_PAIR:
505 /* Effects both BIOs in the pair -- call just once!
506 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
507 bio_destroy_pair(bio);
511 case BIO_C_GET_WRITE_GUARANTEE:
512 /* How many bytes can the caller feed to the next write
513 * without having to keep any? */
514 if (b->peer == NULL || b->closed)
517 ret = (long) b->size - b->len;
520 case BIO_C_GET_READ_REQUEST:
521 /* If the peer unsuccessfully tried to read, how many bytes
522 * were requested? (As with BIO_CTRL_PENDING, that number
523 * can usually be treated as boolean.) */
524 ret = (long) b->request;
527 case BIO_C_RESET_READ_REQUEST:
528 /* Reset request. (Can be useful after read attempts
529 * at the other side that are meant to be non-blocking,
530 * e.g. when probing SSL_read to see if any data is
536 case BIO_C_SHUTDOWN_WR:
537 /* similar to shutdown(..., SHUT_WR) */
543 /* prepare for non-copying read */
544 ret = (long) bio_nread0(bio, ptr);
548 /* non-copying read */
549 ret = (long) bio_nread(bio, ptr, (size_t) num);
553 /* prepare for non-copying write */
554 ret = (long) bio_nwrite0(bio, ptr);
558 /* non-copying write */
559 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
563 /* standard CTRL codes follow */
574 case BIO_CTRL_GET_CLOSE:
578 case BIO_CTRL_SET_CLOSE:
579 bio->shutdown = (int) num;
583 case BIO_CTRL_PENDING:
586 struct bio_bio_st *peer_b = b->peer->ptr;
588 ret = (long) peer_b->len;
594 case BIO_CTRL_WPENDING:
602 /* See BIO_dup_chain for circumstances we have to expect. */
604 BIO *other_bio = ptr;
605 struct bio_bio_st *other_b;
607 assert(other_bio != NULL);
608 other_b = other_bio->ptr;
609 assert(other_b != NULL);
611 assert(other_b->buf == NULL); /* other_bio is always fresh */
613 other_b->size = b->size;
625 BIO *other_bio = ptr;
629 struct bio_bio_st *other_b = other_bio->ptr;
631 assert(other_b != NULL);
632 ret = other_b->len == 0 && other_b->closed;
645 static int bio_puts(BIO *bio, const char *str)
647 return bio_write(bio, str, strlen(str));
651 static int bio_make_pair(BIO *bio1, BIO *bio2)
653 struct bio_bio_st *b1, *b2;
655 assert(bio1 != NULL);
656 assert(bio2 != NULL);
661 if (b1->peer != NULL || b2->peer != NULL)
663 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
669 b1->buf = OPENSSL_malloc(b1->size);
672 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
681 b2->buf = OPENSSL_malloc(b2->size);
684 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
704 static void bio_destroy_pair(BIO *bio)
706 struct bio_bio_st *b = bio->ptr;
710 BIO *peer_bio = b->peer;
712 if (peer_bio != NULL)
714 struct bio_bio_st *peer_b = peer_bio->ptr;
716 assert(peer_b != NULL);
717 assert(peer_b->peer == bio);
721 assert(peer_b->buf != NULL);
727 assert(b->buf != NULL);
735 /* Exported convenience functions */
736 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
737 BIO **bio2_p, size_t writebuf2)
739 BIO *bio1 = NULL, *bio2 = NULL;
743 bio1 = BIO_new(BIO_s_bio());
746 bio2 = BIO_new(BIO_s_bio());
752 r = BIO_set_write_buf_size(bio1, writebuf1);
758 r = BIO_set_write_buf_size(bio2, writebuf2);
763 r = BIO_make_bio_pair(bio1, bio2);
788 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
790 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
793 size_t BIO_ctrl_get_read_request(BIO *bio)
795 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
798 int BIO_ctrl_reset_read_request(BIO *bio)
800 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
804 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
805 * (conceivably some other BIOs could allow non-copying reads and writes too.)
807 int BIO_nread0(BIO *bio, char **buf)
813 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
817 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
824 int BIO_nread(BIO *bio, char **buf, int num)
830 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
834 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
836 bio->num_read += ret;
840 int BIO_nwrite0(BIO *bio, char **buf)
846 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
850 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
857 int BIO_nwrite(BIO *bio, char **buf, int num)
863 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
867 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
869 bio->num_read += ret;