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