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
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/crypto.h>
22 static int bio_new(BIO *bio);
23 static int bio_free(BIO *bio);
24 static int bio_read(BIO *bio, char *buf, int size);
25 static int bio_write(BIO *bio, char *buf, int num);
26 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
27 static int bio_puts(BIO *bio, char *str);
29 static int bio_make_pair(BIO *bio1, BIO *bio2);
30 static void bio_destroy_pair(BIO *bio);
32 static BIO_METHOD methods_biop =
39 NULL /* no bio_gets */,
45 BIO_METHOD *BIO_s_bio(void)
52 BIO *peer; /* NULL if buf == NULL.
53 * If peer != NULL, then peer->ptr is also a bio_bio_st,
54 * and its "peer" member points back to us.
55 * peer != NULL iff init != 0 in the BIO. */
57 /* This is for what we write (i.e. reading uses peer's struct): */
58 int closed; /* valid iff peer != NULL */
59 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
60 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
62 char *buf; /* "size" elements (if != NULL) */
64 size_t request; /* valid iff peer != NULL; 0 if len != 0;
65 * otherwise set by peer to number of bytes
66 * it (unsuccesfully) tried to read. */
69 static int bio_new(BIO *bio)
73 b = Malloc(sizeof *b);
78 b->size = 17*1024; /* enough for one TLS record (just a default) */
86 static int bio_free(BIO *bio)
97 bio_destroy_pair(bio);
111 static int bio_read(BIO *bio, char *buf, int size_)
115 struct bio_bio_st *b, *peer_b;
117 BIO_clear_retry_flags(bio);
124 assert(b->peer != NULL);
125 peer_b = b->peer->ptr;
126 assert(peer_b != NULL);
127 assert(peer_b->buf != NULL);
129 peer_b->request = 0; /* will be set in "retry_read" situation */
131 if (buf == NULL || size == 0)
134 if (peer_b->len == 0)
137 return 0; /* writer has closed, and no data is left */
140 BIO_set_retry_read(bio); /* buffer is empty */
141 if (size <= peer_b->size)
142 peer_b->request = size;
144 peer_b->request = peer_b->size; /* don't ask for more than
145 * the peer can deliver
152 if (peer_b->len < size)
155 /* now read "size" bytes */
160 do /* one or two iterations */
164 assert(rest <= peer_b->len);
165 if (peer_b->offset + rest <= peer_b->size)
168 /* wrap around ring buffer */
169 chunk = peer_b->size - peer_b->offset;
170 assert(peer_b->offset + chunk <= peer_b->size);
172 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
174 peer_b->len -= chunk;
177 peer_b->offset += chunk;
178 assert(peer_b->offset <= peer_b->size);
179 if (peer_b->offset == peer_b->size)
185 /* buffer now empty, no need to advance "buf" */
186 assert(chunk == rest);
196 static int bio_write(BIO *bio, char *buf, int num_)
200 struct bio_bio_st *b;
202 BIO_clear_retry_flags(bio);
204 if (!bio->init || buf == NULL || num == 0)
209 assert(b->peer != NULL);
210 assert(b->buf != NULL);
215 /* we already closed */
216 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
220 assert(b->len <= b->size);
222 if (b->len == b->size)
224 BIO_set_retry_write(bio); /* buffer is full */
229 if (num > b->size - b->len)
230 num = b->size - b->len;
232 /* now write "num" bytes */
237 do /* one or two iterations */
242 assert(b->len + rest <= b->size);
244 write_offset = b->offset + b->len;
245 if (write_offset >= b->size)
246 write_offset -= b->size;
247 /* b->buf[write_offset] is the first byte we can write to. */
249 if (write_offset + rest <= b->size)
252 /* wrap around ring buffer */
253 chunk = b->size - write_offset;
255 memcpy(b->buf + write_offset, buf, chunk);
259 assert(b->len <= b->size);
270 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
273 struct bio_bio_st *b = bio->ptr;
279 /* specific CTRL codes */
281 case BIO_C_SET_WRITE_BUF_SIZE:
284 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
289 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
294 size_t new_size = num;
296 if (b->size != new_size)
309 case BIO_C_GET_WRITE_BUF_SIZE:
310 num = (long) b->size;
312 case BIO_C_MAKE_BIO_PAIR:
314 BIO *other_bio = ptr;
316 if (bio_make_pair(bio, other_bio))
323 case BIO_C_DESTROY_BIO_PAIR:
324 /* Effects both BIOs in the pair -- call just once!
325 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
326 bio_destroy_pair(bio);
330 case BIO_C_GET_WRITE_GUARANTEE:
331 /* How many bytes can the caller feed to the next write
332 * withouth having to keep any? */
333 if (b->peer == NULL || b->closed)
336 ret = (long) b->size - b->len;
339 case BIO_C_GET_READ_REQUEST:
340 /* If the peer unsuccesfully tried to read, how many bytes
341 * were requested? (As with BIO_CTRL_PENDING, that number
342 * can usually be treated as boolean.) */
343 ret = (long) b->request;
346 case BIO_C_SHUTDOWN_WR:
347 /* similar to shutdown(..., SHUT_WR) */
353 /* standard CTRL codes follow */
364 case BIO_CTRL_GET_CLOSE:
368 case BIO_CTRL_SET_CLOSE:
369 bio->shutdown = (int) num;
373 case BIO_CTRL_PENDING:
376 struct bio_bio_st *peer_b =b->peer->ptr;
378 ret = (long) peer_b->len;
384 case BIO_CTRL_WPENDING:
392 /* See BIO_dup_chain for circumstances we have to expect. */
394 BIO *other_bio = ptr;
395 struct bio_bio_st *other_b;
397 assert(other_bio != NULL);
398 other_b = other_bio->ptr;
399 assert(other_b != NULL);
401 assert(other_b->buf == NULL); /* other_bio is always fresh */
403 other_b->size = b->size;
415 BIO *other_bio = ptr;
419 struct bio_bio_st *other_b = other_bio->ptr;
421 assert(other_b != NULL);
422 ret = other_b->len == 0 && other_b->closed;
435 static int bio_puts(BIO *bio, char *str)
437 return bio_write(bio, str, strlen(str));
441 static int bio_make_pair(BIO *bio1, BIO *bio2)
443 struct bio_bio_st *b1, *b2;
445 assert(bio1 != NULL);
446 assert(bio2 != NULL);
451 if (b1->peer != NULL || b2->peer != NULL)
453 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
459 b1->buf = Malloc(b1->size);
462 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
471 b2->buf = Malloc(b2->size);
474 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
494 static void bio_destroy_pair(BIO *bio)
496 struct bio_bio_st *b = bio->ptr;
500 BIO *peer_bio = b->peer;
502 if (peer_bio != NULL)
504 struct bio_bio_st *peer_b = peer_bio->ptr;
506 assert(peer_b != NULL);
507 assert(peer_b->peer == bio);
511 assert(peer_b->buf != NULL);
517 assert(b->buf != NULL);
525 /* Exported convenience functions */
526 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
527 BIO **bio2_p, size_t writebuf2)
529 BIO *bio1 = NULL, *bio2 = NULL;
533 bio1 = BIO_new(BIO_s_bio());
536 bio2 = BIO_new(BIO_s_bio());
542 r = BIO_set_write_buf_size(bio1, writebuf1);
548 r = BIO_set_write_buf_size(bio2, writebuf2);
553 r = BIO_make_bio_pair(bio1, bio2);
578 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
580 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
583 size_t BIO_ctrl_get_read_request(BIO *bio)
585 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);