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