Non-copying interface to BIO pairs.
[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 totally untested as of yet --
206  * I wrote it, but have not yet read it; and surely it still is full
207  * of bugs. */
208 static size_t bio_nread0(BIO *bio, char **buf)
209         {
210         struct bio_bio_st *b, *peer_b;
211         size_t num;
212         
213         BIO_clear_retry_flags(bio);
214
215         if (!bio->init)
216                 return 0;
217         
218         b = bio->ptr;
219         assert(b != NULL);
220         assert(b->peer != NULL);
221         peer_b = b->peer->ptr;
222         assert(peer_b != NULL);
223         assert(peer_b->buf != NULL);
224         
225         peer_b->request = 0;
226         
227         if (peer_b->len == 0)
228                 {
229                 char dummy;
230                 
231                 /* avoid code duplication -- nothing available for reading */
232                 return bio_read(bio, &dummy, num); /* returns 0 or -1 */
233                 }
234
235         num = peer_b->len;
236         if (peer_b->size < peer_b->offset + num)
237                 /* no ring buffer wrap-around for non-copying interface */
238                 num = peer_b->size - peer_b->offset;
239         assert(num > 0);
240
241         if (buf != NULL)
242                 *buf = peer_b->buf + peer_b->offset;
243         return num;
244         }
245
246 static size_t bio_nread(BIO *bio, char **buf, size_t num)
247         {
248         struct bio_bio_st *b, *peer_b;
249         size_t available;
250
251         available = bio_nread0(bio, buf);
252         if (num > available)
253                 num = available;
254         if (num <= 0)
255                 return num;
256
257         b = bio->ptr;
258         peer_b = b->peer->ptr;
259
260         peer_b->len -= num;
261         if (peer_b->len) 
262                 {
263                 peer_b->offset += num;
264                 assert(peer_b->offset <= peer_b->size);
265                 if (peer_b->offset == peer_b->size)
266                         peer_b->offset = 0;
267                 }
268         else
269                 peer_b->offset = 0;
270
271         return num;
272         }
273
274
275 static int bio_write(BIO *bio, char *buf, int num_)
276         {
277         size_t num = num_;
278         size_t rest;
279         struct bio_bio_st *b;
280
281         BIO_clear_retry_flags(bio);
282
283         if (!bio->init || buf == NULL || num == 0)
284                 return 0;
285
286         b = bio->ptr;           
287         assert(b != NULL);
288         assert(b->peer != NULL);
289         assert(b->buf != NULL);
290
291         b->request = 0;
292         if (b->closed)
293                 {
294                 /* we already closed */
295                 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
296                 return -1;
297                 }
298
299         assert(b->len <= b->size);
300
301         if (b->len == b->size)
302                 {
303                 BIO_set_retry_write(bio); /* buffer is full */
304                 return -1;
305                 }
306
307         /* we can write */
308         if (num > b->size - b->len)
309                 num = b->size - b->len;
310         
311         /* now write "num" bytes */
312
313         rest = num;
314         
315         assert(rest > 0);
316         do /* one or two iterations */
317                 {
318                 size_t write_offset;
319                 size_t chunk;
320
321                 assert(b->len + rest <= b->size);
322
323                 write_offset = b->offset + b->len;
324                 if (write_offset >= b->size)
325                         write_offset -= b->size;
326                 /* b->buf[write_offset] is the first byte we can write to. */
327
328                 if (write_offset + rest <= b->size)
329                         chunk = rest;
330                 else
331                         /* wrap around ring buffer */
332                         chunk = b->size - write_offset;
333                 
334                 memcpy(b->buf + write_offset, buf, chunk);
335                 
336                 b->len += chunk;
337
338                 assert(b->len <= b->size);
339                 
340                 rest -= chunk;
341                 buf += chunk;
342                 }
343         while (rest);
344
345         return num;
346         }
347
348 /* non-copying interface: provide pointer to region to write to
349  *   bio_nwrite0:  check how much space is available
350  *   bio_nwrite:   also increase length
351  * (example usage:  bio_nwrite0(), write to buffer, bio_nwrite()
352  *  or just         bio_nwrite(), write to buffer)
353  */
354 static size_t bio_nwrite0(BIO *bio, char **buf)
355         {
356         struct bio_bio_st *b;
357         size_t num;
358         size_t write_offset;
359
360         BIO_clear_retry_flags(bio);
361
362         if (!bio->init)
363                 return 0;
364
365         b = bio->ptr;           
366         assert(b != NULL);
367         assert(b->peer != NULL);
368         assert(b->buf != NULL);
369
370         b->request = 0;
371         if (b->closed)
372                 {
373                 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
374                 return -1;
375                 }
376
377         assert(b->len <= b->size);
378
379         if (b->len == b->size)
380                 {
381                 BIO_set_retry_write(bio);
382                 return -1;
383                 }
384
385         num = b->size - b->len;
386         write_offset = b->offset + b->len;
387         if (write_offset >= b->size)
388                 write_offset -= b->size;
389         if (write_offset + num > b->size)
390                 /* no ring buffer wrap-around for non-copying interface
391                  * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
392                  * BIO_nwrite may have to be called twice) */
393                 num = b->size - write_offset;
394
395         if (buf != NULL)
396                 *buf = b->buf + write_offset;
397         assert(write_offset + num <= b->size);
398
399         return num;
400         }
401
402 static size_t bio_nwrite(BIO *bio, char **buf, size_t num)
403         {
404         struct bio_bio_st *b;
405         size_t space;
406
407         space = bio_nwrite0(bio, buf);
408         if (num > space)
409                 num = space;
410         if (num <= 0)
411                 return num;
412         b->len += num;
413         assert(b->len <= b->size);
414
415         return num;
416         }
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                 {
428         /* specific CTRL codes */
429
430         case BIO_C_SET_WRITE_BUF_SIZE:
431                 if (b->peer)
432                         {
433                         BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
434                         ret = 0;
435                         }
436                 else if (num == 0)
437                         {
438                         BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
439                         ret = 0;
440                         }
441                 else
442                         {
443                         size_t new_size = num;
444
445                         if (b->size != new_size)
446                                 {
447                                 if (b->buf) 
448                                         {
449                                         Free(b->buf);
450                                         b->buf = NULL;
451                                         }
452                                 b->size = new_size;
453                                 }
454                         ret = 1;
455                         }
456                 break;
457
458         case BIO_C_GET_WRITE_BUF_SIZE:
459                 num = (long) b->size;
460
461         case BIO_C_MAKE_BIO_PAIR:
462                 {
463                 BIO *other_bio = ptr;
464                 
465                 if (bio_make_pair(bio, other_bio))
466                         ret = 1;
467                 else
468                         ret = 0;
469                 }
470                 break;
471                 
472         case BIO_C_DESTROY_BIO_PAIR:
473                 /* Effects both BIOs in the pair -- call just once!
474                  * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
475                 bio_destroy_pair(bio);
476                 ret = 1;
477                 break;
478
479         case BIO_C_GET_WRITE_GUARANTEE:
480                 /* How many bytes can the caller feed to the next write
481                  * withouth having to keep any? */
482                 if (b->peer == NULL || b->closed)
483                         ret = 0;
484                 else
485                         ret = (long) b->size - b->len;
486                 break;
487
488         case BIO_C_GET_READ_REQUEST:
489                 /* If the peer unsuccesfully tried to read, how many bytes
490                  * were requested?  (As with BIO_CTRL_PENDING, that number
491                  * can usually be treated as boolean.) */
492                 ret = (long) b->request;
493                 break;
494
495         case BIO_C_SHUTDOWN_WR:
496                 /* similar to shutdown(..., SHUT_WR) */
497                 b->closed = 1;
498                 ret = 1;
499                 break;
500
501         case BIO_C_NREAD:
502                 /* non-copying read */
503                 ret = (long) bio_nread(bio, ptr, (size_t) num);
504                 break;
505                 
506         case BIO_C_NWRITE0:
507                 /* prepare for non-copying write */
508                 ret = (long) bio_nwrite0(bio, ptr);
509                 break;
510
511         case BIO_C_NWRITE:
512                 /* non-copying write */
513                 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
514                 break;
515                 
516
517         /* standard CTRL codes follow */
518
519         case BIO_CTRL_RESET:
520                 if (b->buf != NULL)
521                         {
522                         b->len = 0;
523                         b->offset = 0;
524                         }
525                 ret = 0;
526                 break;          
527
528         case BIO_CTRL_GET_CLOSE:
529                 ret = bio->shutdown;
530                 break;
531
532         case BIO_CTRL_SET_CLOSE:
533                 bio->shutdown = (int) num;
534                 ret = 1;
535                 break;
536
537         case BIO_CTRL_PENDING:
538                 if (b->peer != NULL)
539                         {
540                         struct bio_bio_st *peer_b = b->peer->ptr;
541                         
542                         ret = (long) peer_b->len;
543                         }
544                 else
545                         ret = 0;
546                 break;
547
548         case BIO_CTRL_WPENDING:
549                 if (b->buf != NULL)
550                         ret = (long) b->len;
551                 else
552                         ret = 0;
553                 break;
554
555         case BIO_CTRL_DUP:
556                 /* See BIO_dup_chain for circumstances we have to expect. */
557                 {
558                 BIO *other_bio = ptr;
559                 struct bio_bio_st *other_b;
560                 
561                 assert(other_bio != NULL);
562                 other_b = other_bio->ptr;
563                 assert(other_b != NULL);
564                 
565                 assert(other_b->buf == NULL); /* other_bio is always fresh */
566
567                 other_b->size = b->size;
568                 }
569
570                 ret = 1;
571                 break;
572
573         case BIO_CTRL_FLUSH:
574                 ret = 1;
575                 break;
576
577         case BIO_CTRL_EOF:
578                 {
579                 BIO *other_bio = ptr;
580                 
581                 if (other_bio)
582                         {
583                         struct bio_bio_st *other_b = other_bio->ptr;
584                         
585                         assert(other_b != NULL);
586                         ret = other_b->len == 0 && other_b->closed;
587                         }
588                 else
589                         ret = 1;
590                 }
591                 break;
592
593         default:
594                 ret = 0;
595                 }
596         return ret;
597         }
598
599 static int bio_puts(BIO *bio, char *str)
600         {
601         return bio_write(bio, str, strlen(str));
602         }
603
604
605 static int bio_make_pair(BIO *bio1, BIO *bio2)
606         {
607         struct bio_bio_st *b1, *b2;
608
609         assert(bio1 != NULL);
610         assert(bio2 != NULL);
611
612         b1 = bio1->ptr;
613         b2 = bio2->ptr;
614         
615         if (b1->peer != NULL || b2->peer != NULL)
616                 {
617                 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
618                 return 0;
619                 }
620         
621         if (b1->buf == NULL)
622                 {
623                 b1->buf = Malloc(b1->size);
624                 if (b1->buf == NULL)
625                         {
626                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
627                         return 0;
628                         }
629                 b1->len = 0;
630                 b1->offset = 0;
631                 }
632         
633         if (b2->buf == NULL)
634                 {
635                 b2->buf = Malloc(b2->size);
636                 if (b2->buf == NULL)
637                         {
638                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
639                         return 0;
640                         }
641                 b2->len = 0;
642                 b2->offset = 0;
643                 }
644         
645         b1->peer = bio2;
646         b1->closed = 0;
647         b1->request = 0;
648         b2->peer = bio1;
649         b2->closed = 0;
650         b2->request = 0;
651
652         bio1->init = 1;
653         bio2->init = 1;
654
655         return 1;
656         }
657
658 static void bio_destroy_pair(BIO *bio)
659         {
660         struct bio_bio_st *b = bio->ptr;
661
662         if (b != NULL)
663                 {
664                 BIO *peer_bio = b->peer;
665
666                 if (peer_bio != NULL)
667                         {
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
689 /* Exported convenience functions */
690 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
691         BIO **bio2_p, size_t writebuf2)
692          {
693          BIO *bio1 = NULL, *bio2 = NULL;
694          long r;
695          int ret = 0;
696
697          bio1 = BIO_new(BIO_s_bio());
698          if (bio1 == NULL)
699                  goto err;
700          bio2 = BIO_new(BIO_s_bio());
701          if (bio2 == NULL)
702                  goto err;
703
704          if (writebuf1)
705                  {
706                  r = BIO_set_write_buf_size(bio1, writebuf1);
707                  if (!r)
708                          goto err;
709                  }
710          if (writebuf2)
711                  {
712                  r = BIO_set_write_buf_size(bio2, writebuf2);
713                  if (!r)
714                          goto err;
715                  }
716
717          r = BIO_make_bio_pair(bio1, bio2);
718          if (!r)
719                  goto err;
720          ret = 1;
721
722  err:
723          if (ret == 0)
724                  {
725                  if (bio1)
726                          {
727                          BIO_free(bio1);
728                          bio1 = NULL;
729                          }
730                  if (bio2)
731                          {
732                          BIO_free(bio2);
733                          bio2 = NULL;
734                          }
735                  }
736
737          *bio1_p = bio1;
738          *bio2_p = bio2;
739          return ret;
740          }
741
742 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
743         {
744         return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
745         }
746
747 size_t BIO_ctrl_get_read_request(BIO *bio)
748         {
749         return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
750         }
751
752
753 /* BIO_nread0/nread/nwrite0/nwrite are availabe only for BIO pairs for now
754  * (conceivably some other BIOs could allow non-copying reads and writes too.)
755  */
756 int BIO_nread0(BIO *bio, char **buf)
757         {
758         long ret;
759
760         if (!bio->init)
761                 {
762                 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
763                 return -2;
764                 }
765
766         ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
767         if (ret > INT_MAX)
768                 return INT_MAX;
769         else
770                 return (int) ret;
771         }
772
773 int BIO_nread(BIO *bio, char **buf, int num)
774         {
775         int ret;
776
777         if (!bio->init)
778                 {
779                 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
780                 return -2;
781                 }
782
783         ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
784         if (ret > 0)
785                 bio->num_read += ret;
786         return ret;
787         }
788
789 int BIO_nwrite0(BIO *bio, char **buf)
790         {
791         long ret;
792
793         if (!bio->init)
794                 {
795                 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
796                 return -2;
797                 }
798
799         ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
800         if (ret > INT_MAX)
801                 return INT_MAX;
802         else
803                 return (int) ret;
804         }
805
806 int BIO_nwrite(BIO *bio, char **buf, int num)
807         {
808         int ret;
809
810         if (!bio->init)
811                 {
812                 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
813                 return -2;
814                 }
815
816         ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
817         if (ret > 0)
818                 bio->num_read += ret;
819         return ret;
820         }