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/crypto.h>
24 static int bio_new(BIO *bio);
25 static int bio_free(BIO *bio);
26 static int bio_read(BIO *bio, char *buf, int size);
27 static int bio_write(BIO *bio, char *buf, int num);
28 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
29 static int bio_puts(BIO *bio, char *str);
31 static int bio_make_pair(BIO *bio1, BIO *bio2);
32 static void bio_destroy_pair(BIO *bio);
34 static BIO_METHOD methods_biop =
41 NULL /* no bio_gets */,
47 BIO_METHOD *BIO_s_bio(void)
54 BIO *peer; /* NULL if buf == NULL.
55 * If peer != NULL, then peer->ptr is also a bio_bio_st,
56 * and its "peer" member points back to us.
57 * peer != NULL iff init != 0 in the BIO. */
59 /* This is for what we write (i.e. reading uses peer's struct): */
60 int closed; /* valid iff peer != NULL */
61 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
62 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
64 char *buf; /* "size" elements (if != NULL) */
66 size_t request; /* valid iff peer != NULL; 0 if len != 0,
67 * otherwise set by peer to number of bytes
68 * it (unsuccessfully) tried to read,
69 * never more than buffer space (size-len) warrants. */
72 static int bio_new(BIO *bio)
76 b = Malloc(sizeof *b);
81 b->size = 17*1024; /* enough for one TLS record (just a default) */
89 static int bio_free(BIO *bio)
100 bio_destroy_pair(bio);
114 static int bio_read(BIO *bio, char *buf, int size_)
118 struct bio_bio_st *b, *peer_b;
120 BIO_clear_retry_flags(bio);
127 assert(b->peer != NULL);
128 peer_b = b->peer->ptr;
129 assert(peer_b != NULL);
130 assert(peer_b->buf != NULL);
132 peer_b->request = 0; /* will be set in "retry_read" situation */
134 if (buf == NULL || size == 0)
137 if (peer_b->len == 0)
140 return 0; /* writer has closed, and no data is left */
143 BIO_set_retry_read(bio); /* buffer is empty */
144 if (size <= peer_b->size)
145 peer_b->request = size;
147 /* don't ask for more than the peer can
148 * deliver in one write */
149 peer_b->request = peer_b->size;
155 if (peer_b->len < size)
158 /* now read "size" bytes */
163 do /* one or two iterations */
167 assert(rest <= peer_b->len);
168 if (peer_b->offset + rest <= peer_b->size)
171 /* wrap around ring buffer */
172 chunk = peer_b->size - peer_b->offset;
173 assert(peer_b->offset + chunk <= peer_b->size);
175 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
177 peer_b->len -= chunk;
180 peer_b->offset += chunk;
181 assert(peer_b->offset <= peer_b->size);
182 if (peer_b->offset == peer_b->size)
188 /* buffer now empty, no need to advance "buf" */
189 assert(chunk == rest);
199 /* non-copying interface: provide pointer to available data in buffer
200 * bio_nread0: return number of available bytes
201 * bio_nread: also advance index
202 * (example usage: bio_nread0(), read from buffer, bio_nread()
203 * or just bio_nread(), read from buffer)
205 /* WARNING: The non-copying interface is largely untested as of yet
206 * and may contain bugs. */
207 static size_t bio_nread0(BIO *bio, char **buf)
209 struct bio_bio_st *b, *peer_b;
212 BIO_clear_retry_flags(bio);
219 assert(b->peer != NULL);
220 peer_b = b->peer->ptr;
221 assert(peer_b != NULL);
222 assert(peer_b->buf != NULL);
226 if (peer_b->len == 0)
230 /* avoid code duplication -- nothing available for reading */
231 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
235 if (peer_b->size < peer_b->offset + num)
236 /* no ring buffer wrap-around for non-copying interface */
237 num = peer_b->size - peer_b->offset;
241 *buf = peer_b->buf + peer_b->offset;
245 static size_t bio_nread(BIO *bio, char **buf, size_t num)
247 struct bio_bio_st *b, *peer_b;
250 available = bio_nread0(bio, buf);
257 peer_b = b->peer->ptr;
262 peer_b->offset += num;
263 assert(peer_b->offset <= peer_b->size);
264 if (peer_b->offset == peer_b->size)
274 static int bio_write(BIO *bio, char *buf, int num_)
278 struct bio_bio_st *b;
280 BIO_clear_retry_flags(bio);
282 if (!bio->init || buf == NULL || num == 0)
287 assert(b->peer != NULL);
288 assert(b->buf != NULL);
293 /* we already closed */
294 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
298 assert(b->len <= b->size);
300 if (b->len == b->size)
302 BIO_set_retry_write(bio); /* buffer is full */
307 if (num > b->size - b->len)
308 num = b->size - b->len;
310 /* now write "num" bytes */
315 do /* one or two iterations */
320 assert(b->len + rest <= b->size);
322 write_offset = b->offset + b->len;
323 if (write_offset >= b->size)
324 write_offset -= b->size;
325 /* b->buf[write_offset] is the first byte we can write to. */
327 if (write_offset + rest <= b->size)
330 /* wrap around ring buffer */
331 chunk = b->size - write_offset;
333 memcpy(b->buf + write_offset, buf, chunk);
337 assert(b->len <= b->size);
347 /* non-copying interface: provide pointer to region to write to
348 * bio_nwrite0: check how much space is available
349 * bio_nwrite: also increase length
350 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
351 * or just bio_nwrite(), write to buffer)
353 static size_t bio_nwrite0(BIO *bio, char **buf)
355 struct bio_bio_st *b;
359 BIO_clear_retry_flags(bio);
366 assert(b->peer != NULL);
367 assert(b->buf != NULL);
372 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
376 assert(b->len <= b->size);
378 if (b->len == b->size)
380 BIO_set_retry_write(bio);
384 num = b->size - b->len;
385 write_offset = b->offset + b->len;
386 if (write_offset >= b->size)
387 write_offset -= b->size;
388 if (write_offset + num > b->size)
389 /* no ring buffer wrap-around for non-copying interface
390 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
391 * BIO_nwrite may have to be called twice) */
392 num = b->size - write_offset;
395 *buf = b->buf + write_offset;
396 assert(write_offset + num <= b->size);
401 static size_t bio_nwrite(BIO *bio, char **buf, size_t num)
403 struct bio_bio_st *b;
406 space = bio_nwrite0(bio, buf);
414 assert(b->len <= b->size);
420 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
423 struct bio_bio_st *b = bio->ptr;
429 /* specific CTRL codes */
431 case BIO_C_SET_WRITE_BUF_SIZE:
434 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
439 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
444 size_t new_size = num;
446 if (b->size != new_size)
459 case BIO_C_GET_WRITE_BUF_SIZE:
460 num = (long) b->size;
462 case BIO_C_MAKE_BIO_PAIR:
464 BIO *other_bio = ptr;
466 if (bio_make_pair(bio, other_bio))
473 case BIO_C_DESTROY_BIO_PAIR:
474 /* Effects both BIOs in the pair -- call just once!
475 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
476 bio_destroy_pair(bio);
480 case BIO_C_GET_WRITE_GUARANTEE:
481 /* How many bytes can the caller feed to the next write
482 * without having to keep any? */
483 if (b->peer == NULL || b->closed)
486 ret = (long) b->size - b->len;
489 case BIO_C_GET_READ_REQUEST:
490 /* If the peer unsuccessfully tried to read, how many bytes
491 * were requested? (As with BIO_CTRL_PENDING, that number
492 * can usually be treated as boolean.) */
493 ret = (long) b->request;
496 case BIO_C_RESET_READ_REQUEST:
497 /* Reset request. (Can be useful after read attempts
498 * at the other side that are meant to be non-blocking,
499 * e.g. when probing SSL_read to see if any data is
505 case BIO_C_SHUTDOWN_WR:
506 /* similar to shutdown(..., SHUT_WR) */
512 /* non-copying read */
513 ret = (long) bio_nread(bio, ptr, (size_t) num);
517 /* prepare for non-copying write */
518 ret = (long) bio_nwrite0(bio, ptr);
522 /* non-copying write */
523 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
527 /* standard CTRL codes follow */
538 case BIO_CTRL_GET_CLOSE:
542 case BIO_CTRL_SET_CLOSE:
543 bio->shutdown = (int) num;
547 case BIO_CTRL_PENDING:
550 struct bio_bio_st *peer_b = b->peer->ptr;
552 ret = (long) peer_b->len;
558 case BIO_CTRL_WPENDING:
566 /* See BIO_dup_chain for circumstances we have to expect. */
568 BIO *other_bio = ptr;
569 struct bio_bio_st *other_b;
571 assert(other_bio != NULL);
572 other_b = other_bio->ptr;
573 assert(other_b != NULL);
575 assert(other_b->buf == NULL); /* other_bio is always fresh */
577 other_b->size = b->size;
589 BIO *other_bio = ptr;
593 struct bio_bio_st *other_b = other_bio->ptr;
595 assert(other_b != NULL);
596 ret = other_b->len == 0 && other_b->closed;
609 static int bio_puts(BIO *bio, char *str)
611 return bio_write(bio, str, strlen(str));
615 static int bio_make_pair(BIO *bio1, BIO *bio2)
617 struct bio_bio_st *b1, *b2;
619 assert(bio1 != NULL);
620 assert(bio2 != NULL);
625 if (b1->peer != NULL || b2->peer != NULL)
627 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
633 b1->buf = Malloc(b1->size);
636 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
645 b2->buf = Malloc(b2->size);
648 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
668 static void bio_destroy_pair(BIO *bio)
670 struct bio_bio_st *b = bio->ptr;
674 BIO *peer_bio = b->peer;
676 if (peer_bio != NULL)
678 struct bio_bio_st *peer_b = peer_bio->ptr;
680 assert(peer_b != NULL);
681 assert(peer_b->peer == bio);
685 assert(peer_b->buf != NULL);
691 assert(b->buf != NULL);
699 /* Exported convenience functions */
700 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
701 BIO **bio2_p, size_t writebuf2)
703 BIO *bio1 = NULL, *bio2 = NULL;
707 bio1 = BIO_new(BIO_s_bio());
710 bio2 = BIO_new(BIO_s_bio());
716 r = BIO_set_write_buf_size(bio1, writebuf1);
722 r = BIO_set_write_buf_size(bio2, writebuf2);
727 r = BIO_make_bio_pair(bio1, bio2);
752 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
754 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
757 size_t BIO_ctrl_get_read_request(BIO *bio)
759 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
762 int BIO_ctrl_reset_read_request(BIO *bio)
764 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
768 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
769 * (conceivably some other BIOs could allow non-copying reads and writes too.)
771 int BIO_nread0(BIO *bio, char **buf)
777 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
781 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
788 int BIO_nread(BIO *bio, char **buf, int num)
794 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
798 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
800 bio->num_read += ret;
804 int BIO_nwrite0(BIO *bio, char **buf)
810 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
814 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
821 int BIO_nwrite(BIO *bio, char **buf, int num)
827 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
831 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
833 bio->num_read += ret;