BIO: at the end of BIO_new, declare the BIO inited if no create method present
[openssl.git] / crypto / bio / bio_lib.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include <openssl/crypto.h>
13 #include "bio_lcl.h"
14 #include "internal/cryptlib.h"
15
16
17 /*
18  * Helper macro for the callback to determine whether an operator expects a
19  * len parameter or not
20  */
21 #define HAS_LEN_OPER(o)        ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE || \
22                                 (o) == BIO_CB_GETS)
23
24 /*
25  * Helper function to work out whether to call the new style callback or the old
26  * one, and translate between the two.
27  *
28  * This has a long return type for consistency with the old callback. Similarly
29  * for the "long" used for "inret"
30  */
31 static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
32                               int argi, long argl, long inret, size_t *processed)
33 {
34     long ret;
35     int bareoper;
36
37     if (b->callback_ex != NULL) {
38         return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
39     }
40
41     /* Strip off any BIO_CB_RETURN flag */
42     bareoper = oper & ~BIO_CB_RETURN;
43
44     /*
45      * We have an old style callback, so we will have to do nasty casts and
46      * check for overflows.
47      */
48     if (HAS_LEN_OPER(bareoper)) {
49         /* In this case |len| is set, and should be used instead of |argi| */
50         if (len > INT_MAX)
51             return -1;
52
53         argi = (int)len;
54
55         if (inret && (oper & BIO_CB_RETURN)) {
56             if (*processed > INT_MAX)
57                 return -1;
58             inret = *processed;
59         }
60     }
61
62     ret = b->callback(b, oper, argp, argi, argl, inret);
63
64     if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
65         *processed = (size_t)ret;
66         ret = 1;
67     }
68
69     return ret;
70 }
71
72 BIO *BIO_new(const BIO_METHOD *method)
73 {
74     BIO *bio = OPENSSL_zalloc(sizeof(*bio));
75
76     if (bio == NULL) {
77         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
78         return NULL;
79     }
80
81     bio->method = method;
82     bio->shutdown = 1;
83     bio->references = 1;
84
85     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
86         goto err;
87
88     bio->lock = CRYPTO_THREAD_lock_new();
89     if (bio->lock == NULL) {
90         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
91         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
92         goto err;
93     }
94
95     if (method->create != NULL && !method->create(bio)) {
96         BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
97         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
98         CRYPTO_THREAD_lock_free(bio->lock);
99         goto err;
100     }
101     if (method->create == NULL)
102         bio->init = 1;
103
104     return bio;
105
106 err:
107     OPENSSL_free(bio);
108     return NULL;
109 }
110
111 int BIO_free(BIO *a)
112 {
113     int ret;
114
115     if (a == NULL)
116         return 0;
117
118     if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
119         return 0;
120
121     REF_PRINT_COUNT("BIO", a);
122     if (ret > 0)
123         return 1;
124     REF_ASSERT_ISNT(ret < 0);
125
126     if (a->callback != NULL || a->callback_ex != NULL) {
127         ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
128         if (ret <= 0)
129             return ret;
130     }
131
132     if ((a->method != NULL) && (a->method->destroy != NULL))
133         a->method->destroy(a);
134
135     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
136
137     CRYPTO_THREAD_lock_free(a->lock);
138
139     OPENSSL_free(a);
140
141     return 1;
142 }
143
144 void BIO_set_data(BIO *a, void *ptr)
145 {
146     a->ptr = ptr;
147 }
148
149 void *BIO_get_data(BIO *a)
150 {
151     return a->ptr;
152 }
153
154 void BIO_set_init(BIO *a, int init)
155 {
156     a->init = init;
157 }
158
159 int BIO_get_init(BIO *a)
160 {
161     return a->init;
162 }
163
164 void BIO_set_shutdown(BIO *a, int shut)
165 {
166     a->shutdown = shut;
167 }
168
169 int BIO_get_shutdown(BIO *a)
170 {
171     return a->shutdown;
172 }
173
174 void BIO_vfree(BIO *a)
175 {
176     BIO_free(a);
177 }
178
179 int BIO_up_ref(BIO *a)
180 {
181     int i;
182
183     if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
184         return 0;
185
186     REF_PRINT_COUNT("BIO", a);
187     REF_ASSERT_ISNT(i < 2);
188     return ((i > 1) ? 1 : 0);
189 }
190
191 void BIO_clear_flags(BIO *b, int flags)
192 {
193     b->flags &= ~flags;
194 }
195
196 int BIO_test_flags(const BIO *b, int flags)
197 {
198     return (b->flags & flags);
199 }
200
201 void BIO_set_flags(BIO *b, int flags)
202 {
203     b->flags |= flags;
204 }
205
206 BIO_callback_fn BIO_get_callback(const BIO *b)
207 {
208     return b->callback;
209 }
210
211 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
212 {
213     b->callback = cb;
214 }
215
216 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
217 {
218     return b->callback_ex;
219 }
220
221 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
222 {
223     b->callback_ex = cb;
224 }
225
226 void BIO_set_callback_arg(BIO *b, char *arg)
227 {
228     b->cb_arg = arg;
229 }
230
231 char *BIO_get_callback_arg(const BIO *b)
232 {
233     return b->cb_arg;
234 }
235
236 const char *BIO_method_name(const BIO *b)
237 {
238     return b->method->name;
239 }
240
241 int BIO_method_type(const BIO *b)
242 {
243     return b->method->type;
244 }
245
246 /*
247  * This is essentially the same as BIO_read_ex() except that it allows
248  * 0 or a negative value to indicate failure (retryable or not) in the return.
249  * This is for compatibility with the old style BIO_read(), where existing code
250  * may make assumptions about the return value that it might get.
251  */
252 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
253 {
254     int ret;
255
256     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
257         BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
258         return -2;
259     }
260
261     if ((b->callback != NULL || b->callback_ex != NULL) &&
262         ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
263                                        readbytes)) <= 0))
264         return ret;
265
266     if (!b->init) {
267         BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
268         return -2;
269     }
270
271     ret = b->method->bread(b, data, dlen, readbytes);
272
273     if (ret > 0)
274         b->num_read += (uint64_t)*readbytes;
275
276     if (b->callback != NULL || b->callback_ex != NULL)
277         ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
278                                      dlen, 0, 0L, ret, readbytes);
279
280     /* Shouldn't happen */
281     if (ret > 0 && *readbytes > dlen) {
282         BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
283         return -1;
284     }
285
286     return ret;
287 }
288
289 int BIO_read(BIO *b, void *data, int dlen)
290 {
291     size_t readbytes;
292     int ret;
293
294     if (dlen < 0)
295         return 0;
296
297     ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
298
299     if (ret > 0) {
300         /* *readbytes should always be <= dlen */
301         ret = (int)readbytes;
302     }
303
304     return ret;
305 }
306
307 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
308 {
309     int ret;
310
311     ret = bio_read_intern(b, data, dlen, readbytes);
312
313     if (ret > 0)
314         ret = 1;
315     else
316         ret = 0;
317
318     return ret;
319 }
320
321 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
322                             size_t *written)
323 {
324     int ret;
325
326     if (b == NULL)
327         return 0;
328
329     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
330         BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
331         return -2;
332     }
333
334     if ((b->callback != NULL || b->callback_ex != NULL) &&
335         ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
336                                        written)) <= 0))
337         return ret;
338
339     if (!b->init) {
340         BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
341         return -2;
342     }
343
344     ret = b->method->bwrite(b, data, dlen, written);
345
346     if (ret > 0)
347         b->num_write += (uint64_t)*written;
348
349     if (b->callback != NULL || b->callback_ex != NULL)
350         ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
351                                      dlen, 0, 0L, ret, written);
352
353     return ret;
354 }
355
356 int BIO_write(BIO *b, const void *data, int dlen)
357 {
358     size_t written;
359     int ret;
360
361     if (dlen < 0)
362         return 0;
363
364     ret = bio_write_intern(b, data, (size_t)dlen, &written);
365
366     if (ret > 0) {
367         /* *written should always be <= dlen */
368         ret = (int)written;
369     }
370
371     return ret;
372 }
373
374 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
375 {
376     int ret;
377
378     ret = bio_write_intern(b, data, dlen, written);
379
380     if (ret > 0)
381         ret = 1;
382     else
383         ret = 0;
384
385     return ret;
386 }
387
388 int BIO_puts(BIO *b, const char *buf)
389 {
390     int ret;
391     size_t written = 0;
392
393     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
394         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
395         return -2;
396     }
397
398     if (b->callback != NULL || b->callback_ex != NULL) {
399         ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
400         if (ret <= 0)
401             return ret;
402     }
403
404     if (!b->init) {
405         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
406         return -2;
407     }
408
409     ret = b->method->bputs(b, buf);
410
411     if (ret > 0) {
412         b->num_write += (uint64_t)ret;
413         written = ret;
414         ret = 1;
415     }
416
417     if (b->callback != NULL || b->callback_ex != NULL)
418         ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
419                                      0L, ret, &written);
420
421     if (ret > 0) {
422         if (written > INT_MAX) {
423             BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
424             ret = -1;
425         } else {
426             ret = (int)written;
427         }
428     }
429
430     return ret;
431 }
432
433 int BIO_gets(BIO *b, char *buf, int size)
434 {
435     int ret;
436     size_t readbytes = 0;
437
438     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
439         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
440         return -2;
441     }
442
443     if (size < 0) {
444         BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
445         return 0;
446     }
447
448     if (b->callback != NULL || b->callback_ex != NULL) {
449         ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
450         if (ret <= 0)
451             return ret;
452     }
453
454     if (!b->init) {
455         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
456         return -2;
457     }
458
459     ret = b->method->bgets(b, buf, size);
460
461     if (ret > 0) {
462         readbytes = ret;
463         ret = 1;
464     }
465
466     if (b->callback != NULL || b->callback_ex != NULL)
467         ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
468                                      0, 0L, ret, &readbytes);
469
470     if (ret > 0) {
471         /* Shouldn't happen */
472         if (readbytes > (size_t)size)
473             ret = -1;
474         else
475             ret = (int)readbytes;
476     }
477
478     return ret;
479 }
480
481 int BIO_indent(BIO *b, int indent, int max)
482 {
483     if (indent < 0)
484         indent = 0;
485     if (indent > max)
486         indent = max;
487     while (indent--)
488         if (BIO_puts(b, " ") != 1)
489             return 0;
490     return 1;
491 }
492
493 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
494 {
495     int i;
496
497     i = iarg;
498     return BIO_ctrl(b, cmd, larg, (char *)&i);
499 }
500
501 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
502 {
503     void *p = NULL;
504
505     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
506         return NULL;
507     else
508         return p;
509 }
510
511 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
512 {
513     long ret;
514
515     if (b == NULL)
516         return 0;
517
518     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
519         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
520         return -2;
521     }
522
523     if (b->callback != NULL || b->callback_ex != NULL) {
524         ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
525         if (ret <= 0)
526             return ret;
527     }
528
529     ret = b->method->ctrl(b, cmd, larg, parg);
530
531     if (b->callback != NULL || b->callback_ex != NULL)
532         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
533                                 larg, ret, NULL);
534
535     return ret;
536 }
537
538 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
539 {
540     long ret;
541
542     if (b == NULL)
543         return 0;
544
545     if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
546         BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
547         return -2;
548     }
549
550     if (b->callback != NULL || b->callback_ex != NULL) {
551         ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
552                                 NULL);
553         if (ret <= 0)
554             return ret;
555     }
556
557     ret = b->method->callback_ctrl(b, cmd, fp);
558
559     if (b->callback != NULL || b->callback_ex != NULL)
560         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
561                                 cmd, 0, ret, NULL);
562
563     return ret;
564 }
565
566 /*
567  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
568  * do; but those macros have inappropriate return type, and for interfacing
569  * from other programming languages, C macros aren't much of a help anyway.
570  */
571 size_t BIO_ctrl_pending(BIO *bio)
572 {
573     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
574 }
575
576 size_t BIO_ctrl_wpending(BIO *bio)
577 {
578     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
579 }
580
581 /* put the 'bio' on the end of b's list of operators */
582 BIO *BIO_push(BIO *b, BIO *bio)
583 {
584     BIO *lb;
585
586     if (b == NULL)
587         return bio;
588     lb = b;
589     while (lb->next_bio != NULL)
590         lb = lb->next_bio;
591     lb->next_bio = bio;
592     if (bio != NULL)
593         bio->prev_bio = lb;
594     /* called to do internal processing */
595     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
596     return b;
597 }
598
599 /* Remove the first and return the rest */
600 BIO *BIO_pop(BIO *b)
601 {
602     BIO *ret;
603
604     if (b == NULL)
605         return NULL;
606     ret = b->next_bio;
607
608     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
609
610     if (b->prev_bio != NULL)
611         b->prev_bio->next_bio = b->next_bio;
612     if (b->next_bio != NULL)
613         b->next_bio->prev_bio = b->prev_bio;
614
615     b->next_bio = NULL;
616     b->prev_bio = NULL;
617     return ret;
618 }
619
620 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
621 {
622     BIO *b, *last;
623
624     b = last = bio;
625     for (;;) {
626         if (!BIO_should_retry(b))
627             break;
628         last = b;
629         b = b->next_bio;
630         if (b == NULL)
631             break;
632     }
633     if (reason != NULL)
634         *reason = last->retry_reason;
635     return last;
636 }
637
638 int BIO_get_retry_reason(BIO *bio)
639 {
640     return bio->retry_reason;
641 }
642
643 void BIO_set_retry_reason(BIO *bio, int reason)
644 {
645     bio->retry_reason = reason;
646 }
647
648 BIO *BIO_find_type(BIO *bio, int type)
649 {
650     int mt, mask;
651
652     if (bio == NULL)
653         return NULL;
654     mask = type & 0xff;
655     do {
656         if (bio->method != NULL) {
657             mt = bio->method->type;
658
659             if (!mask) {
660                 if (mt & type)
661                     return bio;
662             } else if (mt == type)
663                 return bio;
664         }
665         bio = bio->next_bio;
666     } while (bio != NULL);
667     return NULL;
668 }
669
670 BIO *BIO_next(BIO *b)
671 {
672     if (b == NULL)
673         return NULL;
674     return b->next_bio;
675 }
676
677 void BIO_set_next(BIO *b, BIO *next)
678 {
679     b->next_bio = next;
680 }
681
682 void BIO_free_all(BIO *bio)
683 {
684     BIO *b;
685     int ref;
686
687     while (bio != NULL) {
688         b = bio;
689         ref = b->references;
690         bio = bio->next_bio;
691         BIO_free(b);
692         /* Since ref count > 1, don't free anyone else. */
693         if (ref > 1)
694             break;
695     }
696 }
697
698 BIO *BIO_dup_chain(BIO *in)
699 {
700     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
701
702     for (bio = in; bio != NULL; bio = bio->next_bio) {
703         if ((new_bio = BIO_new(bio->method)) == NULL)
704             goto err;
705         new_bio->callback = bio->callback;
706         new_bio->callback_ex = bio->callback_ex;
707         new_bio->cb_arg = bio->cb_arg;
708         new_bio->init = bio->init;
709         new_bio->shutdown = bio->shutdown;
710         new_bio->flags = bio->flags;
711
712         /* This will let SSL_s_sock() work with stdin/stdout */
713         new_bio->num = bio->num;
714
715         if (!BIO_dup_state(bio, (char *)new_bio)) {
716             BIO_free(new_bio);
717             goto err;
718         }
719
720         /* copy app data */
721         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
722                                 &bio->ex_data)) {
723             BIO_free(new_bio);
724             goto err;
725         }
726
727         if (ret == NULL) {
728             eoc = new_bio;
729             ret = eoc;
730         } else {
731             BIO_push(eoc, new_bio);
732             eoc = new_bio;
733         }
734     }
735     return ret;
736  err:
737     BIO_free_all(ret);
738
739     return NULL;
740 }
741
742 void BIO_copy_next_retry(BIO *b)
743 {
744     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
745     b->retry_reason = b->next_bio->retry_reason;
746 }
747
748 int BIO_set_ex_data(BIO *bio, int idx, void *data)
749 {
750     return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
751 }
752
753 void *BIO_get_ex_data(BIO *bio, int idx)
754 {
755     return CRYPTO_get_ex_data(&(bio->ex_data), idx);
756 }
757
758 uint64_t BIO_number_read(BIO *bio)
759 {
760     if (bio)
761         return bio->num_read;
762     return 0;
763 }
764
765 uint64_t BIO_number_written(BIO *bio)
766 {
767     if (bio)
768         return bio->num_write;
769     return 0;
770 }
771
772 void bio_free_ex_data(BIO *bio)
773 {
774     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
775 }
776
777 void bio_cleanup(void)
778 {
779 #ifndef OPENSSL_NO_SOCK
780     bio_sock_cleanup_int();
781     CRYPTO_THREAD_lock_free(bio_lookup_lock);
782     bio_lookup_lock = NULL;
783 #endif
784     CRYPTO_THREAD_lock_free(bio_type_lock);
785     bio_type_lock = NULL;
786 }