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. */
70 static int bio_new(BIO *bio)
74 b = Malloc(sizeof *b);
79 b->size = 17*1024; /* enough for one TLS record (just a default) */
87 static int bio_free(BIO *bio)
98 bio_destroy_pair(bio);
112 static int bio_read(BIO *bio, char *buf, int size_)
116 struct bio_bio_st *b, *peer_b;
118 BIO_clear_retry_flags(bio);
125 assert(b->peer != NULL);
126 peer_b = b->peer->ptr;
127 assert(peer_b != NULL);
128 assert(peer_b->buf != NULL);
130 peer_b->request = 0; /* will be set in "retry_read" situation */
132 if (buf == NULL || size == 0)
135 if (peer_b->len == 0)
138 return 0; /* writer has closed, and no data is left */
141 BIO_set_retry_read(bio); /* buffer is empty */
142 if (size <= peer_b->size)
143 peer_b->request = size;
145 peer_b->request = peer_b->size; /* don't ask for more than
146 * the peer can deliver
153 if (peer_b->len < size)
156 /* now read "size" bytes */
161 do /* one or two iterations */
165 assert(rest <= peer_b->len);
166 if (peer_b->offset + rest <= peer_b->size)
169 /* wrap around ring buffer */
170 chunk = peer_b->size - peer_b->offset;
171 assert(peer_b->offset + chunk <= peer_b->size);
173 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
175 peer_b->len -= chunk;
178 peer_b->offset += chunk;
179 assert(peer_b->offset <= peer_b->size);
180 if (peer_b->offset == peer_b->size)
186 /* buffer now empty, no need to advance "buf" */
187 assert(chunk == rest);
197 static int bio_write(BIO *bio, char *buf, int num_)
201 struct bio_bio_st *b;
203 BIO_clear_retry_flags(bio);
205 if (!bio->init || buf == NULL || num == 0)
210 assert(b->peer != NULL);
211 assert(b->buf != NULL);
216 /* we already closed */
217 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
221 assert(b->len <= b->size);
223 if (b->len == b->size)
225 BIO_set_retry_write(bio); /* buffer is full */
230 if (num > b->size - b->len)
231 num = b->size - b->len;
233 /* now write "num" bytes */
238 do /* one or two iterations */
243 assert(b->len + rest <= b->size);
245 write_offset = b->offset + b->len;
246 if (write_offset >= b->size)
247 write_offset -= b->size;
248 /* b->buf[write_offset] is the first byte we can write to. */
250 if (write_offset + rest <= b->size)
253 /* wrap around ring buffer */
254 chunk = b->size - write_offset;
256 memcpy(b->buf + write_offset, buf, chunk);
260 assert(b->len <= b->size);
271 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
274 struct bio_bio_st *b = bio->ptr;
280 /* specific CTRL codes */
282 case BIO_C_SET_WRITE_BUF_SIZE:
285 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
290 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
295 size_t new_size = num;
297 if (b->size != new_size)
310 case BIO_C_GET_WRITE_BUF_SIZE:
311 num = (long) b->size;
313 case BIO_C_MAKE_BIO_PAIR:
315 BIO *other_bio = ptr;
317 if (bio_make_pair(bio, other_bio))
324 case BIO_C_DESTROY_BIO_PAIR:
325 /* Effects both BIOs in the pair -- call just once!
326 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
327 bio_destroy_pair(bio);
331 case BIO_C_GET_WRITE_GUARANTEE:
332 /* How many bytes can the caller feed to the next write
333 * withouth having to keep any? */
334 if (b->peer == NULL || b->closed)
337 ret = (long) b->size - b->len;
340 case BIO_C_GET_READ_REQUEST:
341 /* If the peer unsuccesfully tried to read, how many bytes
342 * were requested? (As with BIO_CTRL_PENDING, that number
343 * can usually be treated as boolean.) */
344 ret = (long) b->request;
347 case BIO_C_SHUTDOWN_WR:
348 /* similar to shutdown(..., SHUT_WR) */
354 /* standard CTRL codes follow */
365 case BIO_CTRL_GET_CLOSE:
369 case BIO_CTRL_SET_CLOSE:
370 bio->shutdown = (int) num;
374 case BIO_CTRL_PENDING:
377 struct bio_bio_st *peer_b = b->peer->ptr;
379 ret = (long) peer_b->len;
385 case BIO_CTRL_WPENDING:
393 /* See BIO_dup_chain for circumstances we have to expect. */
395 BIO *other_bio = ptr;
396 struct bio_bio_st *other_b;
398 assert(other_bio != NULL);
399 other_b = other_bio->ptr;
400 assert(other_b != NULL);
402 assert(other_b->buf == NULL); /* other_bio is always fresh */
404 other_b->size = b->size;
416 BIO *other_bio = ptr;
420 struct bio_bio_st *other_b = other_bio->ptr;
422 assert(other_b != NULL);
423 ret = other_b->len == 0 && other_b->closed;
436 static int bio_puts(BIO *bio, char *str)
438 return bio_write(bio, str, strlen(str));
442 static int bio_make_pair(BIO *bio1, BIO *bio2)
444 struct bio_bio_st *b1, *b2;
446 assert(bio1 != NULL);
447 assert(bio2 != NULL);
452 if (b1->peer != NULL || b2->peer != NULL)
454 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
460 b1->buf = Malloc(b1->size);
463 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
472 b2->buf = Malloc(b2->size);
475 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
495 static void bio_destroy_pair(BIO *bio)
497 struct bio_bio_st *b = bio->ptr;
501 BIO *peer_bio = b->peer;
503 if (peer_bio != NULL)
505 struct bio_bio_st *peer_b = peer_bio->ptr;
507 assert(peer_b != NULL);
508 assert(peer_b->peer == bio);
512 assert(peer_b->buf != NULL);
518 assert(b->buf != NULL);
526 /* Exported convenience functions */
527 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
528 BIO **bio2_p, size_t writebuf2)
530 BIO *bio1 = NULL, *bio2 = NULL;
534 bio1 = BIO_new(BIO_s_bio());
537 bio2 = BIO_new(BIO_s_bio());
543 r = BIO_set_write_buf_size(bio1, writebuf1);
549 r = BIO_set_write_buf_size(bio2, writebuf2);
554 r = BIO_make_bio_pair(bio1, bio2);
579 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
581 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
584 size_t BIO_ctrl_get_read_request(BIO *bio)
586 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);