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