94888394c198e66b808a6f8541ee934d83b4571c
[oweals/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 long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
146                                         int, long, long) {
147     return b->callback;
148 }
149
150 void BIO_set_callback(BIO *b,
151                       long (*cb) (struct bio_st *, int, const char *, int,
152                                   long, long))
153 {
154     b->callback = cb;
155 }
156
157 void BIO_set_callback_arg(BIO *b, char *arg)
158 {
159     b->cb_arg = arg;
160 }
161
162 char *BIO_get_callback_arg(const BIO *b)
163 {
164     return b->cb_arg;
165 }
166
167 const char *BIO_method_name(const BIO *b)
168 {
169     return b->method->name;
170 }
171
172 int BIO_method_type(const BIO *b)
173 {
174     return b->method->type;
175 }
176
177 int BIO_read(BIO *b, void *out, int outl)
178 {
179     int i;
180     long (*cb) (BIO *, int, const char *, int, long, long);
181
182     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
183         BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
184         return (-2);
185     }
186
187     cb = b->callback;
188     if ((cb != NULL) &&
189         ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
190         return (i);
191
192     if (!b->init) {
193         BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
194         return (-2);
195     }
196
197     i = b->method->bread(b, out, outl);
198
199     if (i > 0)
200         b->num_read += (uint64_t)i;
201
202     if (cb != NULL)
203         i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
204     return (i);
205 }
206
207 int BIO_write(BIO *b, const void *in, int inl)
208 {
209     int i;
210     long (*cb) (BIO *, int, const char *, int, long, long);
211
212     if (b == NULL)
213         return (0);
214
215     cb = b->callback;
216     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
217         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
218         return (-2);
219     }
220
221     if ((cb != NULL) &&
222         ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
223         return (i);
224
225     if (!b->init) {
226         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
227         return (-2);
228     }
229
230     i = b->method->bwrite(b, in, inl);
231
232     if (i > 0)
233         b->num_write += (uint64_t)i;
234
235     if (cb != NULL)
236         i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
237     return (i);
238 }
239
240 int BIO_puts(BIO *b, const char *in)
241 {
242     int i;
243     long (*cb) (BIO *, int, const char *, int, long, long);
244
245     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
246         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
247         return (-2);
248     }
249
250     cb = b->callback;
251
252     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
253         return (i);
254
255     if (!b->init) {
256         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
257         return (-2);
258     }
259
260     i = b->method->bputs(b, in);
261
262     if (i > 0)
263         b->num_write += (uint64_t)i;
264
265     if (cb != NULL)
266         i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
267     return (i);
268 }
269
270 int BIO_gets(BIO *b, char *in, int inl)
271 {
272     int i;
273     long (*cb) (BIO *, int, const char *, int, long, long);
274
275     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
276         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
277         return (-2);
278     }
279
280     cb = b->callback;
281
282     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
283         return (i);
284
285     if (!b->init) {
286         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
287         return (-2);
288     }
289
290     i = b->method->bgets(b, in, inl);
291
292     if (cb != NULL)
293         i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
294     return (i);
295 }
296
297 int BIO_indent(BIO *b, int indent, int max)
298 {
299     if (indent < 0)
300         indent = 0;
301     if (indent > max)
302         indent = max;
303     while (indent--)
304         if (BIO_puts(b, " ") != 1)
305             return 0;
306     return 1;
307 }
308
309 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
310 {
311     int i;
312
313     i = iarg;
314     return (BIO_ctrl(b, cmd, larg, (char *)&i));
315 }
316
317 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
318 {
319     void *p = NULL;
320
321     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
322         return (NULL);
323     else
324         return (p);
325 }
326
327 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
328 {
329     long ret;
330     long (*cb) (BIO *, int, const char *, int, long, long);
331
332     if (b == NULL)
333         return (0);
334
335     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
336         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
337         return (-2);
338     }
339
340     cb = b->callback;
341
342     if ((cb != NULL) &&
343         ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
344         return (ret);
345
346     ret = b->method->ctrl(b, cmd, larg, parg);
347
348     if (cb != NULL)
349         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
350     return (ret);
351 }
352
353 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
354 {
355     long ret;
356     long (*cb) (BIO *, int, const char *, int, long, long);
357
358     if (b == NULL)
359         return (0);
360
361     if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
362         BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
363         return (-2);
364     }
365
366     cb = b->callback;
367
368     if ((cb != NULL) &&
369         ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
370         return (ret);
371
372     ret = b->method->callback_ctrl(b, cmd, fp);
373
374     if (cb != NULL)
375         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
376     return (ret);
377 }
378
379 /*
380  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
381  * do; but those macros have inappropriate return type, and for interfacing
382  * from other programming languages, C macros aren't much of a help anyway.
383  */
384 size_t BIO_ctrl_pending(BIO *bio)
385 {
386     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
387 }
388
389 size_t BIO_ctrl_wpending(BIO *bio)
390 {
391     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
392 }
393
394 /* put the 'bio' on the end of b's list of operators */
395 BIO *BIO_push(BIO *b, BIO *bio)
396 {
397     BIO *lb;
398
399     if (b == NULL)
400         return (bio);
401     lb = b;
402     while (lb->next_bio != NULL)
403         lb = lb->next_bio;
404     lb->next_bio = bio;
405     if (bio != NULL)
406         bio->prev_bio = lb;
407     /* called to do internal processing */
408     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
409     return (b);
410 }
411
412 /* Remove the first and return the rest */
413 BIO *BIO_pop(BIO *b)
414 {
415     BIO *ret;
416
417     if (b == NULL)
418         return (NULL);
419     ret = b->next_bio;
420
421     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
422
423     if (b->prev_bio != NULL)
424         b->prev_bio->next_bio = b->next_bio;
425     if (b->next_bio != NULL)
426         b->next_bio->prev_bio = b->prev_bio;
427
428     b->next_bio = NULL;
429     b->prev_bio = NULL;
430     return (ret);
431 }
432
433 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
434 {
435     BIO *b, *last;
436
437     b = last = bio;
438     for (;;) {
439         if (!BIO_should_retry(b))
440             break;
441         last = b;
442         b = b->next_bio;
443         if (b == NULL)
444             break;
445     }
446     if (reason != NULL)
447         *reason = last->retry_reason;
448     return (last);
449 }
450
451 int BIO_get_retry_reason(BIO *bio)
452 {
453     return (bio->retry_reason);
454 }
455
456 void BIO_set_retry_reason(BIO *bio, int reason)
457 {
458     bio->retry_reason = reason;
459 }
460
461 BIO *BIO_find_type(BIO *bio, int type)
462 {
463     int mt, mask;
464
465     if (bio == NULL)
466         return NULL;
467     mask = type & 0xff;
468     do {
469         if (bio->method != NULL) {
470             mt = bio->method->type;
471
472             if (!mask) {
473                 if (mt & type)
474                     return (bio);
475             } else if (mt == type)
476                 return (bio);
477         }
478         bio = bio->next_bio;
479     } while (bio != NULL);
480     return (NULL);
481 }
482
483 BIO *BIO_next(BIO *b)
484 {
485     if (b == NULL)
486         return NULL;
487     return b->next_bio;
488 }
489
490 void BIO_set_next(BIO *b, BIO *next)
491 {
492     b->next_bio = next;
493 }
494
495 void BIO_free_all(BIO *bio)
496 {
497     BIO *b;
498     int ref;
499
500     while (bio != NULL) {
501         b = bio;
502         ref = b->references;
503         bio = bio->next_bio;
504         BIO_free(b);
505         /* Since ref count > 1, don't free anyone else. */
506         if (ref > 1)
507             break;
508     }
509 }
510
511 BIO *BIO_dup_chain(BIO *in)
512 {
513     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
514
515     for (bio = in; bio != NULL; bio = bio->next_bio) {
516         if ((new_bio = BIO_new(bio->method)) == NULL)
517             goto err;
518         new_bio->callback = bio->callback;
519         new_bio->cb_arg = bio->cb_arg;
520         new_bio->init = bio->init;
521         new_bio->shutdown = bio->shutdown;
522         new_bio->flags = bio->flags;
523
524         /* This will let SSL_s_sock() work with stdin/stdout */
525         new_bio->num = bio->num;
526
527         if (!BIO_dup_state(bio, (char *)new_bio)) {
528             BIO_free(new_bio);
529             goto err;
530         }
531
532         /* copy app data */
533         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
534                                 &bio->ex_data)) {
535             BIO_free(new_bio);
536             goto err;
537         }
538
539         if (ret == NULL) {
540             eoc = new_bio;
541             ret = eoc;
542         } else {
543             BIO_push(eoc, new_bio);
544             eoc = new_bio;
545         }
546     }
547     return (ret);
548  err:
549     BIO_free_all(ret);
550
551     return (NULL);
552 }
553
554 void BIO_copy_next_retry(BIO *b)
555 {
556     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
557     b->retry_reason = b->next_bio->retry_reason;
558 }
559
560 int BIO_set_ex_data(BIO *bio, int idx, void *data)
561 {
562     return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
563 }
564
565 void *BIO_get_ex_data(BIO *bio, int idx)
566 {
567     return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
568 }
569
570 uint64_t BIO_number_read(BIO *bio)
571 {
572     if (bio)
573         return bio->num_read;
574     return 0;
575 }
576
577 uint64_t BIO_number_written(BIO *bio)
578 {
579     if (bio)
580         return bio->num_write;
581     return 0;
582 }
583
584 void bio_free_ex_data(BIO *bio)
585 {
586     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
587 }
588
589 void bio_cleanup(void)
590 {
591 #ifndef OPENSSL_NO_SOCK
592     bio_sock_cleanup_int();
593     CRYPTO_THREAD_lock_free(bio_lookup_lock);
594     bio_lookup_lock = NULL;
595 #endif
596     CRYPTO_THREAD_lock_free(bio_type_lock);
597     bio_type_lock = NULL;
598 }