]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bio/bio_lib.c
Add support for reference counting using C11 atomics
[thirdparty/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 >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
65 *processed = (size_t)ret;
66 ret = 1;
67 }
68
69 return ret;
70 }
71
72 BIO *BIO_new(const BIO_METHOD *method)
73 {
74 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
75
76 if (bio == NULL) {
77 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
78 return (NULL);
79 }
80
81 bio->method = method;
82 bio->shutdown = 1;
83 bio->references = 1;
84
85 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
86 goto err;
87
88 bio->lock = CRYPTO_THREAD_lock_new();
89 if (bio->lock == NULL) {
90 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
91 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
92 goto err;
93 }
94
95 if (method->create != NULL && !method->create(bio)) {
96 BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
97 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
98 CRYPTO_THREAD_lock_free(bio->lock);
99 goto err;
100 }
101
102 return bio;
103
104 err:
105 OPENSSL_free(bio);
106 return NULL;
107 }
108
109 int BIO_free(BIO *a)
110 {
111 int ret;
112
113 if (a == NULL)
114 return 0;
115
116 if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
117 return 0;
118
119 REF_PRINT_COUNT("BIO", a);
120 if (ret > 0)
121 return 1;
122 REF_ASSERT_ISNT(ret < 0);
123
124 if (a->callback != NULL || a->callback_ex != NULL) {
125 ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
126 if (ret <= 0)
127 return ret;
128 }
129
130 if ((a->method != NULL) && (a->method->destroy != NULL))
131 a->method->destroy(a);
132
133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
134
135 CRYPTO_THREAD_lock_free(a->lock);
136
137 OPENSSL_free(a);
138
139 return 1;
140 }
141
142 void BIO_set_data(BIO *a, void *ptr)
143 {
144 a->ptr = ptr;
145 }
146
147 void *BIO_get_data(BIO *a)
148 {
149 return a->ptr;
150 }
151
152 void BIO_set_init(BIO *a, int init)
153 {
154 a->init = init;
155 }
156
157 int BIO_get_init(BIO *a)
158 {
159 return a->init;
160 }
161
162 void BIO_set_shutdown(BIO *a, int shut)
163 {
164 a->shutdown = shut;
165 }
166
167 int BIO_get_shutdown(BIO *a)
168 {
169 return a->shutdown;
170 }
171
172 void BIO_vfree(BIO *a)
173 {
174 BIO_free(a);
175 }
176
177 int BIO_up_ref(BIO *a)
178 {
179 int i;
180
181 if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
182 return 0;
183
184 REF_PRINT_COUNT("BIO", a);
185 REF_ASSERT_ISNT(i < 2);
186 return ((i > 1) ? 1 : 0);
187 }
188
189 void BIO_clear_flags(BIO *b, int flags)
190 {
191 b->flags &= ~flags;
192 }
193
194 int BIO_test_flags(const BIO *b, int flags)
195 {
196 return (b->flags & flags);
197 }
198
199 void BIO_set_flags(BIO *b, int flags)
200 {
201 b->flags |= flags;
202 }
203
204 BIO_callback_fn BIO_get_callback(const BIO *b)
205 {
206 return b->callback;
207 }
208
209 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
210 {
211 b->callback = cb;
212 }
213
214 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
215 {
216 return b->callback_ex;
217 }
218
219 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
220 {
221 b->callback_ex = cb;
222 }
223
224 void BIO_set_callback_arg(BIO *b, char *arg)
225 {
226 b->cb_arg = arg;
227 }
228
229 char *BIO_get_callback_arg(const BIO *b)
230 {
231 return b->cb_arg;
232 }
233
234 const char *BIO_method_name(const BIO *b)
235 {
236 return b->method->name;
237 }
238
239 int BIO_method_type(const BIO *b)
240 {
241 return b->method->type;
242 }
243
244 /*
245 * This is essentially the same as BIO_read_ex() except that it allows
246 * 0 or a negative value to indicate failure (retryable or not) in the return.
247 * This is for compatibility with the old style BIO_read(), where existing code
248 * may make assumptions about the return value that it might get.
249 */
250 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
251 {
252 int ret;
253
254 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
255 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
256 return -2;
257 }
258
259 if ((b->callback != NULL || b->callback_ex != NULL) &&
260 ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
261 readbytes)) <= 0))
262 return ret;
263
264 if (!b->init) {
265 BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
266 return -2;
267 }
268
269 ret = b->method->bread(b, data, dlen, readbytes);
270
271 if (ret > 0)
272 b->num_read += (uint64_t)*read;
273
274 if (b->callback != NULL || b->callback_ex != NULL)
275 ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
276 dlen, 0, 0L, ret, readbytes);
277
278 /* Shouldn't happen */
279 if (ret > 0 && *readbytes > dlen) {
280 BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
281 return -1;
282 }
283
284 return ret;
285 }
286
287 int BIO_read(BIO *b, void *data, int dlen)
288 {
289 size_t readbytes;
290 int ret;
291
292 if (dlen < 0)
293 return 0;
294
295 ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
296
297 if (ret > 0) {
298 /* *readbytes should always be <= dlen */
299 ret = (int)readbytes;
300 }
301
302 return ret;
303 }
304
305 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
306 {
307 int ret;
308
309 ret = bio_read_intern(b, data, dlen, readbytes);
310
311 if (ret > 0)
312 ret = 1;
313 else
314 ret = 0;
315
316 return ret;
317 }
318
319 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
320 size_t *written)
321 {
322 int ret;
323
324 if (b == NULL)
325 return 0;
326
327 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
328 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
329 return -2;
330 }
331
332 if ((b->callback != NULL || b->callback_ex != NULL) &&
333 ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
334 written)) <= 0))
335 return ret;
336
337 if (!b->init) {
338 BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
339 return -2;
340 }
341
342 ret = b->method->bwrite(b, data, dlen, written);
343
344 if (ret > 0)
345 b->num_write += (uint64_t)*written;
346
347 if (b->callback != NULL || b->callback_ex != NULL)
348 ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
349 dlen, 0, 0L, ret, written);
350
351 return ret;
352 }
353
354 int BIO_write(BIO *b, const void *data, int dlen)
355 {
356 size_t written;
357 int ret;
358
359 if (dlen < 0)
360 return 0;
361
362 ret = bio_write_intern(b, data, (size_t)dlen, &written);
363
364 if (ret > 0) {
365 /* *written should always be <= dlen */
366 ret = (int)written;
367 }
368
369 return ret;
370 }
371
372 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
373 {
374 int ret;
375
376 ret = bio_write_intern(b, data, dlen, written);
377
378 if (ret > 0)
379 ret = 1;
380 else
381 ret = 0;
382
383 return ret;
384 }
385
386 int BIO_puts(BIO *b, const char *buf)
387 {
388 int ret;
389 size_t written = 0;
390
391 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
392 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
393 return -2;
394 }
395
396 if (b->callback != NULL || b->callback_ex != NULL) {
397 ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
398 if (ret <= 0)
399 return ret;
400 }
401
402 if (!b->init) {
403 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
404 return -2;
405 }
406
407 ret = b->method->bputs(b, buf);
408
409 if (ret > 0) {
410 b->num_write += (uint64_t)ret;
411 written = ret;
412 ret = 1;
413 }
414
415 if (b->callback != NULL || b->callback_ex != NULL)
416 ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
417 0L, ret, &written);
418
419 if (ret > 0) {
420 if (written > INT_MAX) {
421 BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
422 ret = -1;
423 } else {
424 ret = (int)written;
425 }
426 }
427
428 return ret;
429 }
430
431 int BIO_gets(BIO *b, char *buf, int size)
432 {
433 int ret;
434 size_t readbytes = 0;
435
436 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
437 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
438 return (-2);
439 }
440
441 if (size < 0) {
442 BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
443 return 0;
444 }
445
446 if (b->callback != NULL || b->callback_ex != NULL) {
447 ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
448 if (ret <= 0)
449 return ret;
450 }
451
452 if (!b->init) {
453 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
454 return (-2);
455 }
456
457 ret = b->method->bgets(b, buf, size);
458
459 if (ret > 0) {
460 readbytes = ret;
461 ret = 1;
462 }
463
464 if (b->callback != NULL || b->callback_ex != NULL)
465 ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
466 0, 0L, ret, &readbytes);
467
468 if (ret > 0) {
469 /* Shouldn't happen */
470 if (readbytes > (size_t)size)
471 ret = -1;
472 else
473 ret = (int)readbytes;
474 }
475
476 return ret;
477 }
478
479 int BIO_indent(BIO *b, int indent, int max)
480 {
481 if (indent < 0)
482 indent = 0;
483 if (indent > max)
484 indent = max;
485 while (indent--)
486 if (BIO_puts(b, " ") != 1)
487 return 0;
488 return 1;
489 }
490
491 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
492 {
493 int i;
494
495 i = iarg;
496 return (BIO_ctrl(b, cmd, larg, (char *)&i));
497 }
498
499 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
500 {
501 void *p = NULL;
502
503 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
504 return (NULL);
505 else
506 return (p);
507 }
508
509 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
510 {
511 long ret;
512
513 if (b == NULL)
514 return 0;
515
516 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
517 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
518 return -2;
519 }
520
521 if (b->callback != NULL || b->callback_ex != NULL) {
522 ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
523 if (ret <= 0)
524 return ret;
525 }
526
527 ret = b->method->ctrl(b, cmd, larg, parg);
528
529 if (b->callback != NULL || b->callback_ex != NULL)
530 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
531 larg, ret, NULL);
532
533 return ret;
534 }
535
536 long BIO_callback_ctrl(BIO *b, int cmd,
537 void (*fp) (struct bio_st *, int, const char *, int,
538 long, long))
539 {
540 long ret;
541
542 if (b == NULL)
543 return (0);
544
545 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
546 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
547 return (-2);
548 }
549
550 if (b->callback != NULL || b->callback_ex != NULL) {
551 ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
552 NULL);
553 if (ret <= 0)
554 return ret;
555 }
556
557 ret = b->method->callback_ctrl(b, cmd, fp);
558
559 if (b->callback != NULL || b->callback_ex != NULL)
560 ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
561 cmd, 0, ret, NULL);
562
563 return ret;
564 }
565
566 /*
567 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
568 * do; but those macros have inappropriate return type, and for interfacing
569 * from other programming languages, C macros aren't much of a help anyway.
570 */
571 size_t BIO_ctrl_pending(BIO *bio)
572 {
573 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
574 }
575
576 size_t BIO_ctrl_wpending(BIO *bio)
577 {
578 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
579 }
580
581 /* put the 'bio' on the end of b's list of operators */
582 BIO *BIO_push(BIO *b, BIO *bio)
583 {
584 BIO *lb;
585
586 if (b == NULL)
587 return (bio);
588 lb = b;
589 while (lb->next_bio != NULL)
590 lb = lb->next_bio;
591 lb->next_bio = bio;
592 if (bio != NULL)
593 bio->prev_bio = lb;
594 /* called to do internal processing */
595 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
596 return (b);
597 }
598
599 /* Remove the first and return the rest */
600 BIO *BIO_pop(BIO *b)
601 {
602 BIO *ret;
603
604 if (b == NULL)
605 return (NULL);
606 ret = b->next_bio;
607
608 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
609
610 if (b->prev_bio != NULL)
611 b->prev_bio->next_bio = b->next_bio;
612 if (b->next_bio != NULL)
613 b->next_bio->prev_bio = b->prev_bio;
614
615 b->next_bio = NULL;
616 b->prev_bio = NULL;
617 return (ret);
618 }
619
620 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
621 {
622 BIO *b, *last;
623
624 b = last = bio;
625 for (;;) {
626 if (!BIO_should_retry(b))
627 break;
628 last = b;
629 b = b->next_bio;
630 if (b == NULL)
631 break;
632 }
633 if (reason != NULL)
634 *reason = last->retry_reason;
635 return (last);
636 }
637
638 int BIO_get_retry_reason(BIO *bio)
639 {
640 return (bio->retry_reason);
641 }
642
643 void BIO_set_retry_reason(BIO *bio, int reason)
644 {
645 bio->retry_reason = reason;
646 }
647
648 BIO *BIO_find_type(BIO *bio, int type)
649 {
650 int mt, mask;
651
652 if (bio == NULL)
653 return NULL;
654 mask = type & 0xff;
655 do {
656 if (bio->method != NULL) {
657 mt = bio->method->type;
658
659 if (!mask) {
660 if (mt & type)
661 return (bio);
662 } else if (mt == type)
663 return (bio);
664 }
665 bio = bio->next_bio;
666 } while (bio != NULL);
667 return (NULL);
668 }
669
670 BIO *BIO_next(BIO *b)
671 {
672 if (b == NULL)
673 return NULL;
674 return b->next_bio;
675 }
676
677 void BIO_set_next(BIO *b, BIO *next)
678 {
679 b->next_bio = next;
680 }
681
682 void BIO_free_all(BIO *bio)
683 {
684 BIO *b;
685 int ref;
686
687 while (bio != NULL) {
688 b = bio;
689 ref = b->references;
690 bio = bio->next_bio;
691 BIO_free(b);
692 /* Since ref count > 1, don't free anyone else. */
693 if (ref > 1)
694 break;
695 }
696 }
697
698 BIO *BIO_dup_chain(BIO *in)
699 {
700 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
701
702 for (bio = in; bio != NULL; bio = bio->next_bio) {
703 if ((new_bio = BIO_new(bio->method)) == NULL)
704 goto err;
705 new_bio->callback = bio->callback;
706 new_bio->callback_ex = bio->callback_ex;
707 new_bio->cb_arg = bio->cb_arg;
708 new_bio->init = bio->init;
709 new_bio->shutdown = bio->shutdown;
710 new_bio->flags = bio->flags;
711
712 /* This will let SSL_s_sock() work with stdin/stdout */
713 new_bio->num = bio->num;
714
715 if (!BIO_dup_state(bio, (char *)new_bio)) {
716 BIO_free(new_bio);
717 goto err;
718 }
719
720 /* copy app data */
721 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
722 &bio->ex_data)) {
723 BIO_free(new_bio);
724 goto err;
725 }
726
727 if (ret == NULL) {
728 eoc = new_bio;
729 ret = eoc;
730 } else {
731 BIO_push(eoc, new_bio);
732 eoc = new_bio;
733 }
734 }
735 return (ret);
736 err:
737 BIO_free_all(ret);
738
739 return (NULL);
740 }
741
742 void BIO_copy_next_retry(BIO *b)
743 {
744 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
745 b->retry_reason = b->next_bio->retry_reason;
746 }
747
748 int BIO_set_ex_data(BIO *bio, int idx, void *data)
749 {
750 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
751 }
752
753 void *BIO_get_ex_data(BIO *bio, int idx)
754 {
755 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
756 }
757
758 uint64_t BIO_number_read(BIO *bio)
759 {
760 if (bio)
761 return bio->num_read;
762 return 0;
763 }
764
765 uint64_t BIO_number_written(BIO *bio)
766 {
767 if (bio)
768 return bio->num_write;
769 return 0;
770 }
771
772 void bio_free_ex_data(BIO *bio)
773 {
774 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
775 }
776
777 void bio_cleanup(void)
778 {
779 #ifndef OPENSSL_NO_SOCK
780 bio_sock_cleanup_int();
781 CRYPTO_THREAD_lock_free(bio_lookup_lock);
782 bio_lookup_lock = NULL;
783 #endif
784 CRYPTO_THREAD_lock_free(bio_type_lock);
785 bio_type_lock = NULL;
786 }