Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/bio/bss_bio.c

raw
1 /*
2 * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * Special method for a BIO where the other endpoint is also a BIO of this
12 * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
13 * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
14 * library with I/O interfaces for which no specific BIO method is available.
15 * See ssl/ssltest.c for some hints on how this can be used.
16 */
17
18 #include "internal/e_os.h"
19 #include <assert.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "bio_local.h"
25 #include <openssl/err.h>
26 #include <openssl/crypto.h>
27
28 static int bio_new(BIO *bio);
29 static int bio_free(BIO *bio);
30 static int bio_read(BIO *bio, char *buf, int size);
31 static int bio_write(BIO *bio, const char *buf, int num);
32 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
33 static int bio_puts(BIO *bio, const char *str);
34
35 static int bio_make_pair(BIO *bio1, BIO *bio2);
36 static void bio_destroy_pair(BIO *bio);
37
38 static const BIO_METHOD methods_biop = {
39 BIO_TYPE_BIO,
40 "BIO pair",
41 bwrite_conv,
42 bio_write,
43 bread_conv,
44 bio_read,
45 bio_puts,
46 NULL /* no bio_gets */,
47 bio_ctrl,
48 bio_new,
49 bio_free,
50 NULL /* no bio_callback_ctrl */
51 };
52
53 const BIO_METHOD *BIO_s_bio(void)
54 {
55 return &methods_biop;
56 }
57
58 struct bio_bio_st {
59 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
60 * peer->ptr is also a bio_bio_st, and its
61 * "peer" member points back to us. peer !=
62 * NULL iff init != 0 in the BIO. */
63 /* This is for what we write (i.e. reading uses peer's struct): */
64 int closed; /* valid iff peer != NULL */
65 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
66 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
67 size_t size;
68 char *buf; /* "size" elements (if != NULL) */
69 size_t request; /* valid iff peer != NULL; 0 if len != 0,
70 * otherwise set by peer to number of bytes
71 * it (unsuccessfully) tried to read, never
72 * more than buffer space (size-len)
73 * warrants. */
74 };
75
76 static int bio_new(BIO *bio)
77 {
78 struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
79
80 if (b == NULL)
81 return 0;
82
83 /* enough for one TLS record (just a default) */
84 b->size = 17 * 1024;
85
86 bio->ptr = b;
87 return 1;
88 }
89
90 static int bio_free(BIO *bio)
91 {
92 struct bio_bio_st *b;
93
94 if (bio == NULL)
95 return 0;
96 b = bio->ptr;
97
98 assert(b != NULL);
99
100 if (b->peer)
101 bio_destroy_pair(bio);
102
103 OPENSSL_free(b->buf);
104 OPENSSL_free(b);
105
106 return 1;
107 }
108
109 static int bio_read(BIO *bio, char *buf, int size_)
110 {
111 size_t size = size_;
112 size_t rest;
113 struct bio_bio_st *b, *peer_b;
114
115 BIO_clear_retry_flags(bio);
116
117 if (!bio->init)
118 return 0;
119
120 b = bio->ptr;
121 assert(b != NULL);
122 assert(b->peer != NULL);
123 peer_b = b->peer->ptr;
124 assert(peer_b != NULL);
125 assert(peer_b->buf != NULL);
126
127 peer_b->request = 0; /* will be set in "retry_read" situation */
128
129 if (buf == NULL || size == 0)
130 return 0;
131
132 if (peer_b->len == 0) {
133 if (peer_b->closed)
134 return 0; /* writer has closed, and no data is left */
135 else {
136 BIO_set_retry_read(bio); /* buffer is empty */
137 if (size <= peer_b->size)
138 peer_b->request = size;
139 else
140 /*
141 * don't ask for more than the peer can deliver in one write
142 */
143 peer_b->request = peer_b->size;
144 return -1;
145 }
146 }
147
148 /* we can read */
149 if (peer_b->len < size)
150 size = peer_b->len;
151
152 /* now read "size" bytes */
153
154 rest = size;
155
156 assert(rest > 0);
157 do { /* one or two iterations */
158 size_t chunk;
159
160 assert(rest <= peer_b->len);
161 if (peer_b->offset + rest <= peer_b->size)
162 chunk = rest;
163 else
164 /* wrap around ring buffer */
165 chunk = peer_b->size - peer_b->offset;
166 assert(peer_b->offset + chunk <= peer_b->size);
167
168 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
169
170 peer_b->len -= chunk;
171 if (peer_b->len) {
172 peer_b->offset += chunk;
173 assert(peer_b->offset <= peer_b->size);
174 if (peer_b->offset == peer_b->size)
175 peer_b->offset = 0;
176 buf += chunk;
177 } else {
178 /* buffer now empty, no need to advance "buf" */
179 assert(chunk == rest);
180 peer_b->offset = 0;
181 }
182 rest -= chunk;
183 } while (rest);
184
185 return (int)size;
186 }
187
188 /*-
189 * non-copying interface: provide pointer to available data in buffer
190 * bio_nread0: return number of available bytes
191 * bio_nread: also advance index
192 * (example usage: bio_nread0(), read from buffer, bio_nread()
193 * or just bio_nread(), read from buffer)
194 */
195 /*
196 * WARNING: The non-copying interface is largely untested as of yet and may
197 * contain bugs.
198 */
199 static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
200 {
201 struct bio_bio_st *b, *peer_b;
202 ossl_ssize_t num;
203
204 BIO_clear_retry_flags(bio);
205
206 if (!bio->init)
207 return 0;
208
209 b = bio->ptr;
210 assert(b != NULL);
211 assert(b->peer != NULL);
212 peer_b = b->peer->ptr;
213 assert(peer_b != NULL);
214 assert(peer_b->buf != NULL);
215
216 peer_b->request = 0;
217
218 if (peer_b->len == 0) {
219 char dummy;
220
221 /* avoid code duplication -- nothing available for reading */
222 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
223 }
224
225 num = peer_b->len;
226 if (peer_b->size < peer_b->offset + num)
227 /* no ring buffer wrap-around for non-copying interface */
228 num = peer_b->size - peer_b->offset;
229 assert(num > 0);
230
231 if (buf != NULL)
232 *buf = peer_b->buf + peer_b->offset;
233 return num;
234 }
235
236 static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
237 {
238 struct bio_bio_st *b, *peer_b;
239 ossl_ssize_t num, available;
240
241 if (num_ > OSSL_SSIZE_MAX)
242 num = OSSL_SSIZE_MAX;
243 else
244 num = (ossl_ssize_t)num_;
245
246 available = bio_nread0(bio, buf);
247 if (num > available)
248 num = available;
249 if (num <= 0)
250 return num;
251
252 b = bio->ptr;
253 peer_b = b->peer->ptr;
254
255 peer_b->len -= num;
256 if (peer_b->len) {
257 peer_b->offset += num;
258 assert(peer_b->offset <= peer_b->size);
259 if (peer_b->offset == peer_b->size)
260 peer_b->offset = 0;
261 } else
262 peer_b->offset = 0;
263
264 return num;
265 }
266
267 static int bio_write(BIO *bio, const char *buf, int num_)
268 {
269 size_t num = num_;
270 size_t rest;
271 struct bio_bio_st *b;
272
273 BIO_clear_retry_flags(bio);
274
275 if (!bio->init || buf == NULL || num_ <= 0)
276 return 0;
277
278 b = bio->ptr;
279 assert(b != NULL);
280 assert(b->peer != NULL);
281 assert(b->buf != NULL);
282
283 b->request = 0;
284 if (b->closed) {
285 /* we already closed */
286 ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
287 return -1;
288 }
289
290 assert(b->len <= b->size);
291
292 if (b->len == b->size) {
293 BIO_set_retry_write(bio); /* buffer is full */
294 return -1;
295 }
296
297 /* we can write */
298 if (num > b->size - b->len)
299 num = b->size - b->len;
300
301 /* now write "num" bytes */
302
303 rest = num;
304
305 assert(rest > 0);
306 do { /* one or two iterations */
307 size_t write_offset;
308 size_t chunk;
309
310 assert(b->len + rest <= b->size);
311
312 write_offset = b->offset + b->len;
313 if (write_offset >= b->size)
314 write_offset -= b->size;
315 /* b->buf[write_offset] is the first byte we can write to. */
316
317 if (write_offset + rest <= b->size)
318 chunk = rest;
319 else
320 /* wrap around ring buffer */
321 chunk = b->size - write_offset;
322
323 memcpy(b->buf + write_offset, buf, chunk);
324
325 b->len += chunk;
326
327 assert(b->len <= b->size);
328
329 rest -= chunk;
330 buf += chunk;
331 } while (rest);
332
333 return (int)num;
334 }
335
336 /*-
337 * non-copying interface: provide pointer to region to write to
338 * bio_nwrite0: check how much space is available
339 * bio_nwrite: also increase length
340 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
341 * or just bio_nwrite(), write to buffer)
342 */
343 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
344 {
345 struct bio_bio_st *b;
346 size_t num;
347 size_t write_offset;
348
349 BIO_clear_retry_flags(bio);
350
351 if (!bio->init)
352 return 0;
353
354 b = bio->ptr;
355 assert(b != NULL);
356 assert(b->peer != NULL);
357 assert(b->buf != NULL);
358
359 b->request = 0;
360 if (b->closed) {
361 ERR_raise(ERR_LIB_BIO, BIO_R_BROKEN_PIPE);
362 return -1;
363 }
364
365 assert(b->len <= b->size);
366
367 if (b->len == b->size) {
368 BIO_set_retry_write(bio);
369 return -1;
370 }
371
372 num = b->size - b->len;
373 write_offset = b->offset + b->len;
374 if (write_offset >= b->size)
375 write_offset -= b->size;
376 if (write_offset + num > b->size)
377 /*
378 * no ring buffer wrap-around for non-copying interface (to fulfil
379 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
380 * to be called twice)
381 */
382 num = b->size - write_offset;
383
384 if (buf != NULL)
385 *buf = b->buf + write_offset;
386 assert(write_offset + num <= b->size);
387
388 return num;
389 }
390
391 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
392 {
393 struct bio_bio_st *b;
394 ossl_ssize_t num, space;
395
396 if (num_ > OSSL_SSIZE_MAX)
397 num = OSSL_SSIZE_MAX;
398 else
399 num = (ossl_ssize_t)num_;
400
401 space = bio_nwrite0(bio, buf);
402 if (num > space)
403 num = space;
404 if (num <= 0)
405 return num;
406 b = bio->ptr;
407 assert(b != NULL);
408 b->len += num;
409 assert(b->len <= b->size);
410
411 return num;
412 }
413
414 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
415 {
416 long ret;
417 struct bio_bio_st *b = bio->ptr;
418
419 assert(b != NULL);
420
421 switch (cmd) {
422 /* specific CTRL codes */
423
424 case BIO_C_SET_WRITE_BUF_SIZE:
425 if (b->peer) {
426 ERR_raise(ERR_LIB_BIO, BIO_R_IN_USE);
427 ret = 0;
428 } else if (num == 0) {
429 ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
430 ret = 0;
431 } else {
432 size_t new_size = num;
433
434 if (b->size != new_size) {
435 OPENSSL_free(b->buf);
436 b->buf = NULL;
437 b->size = new_size;
438 }
439 ret = 1;
440 }
441 break;
442
443 case BIO_C_GET_WRITE_BUF_SIZE:
444 ret = (long)b->size;
445 break;
446
447 case BIO_C_MAKE_BIO_PAIR: {
448 BIO *other_bio = ptr;
449
450 if (bio_make_pair(bio, other_bio))
451 ret = 1;
452 else
453 ret = 0;
454 } break;
455
456 case BIO_C_DESTROY_BIO_PAIR:
457 /*
458 * Affects both BIOs in the pair -- call just once! Or let
459 * BIO_free(bio1); BIO_free(bio2); do the job.
460 */
461 bio_destroy_pair(bio);
462 ret = 1;
463 break;
464
465 case BIO_C_GET_WRITE_GUARANTEE:
466 /*
467 * How many bytes can the caller feed to the next write without
468 * having to keep any?
469 */
470 if (b->peer == NULL || b->closed)
471 ret = 0;
472 else
473 ret = (long)(b->size - b->len);
474 break;
475
476 case BIO_C_GET_READ_REQUEST:
477 /*
478 * If the peer unsuccessfully tried to read, how many bytes were
479 * requested? (As with BIO_CTRL_PENDING, that number can usually be
480 * treated as boolean.)
481 */
482 ret = (long)b->request;
483 break;
484
485 case BIO_C_RESET_READ_REQUEST:
486 /*
487 * Reset request. (Can be useful after read attempts at the other
488 * side that are meant to be non-blocking, e.g. when probing SSL_read
489 * to see if any data is available.)
490 */
491 b->request = 0;
492 ret = 1;
493 break;
494
495 case BIO_C_SHUTDOWN_WR:
496 /* similar to shutdown(..., SHUT_WR) */
497 b->closed = 1;
498 ret = 1;
499 break;
500
501 case BIO_C_NREAD0:
502 /* prepare for non-copying read */
503 ret = (long)bio_nread0(bio, ptr);
504 break;
505
506 case BIO_C_NREAD:
507 /* non-copying read */
508 ret = (long)bio_nread(bio, ptr, (size_t)num);
509 break;
510
511 case BIO_C_NWRITE0:
512 /* prepare for non-copying write */
513 ret = (long)bio_nwrite0(bio, ptr);
514 break;
515
516 case BIO_C_NWRITE:
517 /* non-copying write */
518 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
519 break;
520
521 /* standard CTRL codes follow */
522
523 case BIO_CTRL_RESET:
524 if (b->buf != NULL) {
525 b->len = 0;
526 b->offset = 0;
527 }
528 ret = 0;
529 break;
530
531 case BIO_CTRL_GET_CLOSE:
532 ret = bio->shutdown;
533 break;
534
535 case BIO_CTRL_SET_CLOSE:
536 bio->shutdown = (int)num;
537 ret = 1;
538 break;
539
540 case BIO_CTRL_PENDING:
541 if (b->peer != NULL) {
542 struct bio_bio_st *peer_b = b->peer->ptr;
543
544 ret = (long)peer_b->len;
545 } else
546 ret = 0;
547 break;
548
549 case BIO_CTRL_WPENDING:
550 if (b->buf != NULL)
551 ret = (long)b->len;
552 else
553 ret = 0;
554 break;
555
556 case BIO_CTRL_DUP:
557 /* See BIO_dup_chain for circumstances we have to expect. */
558 {
559 BIO *other_bio = ptr;
560 struct bio_bio_st *other_b;
561
562 assert(other_bio != NULL);
563 other_b = other_bio->ptr;
564 assert(other_b != NULL);
565
566 assert(other_b->buf == NULL); /* other_bio is always fresh */
567
568 other_b->size = b->size;
569 }
570
571 ret = 1;
572 break;
573
574 case BIO_CTRL_FLUSH:
575 ret = 1;
576 break;
577
578 case BIO_CTRL_EOF:
579 if (b->peer != NULL) {
580 struct bio_bio_st *peer_b = b->peer->ptr;
581
582 if (peer_b->len == 0 && peer_b->closed)
583 ret = 1;
584 else
585 ret = 0;
586 } else {
587 ret = 1;
588 }
589 break;
590
591 default:
592 ret = 0;
593 }
594 return ret;
595 }
596
597 static int bio_puts(BIO *bio, const char *str)
598 {
599 size_t len = strlen(str);
600
601 if (len > INT_MAX)
602 return -1;
603 return bio_write(bio, str, (int)len);
604 }
605
606 static int bio_make_pair(BIO *bio1, BIO *bio2)
607 {
608 struct bio_bio_st *b1, *b2;
609
610 assert(bio1 != NULL);
611 assert(bio2 != NULL);
612
613 b1 = bio1->ptr;
614 b2 = bio2->ptr;
615
616 if (b1->peer != NULL || b2->peer != NULL) {
617 ERR_raise(ERR_LIB_BIO, BIO_R_IN_USE);
618 return 0;
619 }
620
621 if (b1->buf == NULL) {
622 b1->buf = OPENSSL_malloc(b1->size);
623 if (b1->buf == NULL)
624 return 0;
625 b1->len = 0;
626 b1->offset = 0;
627 }
628
629 if (b2->buf == NULL) {
630 b2->buf = OPENSSL_malloc(b2->size);
631 if (b2->buf == NULL)
632 return 0;
633 b2->len = 0;
634 b2->offset = 0;
635 }
636
637 b1->peer = bio2;
638 b1->closed = 0;
639 b1->request = 0;
640 b2->peer = bio1;
641 b2->closed = 0;
642 b2->request = 0;
643
644 bio1->init = 1;
645 bio2->init = 1;
646
647 return 1;
648 }
649
650 static void bio_destroy_pair(BIO *bio)
651 {
652 struct bio_bio_st *b = bio->ptr;
653
654 if (b != NULL) {
655 BIO *peer_bio = b->peer;
656
657 if (peer_bio != NULL) {
658 struct bio_bio_st *peer_b = peer_bio->ptr;
659
660 assert(peer_b != NULL);
661 assert(peer_b->peer == bio);
662
663 peer_b->peer = NULL;
664 peer_bio->init = 0;
665 assert(peer_b->buf != NULL);
666 peer_b->len = 0;
667 peer_b->offset = 0;
668
669 b->peer = NULL;
670 bio->init = 0;
671 assert(b->buf != NULL);
672 b->len = 0;
673 b->offset = 0;
674 }
675 }
676 }
677
678 /* Exported convenience functions */
679 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
680 BIO **bio2_p, size_t writebuf2)
681 {
682 BIO *bio1 = NULL, *bio2 = NULL;
683 long r;
684 int ret = 0;
685
686 if (writebuf1 > LONG_MAX || writebuf2 > LONG_MAX)
687 goto err;
688
689 bio1 = BIO_new(BIO_s_bio());
690 if (bio1 == NULL)
691 goto err;
692 bio2 = BIO_new(BIO_s_bio());
693 if (bio2 == NULL)
694 goto err;
695
696 if (writebuf1) {
697 r = BIO_set_write_buf_size(bio1, (long)writebuf1);
698 if (!r)
699 goto err;
700 }
701 if (writebuf2) {
702 r = BIO_set_write_buf_size(bio2, (long)writebuf2);
703 if (!r)
704 goto err;
705 }
706
707 r = BIO_make_bio_pair(bio1, bio2);
708 if (!r)
709 goto err;
710 ret = 1;
711
712 err:
713 if (ret == 0) {
714 BIO_free(bio1);
715 bio1 = NULL;
716 BIO_free(bio2);
717 bio2 = NULL;
718 }
719
720 *bio1_p = bio1;
721 *bio2_p = bio2;
722 return ret;
723 }
724
725 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
726 {
727 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
728 }
729
730 size_t BIO_ctrl_get_read_request(BIO *bio)
731 {
732 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
733 }
734
735 int BIO_ctrl_reset_read_request(BIO *bio)
736 {
737 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
738 }
739
740 /*
741 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
742 * (conceivably some other BIOs could allow non-copying reads and writes
743 * too.)
744 */
745 int BIO_nread0(BIO *bio, char **buf)
746 {
747 long ret;
748
749 if (!bio->init) {
750 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
751 return -2;
752 }
753
754 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
755 if (ret > INT_MAX)
756 return INT_MAX;
757 else
758 return (int)ret;
759 }
760
761 int BIO_nread(BIO *bio, char **buf, int num)
762 {
763 int ret;
764
765 if (!bio->init) {
766 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
767 return -2;
768 }
769
770 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
771 if (ret > 0)
772 bio->num_read += ret;
773 return ret;
774 }
775
776 int BIO_nwrite0(BIO *bio, char **buf)
777 {
778 long ret;
779
780 if (!bio->init) {
781 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
782 return -2;
783 }
784
785 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
786 if (ret > INT_MAX)
787 return INT_MAX;
788 else
789 return (int)ret;
790 }
791
792 int BIO_nwrite(BIO *bio, char **buf, int num)
793 {
794 int ret;
795
796 if (!bio->init) {
797 ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
798 return -2;
799 }
800
801 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
802 if (ret > 0)
803 bio->num_write += ret;
804 return ret;
805 }