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