2 * Copyright 2019-2021 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_gets_fn *c_bio_gets = NULL;
20 static OSSL_FUNC_BIO_puts_fn *c_bio_puts = NULL;
21 static OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl = NULL;
22 static OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref = NULL;
23 static OSSL_FUNC_BIO_free_fn *c_bio_free = NULL;
24 static OSSL_FUNC_BIO_vprintf_fn *c_bio_vprintf = NULL;
26 int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
28 for (; fns->function_id != 0; fns++) {
29 switch (fns->function_id) {
30 case OSSL_FUNC_BIO_NEW_FILE:
31 if (c_bio_new_file == NULL)
32 c_bio_new_file = OSSL_FUNC_BIO_new_file(fns);
34 case OSSL_FUNC_BIO_NEW_MEMBUF:
35 if (c_bio_new_membuf == NULL)
36 c_bio_new_membuf = OSSL_FUNC_BIO_new_membuf(fns);
38 case OSSL_FUNC_BIO_READ_EX:
39 if (c_bio_read_ex == NULL)
40 c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
42 case OSSL_FUNC_BIO_WRITE_EX:
43 if (c_bio_write_ex == NULL)
44 c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
46 case OSSL_FUNC_BIO_GETS:
47 if (c_bio_gets == NULL)
48 c_bio_gets = OSSL_FUNC_BIO_gets(fns);
50 case OSSL_FUNC_BIO_PUTS:
51 if (c_bio_puts == NULL)
52 c_bio_puts = OSSL_FUNC_BIO_puts(fns);
54 case OSSL_FUNC_BIO_CTRL:
55 if (c_bio_ctrl == NULL)
56 c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
58 case OSSL_FUNC_BIO_UP_REF:
59 if (c_bio_up_ref == NULL)
60 c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
62 case OSSL_FUNC_BIO_FREE:
63 if (c_bio_free == NULL)
64 c_bio_free = OSSL_FUNC_BIO_free(fns);
66 case OSSL_FUNC_BIO_VPRINTF:
67 if (c_bio_vprintf == NULL)
68 c_bio_vprintf = OSSL_FUNC_BIO_vprintf(fns);
76 OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
78 if (c_bio_new_file == NULL)
80 return c_bio_new_file(filename, mode);
83 OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
85 if (c_bio_new_membuf == NULL)
87 return c_bio_new_membuf(filename, len);
90 int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
93 if (c_bio_read_ex == NULL)
95 return c_bio_read_ex(bio, data, data_len, bytes_read);
98 int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
101 if (c_bio_write_ex == NULL)
103 return c_bio_write_ex(bio, data, data_len, written);
106 int ossl_prov_bio_gets(OSSL_CORE_BIO *bio, char *buf, int size)
108 if (c_bio_gets == NULL)
110 return c_bio_gets(bio, buf, size);
113 int ossl_prov_bio_puts(OSSL_CORE_BIO *bio, const char *str)
115 if (c_bio_puts == NULL)
117 return c_bio_puts(bio, str);
120 int ossl_prov_bio_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
122 if (c_bio_ctrl == NULL)
124 return c_bio_ctrl(bio, cmd, num, ptr);
127 int ossl_prov_bio_up_ref(OSSL_CORE_BIO *bio)
129 if (c_bio_up_ref == NULL)
131 return c_bio_up_ref(bio);
134 int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
136 if (c_bio_free == NULL)
138 return c_bio_free(bio);
141 int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
143 if (c_bio_vprintf == NULL)
145 return c_bio_vprintf(bio, format, ap);
148 int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
153 va_start(ap, format);
154 ret = ossl_prov_bio_vprintf(bio, format, ap);
162 /* No direct BIO support in the FIPS module */
164 static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
167 return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
170 static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
173 return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
176 static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
178 return ossl_prov_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
181 static int bio_core_gets(BIO *bio, char *buf, int size)
183 return ossl_prov_bio_gets(BIO_get_data(bio), buf, size);
186 static int bio_core_puts(BIO *bio, const char *str)
188 return ossl_prov_bio_puts(BIO_get_data(bio), str);
191 static int bio_core_new(BIO *bio)
193 BIO_set_init(bio, 1);
198 static int bio_core_free(BIO *bio)
200 BIO_set_init(bio, 0);
201 ossl_prov_bio_free(BIO_get_data(bio));
206 BIO_METHOD *ossl_bio_prov_init_bio_method(void)
208 BIO_METHOD *corebiometh = NULL;
210 corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
211 if (corebiometh == NULL
212 || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
213 || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
214 || !BIO_meth_set_puts(corebiometh, bio_core_puts)
215 || !BIO_meth_set_gets(corebiometh, bio_core_gets)
216 || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
217 || !BIO_meth_set_create(corebiometh, bio_core_new)
218 || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
219 BIO_meth_free(corebiometh);
226 BIO *ossl_bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
229 BIO_METHOD *corebiometh = ossl_prov_ctx_get0_core_bio_method(provctx);
231 if (corebiometh == NULL)
234 if ((outbio = BIO_new(corebiometh)) == NULL)
236 if (!ossl_prov_bio_up_ref(corebio)) {
240 BIO_set_data(outbio, corebio);