]>
git.proxmox.com Git - efi-boot-shim.git/blob - Cryptlib/OpenSSL/crypto/bio/bio_lib.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/crypto.h>
14 #include "internal/cryptlib.h"
16 BIO
*BIO_new(const BIO_METHOD
*method
)
18 BIO
*bio
= OPENSSL_zalloc(sizeof(*bio
));
21 BIOerr(BIO_F_BIO_NEW
, ERR_R_MALLOC_FAILURE
);
29 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
))
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
);
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
);
60 if (CRYPTO_atomic_add(&a
->references
, -1, &i
, a
->lock
) <= 0)
63 REF_PRINT_COUNT("BIO", a
);
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))
71 if ((a
->method
!= NULL
) && (a
->method
->destroy
!= NULL
))
72 a
->method
->destroy(a
);
74 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, a
, &a
->ex_data
);
76 CRYPTO_THREAD_lock_free(a
->lock
);
83 void BIO_set_data(BIO
*a
, void *ptr
)
88 void *BIO_get_data(BIO
*a
)
93 void BIO_set_init(BIO
*a
, int init
)
98 int BIO_get_init(BIO
*a
)
103 void BIO_set_shutdown(BIO
*a
, int shut
)
108 int BIO_get_shutdown(BIO
*a
)
113 void BIO_vfree(BIO
*a
)
118 int BIO_up_ref(BIO
*a
)
122 if (CRYPTO_atomic_add(&a
->references
, 1, &i
, a
->lock
) <= 0)
125 REF_PRINT_COUNT("BIO", a
);
126 REF_ASSERT_ISNT(i
< 2);
127 return ((i
> 1) ? 1 : 0);
130 void BIO_clear_flags(BIO
*b
, int flags
)
135 int BIO_test_flags(const BIO
*b
, int flags
)
137 return (b
->flags
& flags
);
140 void BIO_set_flags(BIO
*b
, int flags
)
145 long (*BIO_get_callback(const BIO
*b
)) (struct bio_st
*, int, const char *,
150 void BIO_set_callback(BIO
*b
,
151 long (*cb
) (struct bio_st
*, int, const char *, int,
157 void BIO_set_callback_arg(BIO
*b
, char *arg
)
162 char *BIO_get_callback_arg(const BIO
*b
)
167 const char *BIO_method_name(const BIO
*b
)
169 return b
->method
->name
;
172 int BIO_method_type(const BIO
*b
)
174 return b
->method
->type
;
177 int BIO_read(BIO
*b
, void *out
, int outl
)
180 long (*cb
) (BIO
*, int, const char *, int, long, long);
182 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bread
== NULL
)) {
183 BIOerr(BIO_F_BIO_READ
, BIO_R_UNSUPPORTED_METHOD
);
189 ((i
= (int)cb(b
, BIO_CB_READ
, out
, outl
, 0L, 1L)) <= 0))
193 BIOerr(BIO_F_BIO_READ
, BIO_R_UNINITIALIZED
);
197 i
= b
->method
->bread(b
, out
, outl
);
200 b
->num_read
+= (uint64_t)i
;
203 i
= (int)cb(b
, BIO_CB_READ
| BIO_CB_RETURN
, out
, outl
, 0L, (long)i
);
207 int BIO_write(BIO
*b
, const void *in
, int inl
)
210 long (*cb
) (BIO
*, int, const char *, int, long, long);
216 if ((b
->method
== NULL
) || (b
->method
->bwrite
== NULL
)) {
217 BIOerr(BIO_F_BIO_WRITE
, BIO_R_UNSUPPORTED_METHOD
);
222 ((i
= (int)cb(b
, BIO_CB_WRITE
, in
, inl
, 0L, 1L)) <= 0))
226 BIOerr(BIO_F_BIO_WRITE
, BIO_R_UNINITIALIZED
);
230 i
= b
->method
->bwrite(b
, in
, inl
);
233 b
->num_write
+= (uint64_t)i
;
236 i
= (int)cb(b
, BIO_CB_WRITE
| BIO_CB_RETURN
, in
, inl
, 0L, (long)i
);
240 int BIO_puts(BIO
*b
, const char *in
)
243 long (*cb
) (BIO
*, int, const char *, int, long, long);
245 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bputs
== NULL
)) {
246 BIOerr(BIO_F_BIO_PUTS
, BIO_R_UNSUPPORTED_METHOD
);
252 if ((cb
!= NULL
) && ((i
= (int)cb(b
, BIO_CB_PUTS
, in
, 0, 0L, 1L)) <= 0))
256 BIOerr(BIO_F_BIO_PUTS
, BIO_R_UNINITIALIZED
);
260 i
= b
->method
->bputs(b
, in
);
263 b
->num_write
+= (uint64_t)i
;
266 i
= (int)cb(b
, BIO_CB_PUTS
| BIO_CB_RETURN
, in
, 0, 0L, (long)i
);
270 int BIO_gets(BIO
*b
, char *in
, int inl
)
273 long (*cb
) (BIO
*, int, const char *, int, long, long);
275 if ((b
== NULL
) || (b
->method
== NULL
) || (b
->method
->bgets
== NULL
)) {
276 BIOerr(BIO_F_BIO_GETS
, BIO_R_UNSUPPORTED_METHOD
);
282 if ((cb
!= NULL
) && ((i
= (int)cb(b
, BIO_CB_GETS
, in
, inl
, 0L, 1L)) <= 0))
286 BIOerr(BIO_F_BIO_GETS
, BIO_R_UNINITIALIZED
);
290 i
= b
->method
->bgets(b
, in
, inl
);
293 i
= (int)cb(b
, BIO_CB_GETS
| BIO_CB_RETURN
, in
, inl
, 0L, (long)i
);
297 int BIO_indent(BIO
*b
, int indent
, int max
)
304 if (BIO_puts(b
, " ") != 1)
309 long BIO_int_ctrl(BIO
*b
, int cmd
, long larg
, int iarg
)
314 return (BIO_ctrl(b
, cmd
, larg
, (char *)&i
));
317 void *BIO_ptr_ctrl(BIO
*b
, int cmd
, long larg
)
321 if (BIO_ctrl(b
, cmd
, larg
, (char *)&p
) <= 0)
327 long BIO_ctrl(BIO
*b
, int cmd
, long larg
, void *parg
)
330 long (*cb
) (BIO
*, int, const char *, int, long, long);
335 if ((b
->method
== NULL
) || (b
->method
->ctrl
== NULL
)) {
336 BIOerr(BIO_F_BIO_CTRL
, BIO_R_UNSUPPORTED_METHOD
);
343 ((ret
= cb(b
, BIO_CB_CTRL
, parg
, cmd
, larg
, 1L)) <= 0))
346 ret
= b
->method
->ctrl(b
, cmd
, larg
, parg
);
349 ret
= cb(b
, BIO_CB_CTRL
| BIO_CB_RETURN
, parg
, cmd
, larg
, ret
);
353 long BIO_callback_ctrl(BIO
*b
, int cmd
,
354 void (*fp
) (struct bio_st
*, int, const char *, int,
358 long (*cb
) (BIO
*, int, const char *, int, long, long);
363 if ((b
->method
== NULL
) || (b
->method
->callback_ctrl
== NULL
)) {
364 BIOerr(BIO_F_BIO_CALLBACK_CTRL
, BIO_R_UNSUPPORTED_METHOD
);
371 ((ret
= cb(b
, BIO_CB_CTRL
, (void *)&fp
, cmd
, 0, 1L)) <= 0))
374 ret
= b
->method
->callback_ctrl(b
, cmd
, fp
);
377 ret
= cb(b
, BIO_CB_CTRL
| BIO_CB_RETURN
, (void *)&fp
, cmd
, 0, ret
);
382 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
383 * do; but those macros have inappropriate return type, and for interfacing
384 * from other programming languages, C macros aren't much of a help anyway.
386 size_t BIO_ctrl_pending(BIO
*bio
)
388 return BIO_ctrl(bio
, BIO_CTRL_PENDING
, 0, NULL
);
391 size_t BIO_ctrl_wpending(BIO
*bio
)
393 return BIO_ctrl(bio
, BIO_CTRL_WPENDING
, 0, NULL
);
396 /* put the 'bio' on the end of b's list of operators */
397 BIO
*BIO_push(BIO
*b
, BIO
*bio
)
404 while (lb
->next_bio
!= NULL
)
409 /* called to do internal processing */
410 BIO_ctrl(b
, BIO_CTRL_PUSH
, 0, lb
);
414 /* Remove the first and return the rest */
423 BIO_ctrl(b
, BIO_CTRL_POP
, 0, b
);
425 if (b
->prev_bio
!= NULL
)
426 b
->prev_bio
->next_bio
= b
->next_bio
;
427 if (b
->next_bio
!= NULL
)
428 b
->next_bio
->prev_bio
= b
->prev_bio
;
435 BIO
*BIO_get_retry_BIO(BIO
*bio
, int *reason
)
441 if (!BIO_should_retry(b
))
449 *reason
= last
->retry_reason
;
453 int BIO_get_retry_reason(BIO
*bio
)
455 return (bio
->retry_reason
);
458 void BIO_set_retry_reason(BIO
*bio
, int reason
)
460 bio
->retry_reason
= reason
;
463 BIO
*BIO_find_type(BIO
*bio
, int type
)
471 if (bio
->method
!= NULL
) {
472 mt
= bio
->method
->type
;
477 } else if (mt
== type
)
481 } while (bio
!= NULL
);
485 BIO
*BIO_next(BIO
*b
)
492 void BIO_set_next(BIO
*b
, BIO
*next
)
497 void BIO_free_all(BIO
*bio
)
502 while (bio
!= NULL
) {
507 /* Since ref count > 1, don't free anyone else. */
513 BIO
*BIO_dup_chain(BIO
*in
)
515 BIO
*ret
= NULL
, *eoc
= NULL
, *bio
, *new_bio
;
517 for (bio
= in
; bio
!= NULL
; bio
= bio
->next_bio
) {
518 if ((new_bio
= BIO_new(bio
->method
)) == NULL
)
520 new_bio
->callback
= bio
->callback
;
521 new_bio
->cb_arg
= bio
->cb_arg
;
522 new_bio
->init
= bio
->init
;
523 new_bio
->shutdown
= bio
->shutdown
;
524 new_bio
->flags
= bio
->flags
;
526 /* This will let SSL_s_sock() work with stdin/stdout */
527 new_bio
->num
= bio
->num
;
529 if (!BIO_dup_state(bio
, (char *)new_bio
)) {
535 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO
, &new_bio
->ex_data
,
545 BIO_push(eoc
, new_bio
);
556 void BIO_copy_next_retry(BIO
*b
)
558 BIO_set_flags(b
, BIO_get_retry_flags(b
->next_bio
));
559 b
->retry_reason
= b
->next_bio
->retry_reason
;
562 int BIO_set_ex_data(BIO
*bio
, int idx
, void *data
)
564 return (CRYPTO_set_ex_data(&(bio
->ex_data
), idx
, data
));
567 void *BIO_get_ex_data(BIO
*bio
, int idx
)
569 return (CRYPTO_get_ex_data(&(bio
->ex_data
), idx
));
572 uint64_t BIO_number_read(BIO
*bio
)
575 return bio
->num_read
;
579 uint64_t BIO_number_written(BIO
*bio
)
582 return bio
->num_write
;
586 void bio_free_ex_data(BIO
*bio
)
588 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO
, bio
, &bio
->ex_data
);
591 void bio_cleanup(void)
593 #ifndef OPENSSL_NO_SOCK
594 bio_sock_cleanup_int();
595 CRYPTO_THREAD_lock_free(bio_lookup_lock
);
596 bio_lookup_lock
= NULL
;
598 CRYPTO_THREAD_lock_free(bio_type_lock
);
599 bio_type_lock
= NULL
;