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 */
19 #include <sys/types.h>
21 #include <openssl/bio.h>
22 #include <openssl/err.h>
23 #include <openssl/crypto.h>
25 static int bio_new(BIO *bio);
26 static int bio_free(BIO *bio);
27 static int bio_read(BIO *bio, char *buf, int size);
28 static int bio_write(BIO *bio, char *buf, int num);
29 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
30 static int bio_puts(BIO *bio, char *str);
32 static int bio_make_pair(BIO *bio1, BIO *bio2);
33 static void bio_destroy_pair(BIO *bio);
35 static BIO_METHOD methods_biop =
42 NULL /* no bio_gets */,
46 NULL /* no bio_callback_ctrl */
49 BIO_METHOD *BIO_s_bio(void)
56 BIO *peer; /* NULL if buf == NULL.
57 * If peer != NULL, then peer->ptr is also a bio_bio_st,
58 * and its "peer" member points back to us.
59 * peer != NULL iff init != 0 in the BIO. */
61 /* This is for what we write (i.e. reading uses peer's struct): */
62 int closed; /* valid iff peer != NULL */
63 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
64 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
66 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,
71 * never more than buffer space (size-len) warrants. */
74 static int bio_new(BIO *bio)
78 b = Malloc(sizeof *b);
83 b->size = 17*1024; /* enough for one TLS record (just a default) */
91 static int bio_free(BIO *bio)
102 bio_destroy_pair(bio);
116 static int bio_read(BIO *bio, char *buf, int size_)
120 struct bio_bio_st *b, *peer_b;
122 BIO_clear_retry_flags(bio);
129 assert(b->peer != NULL);
130 peer_b = b->peer->ptr;
131 assert(peer_b != NULL);
132 assert(peer_b->buf != NULL);
134 peer_b->request = 0; /* will be set in "retry_read" situation */
136 if (buf == NULL || size == 0)
139 if (peer_b->len == 0)
142 return 0; /* writer has closed, and no data is left */
145 BIO_set_retry_read(bio); /* buffer is empty */
146 if (size <= peer_b->size)
147 peer_b->request = size;
149 /* don't ask for more than the peer can
150 * deliver in one write */
151 peer_b->request = peer_b->size;
157 if (peer_b->len < size)
160 /* now read "size" bytes */
165 do /* one or two iterations */
169 assert(rest <= peer_b->len);
170 if (peer_b->offset + rest <= peer_b->size)
173 /* wrap around ring buffer */
174 chunk = peer_b->size - peer_b->offset;
175 assert(peer_b->offset + chunk <= peer_b->size);
177 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
179 peer_b->len -= chunk;
182 peer_b->offset += chunk;
183 assert(peer_b->offset <= peer_b->size);
184 if (peer_b->offset == peer_b->size)
190 /* buffer now empty, no need to advance "buf" */
191 assert(chunk == rest);
201 /* non-copying interface: provide pointer to available data in buffer
202 * bio_nread0: return number of available bytes
203 * bio_nread: also advance index
204 * (example usage: bio_nread0(), read from buffer, bio_nread()
205 * or just bio_nread(), read from buffer)
207 /* WARNING: The non-copying interface is largely untested as of yet
208 * and may contain bugs. */
209 static ssize_t bio_nread0(BIO *bio, char **buf)
211 struct bio_bio_st *b, *peer_b;
214 BIO_clear_retry_flags(bio);
221 assert(b->peer != NULL);
222 peer_b = b->peer->ptr;
223 assert(peer_b != NULL);
224 assert(peer_b->buf != NULL);
228 if (peer_b->len == 0)
232 /* avoid code duplication -- nothing available for reading */
233 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
237 if (peer_b->size < peer_b->offset + num)
238 /* no ring buffer wrap-around for non-copying interface */
239 num = peer_b->size - peer_b->offset;
243 *buf = peer_b->buf + peer_b->offset;
247 static ssize_t bio_nread(BIO *bio, char **buf, size_t num)
249 struct bio_bio_st *b, *peer_b;
252 available = bio_nread0(bio, buf);
259 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)
276 static int bio_write(BIO *bio, char *buf, int num_)
280 struct bio_bio_st *b;
282 BIO_clear_retry_flags(bio);
284 if (!bio->init || buf == NULL || num == 0)
289 assert(b->peer != NULL);
290 assert(b->buf != NULL);
295 /* we already closed */
296 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
300 assert(b->len <= b->size);
302 if (b->len == b->size)
304 BIO_set_retry_write(bio); /* buffer is full */
309 if (num > b->size - b->len)
310 num = b->size - b->len;
312 /* now write "num" bytes */
317 do /* one or two iterations */
322 assert(b->len + rest <= b->size);
324 write_offset = b->offset + b->len;
325 if (write_offset >= b->size)
326 write_offset -= b->size;
327 /* b->buf[write_offset] is the first byte we can write to. */
329 if (write_offset + rest <= b->size)
332 /* wrap around ring buffer */
333 chunk = b->size - write_offset;
335 memcpy(b->buf + write_offset, buf, chunk);
339 assert(b->len <= b->size);
349 /* non-copying interface: provide pointer to region to write to
350 * bio_nwrite0: check how much space is available
351 * bio_nwrite: also increase length
352 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
353 * or just bio_nwrite(), write to buffer)
355 static size_t bio_nwrite0(BIO *bio, char **buf)
357 struct bio_bio_st *b;
361 BIO_clear_retry_flags(bio);
368 assert(b->peer != NULL);
369 assert(b->buf != NULL);
374 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
378 assert(b->len <= b->size);
380 if (b->len == b->size)
382 BIO_set_retry_write(bio);
386 num = b->size - b->len;
387 write_offset = b->offset + b->len;
388 if (write_offset >= b->size)
389 write_offset -= b->size;
390 if (write_offset + num > b->size)
391 /* no ring buffer wrap-around for non-copying interface
392 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
393 * BIO_nwrite may have to be called twice) */
394 num = b->size - write_offset;
397 *buf = b->buf + write_offset;
398 assert(write_offset + num <= b->size);
403 static size_t bio_nwrite(BIO *bio, char **buf, size_t num)
405 struct bio_bio_st *b;
408 space = bio_nwrite0(bio, buf);
416 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;
431 /* specific CTRL codes */
433 case BIO_C_SET_WRITE_BUF_SIZE:
436 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
441 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
446 size_t new_size = num;
448 if (b->size != new_size)
461 case BIO_C_GET_WRITE_BUF_SIZE:
462 num = (long) b->size;
464 case BIO_C_MAKE_BIO_PAIR:
466 BIO *other_bio = ptr;
468 if (bio_make_pair(bio, other_bio))
475 case BIO_C_DESTROY_BIO_PAIR:
476 /* Effects both BIOs in the pair -- call just once!
477 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
478 bio_destroy_pair(bio);
482 case BIO_C_GET_WRITE_GUARANTEE:
483 /* How many bytes can the caller feed to the next write
484 * without having to keep any? */
485 if (b->peer == NULL || b->closed)
488 ret = (long) b->size - b->len;
491 case BIO_C_GET_READ_REQUEST:
492 /* If the peer unsuccessfully tried to read, how many bytes
493 * were requested? (As with BIO_CTRL_PENDING, that number
494 * can usually be treated as boolean.) */
495 ret = (long) b->request;
498 case BIO_C_RESET_READ_REQUEST:
499 /* Reset request. (Can be useful after read attempts
500 * at the other side that are meant to be non-blocking,
501 * e.g. when probing SSL_read to see if any data is
507 case BIO_C_SHUTDOWN_WR:
508 /* similar to shutdown(..., SHUT_WR) */
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);
529 /* standard CTRL codes follow */
540 case BIO_CTRL_GET_CLOSE:
544 case BIO_CTRL_SET_CLOSE:
545 bio->shutdown = (int) num;
549 case BIO_CTRL_PENDING:
552 struct bio_bio_st *peer_b = b->peer->ptr;
554 ret = (long) peer_b->len;
560 case BIO_CTRL_WPENDING:
568 /* See BIO_dup_chain for circumstances we have to expect. */
570 BIO *other_bio = ptr;
571 struct bio_bio_st *other_b;
573 assert(other_bio != NULL);
574 other_b = other_bio->ptr;
575 assert(other_b != NULL);
577 assert(other_b->buf == NULL); /* other_bio is always fresh */
579 other_b->size = b->size;
591 BIO *other_bio = ptr;
595 struct bio_bio_st *other_b = other_bio->ptr;
597 assert(other_b != NULL);
598 ret = other_b->len == 0 && other_b->closed;
611 static int bio_puts(BIO *bio, char *str)
613 return bio_write(bio, str, strlen(str));
617 static int bio_make_pair(BIO *bio1, BIO *bio2)
619 struct bio_bio_st *b1, *b2;
621 assert(bio1 != NULL);
622 assert(bio2 != NULL);
627 if (b1->peer != NULL || b2->peer != NULL)
629 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
635 b1->buf = Malloc(b1->size);
638 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
647 b2->buf = Malloc(b2->size);
650 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
670 static void bio_destroy_pair(BIO *bio)
672 struct bio_bio_st *b = bio->ptr;
676 BIO *peer_bio = b->peer;
678 if (peer_bio != NULL)
680 struct bio_bio_st *peer_b = peer_bio->ptr;
682 assert(peer_b != NULL);
683 assert(peer_b->peer == bio);
687 assert(peer_b->buf != NULL);
693 assert(b->buf != NULL);
701 /* Exported convenience functions */
702 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
703 BIO **bio2_p, size_t writebuf2)
705 BIO *bio1 = NULL, *bio2 = NULL;
709 bio1 = BIO_new(BIO_s_bio());
712 bio2 = BIO_new(BIO_s_bio());
718 r = BIO_set_write_buf_size(bio1, writebuf1);
724 r = BIO_set_write_buf_size(bio2, writebuf2);
729 r = BIO_make_bio_pair(bio1, bio2);
754 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
756 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
759 size_t BIO_ctrl_get_read_request(BIO *bio)
761 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
764 int BIO_ctrl_reset_read_request(BIO *bio)
766 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
770 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
771 * (conceivably some other BIOs could allow non-copying reads and writes too.)
773 int BIO_nread0(BIO *bio, char **buf)
779 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
783 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
790 int BIO_nread(BIO *bio, char **buf, int num)
796 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
800 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
802 bio->num_read += ret;
806 int BIO_nwrite0(BIO *bio, char **buf)
812 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
816 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
823 int BIO_nwrite(BIO *bio, char **buf, int num)
829 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
833 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
835 bio->num_read += ret;