Fix ex_data locks issue
[openssl.git] / crypto / bio / bio_lib.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57
58 #include <stdio.h>
59 #include <errno.h>
60 #include <openssl/crypto.h>
61 #include "bio_lcl.h"
62 #include "internal/cryptlib.h"
63 #include <openssl/stack.h>
64
65 BIO *BIO_new(const BIO_METHOD *method)
66 {
67     BIO *ret = OPENSSL_malloc(sizeof(*ret));
68
69     if (ret == NULL) {
70         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
71         return (NULL);
72     }
73     if (!BIO_set(ret, method)) {
74         OPENSSL_free(ret);
75         ret = NULL;
76     }
77     return (ret);
78 }
79
80 int BIO_set(BIO *bio, const BIO_METHOD *method)
81 {
82     bio->method = method;
83     bio->callback = NULL;
84     bio->cb_arg = NULL;
85     bio->init = 0;
86     bio->shutdown = 1;
87     bio->flags = 0;
88     bio->retry_reason = 0;
89     bio->num = 0;
90     bio->ptr = NULL;
91     bio->prev_bio = NULL;
92     bio->next_bio = NULL;
93     bio->references = 1;
94     bio->num_read = 0L;
95     bio->num_write = 0L;
96     CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
97
98     bio->lock = CRYPTO_THREAD_lock_new();
99     if (bio->lock == NULL) {
100         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
101         return 0;
102     }
103
104     if (method->create != NULL) {
105         if (!method->create(bio)) {
106             CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
107             CRYPTO_THREAD_lock_free(bio->lock);
108             return 0;
109         }
110     }
111
112     return 1;
113 }
114
115 int BIO_free(BIO *a)
116 {
117     int i;
118
119     if (a == NULL)
120         return 0;
121
122     if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
123         return 0;
124
125     REF_PRINT_COUNT("BIO", a);
126     if (i > 0)
127         return 1;
128     REF_ASSERT_ISNT(i < 0);
129     if ((a->callback != NULL) &&
130         ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
131         return i;
132
133     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
134
135     CRYPTO_THREAD_lock_free(a->lock);
136
137     if ((a->method != NULL) && (a->method->destroy != NULL))
138         a->method->destroy(a);
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 long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
208                                         int, long, long) {
209     return b->callback;
210 }
211
212 void BIO_set_callback(BIO *b,
213                       long (*cb) (struct bio_st *, int, const char *, int,
214                                   long, long))
215 {
216     b->callback = cb;
217 }
218
219 void BIO_set_callback_arg(BIO *b, char *arg)
220 {
221     b->cb_arg = arg;
222 }
223
224 char *BIO_get_callback_arg(const BIO *b)
225 {
226     return b->cb_arg;
227 }
228
229 const char *BIO_method_name(const BIO *b)
230 {
231     return b->method->name;
232 }
233
234 int BIO_method_type(const BIO *b)
235 {
236     return b->method->type;
237 }
238
239 int BIO_read(BIO *b, void *out, int outl)
240 {
241     int i;
242     long (*cb) (BIO *, int, const char *, int, long, long);
243
244     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
245         BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
246         return (-2);
247     }
248
249     cb = b->callback;
250     if ((cb != NULL) &&
251         ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
252         return (i);
253
254     if (!b->init) {
255         BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
256         return (-2);
257     }
258
259     i = b->method->bread(b, out, outl);
260
261     if (i > 0)
262         b->num_read += (uint64_t)i;
263
264     if (cb != NULL)
265         i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
266     return (i);
267 }
268
269 int BIO_write(BIO *b, const void *in, int inl)
270 {
271     int i;
272     long (*cb) (BIO *, int, const char *, int, long, long);
273
274     if (b == NULL)
275         return (0);
276
277     cb = b->callback;
278     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
279         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
280         return (-2);
281     }
282
283     if ((cb != NULL) &&
284         ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
285         return (i);
286
287     if (!b->init) {
288         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
289         return (-2);
290     }
291
292     i = b->method->bwrite(b, in, inl);
293
294     if (i > 0)
295         b->num_write += (uint64_t)i;
296
297     if (cb != NULL)
298         i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
299     return (i);
300 }
301
302 int BIO_puts(BIO *b, const char *in)
303 {
304     int i;
305     long (*cb) (BIO *, int, const char *, int, long, long);
306
307     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
308         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
309         return (-2);
310     }
311
312     cb = b->callback;
313
314     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
315         return (i);
316
317     if (!b->init) {
318         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
319         return (-2);
320     }
321
322     i = b->method->bputs(b, in);
323
324     if (i > 0)
325         b->num_write += (uint64_t)i;
326
327     if (cb != NULL)
328         i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
329     return (i);
330 }
331
332 int BIO_gets(BIO *b, char *in, int inl)
333 {
334     int i;
335     long (*cb) (BIO *, int, const char *, int, long, long);
336
337     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
338         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
339         return (-2);
340     }
341
342     cb = b->callback;
343
344     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
345         return (i);
346
347     if (!b->init) {
348         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
349         return (-2);
350     }
351
352     i = b->method->bgets(b, in, inl);
353
354     if (cb != NULL)
355         i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
356     return (i);
357 }
358
359 int BIO_indent(BIO *b, int indent, int max)
360 {
361     if (indent < 0)
362         indent = 0;
363     if (indent > max)
364         indent = max;
365     while (indent--)
366         if (BIO_puts(b, " ") != 1)
367             return 0;
368     return 1;
369 }
370
371 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
372 {
373     int i;
374
375     i = iarg;
376     return (BIO_ctrl(b, cmd, larg, (char *)&i));
377 }
378
379 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
380 {
381     void *p = NULL;
382
383     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
384         return (NULL);
385     else
386         return (p);
387 }
388
389 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
390 {
391     long ret;
392     long (*cb) (BIO *, int, const char *, int, long, long);
393
394     if (b == NULL)
395         return (0);
396
397     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
398         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
399         return (-2);
400     }
401
402     cb = b->callback;
403
404     if ((cb != NULL) &&
405         ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
406         return (ret);
407
408     ret = b->method->ctrl(b, cmd, larg, parg);
409
410     if (cb != NULL)
411         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
412     return (ret);
413 }
414
415 long BIO_callback_ctrl(BIO *b, int cmd,
416                        void (*fp) (struct bio_st *, int, const char *, int,
417                                    long, long))
418 {
419     long ret;
420     long (*cb) (BIO *, int, const char *, int, long, long);
421
422     if (b == NULL)
423         return (0);
424
425     if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
426         BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
427         return (-2);
428     }
429
430     cb = b->callback;
431
432     if ((cb != NULL) &&
433         ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
434         return (ret);
435
436     ret = b->method->callback_ctrl(b, cmd, fp);
437
438     if (cb != NULL)
439         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
440     return (ret);
441 }
442
443 /*
444  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
445  * do; but those macros have inappropriate return type, and for interfacing
446  * from other programming languages, C macros aren't much of a help anyway.
447  */
448 size_t BIO_ctrl_pending(BIO *bio)
449 {
450     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
451 }
452
453 size_t BIO_ctrl_wpending(BIO *bio)
454 {
455     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
456 }
457
458 /* put the 'bio' on the end of b's list of operators */
459 BIO *BIO_push(BIO *b, BIO *bio)
460 {
461     BIO *lb;
462
463     if (b == NULL)
464         return (bio);
465     lb = b;
466     while (lb->next_bio != NULL)
467         lb = lb->next_bio;
468     lb->next_bio = bio;
469     if (bio != NULL)
470         bio->prev_bio = lb;
471     /* called to do internal processing */
472     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
473     return (b);
474 }
475
476 /* Remove the first and return the rest */
477 BIO *BIO_pop(BIO *b)
478 {
479     BIO *ret;
480
481     if (b == NULL)
482         return (NULL);
483     ret = b->next_bio;
484
485     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
486
487     if (b->prev_bio != NULL)
488         b->prev_bio->next_bio = b->next_bio;
489     if (b->next_bio != NULL)
490         b->next_bio->prev_bio = b->prev_bio;
491
492     b->next_bio = NULL;
493     b->prev_bio = NULL;
494     return (ret);
495 }
496
497 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
498 {
499     BIO *b, *last;
500
501     b = last = bio;
502     for (;;) {
503         if (!BIO_should_retry(b))
504             break;
505         last = b;
506         b = b->next_bio;
507         if (b == NULL)
508             break;
509     }
510     if (reason != NULL)
511         *reason = last->retry_reason;
512     return (last);
513 }
514
515 int BIO_get_retry_reason(BIO *bio)
516 {
517     return (bio->retry_reason);
518 }
519
520 void BIO_set_retry_reason(BIO *bio, int reason)
521 {
522     bio->retry_reason = reason;
523 }
524
525 BIO *BIO_find_type(BIO *bio, int type)
526 {
527     int mt, mask;
528
529     if (bio == NULL)
530         return NULL;
531     mask = type & 0xff;
532     do {
533         if (bio->method != NULL) {
534             mt = bio->method->type;
535
536             if (!mask) {
537                 if (mt & type)
538                     return (bio);
539             } else if (mt == type)
540                 return (bio);
541         }
542         bio = bio->next_bio;
543     } while (bio != NULL);
544     return (NULL);
545 }
546
547 BIO *BIO_next(BIO *b)
548 {
549     if (b == NULL)
550         return NULL;
551     return b->next_bio;
552 }
553
554 void BIO_set_next(BIO *b, BIO *next)
555 {
556     b->next_bio = next;
557 }
558
559 void BIO_free_all(BIO *bio)
560 {
561     BIO *b;
562     int ref;
563
564     while (bio != NULL) {
565         b = bio;
566         ref = b->references;
567         bio = bio->next_bio;
568         BIO_free(b);
569         /* Since ref count > 1, don't free anyone else. */
570         if (ref > 1)
571             break;
572     }
573 }
574
575 BIO *BIO_dup_chain(BIO *in)
576 {
577     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
578
579     for (bio = in; bio != NULL; bio = bio->next_bio) {
580         if ((new_bio = BIO_new(bio->method)) == NULL)
581             goto err;
582         new_bio->callback = bio->callback;
583         new_bio->cb_arg = bio->cb_arg;
584         new_bio->init = bio->init;
585         new_bio->shutdown = bio->shutdown;
586         new_bio->flags = bio->flags;
587
588         /* This will let SSL_s_sock() work with stdin/stdout */
589         new_bio->num = bio->num;
590
591         if (!BIO_dup_state(bio, (char *)new_bio)) {
592             BIO_free(new_bio);
593             goto err;
594         }
595
596         /* copy app data */
597         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
598                                 &bio->ex_data)) {
599             BIO_free(new_bio);
600             goto err;
601         }
602
603         if (ret == NULL) {
604             eoc = new_bio;
605             ret = eoc;
606         } else {
607             BIO_push(eoc, new_bio);
608             eoc = new_bio;
609         }
610     }
611     return (ret);
612  err:
613     BIO_free_all(ret);
614
615     return (NULL);
616 }
617
618 void BIO_copy_next_retry(BIO *b)
619 {
620     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
621     b->retry_reason = b->next_bio->retry_reason;
622 }
623
624 int BIO_set_ex_data(BIO *bio, int idx, void *data)
625 {
626     return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
627 }
628
629 void *BIO_get_ex_data(BIO *bio, int idx)
630 {
631     return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
632 }
633
634 uint64_t BIO_number_read(BIO *bio)
635 {
636     if (bio)
637         return bio->num_read;
638     return 0;
639 }
640
641 uint64_t BIO_number_written(BIO *bio)
642 {
643     if (bio)
644         return bio->num_write;
645     return 0;
646 }
647
648 void bio_free_ex_data(BIO *bio)
649 {
650     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
651 }
652
653 void bio_cleanup(void)
654 {
655 #ifndef OPENSSL_NO_SOCK
656     bio_sock_cleanup_int();
657     CRYPTO_THREAD_lock_free(bio_lookup_lock);
658     bio_lookup_lock = NULL;
659 #endif
660 }