Fix some bogus uninit variable warnings
[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 > LONG_MAX || ret < LONG_MIN)
65         return -1;
66
67     if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
68         *processed = (size_t)ret;
69         ret = 1;
70     }
71
72     return ret;
73 }
74
75 BIO *BIO_new(const BIO_METHOD *method)
76 {
77     BIO *bio = OPENSSL_zalloc(sizeof(*bio));
78
79     if (bio == NULL) {
80         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
81         return (NULL);
82     }
83
84     bio->method = method;
85     bio->shutdown = 1;
86     bio->references = 1;
87
88     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
89         goto err;
90
91     bio->lock = CRYPTO_THREAD_lock_new();
92     if (bio->lock == NULL) {
93         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
94         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
95         goto err;
96     }
97
98     if (method->create != NULL && !method->create(bio)) {
99         BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
100         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
101         CRYPTO_THREAD_lock_free(bio->lock);
102         goto err;
103     }
104
105     return bio;
106
107 err:
108     OPENSSL_free(bio);
109     return NULL;
110 }
111
112 int BIO_free(BIO *a)
113 {
114     int ret;
115
116     if (a == NULL)
117         return 0;
118
119     if (CRYPTO_atomic_add(&a->references, -1, &ret, a->lock) <= 0)
120         return 0;
121
122     REF_PRINT_COUNT("BIO", a);
123     if (ret > 0)
124         return 1;
125     REF_ASSERT_ISNT(ret < 0);
126
127     if (a->callback != NULL || a->callback_ex != NULL) {
128         ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
129         if (ret <= 0)
130             return ret;
131     }
132
133     if ((a->method != NULL) && (a->method->destroy != NULL))
134         a->method->destroy(a);
135
136     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
137
138     CRYPTO_THREAD_lock_free(a->lock);
139
140     OPENSSL_free(a);
141
142     return 1;
143 }
144
145 void BIO_set_data(BIO *a, void *ptr)
146 {
147     a->ptr = ptr;
148 }
149
150 void *BIO_get_data(BIO *a)
151 {
152     return a->ptr;
153 }
154
155 void BIO_set_init(BIO *a, int init)
156 {
157     a->init = init;
158 }
159
160 int BIO_get_init(BIO *a)
161 {
162     return a->init;
163 }
164
165 void BIO_set_shutdown(BIO *a, int shut)
166 {
167     a->shutdown = shut;
168 }
169
170 int BIO_get_shutdown(BIO *a)
171 {
172     return a->shutdown;
173 }
174
175 void BIO_vfree(BIO *a)
176 {
177     BIO_free(a);
178 }
179
180 int BIO_up_ref(BIO *a)
181 {
182     int i;
183
184     if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
185         return 0;
186
187     REF_PRINT_COUNT("BIO", a);
188     REF_ASSERT_ISNT(i < 2);
189     return ((i > 1) ? 1 : 0);
190 }
191
192 void BIO_clear_flags(BIO *b, int flags)
193 {
194     b->flags &= ~flags;
195 }
196
197 int BIO_test_flags(const BIO *b, int flags)
198 {
199     return (b->flags & flags);
200 }
201
202 void BIO_set_flags(BIO *b, int flags)
203 {
204     b->flags |= flags;
205 }
206
207 BIO_callback_fn BIO_get_callback(const BIO *b)
208 {
209     return b->callback;
210 }
211
212 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
213 {
214     b->callback = cb;
215 }
216
217 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
218 {
219     return b->callback_ex;
220 }
221
222 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
223 {
224     b->callback_ex = cb;
225 }
226
227 void BIO_set_callback_arg(BIO *b, char *arg)
228 {
229     b->cb_arg = arg;
230 }
231
232 char *BIO_get_callback_arg(const BIO *b)
233 {
234     return b->cb_arg;
235 }
236
237 const char *BIO_method_name(const BIO *b)
238 {
239     return b->method->name;
240 }
241
242 int BIO_method_type(const BIO *b)
243 {
244     return b->method->type;
245 }
246
247 int BIO_read(BIO *b, void *out, int outl)
248 {
249     size_t read;
250     int ret;
251
252     if (outl < 0)
253         return 0;
254
255     ret = BIO_read_ex(b, out, (size_t)outl, &read);
256
257     if (ret > 0) {
258         /* *read should always be <= outl */
259         ret = (int)read;
260     }
261
262     return ret;
263 }
264
265 int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
266 {
267     int ret;
268
269     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
270         BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
271         return (-2);
272     }
273
274     if ((b->callback != NULL || b->callback_ex != NULL) &&
275         ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0, 0L, 1L,
276                                        read)) <= 0))
277         return ret;
278
279     if (!b->init) {
280         BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
281         return -2;
282     }
283
284     ret = b->method->bread(b, out, outl, read);
285
286     if (ret > 0)
287         b->num_read += (uint64_t)*read;
288
289     if (b->callback != NULL || b->callback_ex != NULL)
290         ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, out, outl,
291                                      0, 0L, ret, read);
292
293     return ret;
294 }
295
296 int BIO_write(BIO *b, const void *in, int inl)
297 {
298     size_t written;
299     int ret;
300
301     if (inl < 0)
302         return 0;
303
304     ret = BIO_write_ex(b, in, (size_t)inl, &written);
305
306     if (ret > 0) {
307         /* *written should always be <= inl */
308         ret = (int)written;
309     }
310
311     return ret;
312 }
313
314 int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written)
315 {
316     int ret;
317
318     if (b == NULL)
319         return (0);
320
321     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
322         BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
323         return (-2);
324     }
325
326     if ((b->callback != NULL || b->callback_ex != NULL) &&
327         ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0, 0L, 1L,
328                                        written)) <= 0))
329         return ret;
330
331     if (!b->init) {
332         BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
333         return -2;
334     }
335
336     ret = b->method->bwrite(b, in, inl, written);
337
338     if (ret > 0)
339         b->num_write += (uint64_t)*written;
340
341     if (b->callback != NULL || b->callback_ex != NULL)
342         ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl,
343                                      0, 0L, ret, written);
344
345     return ret;
346 }
347
348 int BIO_puts(BIO *b, const char *in)
349 {
350     int ret;
351     size_t written = 0;
352
353     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
354         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
355         return -2;
356     }
357
358     if (b->callback != NULL || b->callback_ex != NULL) {
359         ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
360         if (ret <= 0)
361             return ret;
362     }
363
364     if (!b->init) {
365         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
366         return -2;
367     }
368
369     ret = b->method->bputs(b, in);
370
371     if (ret > 0) {
372         b->num_write += (uint64_t)ret;
373         written = ret;
374         ret = 1;
375     }
376
377     if (b->callback != NULL || b->callback_ex != NULL)
378         ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0,
379                                      0L, ret, &written);
380
381     if (ret > 0) {
382         if (written > INT_MAX)
383             ret = -1;
384         else
385             ret = (int)written;
386     }
387
388     return ret;
389 }
390
391 int BIO_gets(BIO *b, char *out, int outl)
392 {
393     int ret;
394     size_t read = 0;
395
396     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
397         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
398         return (-2);
399     }
400
401     if (b->callback != NULL || b->callback_ex != NULL) {
402         ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
403         if (ret <= 0)
404             return ret;
405     }
406
407     if (!b->init) {
408         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
409         return (-2);
410     }
411
412     ret = b->method->bgets(b, out, outl);
413
414     if (ret > 0) {
415         read = ret;
416         ret = 1;
417     }
418
419     if (b->callback != NULL || b->callback_ex != NULL)
420         ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
421                                      0, 0L, ret, &read);
422
423     if (ret > 0) {
424         if (read > INT_MAX)
425             ret = -1;
426         else
427             ret = (int)read;
428     }
429
430     return ret;
431 }
432
433 int BIO_indent(BIO *b, int indent, int max)
434 {
435     if (indent < 0)
436         indent = 0;
437     if (indent > max)
438         indent = max;
439     while (indent--)
440         if (BIO_puts(b, " ") != 1)
441             return 0;
442     return 1;
443 }
444
445 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
446 {
447     int i;
448
449     i = iarg;
450     return (BIO_ctrl(b, cmd, larg, (char *)&i));
451 }
452
453 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
454 {
455     void *p = NULL;
456
457     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
458         return (NULL);
459     else
460         return (p);
461 }
462
463 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
464 {
465     long ret;
466
467     if (b == NULL)
468         return 0;
469
470     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
471         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
472         return -2;
473     }
474
475     if (b->callback != NULL || b->callback_ex != NULL) {
476         ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
477         if (ret <= 0)
478             return ret;
479     }
480
481     ret = b->method->ctrl(b, cmd, larg, parg);
482
483     if (b->callback != NULL || b->callback_ex != NULL)
484         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
485                                 larg, ret, NULL);
486
487     return ret;
488 }
489
490 long BIO_callback_ctrl(BIO *b, int cmd,
491                        void (*fp) (struct bio_st *, int, const char *, int,
492                                    long, long))
493 {
494     long ret;
495
496     if (b == NULL)
497         return (0);
498
499     if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
500         BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
501         return (-2);
502     }
503
504     if (b->callback != NULL || b->callback_ex != NULL) {
505         ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
506                                 NULL);
507         if (ret <= 0)
508             return ret;
509     }
510
511     ret = b->method->callback_ctrl(b, cmd, fp);
512
513     if (b->callback != NULL || b->callback_ex != NULL)
514         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
515                                 cmd, 0, ret, NULL);
516
517     return ret;
518 }
519
520 /*
521  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
522  * do; but those macros have inappropriate return type, and for interfacing
523  * from other programming languages, C macros aren't much of a help anyway.
524  */
525 size_t BIO_ctrl_pending(BIO *bio)
526 {
527     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
528 }
529
530 size_t BIO_ctrl_wpending(BIO *bio)
531 {
532     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
533 }
534
535 /* put the 'bio' on the end of b's list of operators */
536 BIO *BIO_push(BIO *b, BIO *bio)
537 {
538     BIO *lb;
539
540     if (b == NULL)
541         return (bio);
542     lb = b;
543     while (lb->next_bio != NULL)
544         lb = lb->next_bio;
545     lb->next_bio = bio;
546     if (bio != NULL)
547         bio->prev_bio = lb;
548     /* called to do internal processing */
549     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
550     return (b);
551 }
552
553 /* Remove the first and return the rest */
554 BIO *BIO_pop(BIO *b)
555 {
556     BIO *ret;
557
558     if (b == NULL)
559         return (NULL);
560     ret = b->next_bio;
561
562     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
563
564     if (b->prev_bio != NULL)
565         b->prev_bio->next_bio = b->next_bio;
566     if (b->next_bio != NULL)
567         b->next_bio->prev_bio = b->prev_bio;
568
569     b->next_bio = NULL;
570     b->prev_bio = NULL;
571     return (ret);
572 }
573
574 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
575 {
576     BIO *b, *last;
577
578     b = last = bio;
579     for (;;) {
580         if (!BIO_should_retry(b))
581             break;
582         last = b;
583         b = b->next_bio;
584         if (b == NULL)
585             break;
586     }
587     if (reason != NULL)
588         *reason = last->retry_reason;
589     return (last);
590 }
591
592 int BIO_get_retry_reason(BIO *bio)
593 {
594     return (bio->retry_reason);
595 }
596
597 void BIO_set_retry_reason(BIO *bio, int reason)
598 {
599     bio->retry_reason = reason;
600 }
601
602 BIO *BIO_find_type(BIO *bio, int type)
603 {
604     int mt, mask;
605
606     if (bio == NULL)
607         return NULL;
608     mask = type & 0xff;
609     do {
610         if (bio->method != NULL) {
611             mt = bio->method->type;
612
613             if (!mask) {
614                 if (mt & type)
615                     return (bio);
616             } else if (mt == type)
617                 return (bio);
618         }
619         bio = bio->next_bio;
620     } while (bio != NULL);
621     return (NULL);
622 }
623
624 BIO *BIO_next(BIO *b)
625 {
626     if (b == NULL)
627         return NULL;
628     return b->next_bio;
629 }
630
631 void BIO_set_next(BIO *b, BIO *next)
632 {
633     b->next_bio = next;
634 }
635
636 void BIO_free_all(BIO *bio)
637 {
638     BIO *b;
639     int ref;
640
641     while (bio != NULL) {
642         b = bio;
643         ref = b->references;
644         bio = bio->next_bio;
645         BIO_free(b);
646         /* Since ref count > 1, don't free anyone else. */
647         if (ref > 1)
648             break;
649     }
650 }
651
652 BIO *BIO_dup_chain(BIO *in)
653 {
654     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
655
656     for (bio = in; bio != NULL; bio = bio->next_bio) {
657         if ((new_bio = BIO_new(bio->method)) == NULL)
658             goto err;
659         new_bio->callback = bio->callback;
660         new_bio->callback_ex = bio->callback_ex;
661         new_bio->cb_arg = bio->cb_arg;
662         new_bio->init = bio->init;
663         new_bio->shutdown = bio->shutdown;
664         new_bio->flags = bio->flags;
665
666         /* This will let SSL_s_sock() work with stdin/stdout */
667         new_bio->num = bio->num;
668
669         if (!BIO_dup_state(bio, (char *)new_bio)) {
670             BIO_free(new_bio);
671             goto err;
672         }
673
674         /* copy app data */
675         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
676                                 &bio->ex_data)) {
677             BIO_free(new_bio);
678             goto err;
679         }
680
681         if (ret == NULL) {
682             eoc = new_bio;
683             ret = eoc;
684         } else {
685             BIO_push(eoc, new_bio);
686             eoc = new_bio;
687         }
688     }
689     return (ret);
690  err:
691     BIO_free_all(ret);
692
693     return (NULL);
694 }
695
696 void BIO_copy_next_retry(BIO *b)
697 {
698     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
699     b->retry_reason = b->next_bio->retry_reason;
700 }
701
702 int BIO_set_ex_data(BIO *bio, int idx, void *data)
703 {
704     return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
705 }
706
707 void *BIO_get_ex_data(BIO *bio, int idx)
708 {
709     return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
710 }
711
712 uint64_t BIO_number_read(BIO *bio)
713 {
714     if (bio)
715         return bio->num_read;
716     return 0;
717 }
718
719 uint64_t BIO_number_written(BIO *bio)
720 {
721     if (bio)
722         return bio->num_write;
723     return 0;
724 }
725
726 void bio_free_ex_data(BIO *bio)
727 {
728     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
729 }
730
731 void bio_cleanup(void)
732 {
733 #ifndef OPENSSL_NO_SOCK
734     bio_sock_cleanup_int();
735     CRYPTO_THREAD_lock_free(bio_lookup_lock);
736     bio_lookup_lock = NULL;
737 #endif
738     CRYPTO_THREAD_lock_free(bio_type_lock);
739     bio_type_lock = NULL;
740 }