Bugfix.
[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 #include <assert.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/crypto.h>
17
18 static int bio_new(BIO *bio);
19 static int bio_free(BIO *bio);
20 static int bio_read(BIO *bio, char *buf, int size);
21 static int bio_write(BIO *bio, char *buf, int num);
22 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
23 static int bio_puts(BIO *bio, char *str);
24
25 static int bio_make_pair(BIO *bio1, BIO *bio2);
26 static void bio_destroy_pair(BIO *bio);
27
28 static BIO_METHOD methods_biop =
29 {
30         BIO_TYPE_BIO,
31         "BIO pair",
32         bio_write,
33         bio_read,
34         bio_puts,
35         NULL /* no bio_gets */,
36         bio_ctrl,
37         bio_new,
38         bio_free
39 };
40
41 BIO_METHOD *BIO_s_bio(void)
42         {
43         return &methods_biop;
44         }
45
46 struct bio_bio_st
47 {
48         BIO *peer;     /* NULL if buf == NULL.
49                         * If peer != NULL, then peer->ptr is also a bio_bio_st,
50                         * and its "peer" member points back to us.
51                         * peer != NULL iff init != 0 in the BIO. */
52         
53         /* This is for what we write (i.e. reading uses peer's struct): */
54         int closed;     /* valid iff peer != NULL */
55         size_t len;     /* valid iff buf != NULL; 0 if peer == NULL */
56         size_t offset;  /* valid iff buf != NULL; 0 if len == 0 */
57         size_t size;
58         char *buf;      /* "size" elements (if != NULL) */
59
60         size_t request; /* valid iff peer != NULL; 0 if len != 0;
61                          * otherwise set by peer to number of bytes
62                          * it (unsuccesfully) tried to read. */
63 };
64
65 static int bio_new(BIO *bio)
66         {
67         struct bio_bio_st *b;
68         
69         b = Malloc(sizeof *b);
70         if (b == NULL)
71                 return 0;
72
73         b->peer = NULL;
74         b->size = 17*1024; /* enough for one TLS record (just a default) */
75         b->buf = NULL;
76
77         bio->ptr = b;
78         return 1;
79         }
80
81
82 static int bio_free(BIO *bio)
83         {
84         struct bio_bio_st *b;
85
86         if (bio == NULL)
87                 return 0;
88         b = bio->ptr;
89
90         assert(b != NULL);
91
92         if (b->peer)
93                 bio_destroy_pair(bio);
94         
95         if (b->buf != NULL)
96                 {
97                 Free(b->buf);
98                 }
99
100         Free(b);
101
102         return 1;
103         }
104
105
106
107 static int bio_read(BIO *bio, char *buf, int size_)
108         {
109         size_t size = size_;
110         size_t rest;
111         struct bio_bio_st *b, *peer_b;
112
113         BIO_clear_retry_flags(bio);
114
115         if (!bio->init)
116                 return 0;
117
118         b = bio->ptr;
119         assert(b != NULL);
120         assert(b->peer != NULL);
121         peer_b = b->peer->ptr;
122         assert(peer_b != NULL);
123         assert(peer_b->buf != NULL);
124
125         peer_b->request = 0; /* will be set in "retry_read" situation */
126
127         if (buf == NULL || size == 0)
128                 return 0;
129
130         if (peer_b->len == 0)
131                 {
132                 if (peer_b->closed)
133                         return 0; /* writer has closed, and no data is left */
134                 else
135                         {
136                         BIO_set_retry_read(bio); /* buffer is empty */
137                         if (size <= peer_b->size)
138                                 peer_b->request = size;
139                         else
140                                 peer_b->request = peer_b->size; /* don't ask for more than
141                                                                  * the peer can deliver
142                                                                  * in one write */
143                         return -1;
144                         }
145                 }
146
147         /* we can read */
148         if (peer_b->len < size)
149                 size = peer_b->len;
150
151         /* now read "size" bytes */
152         
153         rest = size;
154         
155         assert(rest > 0);
156         do /* one or two iterations */
157                 {
158                 size_t chunk;
159                 
160                 assert(rest <= peer_b->len);
161                 if (peer_b->offset + rest <= peer_b->size)
162                         chunk = rest;
163                 else
164                         /* wrap around ring buffer */
165                         chunk = peer_b->size - peer_b->offset;
166                 assert(peer_b->offset + chunk <= peer_b->size);
167                 
168                 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
169                 
170                 peer_b->len -= chunk;
171                 if (peer_b->len)
172                         {
173                         peer_b->offset += chunk;
174                         assert(peer_b->offset <= peer_b->size);
175                         if (peer_b->offset == peer_b->size)
176                                 peer_b->offset = 0;
177                         buf += chunk;
178                         }
179                 else
180                         {
181                         /* buffer now empty, no need to advance "buf" */
182                         assert(chunk == rest);
183                         peer_b->offset = 0;
184                         }
185                 rest -= chunk;
186                 }
187         while (rest);
188         
189         return size;
190         }
191
192 static int bio_write(BIO *bio, char *buf, int num_)
193         {
194         size_t num = num_;
195         size_t rest;
196         struct bio_bio_st *b;
197
198         BIO_clear_retry_flags(bio);
199
200         if (!bio->init || buf == NULL || num == 0)
201                 return 0;
202
203         b = bio->ptr;           
204         assert(b != NULL);
205         assert(b->peer != NULL);
206         assert(b->buf != NULL);
207
208         b->request = 0;
209         if (b->closed)
210                 {
211                 /* we already closed */
212                 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
213                 return -1;
214                 }
215
216         assert(b->len <= b->size);
217
218         if (b->len == b->size)
219                 {
220                 BIO_set_retry_write(bio); /* buffer is full */
221                 return -1;
222                 }
223
224         /* we can write */
225         if (num > b->size - b->len)
226                 num = b->size - b->len;
227         
228         /* now write "num" bytes */
229
230         rest = num;
231         
232         assert(rest > 0);
233         do /* one or two iterations */
234                 {
235                 size_t write_offset;
236                 size_t chunk;
237
238                 assert(b->len + rest <= b->size);
239
240                 write_offset = b->offset + b->len;
241                 if (write_offset >= b->size)
242                         write_offset -= b->size;
243                 /* b->buf[write_offset] is the first byte we can write to. */
244
245                 if (write_offset + rest <= b->size)
246                         chunk = rest;
247                 else
248                         /* wrap around ring buffer */
249                         chunk = b->size - write_offset;
250                 
251                 memcpy(b->buf + write_offset, buf, chunk);
252                 
253                 b->len += chunk;
254
255                 assert(b->len <= b->size);
256                 
257                 rest -= chunk;
258                 buf += chunk;
259                 }
260         while (rest);
261
262         return num;
263         }
264
265
266 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
267         {
268         long ret;
269         struct bio_bio_st *b = bio->ptr;
270         
271         assert(b != NULL);
272
273         switch (cmd)
274                 {
275         /* specific CTRL codes */
276
277         case BIO_C_SET_WRITE_BUF_SIZE:
278                 if (b->peer)
279                         {
280                         BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
281                         ret = 0;
282                         }
283                 else
284                         {
285                         size_t new_size = num;
286
287                         if (b->size != new_size)
288                                 {
289                                 if (b->buf) 
290                                         {
291                                         Free(b->buf);
292                                         b->buf = NULL;
293                                         }
294                                 b->size = new_size;
295                                 }
296                         ret = 1;
297                         }
298                 break;
299
300         case BIO_C_GET_WRITE_BUF_SIZE:
301                 num = (long) b->size;
302
303         case BIO_C_MAKE_BIO_PAIR:
304                 {
305                 BIO *other_bio = ptr;
306                 
307                 if (bio_make_pair(bio, other_bio))
308                         ret = 1;
309                 else
310                         ret = 0;
311                 }
312                 break;
313                 
314         case BIO_C_DESTROY_BIO_PAIR:
315                 /* Effects both BIOs in the pair -- call just once!
316                  * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
317                 bio_destroy_pair(bio);
318                 ret = 1;
319                 break;
320
321         case BIO_C_GET_WRITE_GUARANTEE:
322                 /* How many bytes can the caller feed to the next write
323                  * withouth having to keep any? */
324                 if (b->peer == NULL || b->closed)
325                         ret = 0;
326                 else
327                         ret = (long) b->size - b->len;
328                 break;
329
330         case BIO_C_GET_READ_REQUEST:
331                 /* If the peer unsuccesfully tried to read, how many bytes
332                  * were requested?  (As with BIO_CTRL_PENDING, that number
333                  * can usually be treated as boolean.) */
334                 ret = (long) b->request;
335                 break;
336
337         case BIO_C_SHUTDOWN_WR:
338                 /* similar to shutdown(..., SHUT_WR) */
339                 b->closed = 1;
340                 ret = 1;
341                 break;
342
343
344         /* standard CTRL codes follow */
345
346         case BIO_CTRL_RESET:
347                 if (b->buf != NULL)
348                         {
349                         b->len = 0;
350                         b->offset = 0;
351                         }
352                 ret = 0;
353                 break;          
354
355         case BIO_CTRL_GET_CLOSE:
356                 ret = bio->shutdown;
357                 break;
358
359         case BIO_CTRL_SET_CLOSE:
360                 bio->shutdown = (int) num;
361                 ret = 1;
362                 break;
363
364         case BIO_CTRL_PENDING:
365                 if (b->peer != NULL)
366                         {
367                         struct bio_bio_st *peer_b =b->peer->ptr;
368                         
369                         ret = (long) peer_b->len;
370                         }
371                 else
372                         ret = 0;
373                 break;
374
375         case BIO_CTRL_WPENDING:
376                 if (b->buf != NULL)
377                         ret = (long) b->len;
378                 else
379                         ret = 0;
380                 break;
381
382         case BIO_CTRL_DUP:
383                 /* See BIO_dup_chain for circumstances we have to expect. */
384                 {
385                 BIO *other_bio = ptr;
386                 struct bio_bio_st *other_b;
387                 
388                 assert(other_bio != NULL);
389                 other_b = other_bio->ptr;
390                 assert(other_b != NULL);
391                 
392                 assert(other_b->buf == NULL); /* other_bio is always fresh */
393
394                 other_b->size = b->size;
395                 }
396
397                 ret = 1;
398                 break;
399
400         case BIO_CTRL_FLUSH:
401                 ret = 1;
402                 break;
403
404         case BIO_CTRL_EOF:
405                 {
406                 BIO *other_bio = ptr;
407                 
408                 if (other_bio)
409                         {
410                         struct bio_bio_st *other_b = other_bio->ptr;
411                         
412                         assert(other_b != NULL);
413                         ret = other_b->len == 0 && other_b->closed;
414                         }
415                 else
416                         ret = 1;
417                 }
418                 break;
419
420         default:
421                 ret = 0;
422                 }
423         return ret;
424         }
425
426 static int bio_puts(BIO *bio, char *str)
427         {
428         return bio_write(bio, str, strlen(str));
429         }
430
431
432 static int bio_make_pair(BIO *bio1, BIO *bio2)
433         {
434         struct bio_bio_st *b1, *b2;
435
436         assert(bio1 != NULL);
437         assert(bio2 != NULL);
438
439         b1 = bio1->ptr;
440         b2 = bio2->ptr;
441         
442         if (b1->peer != NULL || b2->peer != NULL)
443                 {
444                 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
445                 return 0;
446                 }
447         
448         if (b1->buf == NULL)
449                 {
450                 b1->buf = Malloc(b1->size);
451                 if (b1->buf == NULL)
452                         {
453                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
454                         return 0;
455                         }
456                 b1->len = 0;
457                 b1->offset = 0;
458                 }
459         
460         if (b2->buf == NULL)
461                 {
462                 b2->buf = Malloc(b2->size);
463                 if (b2->buf == NULL)
464                         {
465                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
466                         return 0;
467                         }
468                 b2->len = 0;
469                 b2->offset = 0;
470                 }
471         
472         b1->peer = bio2;
473         b1->closed = 0;
474         b1->request = 0;
475         b2->peer = bio1;
476         b2->closed = 0;
477         b2->request = 0;
478
479         bio1->init = 1;
480         bio2->init = 1;
481
482         return 1;
483         }
484
485 static void bio_destroy_pair(BIO *bio)
486         {
487         struct bio_bio_st *b = bio->ptr;
488
489         if (b != NULL)
490                 {
491                 BIO *peer_bio = b->peer;
492
493                 if (peer_bio != NULL)
494                         {
495                         struct bio_bio_st *peer_b = peer_bio->ptr;
496
497                         assert(peer_b != NULL);
498                         assert(peer_b->peer == bio);
499
500                         peer_b->peer = NULL;
501                         peer_bio->init = 0;
502                         assert(peer_b->buf != NULL);
503                         peer_b->len = 0;
504                         peer_b->offset = 0;
505                         
506                         b->peer = NULL;
507                         bio->init = 0;
508                         assert(b->buf != NULL);
509                         b->len = 0;
510                         b->offset = 0;
511                         }
512                 }
513         }
514  
515
516 /* Exported convenience functions */
517 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
518         BIO **bio2_p, size_t writebuf2)
519          {
520          BIO *bio1 = NULL, *bio2 = NULL;
521          long r;
522          int ret = 0;
523
524          bio1 = BIO_new(BIO_s_bio());
525          if (bio1 == NULL)
526                  goto err;
527          bio2 = BIO_new(BIO_s_bio());
528          if (bio2 == NULL)
529                  goto err;
530
531          if (writebuf1)
532                  {
533                  r = BIO_set_write_buf_size(bio1, writebuf1);
534                  if (!r)
535                          goto err;
536                  }
537          if (writebuf2)
538                  {
539                  r = BIO_set_write_buf_size(bio2, writebuf2);
540                  if (!r)
541                          goto err;
542                  }
543
544          r = BIO_make_bio_pair(bio1, bio2);
545          if (!r)
546                  goto err;
547          ret = 1;
548
549  err:
550          if (ret == 0)
551                  {
552                  if (bio1)
553                          {
554                          BIO_free(bio1);
555                          bio1 = NULL;
556                          }
557                  if (bio2)
558                          {
559                          BIO_free(bio2);
560                          bio2 = NULL;
561                          }
562                  }
563
564          *bio1_p = bio1;
565          *bio2_p = bio2;
566          return ret;
567          }
568
569 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
570     {
571         return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
572         }
573
574 size_t BIO_ctrl_read_request(BIO *bio)
575     {
576         return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
577         }