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 <openssl/bio.h>
20 #include <openssl/err.h>
21 #include <openssl/crypto.h>
23 static int bio_new(BIO *bio);
24 static int bio_free(BIO *bio);
25 static int bio_read(BIO *bio, char *buf, int size);
26 static int bio_write(BIO *bio, char *buf, int num);
27 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
28 static int bio_puts(BIO *bio, char *str);
30 static int bio_make_pair(BIO *bio1, BIO *bio2);
31 static void bio_destroy_pair(BIO *bio);
33 static BIO_METHOD methods_biop =
40 NULL /* no bio_gets */,
46 BIO_METHOD *BIO_s_bio(void)
53 BIO *peer; /* NULL if buf == NULL.
54 * If peer != NULL, then peer->ptr is also a bio_bio_st,
55 * and its "peer" member points back to us.
56 * peer != NULL iff init != 0 in the BIO. */
58 /* This is for what we write (i.e. reading uses peer's struct): */
59 int closed; /* valid iff peer != NULL */
60 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
61 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
63 char *buf; /* "size" elements (if != NULL) */
65 size_t request; /* valid iff peer != NULL; 0 if len != 0,
66 * otherwise set by peer to number of bytes
67 * it (unsuccesfully) tried to read,
68 * never more than buffer space (size-len) warrants. */
71 static int bio_new(BIO *bio)
75 b = Malloc(sizeof *b);
80 b->size = 17*1024; /* enough for one TLS record (just a default) */
88 static int bio_free(BIO *bio)
99 bio_destroy_pair(bio);
113 static int bio_read(BIO *bio, char *buf, int size_)
117 struct bio_bio_st *b, *peer_b;
119 BIO_clear_retry_flags(bio);
126 assert(b->peer != NULL);
127 peer_b = b->peer->ptr;
128 assert(peer_b != NULL);
129 assert(peer_b->buf != NULL);
131 peer_b->request = 0; /* will be set in "retry_read" situation */
133 if (buf == NULL || size == 0)
136 if (peer_b->len == 0)
139 return 0; /* writer has closed, and no data is left */
142 BIO_set_retry_read(bio); /* buffer is empty */
143 if (size <= peer_b->size)
144 peer_b->request = size;
146 peer_b->request = peer_b->size; /* don't ask for more than
147 * the peer can deliver
154 if (peer_b->len < size)
157 /* now read "size" bytes */
162 do /* one or two iterations */
166 assert(rest <= peer_b->len);
167 if (peer_b->offset + rest <= peer_b->size)
170 /* wrap around ring buffer */
171 chunk = peer_b->size - peer_b->offset;
172 assert(peer_b->offset + chunk <= peer_b->size);
174 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
176 peer_b->len -= chunk;
179 peer_b->offset += chunk;
180 assert(peer_b->offset <= peer_b->size);
181 if (peer_b->offset == peer_b->size)
187 /* buffer now empty, no need to advance "buf" */
188 assert(chunk == rest);
198 static int bio_write(BIO *bio, char *buf, int num_)
202 struct bio_bio_st *b;
204 BIO_clear_retry_flags(bio);
206 if (!bio->init || buf == NULL || num == 0)
211 assert(b->peer != NULL);
212 assert(b->buf != NULL);
217 /* we already closed */
218 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
222 assert(b->len <= b->size);
224 if (b->len == b->size)
226 BIO_set_retry_write(bio); /* buffer is full */
231 if (num > b->size - b->len)
232 num = b->size - b->len;
234 /* now write "num" bytes */
239 do /* one or two iterations */
244 assert(b->len + rest <= b->size);
246 write_offset = b->offset + b->len;
247 if (write_offset >= b->size)
248 write_offset -= b->size;
249 /* b->buf[write_offset] is the first byte we can write to. */
251 if (write_offset + rest <= b->size)
254 /* wrap around ring buffer */
255 chunk = b->size - write_offset;
257 memcpy(b->buf + write_offset, buf, chunk);
261 assert(b->len <= b->size);
272 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
275 struct bio_bio_st *b = bio->ptr;
281 /* specific CTRL codes */
283 case BIO_C_SET_WRITE_BUF_SIZE:
286 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
291 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
296 size_t new_size = num;
298 if (b->size != new_size)
311 case BIO_C_GET_WRITE_BUF_SIZE:
312 num = (long) b->size;
314 case BIO_C_MAKE_BIO_PAIR:
316 BIO *other_bio = ptr;
318 if (bio_make_pair(bio, other_bio))
325 case BIO_C_DESTROY_BIO_PAIR:
326 /* Effects both BIOs in the pair -- call just once!
327 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
328 bio_destroy_pair(bio);
332 case BIO_C_GET_WRITE_GUARANTEE:
333 /* How many bytes can the caller feed to the next write
334 * withouth having to keep any? */
335 if (b->peer == NULL || b->closed)
338 ret = (long) b->size - b->len;
341 case BIO_C_GET_READ_REQUEST:
342 /* If the peer unsuccesfully tried to read, how many bytes
343 * were requested? (As with BIO_CTRL_PENDING, that number
344 * can usually be treated as boolean.) */
345 ret = (long) b->request;
348 case BIO_C_SHUTDOWN_WR:
349 /* similar to shutdown(..., SHUT_WR) */
355 /* standard CTRL codes follow */
366 case BIO_CTRL_GET_CLOSE:
370 case BIO_CTRL_SET_CLOSE:
371 bio->shutdown = (int) num;
375 case BIO_CTRL_PENDING:
378 struct bio_bio_st *peer_b = b->peer->ptr;
380 ret = (long) peer_b->len;
386 case BIO_CTRL_WPENDING:
394 /* See BIO_dup_chain for circumstances we have to expect. */
396 BIO *other_bio = ptr;
397 struct bio_bio_st *other_b;
399 assert(other_bio != NULL);
400 other_b = other_bio->ptr;
401 assert(other_b != NULL);
403 assert(other_b->buf == NULL); /* other_bio is always fresh */
405 other_b->size = b->size;
417 BIO *other_bio = ptr;
421 struct bio_bio_st *other_b = other_bio->ptr;
423 assert(other_b != NULL);
424 ret = other_b->len == 0 && other_b->closed;
437 static int bio_puts(BIO *bio, char *str)
439 return bio_write(bio, str, strlen(str));
443 static int bio_make_pair(BIO *bio1, BIO *bio2)
445 struct bio_bio_st *b1, *b2;
447 assert(bio1 != NULL);
448 assert(bio2 != NULL);
453 if (b1->peer != NULL || b2->peer != NULL)
455 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
461 b1->buf = Malloc(b1->size);
464 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
473 b2->buf = Malloc(b2->size);
476 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
496 static void bio_destroy_pair(BIO *bio)
498 struct bio_bio_st *b = bio->ptr;
502 BIO *peer_bio = b->peer;
504 if (peer_bio != NULL)
506 struct bio_bio_st *peer_b = peer_bio->ptr;
508 assert(peer_b != NULL);
509 assert(peer_b->peer == bio);
513 assert(peer_b->buf != NULL);
519 assert(b->buf != NULL);
527 /* Exported convenience functions */
528 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
529 BIO **bio2_p, size_t writebuf2)
531 BIO *bio1 = NULL, *bio2 = NULL;
535 bio1 = BIO_new(BIO_s_bio());
538 bio2 = BIO_new(BIO_s_bio());
544 r = BIO_set_write_buf_size(bio1, writebuf1);
550 r = BIO_set_write_buf_size(bio2, writebuf2);
555 r = BIO_make_bio_pair(bio1, bio2);
580 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
582 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
585 size_t BIO_ctrl_get_read_request(BIO *bio)
587 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);