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