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