]> WPIA git - cassiopeia.git/blob - lib/openssl/crypto/bio/bss_bio.c
upd: openssl to 1.1.0
[cassiopeia.git] / lib / openssl / crypto / bio / bss_bio.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <assert.h>
19 #include <limits.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "bio_lcl.h"
24 #include <openssl/err.h>
25 #include <openssl/crypto.h>
26
27 #include "e_os.h"
28
29 static int bio_new(BIO *bio);
30 static int bio_free(BIO *bio);
31 static int bio_read(BIO *bio, char *buf, int size);
32 static int bio_write(BIO *bio, const char *buf, int num);
33 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
34 static int bio_puts(BIO *bio, const char *str);
35
36 static int bio_make_pair(BIO *bio1, BIO *bio2);
37 static void bio_destroy_pair(BIO *bio);
38
39 static const BIO_METHOD methods_biop = {
40     BIO_TYPE_BIO,
41     "BIO pair",
42     bio_write,
43     bio_read,
44     bio_puts,
45     NULL /* no bio_gets */ ,
46     bio_ctrl,
47     bio_new,
48     bio_free,
49     NULL                        /* no bio_callback_ctrl */
50 };
51
52 const BIO_METHOD *BIO_s_bio(void)
53 {
54     return &methods_biop;
55 }
56
57 struct bio_bio_st {
58     BIO *peer;                  /* NULL if buf == NULL. If peer != NULL, then
59                                  * peer->ptr is also a bio_bio_st, and its
60                                  * "peer" member points back to us. peer !=
61                                  * NULL iff init != 0 in the BIO. */
62     /* This is for what we write (i.e. reading uses peer's struct): */
63     int closed;                 /* valid iff peer != NULL */
64     size_t len;                 /* valid iff buf != NULL; 0 if peer == NULL */
65     size_t offset;              /* valid iff buf != NULL; 0 if len == 0 */
66     size_t size;
67     char *buf;                  /* "size" elements (if != NULL) */
68     size_t request;             /* valid iff peer != NULL; 0 if len != 0,
69                                  * otherwise set by peer to number of bytes
70                                  * it (unsuccessfully) tried to read, never
71                                  * more than buffer space (size-len)
72                                  * warrants. */
73 };
74
75 static int bio_new(BIO *bio)
76 {
77     struct bio_bio_st *b = OPENSSL_zalloc(sizeof(*b));
78
79     if (b == NULL)
80         return 0;
81
82     /* enough for one TLS record (just a default) */
83     b->size = 17 * 1024;
84
85     bio->ptr = b;
86     return 1;
87 }
88
89 static int bio_free(BIO *bio)
90 {
91     struct bio_bio_st *b;
92
93     if (bio == NULL)
94         return 0;
95     b = bio->ptr;
96
97     assert(b != NULL);
98
99     if (b->peer)
100         bio_destroy_pair(bio);
101
102     OPENSSL_free(b->buf);
103     OPENSSL_free(b);
104
105     return 1;
106 }
107
108 static int bio_read(BIO *bio, char *buf, int size_)
109 {
110     size_t size = size_;
111     size_t rest;
112     struct bio_bio_st *b, *peer_b;
113
114     BIO_clear_retry_flags(bio);
115
116     if (!bio->init)
117         return 0;
118
119     b = bio->ptr;
120     assert(b != NULL);
121     assert(b->peer != NULL);
122     peer_b = b->peer->ptr;
123     assert(peer_b != NULL);
124     assert(peer_b->buf != NULL);
125
126     peer_b->request = 0;        /* will be set in "retry_read" situation */
127
128     if (buf == NULL || size == 0)
129         return 0;
130
131     if (peer_b->len == 0) {
132         if (peer_b->closed)
133             return 0;           /* writer has closed, and no data is left */
134         else {
135             BIO_set_retry_read(bio); /* buffer is empty */
136             if (size <= peer_b->size)
137                 peer_b->request = size;
138             else
139                 /*
140                  * don't ask for more than the peer can deliver in one write
141                  */
142                 peer_b->request = peer_b->size;
143             return -1;
144         }
145     }
146
147     /* we can read */
148     if (peer_b->len < size)
149         size = peer_b->len;
150
151     /* now read "size" bytes */
152
153     rest = size;
154
155     assert(rest > 0);
156     do {                        /* one or two iterations */
157         size_t chunk;
158
159         assert(rest <= peer_b->len);
160         if (peer_b->offset + rest <= peer_b->size)
161             chunk = rest;
162         else
163             /* wrap around ring buffer */
164             chunk = peer_b->size - peer_b->offset;
165         assert(peer_b->offset + chunk <= peer_b->size);
166
167         memcpy(buf, peer_b->buf + peer_b->offset, chunk);
168
169         peer_b->len -= chunk;
170         if (peer_b->len) {
171             peer_b->offset += chunk;
172             assert(peer_b->offset <= peer_b->size);
173             if (peer_b->offset == peer_b->size)
174                 peer_b->offset = 0;
175             buf += chunk;
176         } else {
177             /* buffer now empty, no need to advance "buf" */
178             assert(chunk == rest);
179             peer_b->offset = 0;
180         }
181         rest -= chunk;
182     }
183     while (rest);
184
185     return 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         BIOerr(BIO_F_BIO_WRITE, 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     }
332     while (rest);
333
334     return num;
335 }
336
337 /*-
338  * non-copying interface: provide pointer to region to write to
339  *   bio_nwrite0:  check how much space is available
340  *   bio_nwrite:   also increase length
341  * (example usage:  bio_nwrite0(), write to buffer, bio_nwrite()
342  *  or just         bio_nwrite(), write to buffer)
343  */
344 static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
345 {
346     struct bio_bio_st *b;
347     size_t num;
348     size_t write_offset;
349
350     BIO_clear_retry_flags(bio);
351
352     if (!bio->init)
353         return 0;
354
355     b = bio->ptr;
356     assert(b != NULL);
357     assert(b->peer != NULL);
358     assert(b->buf != NULL);
359
360     b->request = 0;
361     if (b->closed) {
362         BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
363         return -1;
364     }
365
366     assert(b->len <= b->size);
367
368     if (b->len == b->size) {
369         BIO_set_retry_write(bio);
370         return -1;
371     }
372
373     num = b->size - b->len;
374     write_offset = b->offset + b->len;
375     if (write_offset >= b->size)
376         write_offset -= b->size;
377     if (write_offset + num > b->size)
378         /*
379          * no ring buffer wrap-around for non-copying interface (to fulfil
380          * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
381          * to be called twice)
382          */
383         num = b->size - write_offset;
384
385     if (buf != NULL)
386         *buf = b->buf + write_offset;
387     assert(write_offset + num <= b->size);
388
389     return num;
390 }
391
392 static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
393 {
394     struct bio_bio_st *b;
395     ossl_ssize_t num, space;
396
397     if (num_ > OSSL_SSIZE_MAX)
398         num = OSSL_SSIZE_MAX;
399     else
400         num = (ossl_ssize_t) num_;
401
402     space = bio_nwrite0(bio, buf);
403     if (num > space)
404         num = space;
405     if (num <= 0)
406         return num;
407     b = bio->ptr;
408     assert(b != NULL);
409     b->len += num;
410     assert(b->len <= b->size);
411
412     return num;
413 }
414
415 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
416 {
417     long ret;
418     struct bio_bio_st *b = bio->ptr;
419
420     assert(b != NULL);
421
422     switch (cmd) {
423         /* specific CTRL codes */
424
425     case BIO_C_SET_WRITE_BUF_SIZE:
426         if (b->peer) {
427             BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
428             ret = 0;
429         } else if (num == 0) {
430             BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
431             ret = 0;
432         } else {
433             size_t new_size = num;
434
435             if (b->size != new_size) {
436                 OPENSSL_free(b->buf);
437                 b->buf = NULL;
438                 b->size = new_size;
439             }
440             ret = 1;
441         }
442         break;
443
444     case BIO_C_GET_WRITE_BUF_SIZE:
445         ret = (long)b->size;
446         break;
447
448     case BIO_C_MAKE_BIO_PAIR:
449         {
450             BIO *other_bio = ptr;
451
452             if (bio_make_pair(bio, other_bio))
453                 ret = 1;
454             else
455                 ret = 0;
456         }
457         break;
458
459     case BIO_C_DESTROY_BIO_PAIR:
460         /*
461          * Affects both BIOs in the pair -- call just once! Or let
462          * BIO_free(bio1); BIO_free(bio2); do the job.
463          */
464         bio_destroy_pair(bio);
465         ret = 1;
466         break;
467
468     case BIO_C_GET_WRITE_GUARANTEE:
469         /*
470          * How many bytes can the caller feed to the next write without
471          * having to keep any?
472          */
473         if (b->peer == NULL || b->closed)
474             ret = 0;
475         else
476             ret = (long)b->size - b->len;
477         break;
478
479     case BIO_C_GET_READ_REQUEST:
480         /*
481          * If the peer unsuccessfully tried to read, how many bytes were
482          * requested? (As with BIO_CTRL_PENDING, that number can usually be
483          * treated as boolean.)
484          */
485         ret = (long)b->request;
486         break;
487
488     case BIO_C_RESET_READ_REQUEST:
489         /*
490          * Reset request.  (Can be useful after read attempts at the other
491          * side that are meant to be non-blocking, e.g. when probing SSL_read
492          * to see if any data is available.)
493          */
494         b->request = 0;
495         ret = 1;
496         break;
497
498     case BIO_C_SHUTDOWN_WR:
499         /* similar to shutdown(..., SHUT_WR) */
500         b->closed = 1;
501         ret = 1;
502         break;
503
504     case BIO_C_NREAD0:
505         /* prepare for non-copying read */
506         ret = (long)bio_nread0(bio, ptr);
507         break;
508
509     case BIO_C_NREAD:
510         /* non-copying read */
511         ret = (long)bio_nread(bio, ptr, (size_t)num);
512         break;
513
514     case BIO_C_NWRITE0:
515         /* prepare for non-copying write */
516         ret = (long)bio_nwrite0(bio, ptr);
517         break;
518
519     case BIO_C_NWRITE:
520         /* non-copying write */
521         ret = (long)bio_nwrite(bio, ptr, (size_t)num);
522         break;
523
524         /* standard CTRL codes follow */
525
526     case BIO_CTRL_RESET:
527         if (b->buf != NULL) {
528             b->len = 0;
529             b->offset = 0;
530         }
531         ret = 0;
532         break;
533
534     case BIO_CTRL_GET_CLOSE:
535         ret = bio->shutdown;
536         break;
537
538     case BIO_CTRL_SET_CLOSE:
539         bio->shutdown = (int)num;
540         ret = 1;
541         break;
542
543     case BIO_CTRL_PENDING:
544         if (b->peer != NULL) {
545             struct bio_bio_st *peer_b = b->peer->ptr;
546
547             ret = (long)peer_b->len;
548         } else
549             ret = 0;
550         break;
551
552     case BIO_CTRL_WPENDING:
553         if (b->buf != NULL)
554             ret = (long)b->len;
555         else
556             ret = 0;
557         break;
558
559     case BIO_CTRL_DUP:
560         /* See BIO_dup_chain for circumstances we have to expect. */
561         {
562             BIO *other_bio = ptr;
563             struct bio_bio_st *other_b;
564
565             assert(other_bio != NULL);
566             other_b = other_bio->ptr;
567             assert(other_b != NULL);
568
569             assert(other_b->buf == NULL); /* other_bio is always fresh */
570
571             other_b->size = b->size;
572         }
573
574         ret = 1;
575         break;
576
577     case BIO_CTRL_FLUSH:
578         ret = 1;
579         break;
580
581     case BIO_CTRL_EOF:
582         if (b->peer != NULL) {
583             struct bio_bio_st *peer_b = b->peer->ptr;
584
585             if (peer_b->len == 0 && peer_b->closed)
586                 ret = 1;
587             else
588                 ret = 0;
589         } else {
590             ret = 1;
591         }
592         break;
593
594     default:
595         ret = 0;
596     }
597     return ret;
598 }
599
600 static int bio_puts(BIO *bio, const char *str)
601 {
602     return bio_write(bio, str, strlen(str));
603 }
604
605 static int bio_make_pair(BIO *bio1, BIO *bio2)
606 {
607     struct bio_bio_st *b1, *b2;
608
609     assert(bio1 != NULL);
610     assert(bio2 != NULL);
611
612     b1 = bio1->ptr;
613     b2 = bio2->ptr;
614
615     if (b1->peer != NULL || b2->peer != NULL) {
616         BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
617         return 0;
618     }
619
620     if (b1->buf == NULL) {
621         b1->buf = OPENSSL_malloc(b1->size);
622         if (b1->buf == NULL) {
623             BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
624             return 0;
625         }
626         b1->len = 0;
627         b1->offset = 0;
628     }
629
630     if (b2->buf == NULL) {
631         b2->buf = OPENSSL_malloc(b2->size);
632         if (b2->buf == NULL) {
633             BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
634             return 0;
635         }
636         b2->len = 0;
637         b2->offset = 0;
638     }
639
640     b1->peer = bio2;
641     b1->closed = 0;
642     b1->request = 0;
643     b2->peer = bio1;
644     b2->closed = 0;
645     b2->request = 0;
646
647     bio1->init = 1;
648     bio2->init = 1;
649
650     return 1;
651 }
652
653 static void bio_destroy_pair(BIO *bio)
654 {
655     struct bio_bio_st *b = bio->ptr;
656
657     if (b != NULL) {
658         BIO *peer_bio = b->peer;
659
660         if (peer_bio != NULL) {
661             struct bio_bio_st *peer_b = peer_bio->ptr;
662
663             assert(peer_b != NULL);
664             assert(peer_b->peer == bio);
665
666             peer_b->peer = NULL;
667             peer_bio->init = 0;
668             assert(peer_b->buf != NULL);
669             peer_b->len = 0;
670             peer_b->offset = 0;
671
672             b->peer = NULL;
673             bio->init = 0;
674             assert(b->buf != NULL);
675             b->len = 0;
676             b->offset = 0;
677         }
678     }
679 }
680
681 /* Exported convenience functions */
682 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
683                      BIO **bio2_p, size_t writebuf2)
684 {
685     BIO *bio1 = NULL, *bio2 = NULL;
686     long r;
687     int ret = 0;
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, writebuf1);
698         if (!r)
699             goto err;
700     }
701     if (writebuf2) {
702         r = BIO_set_write_buf_size(bio2, 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         BIOerr(BIO_F_BIO_NREAD0, 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         BIOerr(BIO_F_BIO_NREAD, 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         BIOerr(BIO_F_BIO_NWRITE0, 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         BIOerr(BIO_F_BIO_NWRITE, 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 }