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 */,
45 NULL /* no bio_callback_ctrl */
48 BIO_METHOD *BIO_s_bio(void)
55 BIO *peer; /* NULL if buf == NULL.
56 * If peer != NULL, then peer->ptr is also a bio_bio_st,
57 * and its "peer" member points back to us.
58 * peer != NULL iff init != 0 in the BIO. */
60 /* This is for what we write (i.e. reading uses peer's struct): */
61 int closed; /* valid iff peer != NULL */
62 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
63 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
65 char *buf; /* "size" elements (if != NULL) */
67 size_t request; /* valid iff peer != NULL; 0 if len != 0,
68 * otherwise set by peer to number of bytes
69 * it (unsuccessfully) tried to read,
70 * never more than buffer space (size-len) warrants. */
73 static int bio_new(BIO *bio)
77 b = Malloc(sizeof *b);
82 b->size = 17*1024; /* enough for one TLS record (just a default) */
90 static int bio_free(BIO *bio)
101 bio_destroy_pair(bio);
115 static int bio_read(BIO *bio, char *buf, int size_)
119 struct bio_bio_st *b, *peer_b;
121 BIO_clear_retry_flags(bio);
128 assert(b->peer != NULL);
129 peer_b = b->peer->ptr;
130 assert(peer_b != NULL);
131 assert(peer_b->buf != NULL);
133 peer_b->request = 0; /* will be set in "retry_read" situation */
135 if (buf == NULL || size == 0)
138 if (peer_b->len == 0)
141 return 0; /* writer has closed, and no data is left */
144 BIO_set_retry_read(bio); /* buffer is empty */
145 if (size <= peer_b->size)
146 peer_b->request = size;
148 /* don't ask for more than the peer can
149 * deliver in one write */
150 peer_b->request = peer_b->size;
156 if (peer_b->len < size)
159 /* now read "size" bytes */
164 do /* one or two iterations */
168 assert(rest <= peer_b->len);
169 if (peer_b->offset + rest <= peer_b->size)
172 /* wrap around ring buffer */
173 chunk = peer_b->size - peer_b->offset;
174 assert(peer_b->offset + chunk <= peer_b->size);
176 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
178 peer_b->len -= chunk;
181 peer_b->offset += chunk;
182 assert(peer_b->offset <= peer_b->size);
183 if (peer_b->offset == peer_b->size)
189 /* buffer now empty, no need to advance "buf" */
190 assert(chunk == rest);
200 /* non-copying interface: provide pointer to available data in buffer
201 * bio_nread0: return number of available bytes
202 * bio_nread: also advance index
203 * (example usage: bio_nread0(), read from buffer, bio_nread()
204 * or just bio_nread(), read from buffer)
206 /* WARNING: The non-copying interface is largely untested as of yet
207 * and may contain bugs. */
208 static size_t bio_nread0(BIO *bio, char **buf)
210 struct bio_bio_st *b, *peer_b;
213 BIO_clear_retry_flags(bio);
220 assert(b->peer != NULL);
221 peer_b = b->peer->ptr;
222 assert(peer_b != NULL);
223 assert(peer_b->buf != NULL);
227 if (peer_b->len == 0)
231 /* avoid code duplication -- nothing available for reading */
232 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
236 if (peer_b->size < peer_b->offset + num)
237 /* no ring buffer wrap-around for non-copying interface */
238 num = peer_b->size - peer_b->offset;
242 *buf = peer_b->buf + peer_b->offset;
246 static size_t bio_nread(BIO *bio, char **buf, size_t num)
248 struct bio_bio_st *b, *peer_b;
251 available = bio_nread0(bio, buf);
258 peer_b = b->peer->ptr;
263 peer_b->offset += num;
264 assert(peer_b->offset <= peer_b->size);
265 if (peer_b->offset == peer_b->size)
275 static int bio_write(BIO *bio, char *buf, int num_)
279 struct bio_bio_st *b;
281 BIO_clear_retry_flags(bio);
283 if (!bio->init || buf == NULL || num == 0)
288 assert(b->peer != NULL);
289 assert(b->buf != NULL);
294 /* we already closed */
295 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
299 assert(b->len <= b->size);
301 if (b->len == b->size)
303 BIO_set_retry_write(bio); /* buffer is full */
308 if (num > b->size - b->len)
309 num = b->size - b->len;
311 /* now write "num" bytes */
316 do /* one or two iterations */
321 assert(b->len + rest <= b->size);
323 write_offset = b->offset + b->len;
324 if (write_offset >= b->size)
325 write_offset -= b->size;
326 /* b->buf[write_offset] is the first byte we can write to. */
328 if (write_offset + rest <= b->size)
331 /* wrap around ring buffer */
332 chunk = b->size - write_offset;
334 memcpy(b->buf + write_offset, buf, chunk);
338 assert(b->len <= b->size);
348 /* non-copying interface: provide pointer to region to write to
349 * bio_nwrite0: check how much space is available
350 * bio_nwrite: also increase length
351 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
352 * or just bio_nwrite(), write to buffer)
354 static size_t bio_nwrite0(BIO *bio, char **buf)
356 struct bio_bio_st *b;
360 BIO_clear_retry_flags(bio);
367 assert(b->peer != NULL);
368 assert(b->buf != NULL);
373 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
377 assert(b->len <= b->size);
379 if (b->len == b->size)
381 BIO_set_retry_write(bio);
385 num = b->size - b->len;
386 write_offset = b->offset + b->len;
387 if (write_offset >= b->size)
388 write_offset -= b->size;
389 if (write_offset + num > b->size)
390 /* no ring buffer wrap-around for non-copying interface
391 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
392 * BIO_nwrite may have to be called twice) */
393 num = b->size - write_offset;
396 *buf = b->buf + write_offset;
397 assert(write_offset + num <= b->size);
402 static size_t bio_nwrite(BIO *bio, char **buf, size_t num)
404 struct bio_bio_st *b;
407 space = bio_nwrite0(bio, buf);
415 assert(b->len <= b->size);
421 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
424 struct bio_bio_st *b = bio->ptr;
430 /* specific CTRL codes */
432 case BIO_C_SET_WRITE_BUF_SIZE:
435 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
440 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
445 size_t new_size = num;
447 if (b->size != new_size)
460 case BIO_C_GET_WRITE_BUF_SIZE:
461 num = (long) b->size;
463 case BIO_C_MAKE_BIO_PAIR:
465 BIO *other_bio = ptr;
467 if (bio_make_pair(bio, other_bio))
474 case BIO_C_DESTROY_BIO_PAIR:
475 /* Effects both BIOs in the pair -- call just once!
476 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
477 bio_destroy_pair(bio);
481 case BIO_C_GET_WRITE_GUARANTEE:
482 /* How many bytes can the caller feed to the next write
483 * without having to keep any? */
484 if (b->peer == NULL || b->closed)
487 ret = (long) b->size - b->len;
490 case BIO_C_GET_READ_REQUEST:
491 /* If the peer unsuccessfully tried to read, how many bytes
492 * were requested? (As with BIO_CTRL_PENDING, that number
493 * can usually be treated as boolean.) */
494 ret = (long) b->request;
497 case BIO_C_RESET_READ_REQUEST:
498 /* Reset request. (Can be useful after read attempts
499 * at the other side that are meant to be non-blocking,
500 * e.g. when probing SSL_read to see if any data is
506 case BIO_C_SHUTDOWN_WR:
507 /* similar to shutdown(..., SHUT_WR) */
513 /* non-copying read */
514 ret = (long) bio_nread(bio, ptr, (size_t) num);
518 /* prepare for non-copying write */
519 ret = (long) bio_nwrite0(bio, ptr);
523 /* non-copying write */
524 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
528 /* standard CTRL codes follow */
539 case BIO_CTRL_GET_CLOSE:
543 case BIO_CTRL_SET_CLOSE:
544 bio->shutdown = (int) num;
548 case BIO_CTRL_PENDING:
551 struct bio_bio_st *peer_b = b->peer->ptr;
553 ret = (long) peer_b->len;
559 case BIO_CTRL_WPENDING:
567 /* See BIO_dup_chain for circumstances we have to expect. */
569 BIO *other_bio = ptr;
570 struct bio_bio_st *other_b;
572 assert(other_bio != NULL);
573 other_b = other_bio->ptr;
574 assert(other_b != NULL);
576 assert(other_b->buf == NULL); /* other_bio is always fresh */
578 other_b->size = b->size;
590 BIO *other_bio = ptr;
594 struct bio_bio_st *other_b = other_bio->ptr;
596 assert(other_b != NULL);
597 ret = other_b->len == 0 && other_b->closed;
610 static int bio_puts(BIO *bio, char *str)
612 return bio_write(bio, str, strlen(str));
616 static int bio_make_pair(BIO *bio1, BIO *bio2)
618 struct bio_bio_st *b1, *b2;
620 assert(bio1 != NULL);
621 assert(bio2 != NULL);
626 if (b1->peer != NULL || b2->peer != NULL)
628 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
634 b1->buf = Malloc(b1->size);
637 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
646 b2->buf = Malloc(b2->size);
649 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
669 static void bio_destroy_pair(BIO *bio)
671 struct bio_bio_st *b = bio->ptr;
675 BIO *peer_bio = b->peer;
677 if (peer_bio != NULL)
679 struct bio_bio_st *peer_b = peer_bio->ptr;
681 assert(peer_b != NULL);
682 assert(peer_b->peer == bio);
686 assert(peer_b->buf != NULL);
692 assert(b->buf != NULL);
700 /* Exported convenience functions */
701 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
702 BIO **bio2_p, size_t writebuf2)
704 BIO *bio1 = NULL, *bio2 = NULL;
708 bio1 = BIO_new(BIO_s_bio());
711 bio2 = BIO_new(BIO_s_bio());
717 r = BIO_set_write_buf_size(bio1, writebuf1);
723 r = BIO_set_write_buf_size(bio2, writebuf2);
728 r = BIO_make_bio_pair(bio1, bio2);
753 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
755 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
758 size_t BIO_ctrl_get_read_request(BIO *bio)
760 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
763 int BIO_ctrl_reset_read_request(BIO *bio)
765 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
769 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
770 * (conceivably some other BIOs could allow non-copying reads and writes too.)
772 int BIO_nread0(BIO *bio, char **buf)
778 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
782 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
789 int BIO_nread(BIO *bio, char **buf, int num)
795 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
799 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
801 bio->num_read += ret;
805 int BIO_nwrite0(BIO *bio, char **buf)
811 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
815 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
822 int BIO_nwrite(BIO *bio, char **buf, int num)
828 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
832 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
834 bio->num_read += ret;