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