2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include <openssl/core_dispatch.h>
12 #include "internal/cryptlib.h"
15 static OSSL_FUNC_BIO_new_file_fn
*c_bio_new_file
= NULL
;
16 static OSSL_FUNC_BIO_new_membuf_fn
*c_bio_new_membuf
= NULL
;
17 static OSSL_FUNC_BIO_read_ex_fn
*c_bio_read_ex
= NULL
;
18 static OSSL_FUNC_BIO_write_ex_fn
*c_bio_write_ex
= NULL
;
19 static OSSL_FUNC_BIO_free_fn
*c_bio_free
= NULL
;
20 static OSSL_FUNC_BIO_vprintf_fn
*c_bio_vprintf
= NULL
;
22 int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH
*fns
)
24 for (; fns
->function_id
!= 0; fns
++) {
25 switch (fns
->function_id
) {
26 case OSSL_FUNC_BIO_NEW_FILE
:
27 if (c_bio_new_file
== NULL
)
28 c_bio_new_file
= OSSL_FUNC_BIO_new_file(fns
);
30 case OSSL_FUNC_BIO_NEW_MEMBUF
:
31 if (c_bio_new_membuf
== NULL
)
32 c_bio_new_membuf
= OSSL_FUNC_BIO_new_membuf(fns
);
34 case OSSL_FUNC_BIO_READ_EX
:
35 if (c_bio_read_ex
== NULL
)
36 c_bio_read_ex
= OSSL_FUNC_BIO_read_ex(fns
);
38 case OSSL_FUNC_BIO_WRITE_EX
:
39 if (c_bio_write_ex
== NULL
)
40 c_bio_write_ex
= OSSL_FUNC_BIO_write_ex(fns
);
42 case OSSL_FUNC_BIO_FREE
:
43 if (c_bio_free
== NULL
)
44 c_bio_free
= OSSL_FUNC_BIO_free(fns
);
46 case OSSL_FUNC_BIO_VPRINTF
:
47 if (c_bio_vprintf
== NULL
)
48 c_bio_vprintf
= OSSL_FUNC_BIO_vprintf(fns
);
56 OSSL_CORE_BIO
*ossl_prov_bio_new_file(const char *filename
, const char *mode
)
58 if (c_bio_new_file
== NULL
)
60 return c_bio_new_file(filename
, mode
);
63 OSSL_CORE_BIO
*ossl_prov_bio_new_membuf(const char *filename
, int len
)
65 if (c_bio_new_membuf
== NULL
)
67 return c_bio_new_membuf(filename
, len
);
70 int ossl_prov_bio_read_ex(OSSL_CORE_BIO
*bio
, void *data
, size_t data_len
,
73 if (c_bio_read_ex
== NULL
)
75 return c_bio_read_ex(bio
, data
, data_len
, bytes_read
);
78 int ossl_prov_bio_write_ex(OSSL_CORE_BIO
*bio
, const void *data
, size_t data_len
,
81 if (c_bio_write_ex
== NULL
)
83 return c_bio_write_ex(bio
, data
, data_len
, written
);
86 int ossl_prov_bio_free(OSSL_CORE_BIO
*bio
)
88 if (c_bio_free
== NULL
)
90 return c_bio_free(bio
);
93 int ossl_prov_bio_vprintf(OSSL_CORE_BIO
*bio
, const char *format
, va_list ap
)
95 if (c_bio_vprintf
== NULL
)
97 return c_bio_vprintf(bio
, format
, ap
);
100 int ossl_prov_bio_printf(OSSL_CORE_BIO
*bio
, const char *format
, ...)
105 va_start(ap
, format
);
106 ret
= ossl_prov_bio_vprintf(bio
, format
, ap
);
114 /* No direct BIO support in the FIPS module */
116 static int bio_core_read_ex(BIO
*bio
, char *data
, size_t data_len
,
119 return ossl_prov_bio_read_ex(BIO_get_data(bio
), data
, data_len
, bytes_read
);
122 static int bio_core_write_ex(BIO
*bio
, const char *data
, size_t data_len
,
125 return ossl_prov_bio_write_ex(BIO_get_data(bio
), data
, data_len
, written
);
128 static long bio_core_ctrl(BIO
*bio
, int cmd
, long num
, void *ptr
)
130 /* We don't support this */
135 static int bio_core_gets(BIO
*bio
, char *buf
, int size
)
137 /* We don't support this */
142 static int bio_core_puts(BIO
*bio
, const char *str
)
144 /* We don't support this */
149 static int bio_core_new(BIO
*bio
)
151 BIO_set_init(bio
, 1);
156 static int bio_core_free(BIO
*bio
)
158 BIO_set_init(bio
, 0);
163 BIO_METHOD
*bio_prov_init_bio_method(void)
165 BIO_METHOD
*corebiometh
= NULL
;
167 corebiometh
= BIO_meth_new(BIO_TYPE_CORE_TO_PROV
, "BIO to Core filter");
168 if (corebiometh
== NULL
169 || !BIO_meth_set_write_ex(corebiometh
, bio_core_write_ex
)
170 || !BIO_meth_set_read_ex(corebiometh
, bio_core_read_ex
)
171 || !BIO_meth_set_puts(corebiometh
, bio_core_puts
)
172 || !BIO_meth_set_gets(corebiometh
, bio_core_gets
)
173 || !BIO_meth_set_ctrl(corebiometh
, bio_core_ctrl
)
174 || !BIO_meth_set_create(corebiometh
, bio_core_new
)
175 || !BIO_meth_set_destroy(corebiometh
, bio_core_free
)) {
176 BIO_meth_free(corebiometh
);
183 BIO
*bio_new_from_core_bio(PROV_CTX
*provctx
, OSSL_CORE_BIO
*corebio
)
186 BIO_METHOD
*corebiometh
= PROV_CTX_get0_core_bio_method(provctx
);
188 if (corebiometh
== NULL
)
191 outbio
= BIO_new(corebiometh
);
193 BIO_set_data(outbio
, corebio
);