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