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. */
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
18 static int bio_new(BIO *bio);
19 static int bio_free(BIO *bio);
20 static int bio_read(BIO *bio, char *buf, int size);
21 static int bio_write(BIO *bio, char *buf, int num);
22 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
23 static int bio_puts(BIO *bio, char *str);
25 static int bio_make_pair(BIO *bio1, BIO *bio2);
26 static void bio_destroy_pair(BIO *bio);
28 static BIO_METHOD methods_biop =
35 NULL /* no bio_gets */,
41 BIO_METHOD *BIO_s_bio(void)
48 BIO *peer; /* NULL if buf == NULL.
49 * If peer != NULL, then peer->ptr is also a bio_bio_st,
50 * and its "peer" member points back to us.
51 * peer != NULL iff init != 0 in the BIO. */
53 /* This is for what we write (i.e. reading uses peer's struct): */
54 int closed; /* valid iff peer != NULL */
55 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
56 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
58 char *buf; /* "size" elements (if != NULL) */
60 size_t request; /* valid iff peer != NULL; 0 if len != 0;
61 * otherwise set by peer to number of bytes
62 * it (unsuccesfully) tried to read. */
65 static int bio_new(BIO *bio)
69 b = Malloc(sizeof *b);
74 b->size = 17*1024; /* enough for one TLS record (just a default) */
82 static int bio_free(BIO *bio)
93 bio_destroy_pair(bio);
107 static int bio_read(BIO *bio, char *buf, int size_)
111 struct bio_bio_st *b, *peer_b;
113 BIO_clear_retry_flags(bio);
120 assert(b->peer != NULL);
121 peer_b = b->peer->ptr;
122 assert(peer_b != NULL);
123 assert(peer_b->buf != NULL);
125 peer_b->request = 0; /* will be set in "retry_read" situation */
127 if (buf == NULL || size == 0)
130 if (peer_b->len == 0)
133 return 0; /* writer has closed, and no data is left */
136 BIO_set_retry_read(bio); /* buffer is empty */
137 if (size <= peer_b->size)
138 peer_b->request = size;
140 peer_b->request = peer_b->size; /* don't ask for more than
141 * the peer can deliver
148 if (peer_b->len < size)
151 /* now read "size" bytes */
156 do /* one or two iterations */
160 assert(rest <= peer_b->len);
161 if (peer_b->offset + rest <= peer_b->size)
164 /* wrap around ring buffer */
165 chunk = peer_b->size - peer_b->offset;
166 assert(peer_b->offset + chunk <= peer_b->size);
168 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
170 peer_b->len -= chunk;
173 peer_b->offset += chunk;
174 assert(peer_b->offset <= peer_b->size);
175 if (peer_b->offset == peer_b->size)
181 /* buffer now empty, no need to advance "buf" */
182 assert(chunk == rest);
189 peer_b->request -= size;
193 static int bio_write(BIO *bio, char *buf, int num_)
197 struct bio_bio_st *b;
199 BIO_clear_retry_flags(bio);
201 if (!bio->init || buf == NULL || num == 0)
206 assert(b->peer != NULL);
207 assert(b->buf != NULL);
212 /* we already closed */
213 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
217 assert(b->len <= b->size);
219 if (b->len == b->size)
221 BIO_set_retry_write(bio); /* buffer is full */
226 if (num > b->size - b->len)
227 num = b->size - b->len;
229 /* now write "num" bytes */
234 do /* one or two iterations */
239 assert(b->len + rest <= b->size);
241 write_offset = b->offset + b->len;
242 if (write_offset >= b->size)
243 write_offset -= b->size;
244 /* b->buf[write_offset] is the first byte we can write to. */
246 if (write_offset + rest <= b->size)
249 /* wrap around ring buffer */
250 chunk = b->size - write_offset;
252 memcpy(b->buf + write_offset, buf, chunk);
256 assert(b->len <= b->size);
267 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
270 struct bio_bio_st *b = bio->ptr;
276 /* specific CTRL codes */
278 case BIO_C_SET_WRITE_BUF_SIZE:
281 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
286 size_t new_size = num;
288 if (b->size != new_size)
301 case BIO_C_GET_WRITE_BUF_SIZE:
302 num = (long) b->size;
304 case BIO_C_MAKE_BIO_PAIR:
306 BIO *other_bio = ptr;
308 if (bio_make_pair(bio, other_bio))
315 case BIO_C_DESTROY_BIO_PAIR:
316 /* Effects both BIOs in the pair -- call just once!
317 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
318 bio_destroy_pair(bio);
322 case BIO_C_GET_WRITE_GUARANTEE:
323 /* How many bytes can the caller feed to the next write
324 * withouth having to keep any? */
325 if (b->peer == NULL || b->closed)
328 ret = (long) b->size - b->len;
331 case BIO_C_GET_READ_REQUEST:
332 /* If the peer unsuccesfully tried to read, how many bytes
333 * were requested? (As with BIO_CTRL_PENDING, that number
334 * can usually be treated as boolean.) */
335 ret = (long) b->request;
338 case BIO_C_SHUTDOWN_WR:
339 /* similar to shutdown(..., SHUT_WR) */
345 /* standard CTRL codes follow */
356 case BIO_CTRL_GET_CLOSE:
360 case BIO_CTRL_SET_CLOSE:
361 bio->shutdown = (int) num;
365 case BIO_CTRL_PENDING:
368 struct bio_bio_st *peer_b =b->peer->ptr;
370 ret = (long) peer_b->len;
376 case BIO_CTRL_WPENDING:
384 /* See BIO_dup_chain for circumstances we have to expect. */
386 BIO *other_bio = ptr;
387 struct bio_bio_st *other_b;
389 assert(other_bio != NULL);
390 other_b = other_bio->ptr;
391 assert(other_b != NULL);
393 assert(other_b->buf == NULL); /* other_bio is always fresh */
395 other_b->size = b->size;
407 BIO *other_bio = ptr;
411 struct bio_bio_st *other_b = other_bio->ptr;
413 assert(other_b != NULL);
414 ret = other_b->len == 0 && other_b->closed;
427 static int bio_puts(BIO *bio, char *str)
429 return bio_write(bio, str, strlen(str));
433 static int bio_make_pair(BIO *bio1, BIO *bio2)
435 struct bio_bio_st *b1, *b2;
437 assert(bio1 != NULL);
438 assert(bio2 != NULL);
443 if (b1->peer != NULL || b2->peer != NULL)
445 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
451 b1->buf = Malloc(b1->size);
454 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
463 b2->buf = Malloc(b2->size);
466 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
486 static void bio_destroy_pair(BIO *bio)
488 struct bio_bio_st *b = bio->ptr;
492 BIO *peer_bio = b->peer;
494 if (peer_bio != NULL)
496 struct bio_bio_st *peer_b = peer_bio->ptr;
498 assert(peer_b != NULL);
499 assert(peer_b->peer == bio);
503 assert(peer_b->buf != NULL);
509 assert(b->buf != NULL);
517 /* Exported convenience functions */
518 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
519 BIO **bio2_p, size_t writebuf2)
521 BIO *bio1 = NULL, *bio2 = NULL;
525 bio1 = BIO_new(BIO_s_bio());
528 bio2 = BIO_new(BIO_s_bio());
534 r = BIO_set_write_buf_size(bio1, writebuf1);
540 r = BIO_set_write_buf_size(bio2, writebuf2);
545 r = BIO_make_bio_pair(bio1, bio2);
570 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
572 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
575 size_t BIO_ctrl_read_request(BIO *bio)
577 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);