summaryrefslogtreecommitdiffstats
path: root/providers/common
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 /providers/common
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)
Diffstat (limited to 'providers/common')
-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
5 files changed, 154 insertions, 23 deletions
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/providers/common/include/prov/providercommon.h
@@ -9,7 +9,7 @@
#include <openssl/provider.h>
-const OSSL_PROVIDER *FIPS_get_provider(OPENSSL_CTX *ctx);
+const OSSL_CORE_HANDLE *FIPS_get_core_handle(OPENSSL_CTX *ctx);
const char *ossl_prov_util_nid_to_name(int nid);
diff --git a/providers/common/provider_ctx.c b/providers/common/provider_ctx.c
index 66c7c74890..04cca1f23e 100644
--- a/providers/common/provider_ctx.c
+++ b/providers/common/provider_ctx.c
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include "prov/provider_ctx.h"
+#include "prov/bio.h"
PROV_CTX *PROV_CTX_new(void)
{
@@ -26,12 +27,17 @@ void PROV_CTX_set0_library_context(PROV_CTX *ctx, OPENSSL_CTX *libctx)
ctx->libctx = libctx;
}
-void PROV_CTX_set0_provider(PROV_CTX *ctx, const OSSL_PROVIDER *provider)
+void PROV_CTX_set0_handle(PROV_CTX *ctx, const OSSL_CORE_HANDLE *handle)
{
if (ctx != NULL)
- ctx->provider = provider;
+ ctx->handle = handle;
}
+void PROV_CTX_set0_core_bio_method(PROV_CTX *ctx, BIO_METHOD *corebiometh)
+{
+ if (ctx != NULL)
+ ctx->corebiometh = corebiometh;
+}
OPENSSL_CTX *PROV_CTX_get0_library_context(PROV_CTX *ctx)
{
@@ -40,9 +46,16 @@ OPENSSL_CTX *PROV_CTX_get0_library_context(PROV_CTX *ctx)
return ctx->libctx;
}
-const OSSL_PROVIDER *PROV_CTX_get0_provider(PROV_CTX *ctx)
+const OSSL_CORE_HANDLE *PROV_CTX_get0_handle(PROV_CTX *ctx)
+{
+ if (ctx == NULL)
+ return NULL;
+ return ctx->handle;
+}
+
+BIO_METHOD *PROV_CTX_get0_core_bio_method(PROV_CTX *ctx)
{
if (ctx == NULL)
return NULL;
- return ctx->provider;
+ return ctx->corebiometh;
}