summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Caswell <matt@openssl.org>2020-05-06 12:29:57 +0100
committerMatt Caswell <matt@openssl.org>2020-05-16 17:10:03 +0100
commitd40b42ab4c8a88740a2cc2a20c709fe869c4dd1e (patch)
tree0dfa4439f3de544d7e52abf56c578e10e5346458
parent827f04d5105e9bec0af214c42b8ad799fba5bb0d (diff)
Maintain strict type discipline between the core and providers
A provider could be linked against a different version of libcrypto than the version of libcrypto that loaded the provider. Different versions of libcrypto could define opaque types differently. It must never occur that a type created in one libcrypto is used directly by the other libcrypto. This will cause crashes. We can "cheat" for "built-in" providers that are part of libcrypto itself, because we know that the two libcrypto versions are the same - but not for other providers. To ensure this does not occur we use different types names for the handful of opaque types that are passed between the core and providers. Reviewed-by: Richard Levitte <levitte@openssl.org> (Merged from https://github.com/openssl/openssl/pull/11758)
-rw-r--r--crypto/initthread.c2
-rw-r--r--crypto/provider_core.c52
-rw-r--r--crypto/serializer/serializer_pkey.c5
-rw-r--r--include/internal/cryptlib.h3
-rw-r--r--include/openssl/bio.h1
-rw-r--r--include/openssl/core.h9
-rw-r--r--include/openssl/core_numbers.h53
-rw-r--r--providers/common/bio_prov.c114
-rw-r--r--providers/common/include/prov/bio.h18
-rw-r--r--providers/common/include/prov/provider_ctx.h22
-rw-r--r--providers/common/include/prov/providercommon.h2
-rw-r--r--providers/common/provider_ctx.c21
-rw-r--r--providers/defltprov.c19
-rw-r--r--providers/fips/fipsprov.c27
-rw-r--r--providers/fips/self_test.c4
-rw-r--r--providers/implementations/serializers/serializer_common.c32
-rw-r--r--providers/implementations/serializers/serializer_dh.c2
-rw-r--r--providers/implementations/serializers/serializer_dh_param.c48
-rw-r--r--providers/implementations/serializers/serializer_dh_priv.c37
-rw-r--r--providers/implementations/serializers/serializer_dh_pub.c60
-rw-r--r--providers/implementations/serializers/serializer_dsa.c3
-rw-r--r--providers/implementations/serializers/serializer_dsa_param.c49
-rw-r--r--providers/implementations/serializers/serializer_dsa_priv.c58
-rw-r--r--providers/implementations/serializers/serializer_dsa_pub.c53
-rw-r--r--providers/implementations/serializers/serializer_ec.c10
-rw-r--r--providers/implementations/serializers/serializer_ec_param.c55
-rw-r--r--providers/implementations/serializers/serializer_ec_priv.c63
-rw-r--r--providers/implementations/serializers/serializer_ec_pub.c53
-rw-r--r--providers/implementations/serializers/serializer_ecx.c2
-rw-r--r--providers/implementations/serializers/serializer_ecx_priv.c38
-rw-r--r--providers/implementations/serializers/serializer_ecx_pub.c59
-rw-r--r--providers/implementations/serializers/serializer_ffc_params.c8
-rw-r--r--providers/implementations/serializers/serializer_rsa.c54
-rw-r--r--providers/implementations/serializers/serializer_rsa_priv.c36
-rw-r--r--providers/implementations/serializers/serializer_rsa_pub.c61
-rw-r--r--providers/legacyprov.c4
-rw-r--r--providers/nullprov.c36
-rw-r--r--test/p_test.c10
38 files changed, 847 insertions, 336 deletions
diff --git a/crypto/initthread.c b/crypto/initthread.c
index 8f0678970a..a97cf359af 100644
--- a/crypto/initthread.c
+++ b/crypto/initthread.c
@@ -360,7 +360,7 @@ int ossl_init_thread_start(const void *index, void *arg,
* libcrypto to tell us about later thread stop events. c_thread_start
* is a callback to libcrypto defined in fipsprov.c
*/
- if (!c_thread_start(FIPS_get_provider(ctx), ossl_ctx_thread_stop))
+ if (!c_thread_start(FIPS_get_core_handle(ctx), ossl_ctx_thread_stop))
return 0;
}
#endif
diff --git a/crypto/provider_core.c b/crypto/provider_core.c
index 1cbe369754..662576cd7b 100644
--- a/crypto/provider_core.c
+++ b/crypto/provider_core.c
@@ -488,8 +488,8 @@ static int provider_activate(OSSL_PROVIDER *prov)
/* Call the initialise function for the provider. */
if (prov->init_function == NULL
- || !prov->init_function(prov, core_dispatch, &provider_dispatch,
- &tmp_provctx)) {
+ || !prov->init_function((OSSL_CORE_HANDLE *)prov, core_dispatch,
+ &provider_dispatch, &tmp_provctx)) {
ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INIT_FAIL, NULL,
"name=%s", prov->name);
#ifndef FIPS_MODULE
@@ -818,15 +818,20 @@ static OSSL_core_clear_last_error_mark_fn core_clear_last_error_mark;
static OSSL_core_pop_error_to_mark_fn core_pop_error_to_mark;
#endif
-static const OSSL_PARAM *core_gettable_params(const OSSL_PROVIDER *prov)
+static const OSSL_PARAM *core_gettable_params(const OSSL_CORE_HANDLE *handle)
{
return param_types;
}
-static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
+static int core_get_params(const OSSL_CORE_HANDLE *handle, OSSL_PARAM params[])
{
int i;
OSSL_PARAM *p;
+ /*
+ * We created this object originally and we know it is actually an
+ * OSSL_PROVIDER *, so the cast is safe
+ */
+ OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
if ((p = OSSL_PARAM_locate(params, "openssl-version")) != NULL)
OSSL_PARAM_set_utf8_ptr(p, OPENSSL_VERSION_STR);
@@ -850,14 +855,26 @@ static int core_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
return 1;
}
-static OPENSSL_CTX *core_get_libctx(const OSSL_PROVIDER *prov)
+static OPENSSL_CORE_CTX *core_get_libctx(const OSSL_CORE_HANDLE *handle)
{
- return ossl_provider_library_context(prov);
+ /*
+ * We created this object originally and we know it is actually an
+ * OSSL_PROVIDER *, so the cast is safe
+ */
+ OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
+
+ return (OPENSSL_CORE_CTX *)ossl_provider_library_context(prov);
}
-static int core_thread_start(const OSSL_PROVIDER *prov,
+static int core_thread_start(const OSSL_CORE_HANDLE *handle,
OSSL_thread_stop_handler_fn handfn)
{
+ /*
+ * We created this object originally and we know it is actually an
+ * OSSL_PROVIDER *, so the cast is safe
+ */
+ OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
+
return ossl_init_thread_start(prov, prov->provctx, handfn);
}
@@ -868,27 +885,33 @@ static int core_thread_start(const OSSL_PROVIDER *prov,
*/
#ifndef FIPS_MODULE
/*
- * TODO(3.0) These error functions should use |prov| to select the proper
+ * TODO(3.0) These error functions should use |handle| to select the proper
* library context to report in the correct error stack, at least if error
* stacks become tied to the library context.
* We cannot currently do that since there's no support for it in the
* ERR subsystem.
*/
-static void core_new_error(const OSSL_PROVIDER *prov)
+static void core_new_error(const OSSL_CORE_HANDLE *handle)
{
ERR_new();
}
-static void core_set_error_debug(const OSSL_PROVIDER *prov,
+static void core_set_error_debug(const OSSL_CORE_HANDLE *handle,
const char *file, int line, const char *func)
{
ERR_set_debug(file, line, func);
}
-static void core_vset_error(const OSSL_PROVIDER *prov,
+static void core_vset_error(const OSSL_CORE_HANDLE *handle,
uint32_t reason, const char *fmt, va_list args)
{
/*
+ * We created this object originally and we know it is actually an
+ * OSSL_PROVIDER *, so the cast is safe
+ */
+ OSSL_PROVIDER *prov = (OSSL_PROVIDER *)handle;
+
+ /*
* If the uppermost 8 bits are non-zero, it's an OpenSSL library
* error and will be treated as such. Otherwise, it's a new style
* provider error and will be treated as such.
@@ -900,17 +923,17 @@ static void core_vset_error(const OSSL_PROVIDER *prov,
}
}
-static int core_set_error_mark(const OSSL_PROVIDER *prov)
+static int core_set_error_mark(const OSSL_CORE_HANDLE *handle)
{
return ERR_set_mark();
}
-static int core_clear_last_error_mark(const OSSL_PROVIDER *prov)
+static int core_clear_last_error_mark(const OSSL_CORE_HANDLE *handle)
{
return ERR_clear_last_mark();
}
-static int core_pop_error_to_mark(const OSSL_PROVIDER *prov)
+static int core_pop_error_to_mark(const OSSL_CORE_HANDLE *handle)
{
return ERR_pop_to_mark();
}
@@ -936,6 +959,7 @@ static const OSSL_DISPATCH core_dispatch_[] = {
{ OSSL_FUNC_BIO_NEW_FILE, (void (*)(void))BIO_new_file },
{ OSSL_FUNC_BIO_NEW_MEMBUF, (void (*)(void))BIO_new_mem_buf },
{ OSSL_FUNC_BIO_READ_EX, (void (*)(void))BIO_read_ex },
+ { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))BIO_write_ex },
{ OSSL_FUNC_BIO_FREE, (void (*)(void))BIO_free },
{ OSSL_FUNC_BIO_VPRINTF, (void (*)(void))BIO_vprintf },
{ OSSL_FUNC_BIO_VSNPRINTF, (void (*)(void))BIO_vsnprintf },
diff --git a/crypto/serializer/serializer_pkey.c b/crypto/serializer/serializer_pkey.c
index 3750ea3df1..a3b854e5da 100644
--- a/crypto/serializer/serializer_pkey.c
+++ b/crypto/serializer/serializer_pkey.c
@@ -255,7 +255,7 @@ static int serializer_write_cb(const OSSL_PARAM params[], void *arg)
OSSL_SERIALIZER_CTX *ctx = write_data->ctx;
BIO *out = write_data->out;
- return ctx->ser->serialize_data(ctx->serctx, params, out,
+ return ctx->ser->serialize_data(ctx->serctx, params, (OSSL_CORE_BIO *)out,
serializer_passphrase_out_cb, ctx);
}
@@ -291,7 +291,8 @@ static int serializer_EVP_PKEY_to_bio(OSSL_SERIALIZER_CTX *ctx, BIO *out)
&serializer_write_cb, &write_data);
}
- return ctx->ser->serialize_object(ctx->serctx, keydata, out,
+ return ctx->ser->serialize_object(ctx->serctx, keydata,
+ (OSSL_CORE_BIO *)out,
serializer_passphrase_out_cb, ctx);
}
diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h
index 03f147888a..b479b58a84 100644
--- a/include/internal/cryptlib.h
+++ b/include/internal/cryptlib.h
@@ -157,7 +157,8 @@ typedef struct ossl_ex_data_global_st {
# define OPENSSL_CTX_FIPS_PROV_INDEX 9
# define OPENSSL_CTX_SERIALIZER_STORE_INDEX 10
# define OPENSSL_CTX_SELF_TEST_CB_INDEX 11
-# define OPENSSL_CTX_MAX_INDEXES 12
+# define OPENSSL_CTX_BIO_PROV_INDEX 12
+# define OPENSSL_CTX_MAX_INDEXES 13
typedef struct openssl_ctx_method {
void *(*new_func)(OPENSSL_CTX *ctx);
diff --git a/include/openssl/bio.h b/include/openssl/bio.h
index b4047d55b9..19f9311c68 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -61,6 +61,7 @@ extern "C" {
# ifndef OPENSSL_NO_SCTP
# define BIO_TYPE_DGRAM_SCTP (24|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR)
# endif
+# define BIO_TYPE_CORE_TO_PROV (25|BIO_TYPE_FILTER)
#define BIO_TYPE_START 128
diff --git a/include/openssl/core.h b/include/openssl/core.h
index 2d653dd60f..5eb992a5c2 100644
--- a/include/openssl/core.h
+++ b/include/openssl/core.h
@@ -25,6 +25,11 @@ extern "C" {
* to communicate data between them.
*/
+/* Opaque handles to be used with core upcall functions from providers */
+typedef struct ossl_core_handle_st OSSL_CORE_HANDLE;
+typedef struct openssl_core_ctx_st OPENSSL_CORE_CTX;
+typedef struct ossl_core_bio_st OSSL_CORE_BIO;
+
/*
* Dispatch table element. function_id numbers are defined further down,
* see macros with '_FUNC' in their names.
@@ -171,7 +176,7 @@ typedef void (*OSSL_thread_stop_handler_fn)(void *arg);
* module, that module is not an OpenSSL provider module.
*/
/*-
- * |provider| pointer to opaque type OSSL_PROVIDER. This can be used
+ * |handle| pointer to opaque type OSSL_CORE_HANDLE. This can be used
* together with some functions passed via |in| to query data.
* |in| is the array of functions that the Core passes to the provider.
* |out| will be the array of base functions that the provider passes
@@ -180,7 +185,7 @@ typedef void (*OSSL_thread_stop_handler_fn)(void *arg);
* provider needs it. This value is passed to other provider
* functions, notably other context constructors.
*/
-typedef int (OSSL_provider_init_fn)(const OSSL_PROVIDER *provider,
+typedef int (OSSL_provider_init_fn)(const OSSL_CORE_HANDLE *handle,
const OSSL_DISPATCH *in,
const OSSL_DISPATCH **out,
void **provctx);
diff --git a/include/openssl/core_numbers.h b/include/openssl/core_numbers.h
index 3d91741601..f7025d1c1d 100644
--- a/include/openssl/core_numbers.h
+++ b/include/openssl/core_numbers.h
@@ -12,7 +12,6 @@
# include <stdarg.h>
# include <openssl/core.h>
-# include <openssl/self_test.h>
# ifdef __cplusplus
extern "C" {
@@ -60,33 +59,33 @@ extern "C" {
/* Functions provided by the Core to the provider, reserved numbers 1-1023 */
# define OSSL_FUNC_CORE_GETTABLE_PARAMS 1
OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *,
- core_gettable_params,(const OSSL_PROVIDER *prov))
+ core_gettable_params,(const OSSL_CORE_HANDLE *prov))
# define OSSL_FUNC_CORE_GET_PARAMS 2
-OSSL_CORE_MAKE_FUNC(int,core_get_params,(const OSSL_PROVIDER *prov,
+OSSL_CORE_MAKE_FUNC(int,core_get_params,(const OSSL_CORE_HANDLE *prov,
OSSL_PARAM params[]))
# define OSSL_FUNC_CORE_THREAD_START 3
-OSSL_CORE_MAKE_FUNC(int,core_thread_start,(const OSSL_PROVIDER *prov,
+OSSL_CORE_MAKE_FUNC(int,core_thread_start,(const OSSL_CORE_HANDLE *prov,
OSSL_thread_stop_handler_fn handfn))
# define OSSL_FUNC_CORE_GET_LIBRARY_CONTEXT 4
-OSSL_CORE_MAKE_FUNC(OPENSSL_CTX *,core_get_library_context,
- (const OSSL_PROVIDER *prov))
+OSSL_CORE_MAKE_FUNC(OPENSSL_CORE_CTX *,core_get_library_context,
+ (const OSSL_CORE_HANDLE *prov))
# define OSSL_FUNC_CORE_NEW_ERROR 5
-OSSL_CORE_MAKE_FUNC(void,core_new_error,(const OSSL_PROVIDER *prov))
+OSSL_CORE_MAKE_FUNC(void,core_new_error,(const OSSL_CORE_HANDLE *prov))
# define OSSL_FUNC_CORE_SET_ERROR_DEBUG 6
OSSL_CORE_MAKE_FUNC(void,core_set_error_debug,
- (const OSSL_PROVIDER *prov,
+ (const OSSL_CORE_HANDLE *prov,
const char *file, int line, const char *func))
# define OSSL_FUNC_CORE_VSET_ERROR 7
OSSL_CORE_MAKE_FUNC(void,core_vset_error,
- (const OSSL_PROVIDER *prov,
+ (const OSSL_CORE_HANDLE *prov,
uint32_t reason, const char *fmt, va_list args))
# define OSSL_FUNC_CORE_SET_ERROR_MARK 8
-OSSL_CORE_MAKE_FUNC(int, core_set_error_mark, (const OSSL_PROVIDER *prov))
+OSSL_CORE_MAKE_FUNC(int, core_set_error_mark, (const OSSL_CORE_HANDLE *prov))
# define OSSL_FUNC_CORE_CLEAR_LAST_ERROR_MARK 9
OSSL_CORE_MAKE_FUNC(int, core_clear_last_error_mark,
- (const OSSL_PROVIDER *prov))
+ (const OSSL_CORE_HANDLE *prov))
# define OSSL_FUNC_CORE_POP_ERROR_TO_MARK 10
-OSSL_CORE_MAKE_FUNC(int, core_pop_error_to_mark, (const OSSL_PROVIDER *prov))
+OSSL_CORE_MAKE_FUNC(int, core_pop_error_to_mark, (const OSSL_CORE_HANDLE *prov))
/* Memory allocation, freeing, clearing. */
#define OSSL_FUNC_CRYPTO_MALLOC 20
@@ -132,22 +131,26 @@ OSSL_CORE_MAKE_FUNC(void,
#define OSSL_FUNC_BIO_NEW_FILE 40
#define OSSL_FUNC_BIO_NEW_MEMBUF 41
#define OSSL_FUNC_BIO_READ_EX 42
-#define OSSL_FUNC_BIO_FREE 43
-#define OSSL_FUNC_BIO_VPRINTF 44
-#define OSSL_FUNC_BIO_VSNPRINTF 45
-
-OSSL_CORE_MAKE_FUNC(BIO *, BIO_new_file, (const char *filename, const char *mode))
-OSSL_CORE_MAKE_FUNC(BIO *, BIO_new_membuf, (const void *buf, int len))
-OSSL_CORE_MAKE_FUNC(int, BIO_read_ex, (BIO *bio, void *data, size_t data_len,
- size_t *bytes_read))
-OSSL_CORE_MAKE_FUNC(int, BIO_free, (BIO *bio))
-OSSL_CORE_MAKE_FUNC(int, BIO_vprintf, (BIO *bio, const char *format,
+#define OSSL_FUNC_BIO_WRITE_EX 43
+#define OSSL_FUNC_BIO_FREE 44
+#define OSSL_FUNC_BIO_VPRINTF 45
+#define OSSL_FUNC_BIO_VSNPRINTF 46
+
+OSSL_CORE_MAKE_FUNC(OSSL_CORE_BIO *, BIO_new_file, (const char *filename,
+ const char *mode))
+OSSL_CORE_MAKE_FUNC(OSSL_CORE_BIO *, BIO_new_membuf, (const void *buf, int len))
+OSSL_CORE_MAKE_FUNC(int, BIO_read_ex, (OSSL_CORE_BIO *bio, void *data,
+ size_t data_len, size_t *bytes_read))
+OSSL_CORE_MAKE_FUNC(int, BIO_write_ex, (OSSL_CORE_BIO *bio, const void *data,
+ size_t data_len, size_t *written))
+OSSL_CORE_MAKE_FUNC(int, BIO_free, (OSSL_CORE_BIO *bio))
+OSSL_CORE_MAKE_FUNC(int, BIO_vprintf, (OSSL_CORE_BIO *bio, const char *format,
va_list args))
OSSL_CORE_MAKE_FUNC(int, BIO_vsnprintf,
(char *buf, size_t n, const char *fmt, va_list args))
#define OSSL_FUNC_SELF_TEST_CB 100
-OSSL_CORE_MAKE_FUNC(void, self_test_cb, (OPENSSL_CTX *ctx, OSSL_CALLBACK **cb,
+OSSL_CORE_MAKE_FUNC(void, self_test_cb, (OPENSSL_CORE_CTX *ctx, OSSL_CALLBACK **cb,
void **cbarg))
/* Functions provided by the provider to the Core, reserved numbers 1024-1535 */
@@ -637,10 +640,10 @@ OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_serializer_settable_ctx_params,
(void))
OSSL_CORE_MAKE_FUNC(int, OP_serializer_serialize_data,
- (void *ctx, const OSSL_PARAM[], BIO *out,
+ (void *ctx, const OSSL_PARAM[], OSSL_CORE_BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg))
OSSL_CORE_MAKE_FUNC(int, OP_serializer_serialize_object,
- (void *ctx, void *obj, BIO *out,
+ (void *ctx, void *obj, OSSL_CORE_BIO *out,
OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg))
# ifdef __cplusplus
diff --git a/providers/common/bio_prov.c b/providers/common/bio_prov.c
index 7b44004399..2bfd14b512 100644
--- a/providers/common/bio_prov.c
+++ b/providers/common/bio_prov.c
@@ -7,12 +7,15 @@
* https://www.openssl.org/source/license.html
*/
+#include <assert.h>
#include <openssl/core_numbers.h>
+#include "internal/cryptlib.h"
#include "prov/bio.h"
static OSSL_BIO_new_file_fn *c_bio_new_file = NULL;
static OSSL_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
static OSSL_BIO_read_ex_fn *c_bio_read_ex = NULL;
+static OSSL_BIO_write_ex_fn *c_bio_write_ex = NULL;
static OSSL_BIO_free_fn *c_bio_free = NULL;
static OSSL_BIO_vprintf_fn *c_bio_vprintf = NULL;
@@ -32,6 +35,10 @@ int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
if (c_bio_read_ex == NULL)
c_bio_read_ex = OSSL_get_BIO_read_ex(fns);
break;
+ case OSSL_FUNC_BIO_WRITE_EX:
+ if (c_bio_write_ex == NULL)
+ c_bio_write_ex = OSSL_get_BIO_write_ex(fns);
+ break;
case OSSL_FUNC_BIO_FREE:
if (c_bio_free == NULL)
c_bio_free = OSSL_get_BIO_free(fns);
@@ -46,21 +53,21 @@ int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
return 1;
}
-BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
+OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
{
if (c_bio_new_file == NULL)
return NULL;
return c_bio_new_file(filename, mode);
}
-BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
+OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
{
if (c_bio_new_membuf == NULL)
return NULL;
return c_bio_new_membuf(filename, len);
}
-int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
+int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
size_t *bytes_read)
{
if (c_bio_read_ex == NULL)
@@ -68,21 +75,29 @@ int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
return c_bio_read_ex(bio, data, data_len, bytes_read);
}
-int ossl_prov_bio_free(BIO *bio)
+int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
+ size_t *written)
+{
+ if (c_bio_write_ex == NULL)
+ return 0;
+ return c_bio_write_ex(bio, data, data_len, written);
+}
+
+int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
{
if (c_bio_free == NULL)
return 0;
return c_bio_free(bio);
}
-int ossl_prov_bio_vprintf(BIO *bio, const char *format, va_list ap)
+int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
{
if (c_bio_vprintf == NULL)
return -1;
return c_bio_vprintf(bio, format, ap);
}
-int ossl_prov_bio_printf(BIO *bio, const char *format, ...)
+int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
{
va_list ap;
int ret;
@@ -94,3 +109,90 @@ int ossl_prov_bio_printf(BIO *bio, const char *format, ...)
return ret;
}
+#ifndef FIPS_MODULE
+
+/* No direct BIO support in the FIPS module */
+
+static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
+ size_t *bytes_read)
+{
+ return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
+}
+
+static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
+ size_t *written)
+{
+ return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
+}
+
+static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
+{
+ /* We don't support this */
+ assert(0);
+ return 0;
+}
+
+static int bio_core_gets(BIO *bio, char *buf, int size)
+{
+ /* We don't support this */
+ assert(0);
+ return -1;
+}
+
+static int bio_core_puts(BIO *bio, const char *str)
+{
+ /* We don't support this */
+ assert(0);
+ return -1;
+}
+
+static int bio_core_new(BIO *bio)
+{
+ BIO_set_init(bio, 1);
+
+ return 1;
+}
+
+static int bio_core_free(BIO *bio)
+{
+ BIO_set_init(bio, 0);
+
+ return 1;
+}
+
+BIO_METHOD *bio_prov_init_bio_method(void)
+{
+ BIO_METHOD *corebiometh = NULL;
+
+ corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
+ if (corebiometh == NULL
+ || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
+ || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
+ || !BIO_meth_set_puts(corebiometh, bio_core_puts)
+ || !BIO_meth_set_gets(corebiometh, bio_core_gets)
+ || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
+ || !BIO_meth_set_create(corebiometh, bio_core_new)
+ || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
+ BIO_meth_free(corebiometh);
+ return NULL;
+ }
+
+ return corebiometh;
+}
+
+BIO *bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
+{
+ BIO *outbio;
+ BIO_METHOD *corebiometh = PROV_CTX_get0_core_bio_method(provctx);
+
+ if (corebiometh == NULL)
+ return NULL;
+
+ outbio = BIO_new(corebiometh);
+ if (outbio != NULL)
+ BIO_set_data(outbio, corebio);
+
+ return outbio;
+}
+
+#endif
diff --git a/providers/common/include/prov/bio.h b/providers/common/include/prov/bio.h
index 63f9d4ec3a..732dc06f03 100644
--- a/providers/common/include/prov/bio.h
+++ b/providers/common/include/prov/bio.h
@@ -10,13 +10,19 @@
#include <stdarg.h>
#include <openssl/bio.h>
#include <openssl/core.h>
+#include "prov/provider_ctx.h"
int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns);
-BIO *ossl_prov_bio_new_file(const char *filename, const char *mode);
-BIO *ossl_prov_bio_new_membuf(const char *filename, int len);
-int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
+OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode);
+OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len);
+int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
size_t *bytes_read);
-int ossl_prov_bio_free(BIO *bio);
-int ossl_prov_bio_vprintf(BIO *bio, const char *format, va_list ap);
-int ossl_prov_bio_printf(BIO *bio, const char *format, ...);
+int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
+ size_t *written);
+int ossl_prov_bio_free(OSSL_CORE_BIO *bio);
+int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap);
+int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...);
+
+BIO_METHOD *bio_prov_init_bio_method(void);
+BIO *bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio);
diff --git a/providers/common/include/prov/provider_ctx.h b/providers/common/include/prov/provider_ctx.h
index fc2df2ee67..a252143e81 100644
--- a/providers/common/include/prov/provider_ctx.h
+++ b/providers/common/include/prov/provider_ctx.h
@@ -7,24 +7,34 @@
* https://www.openssl.org/source/license.html
*/
-#include <openssl/types.h>
-#include <openssl/crypto.h>
+#ifndef OSSL_PROV_PROVIDER_CTX_H
+# define OSSL_PROV_PROVIDER_CTX_H
+
+# include <openssl/types.h>
+# include <openssl/crypto.h>
+# include <openssl/bio.h>
+# include <openssl/core.h>
typedef struct prov_ctx_st {
- const OSSL_PROVIDER *provider;
+ const OSSL_CORE_HANDLE *handle;
OPENSSL_CTX *libctx; /* For all provider modules */
+ BIO_METHOD *corebiometh;
} PROV_CTX;
/*
* To be used anywhere the library context needs to be passed, such as to
* fetching functions.
*/
-#define PROV_LIBRARY_CONTEXT_OF(provctx) \
+# define PROV_LIBRARY_CONTEXT_OF(provctx) \
PROV_CTX_get0_library_context((provctx))
PROV_CTX *PROV_CTX_new(void);
void PROV_CTX_free(PROV_CTX *ctx);
void PROV_CTX_set0_library_context(PROV_CTX *ctx, OPENSSL_CTX *libctx);
-void PROV_CTX_set0_provider(PROV_CTX *ctx, const OSSL_PROVIDER *libctx);
+void PROV_CTX_set0_handle(PROV_CTX *ctx, const OSSL_CORE_HANDLE *handle);
+void PROV_CTX_set0_core_bio_method(PROV_CTX *ctx, BIO_METHOD *corebiometh);
OPENSSL_CTX *PROV_CTX_get0_library_context(PROV_CTX *ctx);
-const OSSL_PROVIDER *PROV_CTX_get0_provider(PROV_CTX *ctx);
+const OSSL_CORE_HANDLE *PROV_CTX_get0_handle(PROV_CTX *ctx);
+BIO_METHOD *PROV_CTX_get0_core_bio_method(PROV_CTX *ctx);
+
+#endif
diff --git a/providers/common/include/prov/providercommon.h b/providers/common/include/prov/providercommon.h
index 5123f78ee1..07c5a67f38 100644
--- a/providers/common/include/prov/providercommon.h
+++ b/