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_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_free_fn *c_bio_free = NULL;
23 static OSSL_FUNC_BIO_vprintf_fn *c_bio_vprintf = NULL;
25 int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
27 for (; fns->function_id != 0; fns++) {
28 switch (fns->function_id) {
29 case OSSL_FUNC_BIO_NEW_FILE:
30 if (c_bio_new_file == NULL)
31 c_bio_new_file = OSSL_FUNC_BIO_new_file(fns);
33 case OSSL_FUNC_BIO_NEW_MEMBUF:
34 if (c_bio_new_membuf == NULL)
35 c_bio_new_membuf = OSSL_FUNC_BIO_new_membuf(fns);
37 case OSSL_FUNC_BIO_READ_EX:
38 if (c_bio_read_ex == NULL)
39 c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
41 case OSSL_FUNC_BIO_WRITE_EX:
42 if (c_bio_write_ex == NULL)
43 c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
45 case OSSL_FUNC_BIO_GETS:
46 if (c_bio_gets == NULL)
47 c_bio_gets = OSSL_FUNC_BIO_gets(fns);
49 case OSSL_FUNC_BIO_PUTS:
50 if (c_bio_puts == NULL)
51 c_bio_puts = OSSL_FUNC_BIO_puts(fns);
53 case OSSL_FUNC_BIO_CTRL:
54 if (c_bio_ctrl == NULL)
55 c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
57 case OSSL_FUNC_BIO_FREE:
58 if (c_bio_free == NULL)
59 c_bio_free = OSSL_FUNC_BIO_free(fns);
61 case OSSL_FUNC_BIO_VPRINTF:
62 if (c_bio_vprintf == NULL)
63 c_bio_vprintf = OSSL_FUNC_BIO_vprintf(fns);
71 OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
73 if (c_bio_new_file == NULL)
75 return c_bio_new_file(filename, mode);
78 OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
80 if (c_bio_new_membuf == NULL)
82 return c_bio_new_membuf(filename, len);
85 int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
88 if (c_bio_read_ex == NULL)
90 return c_bio_read_ex(bio, data, data_len, bytes_read);
93 int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
96 if (c_bio_write_ex == NULL)
98 return c_bio_write_ex(bio, data, data_len, written);
101 int ossl_prov_bio_gets(OSSL_CORE_BIO *bio, char *buf, int size)
103 if (c_bio_gets == NULL)
105 return c_bio_gets(bio, buf, size);
108 int ossl_prov_bio_puts(OSSL_CORE_BIO *bio, const char *str)
110 if (c_bio_puts == NULL)
112 return c_bio_puts(bio, str);
115 int ossl_prov_bio_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
117 if (c_bio_ctrl == NULL)
119 return c_bio_ctrl(bio, cmd, num, ptr);
122 int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
124 if (c_bio_free == NULL)
126 return c_bio_free(bio);
129 int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
131 if (c_bio_vprintf == NULL)
133 return c_bio_vprintf(bio, format, ap);
136 int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
141 va_start(ap, format);
142 ret = ossl_prov_bio_vprintf(bio, format, ap);
150 /* No direct BIO support in the FIPS module */
152 static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
155 return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
158 static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
161 return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
164 static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
166 return ossl_prov_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
169 static int bio_core_gets(BIO *bio, char *buf, int size)
171 return ossl_prov_bio_gets(BIO_get_data(bio), buf, size);
174 static int bio_core_puts(BIO *bio, const char *str)
176 return ossl_prov_bio_puts(BIO_get_data(bio), str);
179 static int bio_core_new(BIO *bio)
181 BIO_set_init(bio, 1);
186 static int bio_core_free(BIO *bio)
188 BIO_set_init(bio, 0);
193 BIO_METHOD *bio_prov_init_bio_method(void)
195 BIO_METHOD *corebiometh = NULL;
197 corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
198 if (corebiometh == NULL
199 || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
200 || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
201 || !BIO_meth_set_puts(corebiometh, bio_core_puts)
202 || !BIO_meth_set_gets(corebiometh, bio_core_gets)
203 || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
204 || !BIO_meth_set_create(corebiometh, bio_core_new)
205 || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
206 BIO_meth_free(corebiometh);
213 BIO *bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
216 BIO_METHOD *corebiometh = PROV_CTX_get0_core_bio_method(provctx);
218 if (corebiometh == NULL)
221 outbio = BIO_new(corebiometh);
223 BIO_set_data(outbio, corebio);