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