a1991a7b909e9e2ec36bcd18f11cb3adebcfc2e4
[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 if (num == 0)
284                         {
285                         BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
286                         ret = 0;
287                         }
288                 else
289                         {
290                         size_t new_size = num;
291
292                         if (b->size != new_size)
293                                 {
294                                 if (b->buf) 
295                                         {
296                                         Free(b->buf);
297                                         b->buf = NULL;
298                                         }
299                                 b->size = new_size;
300                                 }
301                         ret = 1;
302                         }
303                 break;
304
305         case BIO_C_GET_WRITE_BUF_SIZE:
306                 num = (long) b->size;
307
308         case BIO_C_MAKE_BIO_PAIR:
309                 {
310                 BIO *other_bio = ptr;
311                 
312                 if (bio_make_pair(bio, other_bio))
313                         ret = 1;
314                 else
315                         ret = 0;
316                 }
317                 break;
318                 
319         case BIO_C_DESTROY_BIO_PAIR:
320                 /* Effects both BIOs in the pair -- call just once!
321                  * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
322                 bio_destroy_pair(bio);
323                 ret = 1;
324                 break;
325
326         case BIO_C_GET_WRITE_GUARANTEE:
327                 /* How many bytes can the caller feed to the next write
328                  * withouth having to keep any? */
329                 if (b->peer == NULL || b->closed)
330                         ret = 0;
331                 else
332                         ret = (long) b->size - b->len;
333                 break;
334
335         case BIO_C_GET_READ_REQUEST:
336                 /* If the peer unsuccesfully tried to read, how many bytes
337                  * were requested?  (As with BIO_CTRL_PENDING, that number
338                  * can usually be treated as boolean.) */
339                 ret = (long) b->request;
340                 break;
341
342         case BIO_C_SHUTDOWN_WR:
343                 /* similar to shutdown(..., SHUT_WR) */
344                 b->closed = 1;
345                 ret = 1;
346                 break;
347
348
349         /* standard CTRL codes follow */
350
351         case BIO_CTRL_RESET:
352                 if (b->buf != NULL)
353                         {
354                         b->len = 0;
355                         b->offset = 0;
356                         }
357                 ret = 0;
358                 break;          
359
360         case BIO_CTRL_GET_CLOSE:
361                 ret = bio->shutdown;
362                 break;
363
364         case BIO_CTRL_SET_CLOSE:
365                 bio->shutdown = (int) num;
366                 ret = 1;
367                 break;
368
369         case BIO_CTRL_PENDING:
370                 if (b->peer != NULL)
371                         {
372                         struct bio_bio_st *peer_b =b->peer->ptr;
373                         
374                         ret = (long) peer_b->len;
375                         }
376                 else
377                         ret = 0;
378                 break;
379
380         case BIO_CTRL_WPENDING:
381                 if (b->buf != NULL)
382                         ret = (long) b->len;
383                 else
384                         ret = 0;
385                 break;
386
387         case BIO_CTRL_DUP:
388                 /* See BIO_dup_chain for circumstances we have to expect. */
389                 {
390                 BIO *other_bio = ptr;
391                 struct bio_bio_st *other_b;
392                 
393                 assert(other_bio != NULL);
394                 other_b = other_bio->ptr;
395                 assert(other_b != NULL);
396                 
397                 assert(other_b->buf == NULL); /* other_bio is always fresh */
398
399                 other_b->size = b->size;
400                 }
401
402                 ret = 1;
403                 break;
404
405         case BIO_CTRL_FLUSH:
406                 ret = 1;
407                 break;
408
409         case BIO_CTRL_EOF:
410                 {
411                 BIO *other_bio = ptr;
412                 
413                 if (other_bio)
414                         {
415                         struct bio_bio_st *other_b = other_bio->ptr;
416                         
417                         assert(other_b != NULL);
418                         ret = other_b->len == 0 && other_b->closed;
419                         }
420                 else
421                         ret = 1;
422                 }
423                 break;
424
425         default:
426                 ret = 0;
427                 }
428         return ret;
429         }
430
431 static int bio_puts(BIO *bio, char *str)
432         {
433         return bio_write(bio, str, strlen(str));
434         }
435
436
437 static int bio_make_pair(BIO *bio1, BIO *bio2)
438         {
439         struct bio_bio_st *b1, *b2;
440
441         assert(bio1 != NULL);
442         assert(bio2 != NULL);
443
444         b1 = bio1->ptr;
445         b2 = bio2->ptr;
446         
447         if (b1->peer != NULL || b2->peer != NULL)
448                 {
449                 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
450                 return 0;
451                 }
452         
453         if (b1->buf == NULL)
454                 {
455                 b1->buf = Malloc(b1->size);
456                 if (b1->buf == NULL)
457                         {
458                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
459                         return 0;
460                         }
461                 b1->len = 0;
462                 b1->offset = 0;
463                 }
464         
465         if (b2->buf == NULL)
466                 {
467                 b2->buf = Malloc(b2->size);
468                 if (b2->buf == NULL)
469                         {
470                         BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
471                         return 0;
472                         }
473                 b2->len = 0;
474                 b2->offset = 0;
475                 }
476         
477         b1->peer = bio2;
478         b1->closed = 0;
479         b1->request = 0;
480         b2->peer = bio1;
481         b2->closed = 0;
482         b2->request = 0;
483
484         bio1->init = 1;
485         bio2->init = 1;
486
487         return 1;
488         }
489
490 static void bio_destroy_pair(BIO *bio)
491         {
492         struct bio_bio_st *b = bio->ptr;
493
494         if (b != NULL)
495                 {
496                 BIO *peer_bio = b->peer;
497
498                 if (peer_bio != NULL)
499                         {
500                         struct bio_bio_st *peer_b = peer_bio->ptr;
501
502                         assert(peer_b != NULL);
503                         assert(peer_b->peer == bio);
504
505                         peer_b->peer = NULL;
506                         peer_bio->init = 0;
507                         assert(peer_b->buf != NULL);
508                         peer_b->len = 0;
509                         peer_b->offset = 0;
510                         
511                         b->peer = NULL;
512                         bio->init = 0;
513                         assert(b->buf != NULL);
514                         b->len = 0;
515                         b->offset = 0;
516                         }
517                 }
518         }
519  
520
521 /* Exported convenience functions */
522 int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
523         BIO **bio2_p, size_t writebuf2)
524          {
525          BIO *bio1 = NULL, *bio2 = NULL;
526          long r;
527          int ret = 0;
528
529          bio1 = BIO_new(BIO_s_bio());
530          if (bio1 == NULL)
531                  goto err;
532          bio2 = BIO_new(BIO_s_bio());
533          if (bio2 == NULL)
534                  goto err;
535
536          if (writebuf1)
537                  {
538                  r = BIO_set_write_buf_size(bio1, writebuf1);
539                  if (!r)
540                          goto err;
541                  }
542          if (writebuf2)
543                  {
544                  r = BIO_set_write_buf_size(bio2, writebuf2);
545                  if (!r)
546                          goto err;
547                  }
548
549          r = BIO_make_bio_pair(bio1, bio2);
550          if (!r)
551                  goto err;
552          ret = 1;
553
554  err:
555          if (ret == 0)
556                  {
557                  if (bio1)
558                          {
559                          BIO_free(bio1);
560                          bio1 = NULL;
561                          }
562                  if (bio2)
563                          {
564                          BIO_free(bio2);
565                          bio2 = NULL;
566                          }
567                  }
568
569          *bio1_p = bio1;
570          *bio2_p = bio2;
571          return ret;
572          }
573
574 size_t BIO_ctrl_get_write_guarantee(BIO *bio)
575     {
576         return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
577         }
578
579 size_t BIO_ctrl_read_request(BIO *bio)
580     {
581         return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
582         }