More parameter naming of BIO_read*/BIO_write* related functions
[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 /*
248  * This is essentially the same as BIO_read_ex() except that it allows
249  * 0 or a -ve value to indicate failure (retryable or not) in the return. This
250  * is for compatibility with the old style BIO_read(), where existing code may
251  * make assumptions about the return value that it might get.
252  */
253 static int bio_read_intern(BIO *b, void *data, size_t datal, size_t *read)
254 {
255     int ret;
256
257     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
258         BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
259         return -2;
260     }
261
262     if ((b->callback != NULL || b->callback_ex != NULL) &&
263         ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, datal, 0, 0L, 1L,
264                                        read)) <= 0))
265         return ret;
266
267     if (!b->init) {
268         BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
269         return -2;
270     }
271
272     ret = b->method->bread(b, data, datal, read);
273
274     if (ret > 0)
275         b->num_read += (uint64_t)*read;
276
277     if (b->callback != NULL || b->callback_ex != NULL)
278         ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
279                                      datal, 0, 0L, ret, read);
280
281     return ret;
282 }
283
284 int BIO_read(BIO *b, void *data, int datal)
285 {
286     size_t read;
287     int ret;
288
289     if (datal < 0)
290         return 0;
291
292     ret = bio_read_intern(b, data, (size_t)datal, &read);
293
294     if (ret > 0) {
295         /* *read should always be <= outl */
296         ret = (int)read;
297     }
298
299     return ret;
300 }
301
302 int BIO_read_ex(BIO *b, void *data, size_t datal, size_t *read)
303 {
304     int ret;
305
306     ret = bio_read_intern(b, data, datal, read);
307
308     if (ret > 0)
309         ret = 1;
310     else
311         ret = 0;
312
313     return ret;
314 }
315
316 static int bio_write_intern(BIO *b, const void *data, size_t datal,
317                             size_t *written)
318 {
319     int ret;
320
321     if (b == NULL)
322         return 0;
323
324     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
325         BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
326         return -2;
327     }
328
329     if ((b->callback != NULL || b->callback_ex != NULL) &&
330         ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, datal, 0, 0L, 1L,
331                                        written)) <= 0))
332         return ret;
333
334     if (!b->init) {
335         BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
336         return -2;
337     }
338
339     ret = b->method->bwrite(b, data, datal, written);
340
341     if (ret > 0)
342         b->num_write += (uint64_t)*written;
343
344     if (b->callback != NULL || b->callback_ex != NULL)
345         ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
346                                      datal, 0, 0L, ret, written);
347
348     return ret;
349 }
350
351 int BIO_write(BIO *b, const void *data, int datal)
352 {
353     size_t written;
354     int ret;
355
356     if (datal < 0)
357         return 0;
358
359     ret = bio_write_intern(b, data, (size_t)datal, &written);
360
361     if (ret > 0) {
362         /* *written should always be <= inl */
363         ret = (int)written;
364     }
365
366     return ret;
367 }
368
369 int BIO_write_ex(BIO *b, const void *data, size_t datal, size_t *written)
370 {
371     int ret;
372
373     ret = bio_write_intern(b, data, datal, written);
374
375     if (ret > 0)
376         ret = 1;
377     else
378         ret = 0;
379
380     return ret;
381 }
382
383 int BIO_puts(BIO *b, const char *in)
384 {
385     int ret;
386     size_t written = 0;
387
388     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
389         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
390         return -2;
391     }
392
393     if (b->callback != NULL || b->callback_ex != NULL) {
394         ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
395         if (ret <= 0)
396             return ret;
397     }
398
399     if (!b->init) {
400         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
401         return -2;
402     }
403
404     ret = b->method->bputs(b, in);
405
406     if (ret > 0) {
407         b->num_write += (uint64_t)ret;
408         written = ret;
409         ret = 1;
410     }
411
412     if (b->callback != NULL || b->callback_ex != NULL)
413         ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0,
414                                      0L, ret, &written);
415
416     if (ret > 0) {
417         if (written > INT_MAX)
418             ret = -1;
419         else
420             ret = (int)written;
421     }
422
423     return ret;
424 }
425
426 int BIO_gets(BIO *b, char *out, int outl)
427 {
428     int ret;
429     size_t read = 0;
430
431     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
432         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
433         return (-2);
434     }
435
436     if (b->callback != NULL || b->callback_ex != NULL) {
437         ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
438         if (ret <= 0)
439             return ret;
440     }
441
442     if (!b->init) {
443         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
444         return (-2);
445     }
446
447     ret = b->method->bgets(b, out, outl);
448
449     if (ret > 0) {
450         read = ret;
451         ret = 1;
452     }
453
454     if (b->callback != NULL || b->callback_ex != NULL)
455         ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
456                                      0, 0L, ret, &read);
457
458     if (ret > 0) {
459         if (read > INT_MAX)
460             ret = -1;
461         else
462             ret = (int)read;
463     }
464
465     return ret;
466 }
467
468 int BIO_indent(BIO *b, int indent, int max)
469 {
470     if (indent < 0)
471         indent = 0;
472     if (indent > max)
473         indent = max;
474     while (indent--)
475         if (BIO_puts(b, " ") != 1)
476             return 0;
477     return 1;
478 }
479
480 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
481 {
482     int i;
483
484     i = iarg;
485     return (BIO_ctrl(b, cmd, larg, (char *)&i));
486 }
487
488 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
489 {
490     void *p = NULL;
491
492     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
493         return (NULL);
494     else
495         return (p);
496 }
497
498 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
499 {
500     long ret;
501
502     if (b == NULL)
503         return 0;
504
505     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
506         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
507         return -2;
508     }
509
510     if (b->callback != NULL || b->callback_ex != NULL) {
511         ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
512         if (ret <= 0)
513             return ret;
514     }
515
516     ret = b->method->ctrl(b, cmd, larg, parg);
517
518     if (b->callback != NULL || b->callback_ex != NULL)
519         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
520                                 larg, ret, NULL);
521
522     return ret;
523 }
524
525 long BIO_callback_ctrl(BIO *b, int cmd,
526                        void (*fp) (struct bio_st *, int, const char *, int,
527                                    long, long))
528 {
529     long ret;
530
531     if (b == NULL)
532         return (0);
533
534     if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
535         BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
536         return (-2);
537     }
538
539     if (b->callback != NULL || b->callback_ex != NULL) {
540         ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
541                                 NULL);
542         if (ret <= 0)
543             return ret;
544     }
545
546     ret = b->method->callback_ctrl(b, cmd, fp);
547
548     if (b->callback != NULL || b->callback_ex != NULL)
549         ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
550                                 cmd, 0, ret, NULL);
551
552     return ret;
553 }
554
555 /*
556  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
557  * do; but those macros have inappropriate return type, and for interfacing
558  * from other programming languages, C macros aren't much of a help anyway.
559  */
560 size_t BIO_ctrl_pending(BIO *bio)
561 {
562     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
563 }
564
565 size_t BIO_ctrl_wpending(BIO *bio)
566 {
567     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
568 }
569
570 /* put the 'bio' on the end of b's list of operators */
571 BIO *BIO_push(BIO *b, BIO *bio)
572 {
573     BIO *lb;
574
575     if (b == NULL)
576         return (bio);
577     lb = b;
578     while (lb->next_bio != NULL)
579         lb = lb->next_bio;
580     lb->next_bio = bio;
581     if (bio != NULL)
582         bio->prev_bio = lb;
583     /* called to do internal processing */
584     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
585     return (b);
586 }
587
588 /* Remove the first and return the rest */
589 BIO *BIO_pop(BIO *b)
590 {
591     BIO *ret;
592
593     if (b == NULL)
594         return (NULL);
595     ret = b->next_bio;
596
597     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
598
599     if (b->prev_bio != NULL)
600         b->prev_bio->next_bio = b->next_bio;
601     if (b->next_bio != NULL)
602         b->next_bio->prev_bio = b->prev_bio;
603
604     b->next_bio = NULL;
605     b->prev_bio = NULL;
606     return (ret);
607 }
608
609 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
610 {
611     BIO *b, *last;
612
613     b = last = bio;
614     for (;;) {
615         if (!BIO_should_retry(b))
616             break;
617         last = b;
618         b = b->next_bio;
619         if (b == NULL)
620             break;
621     }
622     if (reason != NULL)
623         *reason = last->retry_reason;
624     return (last);
625 }
626
627 int BIO_get_retry_reason(BIO *bio)
628 {
629     return (bio->retry_reason);
630 }
631
632 void BIO_set_retry_reason(BIO *bio, int reason)
633 {
634     bio->retry_reason = reason;
635 }
636
637 BIO *BIO_find_type(BIO *bio, int type)
638 {
639     int mt, mask;
640
641     if (bio == NULL)
642         return NULL;
643     mask = type & 0xff;
644     do {
645         if (bio->method != NULL) {
646             mt = bio->method->type;
647
648             if (!mask) {
649                 if (mt & type)
650                     return (bio);
651             } else if (mt == type)
652                 return (bio);
653         }
654         bio = bio->next_bio;
655     } while (bio != NULL);
656     return (NULL);
657 }
658
659 BIO *BIO_next(BIO *b)
660 {
661     if (b == NULL)
662         return NULL;
663     return b->next_bio;
664 }
665
666 void BIO_set_next(BIO *b, BIO *next)
667 {
668     b->next_bio = next;
669 }
670
671 void BIO_free_all(BIO *bio)
672 {
673     BIO *b;
674     int ref;
675
676     while (bio != NULL) {
677         b = bio;
678         ref = b->references;
679         bio = bio->next_bio;
680         BIO_free(b);
681         /* Since ref count > 1, don't free anyone else. */
682         if (ref > 1)
683             break;
684     }
685 }
686
687 BIO *BIO_dup_chain(BIO *in)
688 {
689     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
690
691     for (bio = in; bio != NULL; bio = bio->next_bio) {
692         if ((new_bio = BIO_new(bio->method)) == NULL)
693             goto err;
694         new_bio->callback = bio->callback;
695         new_bio->callback_ex = bio->callback_ex;
696         new_bio->cb_arg = bio->cb_arg;
697         new_bio->init = bio->init;
698         new_bio->shutdown = bio->shutdown;
699         new_bio->flags = bio->flags;
700
701         /* This will let SSL_s_sock() work with stdin/stdout */
702         new_bio->num = bio->num;
703
704         if (!BIO_dup_state(bio, (char *)new_bio)) {
705             BIO_free(new_bio);
706             goto err;
707         }
708
709         /* copy app data */
710         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
711                                 &bio->ex_data)) {
712             BIO_free(new_bio);
713             goto err;
714         }
715
716         if (ret == NULL) {
717             eoc = new_bio;
718             ret = eoc;
719         } else {
720             BIO_push(eoc, new_bio);
721             eoc = new_bio;
722         }
723     }
724     return (ret);
725  err:
726     BIO_free_all(ret);
727
728     return (NULL);
729 }
730
731 void BIO_copy_next_retry(BIO *b)
732 {
733     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
734     b->retry_reason = b->next_bio->retry_reason;
735 }
736
737 int BIO_set_ex_data(BIO *bio, int idx, void *data)
738 {
739     return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
740 }
741
742 void *BIO_get_ex_data(BIO *bio, int idx)
743 {
744     return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
745 }
746
747 uint64_t BIO_number_read(BIO *bio)
748 {
749     if (bio)
750         return bio->num_read;
751     return 0;
752 }
753
754 uint64_t BIO_number_written(BIO *bio)
755 {
756     if (bio)
757         return bio->num_write;
758     return 0;
759 }
760
761 void bio_free_ex_data(BIO *bio)
762 {
763     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
764 }
765
766 void bio_cleanup(void)
767 {
768 #ifndef OPENSSL_NO_SOCK
769     bio_sock_cleanup_int();
770     CRYPTO_THREAD_lock_free(bio_lookup_lock);
771     bio_lookup_lock = NULL;
772 #endif
773     CRYPTO_THREAD_lock_free(bio_type_lock);
774     bio_type_lock = NULL;
775 }