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);
192 static int bio_write(BIO *bio, char *buf, int num_)
196 struct bio_bio_st *b;
198 BIO_clear_retry_flags(bio);
200 if (!bio->init || buf == NULL || num == 0)
205 assert(b->peer != NULL);
206 assert(b->buf != NULL);
211 /* we already closed */
212 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
216 assert(b->len <= b->size);
218 if (b->len == b->size)
220 BIO_set_retry_write(bio); /* buffer is full */
225 if (num > b->size - b->len)
226 num = b->size - b->len;
228 /* now write "num" bytes */
233 do /* one or two iterations */
238 assert(b->len + rest <= b->size);
240 write_offset = b->offset + b->len;
241 if (write_offset >= b->size)
242 write_offset -= b->size;
243 /* b->buf[write_offset] is the first byte we can write to. */
245 if (write_offset + rest <= b->size)
248 /* wrap around ring buffer */
249 chunk = b->size - write_offset;
251 memcpy(b->buf + write_offset, buf, chunk);
255 assert(b->len <= b->size);
266 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
269 struct bio_bio_st *b = bio->ptr;
275 /* specific CTRL codes */
277 case BIO_C_SET_WRITE_BUF_SIZE:
280 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
285 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
290 size_t new_size = num;
292 if (b->size != new_size)
305 case BIO_C_GET_WRITE_BUF_SIZE:
306 num = (long) b->size;
308 case BIO_C_MAKE_BIO_PAIR:
310 BIO *other_bio = ptr;
312 if (bio_make_pair(bio, other_bio))
319 case BIO_C_DESTROY_BIO_PAIR:
320 /* Effects both BIOs in the pair -- call just once!
321 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
322 bio_destroy_pair(bio);
326 case BIO_C_GET_WRITE_GUARANTEE:
327 /* How many bytes can the caller feed to the next write
328 * withouth having to keep any? */
329 if (b->peer == NULL || b->closed)
332 ret = (long) b->size - b->len;
335 case BIO_C_GET_READ_REQUEST:
336 /* If the peer unsuccesfully tried to read, how many bytes
337 * were requested? (As with BIO_CTRL_PENDING, that number
338 * can usually be treated as boolean.) */
339 ret = (long) b->request;
342 case BIO_C_SHUTDOWN_WR:
343 /* similar to shutdown(..., SHUT_WR) */
349 /* standard CTRL codes follow */
360 case BIO_CTRL_GET_CLOSE:
364 case BIO_CTRL_SET_CLOSE:
365 bio->shutdown = (int) num;
369 case BIO_CTRL_PENDING:
372 struct bio_bio_st *peer_b =b->peer->ptr;
374 ret = (long) peer_b->len;
380 case BIO_CTRL_WPENDING:
388 /* See BIO_dup_chain for circumstances we have to expect. */
390 BIO *other_bio = ptr;
391 struct bio_bio_st *other_b;
393 assert(other_bio != NULL);
394 other_b = other_bio->ptr;
395 assert(other_b != NULL);
397 assert(other_b->buf == NULL); /* other_bio is always fresh */
399 other_b->size = b->size;
411 BIO *other_bio = ptr;
415 struct bio_bio_st *other_b = other_bio->ptr;
417 assert(other_b != NULL);
418 ret = other_b->len == 0 && other_b->closed;
431 static int bio_puts(BIO *bio, char *str)
433 return bio_write(bio, str, strlen(str));
437 static int bio_make_pair(BIO *bio1, BIO *bio2)
439 struct bio_bio_st *b1, *b2;
441 assert(bio1 != NULL);
442 assert(bio2 != NULL);
447 if (b1->peer != NULL || b2->peer != NULL)
449 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
455 b1->buf = Malloc(b1->size);
458 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
467 b2->buf = Malloc(b2->size);
470 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
490 static void bio_destroy_pair(BIO *bio)
492 struct bio_bio_st *b = bio->ptr;
496 BIO *peer_bio = b->peer;
498 if (peer_bio != NULL)
500 struct bio_bio_st *peer_b = peer_bio->ptr;
502 assert(peer_b != NULL);
503 assert(peer_b->peer == bio);
507 assert(peer_b->buf != NULL);
513 assert(b->buf != NULL);
521 /* Exported convenience functions */
522 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
523 BIO **bio2_p, size_t writebuf2)
525 BIO *bio1 = NULL, *bio2 = NULL;
529 bio1 = BIO_new(BIO_s_bio());
532 bio2 = BIO_new(BIO_s_bio());
538 r = BIO_set_write_buf_size(bio1, writebuf1);
544 r = BIO_set_write_buf_size(bio2, writebuf2);
549 r = BIO_make_bio_pair(bio1, bio2);
574 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
576 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
579 size_t BIO_ctrl_read_request(BIO *bio)
581 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);