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/err.h>
32 #include <openssl/crypto.h>
36 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
37 #if defined(OPENSSL_SYS_VXWORKS)
41 # define SSIZE_MAX INT_MAX
44 static int bio_new(BIO *bio);
45 static int bio_free(BIO *bio);
46 static int bio_read(BIO *bio, char *buf, int size);
47 static int bio_write(BIO *bio, const char *buf, int num);
48 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
49 static int bio_puts(BIO *bio, const char *str);
51 static int bio_make_pair(BIO *bio1, BIO *bio2);
52 static void bio_destroy_pair(BIO *bio);
54 static BIO_METHOD methods_biop =
61 NULL /* no bio_gets */,
65 NULL /* no bio_callback_ctrl */
68 BIO_METHOD *BIO_s_bio(void)
75 BIO *peer; /* NULL if buf == NULL.
76 * If peer != NULL, then peer->ptr is also a bio_bio_st,
77 * and its "peer" member points back to us.
78 * peer != NULL iff init != 0 in the BIO. */
80 /* This is for what we write (i.e. reading uses peer's struct): */
81 int closed; /* valid iff peer != NULL */
82 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
83 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
85 char *buf; /* "size" elements (if != NULL) */
87 size_t request; /* valid iff peer != NULL; 0 if len != 0,
88 * otherwise set by peer to number of bytes
89 * it (unsuccessfully) tried to read,
90 * never more than buffer space (size-len) warrants. */
93 static int bio_new(BIO *bio)
97 b = OPENSSL_malloc(sizeof *b);
102 b->size = 17*1024; /* enough for one TLS record (just a default) */
110 static int bio_free(BIO *bio)
112 struct bio_bio_st *b;
121 bio_destroy_pair(bio);
125 OPENSSL_free(b->buf);
135 static int bio_read(BIO *bio, char *buf, int size_)
139 struct bio_bio_st *b, *peer_b;
141 BIO_clear_retry_flags(bio);
148 assert(b->peer != NULL);
149 peer_b = b->peer->ptr;
150 assert(peer_b != NULL);
151 assert(peer_b->buf != NULL);
153 peer_b->request = 0; /* will be set in "retry_read" situation */
155 if (buf == NULL || size == 0)
158 if (peer_b->len == 0)
161 return 0; /* writer has closed, and no data is left */
164 BIO_set_retry_read(bio); /* buffer is empty */
165 if (size <= peer_b->size)
166 peer_b->request = size;
168 /* don't ask for more than the peer can
169 * deliver in one write */
170 peer_b->request = peer_b->size;
176 if (peer_b->len < size)
179 /* now read "size" bytes */
184 do /* one or two iterations */
188 assert(rest <= peer_b->len);
189 if (peer_b->offset + rest <= peer_b->size)
192 /* wrap around ring buffer */
193 chunk = peer_b->size - peer_b->offset;
194 assert(peer_b->offset + chunk <= peer_b->size);
196 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
198 peer_b->len -= chunk;
201 peer_b->offset += chunk;
202 assert(peer_b->offset <= peer_b->size);
203 if (peer_b->offset == peer_b->size)
209 /* buffer now empty, no need to advance "buf" */
210 assert(chunk == rest);
220 /* non-copying interface: provide pointer to available data in buffer
221 * bio_nread0: return number of available bytes
222 * bio_nread: also advance index
223 * (example usage: bio_nread0(), read from buffer, bio_nread()
224 * or just bio_nread(), read from buffer)
226 /* WARNING: The non-copying interface is largely untested as of yet
227 * and may contain bugs. */
228 static ssize_t bio_nread0(BIO *bio, char **buf)
230 struct bio_bio_st *b, *peer_b;
233 BIO_clear_retry_flags(bio);
240 assert(b->peer != NULL);
241 peer_b = b->peer->ptr;
242 assert(peer_b != NULL);
243 assert(peer_b->buf != NULL);
247 if (peer_b->len == 0)
251 /* avoid code duplication -- nothing available for reading */
252 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
256 if (peer_b->size < peer_b->offset + num)
257 /* no ring buffer wrap-around for non-copying interface */
258 num = peer_b->size - peer_b->offset;
262 *buf = peer_b->buf + peer_b->offset;
266 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
268 struct bio_bio_st *b, *peer_b;
269 ssize_t num, available;
271 if (num_ > SSIZE_MAX)
276 available = bio_nread0(bio, buf);
283 peer_b = b->peer->ptr;
288 peer_b->offset += num;
289 assert(peer_b->offset <= peer_b->size);
290 if (peer_b->offset == peer_b->size)
300 static int bio_write(BIO *bio, const char *buf, int num_)
304 struct bio_bio_st *b;
306 BIO_clear_retry_flags(bio);
308 if (!bio->init || buf == NULL || num == 0)
313 assert(b->peer != NULL);
314 assert(b->buf != NULL);
319 /* we already closed */
320 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
324 assert(b->len <= b->size);
326 if (b->len == b->size)
328 BIO_set_retry_write(bio); /* buffer is full */
333 if (num > b->size - b->len)
334 num = b->size - b->len;
336 /* now write "num" bytes */
341 do /* one or two iterations */
346 assert(b->len + rest <= b->size);
348 write_offset = b->offset + b->len;
349 if (write_offset >= b->size)
350 write_offset -= b->size;
351 /* b->buf[write_offset] is the first byte we can write to. */
353 if (write_offset + rest <= b->size)
356 /* wrap around ring buffer */
357 chunk = b->size - write_offset;
359 memcpy(b->buf + write_offset, buf, chunk);
363 assert(b->len <= b->size);
373 /* non-copying interface: provide pointer to region to write to
374 * bio_nwrite0: check how much space is available
375 * bio_nwrite: also increase length
376 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
377 * or just bio_nwrite(), write to buffer)
379 static ssize_t bio_nwrite0(BIO *bio, char **buf)
381 struct bio_bio_st *b;
385 BIO_clear_retry_flags(bio);
392 assert(b->peer != NULL);
393 assert(b->buf != NULL);
398 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
402 assert(b->len <= b->size);
404 if (b->len == b->size)
406 BIO_set_retry_write(bio);
410 num = b->size - b->len;
411 write_offset = b->offset + b->len;
412 if (write_offset >= b->size)
413 write_offset -= b->size;
414 if (write_offset + num > b->size)
415 /* no ring buffer wrap-around for non-copying interface
416 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
417 * BIO_nwrite may have to be called twice) */
418 num = b->size - write_offset;
421 *buf = b->buf + write_offset;
422 assert(write_offset + num <= b->size);
427 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
429 struct bio_bio_st *b;
432 if (num_ > SSIZE_MAX)
437 space = bio_nwrite0(bio, buf);
445 assert(b->len <= b->size);
451 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
454 struct bio_bio_st *b = bio->ptr;
460 /* specific CTRL codes */
462 case BIO_C_SET_WRITE_BUF_SIZE:
465 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
470 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
475 size_t new_size = num;
477 if (b->size != new_size)
481 OPENSSL_free(b->buf);
490 case BIO_C_GET_WRITE_BUF_SIZE:
491 ret = (long) b->size;
494 case BIO_C_MAKE_BIO_PAIR:
496 BIO *other_bio = ptr;
498 if (bio_make_pair(bio, other_bio))
505 case BIO_C_DESTROY_BIO_PAIR:
506 /* Effects both BIOs in the pair -- call just once!
507 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
508 bio_destroy_pair(bio);
512 case BIO_C_GET_WRITE_GUARANTEE:
513 /* How many bytes can the caller feed to the next write
514 * without having to keep any? */
515 if (b->peer == NULL || b->closed)
518 ret = (long) b->size - b->len;
521 case BIO_C_GET_READ_REQUEST:
522 /* If the peer unsuccessfully tried to read, how many bytes
523 * were requested? (As with BIO_CTRL_PENDING, that number
524 * can usually be treated as boolean.) */
525 ret = (long) b->request;
528 case BIO_C_RESET_READ_REQUEST:
529 /* Reset request. (Can be useful after read attempts
530 * at the other side that are meant to be non-blocking,
531 * e.g. when probing SSL_read to see if any data is
537 case BIO_C_SHUTDOWN_WR:
538 /* similar to shutdown(..., SHUT_WR) */
544 /* prepare for non-copying read */
545 ret = (long) bio_nread0(bio, ptr);
549 /* non-copying read */
550 ret = (long) bio_nread(bio, ptr, (size_t) num);
554 /* prepare for non-copying write */
555 ret = (long) bio_nwrite0(bio, ptr);
559 /* non-copying write */
560 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
564 /* standard CTRL codes follow */
575 case BIO_CTRL_GET_CLOSE:
579 case BIO_CTRL_SET_CLOSE:
580 bio->shutdown = (int) num;
584 case BIO_CTRL_PENDING:
587 struct bio_bio_st *peer_b = b->peer->ptr;
589 ret = (long) peer_b->len;
595 case BIO_CTRL_WPENDING:
603 /* See BIO_dup_chain for circumstances we have to expect. */
605 BIO *other_bio = ptr;
606 struct bio_bio_st *other_b;
608 assert(other_bio != NULL);
609 other_b = other_bio->ptr;
610 assert(other_b != NULL);
612 assert(other_b->buf == NULL); /* other_bio is always fresh */
614 other_b->size = b->size;
626 BIO *other_bio = ptr;
630 struct bio_bio_st *other_b = other_bio->ptr;
632 assert(other_b != NULL);
633 ret = other_b->len == 0 && other_b->closed;
646 static int bio_puts(BIO *bio, const char *str)
648 return bio_write(bio, str, strlen(str));
652 static int bio_make_pair(BIO *bio1, BIO *bio2)
654 struct bio_bio_st *b1, *b2;
656 assert(bio1 != NULL);
657 assert(bio2 != NULL);
662 if (b1->peer != NULL || b2->peer != NULL)
664 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
670 b1->buf = OPENSSL_malloc(b1->size);
673 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
682 b2->buf = OPENSSL_malloc(b2->size);
685 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
705 static void bio_destroy_pair(BIO *bio)
707 struct bio_bio_st *b = bio->ptr;
711 BIO *peer_bio = b->peer;
713 if (peer_bio != NULL)
715 struct bio_bio_st *peer_b = peer_bio->ptr;
717 assert(peer_b != NULL);
718 assert(peer_b->peer == bio);
722 assert(peer_b->buf != NULL);
728 assert(b->buf != NULL);
736 /* Exported convenience functions */
737 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
738 BIO **bio2_p, size_t writebuf2)
740 BIO *bio1 = NULL, *bio2 = NULL;
744 bio1 = BIO_new(BIO_s_bio());
747 bio2 = BIO_new(BIO_s_bio());
753 r = BIO_set_write_buf_size(bio1, writebuf1);
759 r = BIO_set_write_buf_size(bio2, writebuf2);
764 r = BIO_make_bio_pair(bio1, bio2);
789 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
791 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
794 size_t BIO_ctrl_get_read_request(BIO *bio)
796 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
799 int BIO_ctrl_reset_read_request(BIO *bio)
801 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
805 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
806 * (conceivably some other BIOs could allow non-copying reads and writes too.)
808 int BIO_nread0(BIO *bio, char **buf)
814 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
818 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
825 int BIO_nread(BIO *bio, char **buf, int num)
831 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
835 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
837 bio->num_read += ret;
841 int BIO_nwrite0(BIO *bio, char **buf)
847 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
851 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
858 int BIO_nwrite(BIO *bio, char **buf, int num)
864 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
868 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
870 bio->num_read += ret;