Skip to contentSkip to conversation

A built-in OpenSSL3.0 provider for external-keys

PATCH Superseded [email protected]... 3 participants 28 messages Sep 22, 2021 – Nov 5, 2021 2 messages not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover A built-in OpenSSL3.0 provider for external-keys

From: Selva Nair <[email protected]...>

The following series of patches implement a built-in provider for interfacing OpenSSL 3.0 when external keys are in use.

Essentially, to intercept the sign operation, the SSL_CTX object has to be created with properties string set to prioritize our provider. In the provider we implement only keymgmt and signature operations and specify the property string as optional. That allows all operations we do not provide to be used from the default provider.

This patch set stops at interfacing the provider with management-external-key. For pkcs11-helper, only some glue code is needed and is in the works. Same with cryptoapicert
aka CNG, but I want to cleanup the old code a bit before
hooking to the provider.

I haven't attempted to remove any of the deprecated interfaces. That is better done along with Arne's patches. There will be only minor, if at all any, conflicts between that and this patch set.

Selva Nair (9):
A built-in provider for using external key with OpenSSL 3.0 Initialize the xkey provider and use it in SSL context Implement keymgmt in the xkey provider
Implement provider interface for signature operations Implement import of custom external keys
A helper function to load key for management-external-key Enable signing via provider for management-external-key Add a function to encode digests with PKCS1 DigestInfo wrapper Allow management client to announce pss padding support

 configure.ac                            |   11 +
 doc/man-sections/management-options.rst |    8 +-
 doc/management-notes.txt                |   15 +-
 src/openvpn/Makefile.am                 |    2 +
 src/openvpn/crypto_openssl.c            |   19 +
 src/openvpn/manage.h                    |    1 +
 src/openvpn/openssl_compat.h            |   12 +
 src/openvpn/options.c                   |    7 +-
 src/openvpn/ssl_openssl.c               |   17 +-
 src/openvpn/xkey_common.h               |  120 +++
 src/openvpn/xkey_helper.c               |  285 ++++++
 src/openvpn/xkey_provider.c             | 1158 +++++++++++++++++++++++
 12 files changed, 1647 insertions(+), 8 deletions(-)
 create mode 100644 src/openvpn/xkey_common.h
 create mode 100644 src/openvpn/xkey_helper.c
 create mode 100644 src/openvpn/xkey_provider.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/9 A built-in provider for using external key with OpenSSL 3.0

From: Selva Nair <[email protected]...>

Hooking into callbacks in RSA_METHOD and EVP_PKEY_METHOD structures is deprecated in OpenSSL 3.0. For signing with external keys that are not exportable (tokens, stores, etc.) requires a custom provider interface so that key operations are done under its context.

A single provider is enough for handling all external keys we support -- management-external-key, cryptoapicert(CNG) and pkcs11-helper. The series of patches starting with this implement such a provider.

To activate the use of the provider, essentially, the SSL_CTX object is created with property string set to to prioritize our provider. In the provider we implement only keymgmt and signature operations. All other operations get directly used from the default provider.

However, signature operations include verify using peer's public key as well. In particular, we get called for both DigestVerify and DigestSign operations. For the former we call back OpenSSL, for the latter we compute the digest using OpenSSL and then pass it to the backend for signature.

So a lot of glue code is needed and this makes the patches somewhat large even after splitting into many commits.

This patch implements only the provider_init function so that it can be loaded, but has no capabilities. The required interfaces are added in following commits.

---
 configure.ac                |  11 +++
 src/openvpn/Makefile.am     |   1 +
 src/openvpn/xkey_common.h   |  42 +++++++++
 src/openvpn/xkey_provider.c | 177 ++++++++++++++++++++++++++++++++++++
 4 files changed, 231 insertions(+)
 create mode 100644 src/openvpn/xkey_common.h
 create mode 100644 src/openvpn/xkey_provider.c
configure.ac +11 -0
index 7c2ead6a..0390a05e 100644
@@ -821,6 +821,17 @@ if test "${with_crypto_library}" = "openssl"; then
 		AC_DEFINE([HAVE_OPENSSL_ENGINE], [1], [OpenSSL engine support available])
 	fi
 
+	have_openssl_provider="yes"
+	AC_CHECK_FUNCS(
+		[OSSL_PROVIDER_load]
+		,
+		,
+		[have_openssl_provider="no"; break]
+	)
+	if test "${have_openssl_provider}" = "yes"; then
+		AC_DEFINE([HAVE_XKEY_PROVIDER], [1], [External key loading provider can be used])
+	fi
+
 	AC_CHECK_FUNC(
 		[EVP_aes_256_gcm],
 		,
src/openvpn/Makefile.am +1 -0
index 5883c291..432efe73 100644
@@ -128,6 +128,7 @@ openvpn_SOURCES = \
 	tls_crypt.c tls_crypt.h \
 	tun.c tun.h \
 	vlan.c vlan.h \
+	xkey_provider.c xkey_common.h \
 	win32.h win32.c \
 	win32-util.h win32-util.c \
 	cryptoapi.h cryptoapi.c
src/openvpn/xkey_common.h new file +42 -0

42 lines not on this page · raw message

src/openvpn/xkey_provider.c new file +177 -0

177 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/9 Initialize the xkey provider and use it in SSL context

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- The provider is loaded during crypto initialization and unloaded in uninit.
The SSL server and client context are created with properties indicating preference for this provider.

This could be made conditional on use of external keys, but it can't hurt if loaded and used otherwise too. Useful to get the code exercised at least for a period of testing.

As the provider is empty, no functionality gets delegated to it as yet. Verb 4 logs with enable-debug will just show the provider_init and teardown called.

---
 src/openvpn/crypto_openssl.c | 19 +++++++++++++++++++
 src/openvpn/openssl_compat.h | 12 ++++++++++++
 src/openvpn/ssl_openssl.c    |  7 +++++--
 3 files changed, 36 insertions(+), 2 deletions(-)
src/openvpn/crypto_openssl.c +19 -0
index 419265a5..5d7fa847 100644
@@ -42,6 +42,7 @@
 #include "crypto.h"
 #include "crypto_backend.h"
 #include "openssl_compat.h"
+#include "xkey_common.h"
 
 #include <openssl/conf.h>
 #include <openssl/des.h>
@@ -75,6 +76,8 @@ static bool engine_initialized = false; /* GLOBAL */
 
 static ENGINE *engine_persist = NULL;   /* GLOBAL */
 
+static void *xkey_prov;
+
 /* Try to load an engine in a shareable library */
 static ENGINE *
 try_load_engine(const char *engine)
@@ -161,6 +164,15 @@ crypto_init_lib(void)
     OPENSSL_config(NULL);
 #endif
 #endif /* _WIN32 */
+
+#ifdef HAVE_XKEY_PROVIDER
+     if (!xkey_prov)
+     {
+        OSSL_PROVIDER_add_builtin(NULL, "ovpn.xkey", xkey_provider_init);
+        xkey_prov = OSSL_PROVIDER_load(NULL, "ovpn.xkey");
+     }
+#endif
+
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
@@ -190,6 +202,13 @@ crypto_uninit_lib(void)
         engine_initialized = false;
     }
 #endif
+#ifdef HAVE_XKEY_PROVIDER
+    if (xkey_prov)
+    {
+        OSSL_PROVIDER_unload(xkey_prov);
+    }
+#endif
+    xkey_prov = NULL;
 }
 
 void
src/openvpn/openssl_compat.h +12 -0
index ce8e2b36..3dcdde4d 100644
@@ -718,4 +718,16 @@ SSL_CTX_set_max_proto_version(SSL_CTX *ctx, long tls_ver_max)
     return 1;
 }
 #endif /* if OPENSSL_VERSION_NUMBER < 0x10100000L && !defined(ENABLE_CRYPTO_WOLFSSL) */
+
+/** Mimics SSL_CTX_new_ex for OpenSSL < 3 */
+#if OPENSSL_VERSION_NUMBER < 0x30000000L
+static inline SSL_CTX *
+SSL_CTX_new_ex(void *libctx, const char *propq, const SSL_METHOD *method)
+{
+    (void) libctx;
+    (void) propq;
+    return SSL_CTX_new(method);
+}
+#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
+
 #endif /* OPENSSL_COMPAT_H_ */
src/openvpn/ssl_openssl.c +5 -2
index 241206fb..61256620 100644
@@ -45,6 +45,7 @@
 #include "ssl_common.h"
 #include "base64.h"
 #include "openssl_compat.h"
+#include "xkey_common.h"
 
 #ifdef ENABLE_CRYPTOAPI
 #include "cryptoapi.h"
@@ -109,7 +110,8 @@ tls_ctx_server_new(struct tls_root_ctx *ctx)
 {
     ASSERT(NULL != ctx);
 
-    ctx->ctx = SSL_CTX_new(SSLv23_server_method());
+    const char *propq = "?" XKEY_PROV_PROPS;
+    ctx->ctx = SSL_CTX_new_ex(NULL, propq, SSLv23_server_method());
 
     if (ctx->ctx == NULL)
     {
@@ -127,7 +129,8 @@ tls_ctx_client_new(struct tls_root_ctx *ctx)
 {
     ASSERT(NULL != ctx);
 
-    ctx->ctx = SSL_CTX_new(SSLv23_client_method());
+    const char *propq = "?" XKEY_PROV_PROPS;
+    ctx->ctx = SSL_CTX_new_ex(NULL, propq, SSLv23_client_method());
 
     if (ctx->ctx == NULL)
     {
Signature · 1 line
2.20.1
#3 lore raw

Re: [Openvpn-devel] [PATCH 2/9] Initialize the xkey provider and use it in SSL context

src/openvpn/crypto_openssl.c static bool engine_initialized = false; /* GLOBAL */ · line 80

Quoted 9 lines

#include <openssl/conf.h>
#include <openssl/des.h>

src/openvpn/crypto_openssl.c +2 -0
@@ -75,6 +76,8 @@ static bool engine_initialized = false; /* GLOBAL */
 
 static ENGINE *engine_persist = NULL;   /* GLOBAL */
 
+static void *xkey_prov;
+

This is under HAVE_OPENSSL_ENGINE instead have HAVE_XKEY_PROVIDER

Quoted 21 lines
src/openvpn/crypto_openssl.c +9 -0
 /* Try to load an engine in a shareable library */
 static ENGINE *
 try_load_engine(const char *engine)
@@ -161,6 +164,15 @@ crypto_init_lib(void)
     OPENSSL_config(NULL);
 #endif
 #endif /* _WIN32 */
+
+#ifdef HAVE_XKEY_PROVIDER
+     if (!xkey_prov)
+     {
+        OSSL_PROVIDER_add_builtin(NULL, "ovpn.xkey", xkey_provider_init);
+        xkey_prov = OSSL_PROVIDER_load(NULL, "ovpn.xkey");
+     }
+#endif
+
     /*
      * If you build the OpenSSL library and OpenVPN with
      * CRYPTO_MDEBUG, you will get a listing of OpenSSL
@@ -190,6 +202,13 @@ crypto_uninit_lib(void)
         engine_initialized = false;

src/openvpn/crypto_openssl.c crypto_uninit_lib(void) · line 212

Quoted 10 lines
src/openvpn/crypto_openssl.c +7 -0
     }
 #endif
+#ifdef HAVE_XKEY_PROVIDER
+    if (xkey_prov)
+    {
+        OSSL_PROVIDER_unload(xkey_prov);
+    }
+#endif
+    xkey_prov = NULL;
 }

The xkey_prov seems to be only used with HAVE_XKEY_PROVIDER, the last line should probably also be under it.

This is not a full review, just noting the compile errors trying to test the whole set.

Arne

#4 lore raw

PATCH 3/9 Implement keymgmt in the xkey provider

In reply to [email protected]...

From: Selva Nair <[email protected]...>

A minimal set of functions for keymgmt are implemented. No support for external key import as yet, only native keys. Support for native keys is required as all public key ops will get delegated to us once SSL_CTX is initialized in our context. This will include digest-verify using peer's public key.

To test key import, use the normal --key operation, change use_xkey_provider() to return true. The key will import correctly but signing will fail as there is no signature interface in the provider yet. (use enable-debug for the dmsg() chatter).

Sign-verify operations for native keys is in the next commit.

---
 src/openvpn/xkey_provider.c | 353 ++++++++++++++++++++++++++++++++++++
 1 file changed, 353 insertions(+)
src/openvpn/xkey_provider.c +353 -0
index 9d19d37d..4e5ed130 100644
@@ -43,6 +43,9 @@
 #include <openssl/evp.h>
 #include <openssl/err.h>
 
+/* propq set all on all ops we implement */
+static const char *const props = XKEY_PROV_PROPS;
+
 /* A descriptive name */
 static const char *provname = "OpenVPN External Key Provider";
 
@@ -53,6 +56,353 @@ typedef struct
     OSSL_LIB_CTX *libctx;  /* libctx of the core context in which we are running */
 } XKEY_PROVIDER_CTX;
 
+typedef enum
+{
+    ORIGIN_NONAME = 0,
+    OPENSSL_NATIVE, /* native key imported in */
+    EXTERNAL_KEY
+} XKEY_ORIGIN;
+
+/*
+ * XKEY_KEYDATA: Our keydata encapsulation:
+ * ---------------------------------------
+ * We keep an opaque handle provided by the backend for the loaded
+ * key. It's passed back to the backend for any operation on private
+ * keys --- in practice, sign() op only.
+ *
+ * We also keep the public key in the form of a native OpenSSL EVP_PKEY.
+ * This allows us to do all public ops by calling ops in the default provider.
+ */
+typedef struct
+{
+    /* opaque handle dependent on KEY_ORIGIN -- could be NULL */
+    void *handle;
+    /* associated public key as an openvpn native key */
+    EVP_PKEY *pubkey;
+    /* origin of key -- native or external */
+    XKEY_ORIGIN origin;
+    int refcount;                /* reference count */
+} XKEY_KEYDATA;
+
+#define KEYTYPE(key) ((key)->pubkey ? EVP_PKEY_get_id((key)->pubkey) : 0)
+#define KEYSIZE(key) ((key)->pubkey ? EVP_PKEY_get_size((key)->pubkey) : 0)
+
+/* keymgmt provider */
+
+/* keymgmt callbacks we implement */
+static OSSL_FUNC_keymgmt_new_fn keymgmt_new;
+static OSSL_FUNC_keymgmt_free_fn keymgmt_free;
+static OSSL_FUNC_keymgmt_load_fn keymgmt_load;
+static OSSL_FUNC_keymgmt_has_fn keymgmt_has;
+static OSSL_FUNC_keymgmt_match_fn keymgmt_match;
+static OSSL_FUNC_keymgmt_import_fn rsa_keymgmt_import;
+static OSSL_FUNC_keymgmt_import_fn ec_keymgmt_import;
+static OSSL_FUNC_keymgmt_import_types_fn keymgmt_types;
+static OSSL_FUNC_keymgmt_get_params_fn keymgmt_get_params;
+static OSSL_FUNC_keymgmt_gettable_params_fn keymgmt_gettable;
+static OSSL_FUNC_keymgmt_query_operation_name_fn rsa_keymgmt_name;
+static OSSL_FUNC_keymgmt_query_operation_name_fn ec_keymgmt_name;
+
+static XKEY_KEYDATA *
+keydata_new()
+{
+    dmsg(D_LOW, "In keydata_new");
+
+    XKEY_KEYDATA *key = calloc(sizeof(*key), 1);
+    if (!key)
+    {
+        msg(M_NONFATAL, "xkey_keydata_new: out of memory");
+    }
+
+    return key;
+}
+
+static void
+keydata_free(XKEY_KEYDATA *key)
+{
+    dmsg(D_LOW, "In keydata_free");
+
+    if (!key || --key->refcount <= 0) /* free when refcount goes to zero */
+    {
+        return;
+    }
+    if (key->pubkey)
+    {
+        EVP_PKEY_free(key->pubkey);
+    }
+    free(key);
+}
+
+static void *
+keymgmt_new(void *provctx)
+{
+    dmsg(D_LOW, "In keymgmt_new");
+
+    XKEY_KEYDATA *key = keydata_new();
+    return key;
+}
+
+static void *
+keymgmt_load(const void *reference, size_t reference_sz)
+{
+    dmsg(D_LOW, "In keymgmt_load");
+
+    return NULL;
+}
+
+/**
+ * Key import function
+ * When key operations like sign/verify are done in our context
+ * the key gets imported into us.
+ *
+ * For native keys we get called with standard OpenSSL params
+ * appropriate for the key. We just use it to create a native
+ * EVP_PKEY from params and assign to keydata->handle.
+ *
+ * Import of external keys -- to be implemented
+ */
+static int
+keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[], const char *name)
+{
+    dmsg(D_LOW, "In keymgmt_import");
+
+    XKEY_KEYDATA *key = keydata;
+
+    /* Our private key is immutable -- we import only if keydata is empty */
+
+    if (key->handle || key->pubkey
+        || !(selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
+    {
+        msg(M_WARN, "Error: keymgmt_import: keydata not empty or selection not allowed");
+        return 0;
+    }
+
+    /* create a native public key and assign it to key->pubkey */
+    EVP_PKEY *pkey = NULL;
+
+    EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, name, NULL);
+    if (!ctx
+        || (EVP_PKEY_fromdata_init(ctx) != 1)
+        || (EVP_PKEY_fromdata(ctx, &pkey, selection, (OSSL_PARAM*) params) !=1))
+    {
+        msg(M_WARN, "Error: keymgmt_import failed for key type <%s>", name);
+        if (pkey)
+        {
+            EVP_PKEY_free(pkey);
+        }
+        return 0;
+    }
+
+    key->pubkey = pkey;
+    key->origin = OPENSSL_NATIVE;
+    if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
+    {
+        /* just use the same key as handle -- do not up-ref */
+        key->handle = pkey;
+    }
+    return 1;
+}
+
+static int
+rsa_keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[])
+{
+    dmsg(D_LOW, "In rsa_keymgmt_import");
+    return keymgmt_import(keydata, selection, params, "RSA");
+}
+
+static int
+ec_keymgmt_import(void *keydata, int selection, const OSSL_PARAM params[])
+{
+    dmsg(D_LOW, "In ec_keymgmt_import");
+    return keymgmt_import(keydata, selection, params, "EC");
+}
+
+/* This function has to exist for key import to work
+ * though we do not support import of individual params
+ * like n or e. We simply return an empty list here for
+ * both rsa and ec, which works.
+ */
+static const OSSL_PARAM *
+keymgmt_import_types(int selection)
+{
+    dmsg(D_LOW, "In keymgmt import types");
+
+    static const OSSL_PARAM key_types[] = {
+        OSSL_PARAM_END
+    };
+
+    if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
+    {
+       return key_types;
+    }
+    return NULL;
+}
+
+/* We do not support any key export */
+
+static void
+keymgmt_free(void *keydata)
+{
+    dmsg(D_LOW, "In keymgmt_free");
+    keydata_free(keydata);
+}
+
+static int
+keymgmt_has(const void *keydata, int selection)
+{
+    dmsg(D_LOW, "In keymgmt_has with selection = %d", selection);
+
+    const XKEY_KEYDATA *key = keydata;
+    int ok = (key != NULL);
+
+    if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
+    {
+        ok = ok && key->pubkey;
+    }
+    if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
+    {
+        ok = ok && key->handle;
+    }
+
+    return ok;
+}
+
+static int
+keymgmt_match(const void *keydata1, const void *keydata2, int selection)
+{
+    const XKEY_KEYDATA *key1 = keydata1;
+    const XKEY_KEYDATA *key2 = keydata2;
+
+    dmsg(D_LOW, "In keymgmt_match");
+    int ret = 1;
+
+    if (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY
+        && key1->pubkey && key2->pubkey)
+    {
+        ret = ret && EVP_PKEY_eq(key1->pubkey, key2->pubkey);
+    }
+
+    if (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
+    {
+        ret = ret && (key1->origin == key2->origin); /* origins must match */
+        if (key1->origin == OPENSSL_NATIVE)
+        {
+            ret = ret && EVP_PKEY_eq(key1->pubkey, key2->pubkey);
+        }
+        /* private key is opaque. We can only check identity of the handle */
+        else
+        {
+            ret = ret && (key1->handle == key2->handle);
+        }
+    }
+
+    return ret;
+}
+
+/* A minimal set of key params that we can return */
+static const OSSL_PARAM *
+keymgmt_gettable_params(void *provctx)
+{
+    dmsg(D_LOW, "In keymgmt_gettable");
+    static OSSL_PARAM gettable[] = {
+        OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
+        OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
+        OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
+        OSSL_PARAM_END
+    };
+    return gettable;
+}
+
+static int
+keymgmt_get_params(void *keydata, OSSL_PARAM *params)
+{
+    dmsg(D_LOW, "In keymgmt_get_params");
+
+    XKEY_KEYDATA *key = keydata;
+    if (!key || !key->pubkey)
+    {
+        return 0;
+    }
+
+    return EVP_PKEY_get_params(key->pubkey, params);
+}
+
+/**
+ * If the key is an encapsulated native key, we just call
+ * EVP_PKEY_set_params in the default context. Only those params
+ * supported by the default provider would work in that case.
+ */
+static int
+keymgmt_set_params(void *keydata, const OSSL_PARAM *params)
+{
+    XKEY_KEYDATA *key = keydata;
+    ASSERT(key);
+
+    if (key->origin != OPENSSL_NATIVE)
+    {
+        return 0; /* to be implemented */
+    }
+    else if (key->handle == NULL) /* once handle is set our key is immutable */
+    {
+        /* pubkey is always native -- just delegate */
+        return EVP_PKEY_set_params(key->pubkey, (OSSL_PARAM *)params);
+    }
+    else
+    {
+        msg(M_WARN, "xkey keymgmt_set_params: key is immutable");
+    }
+    return 1;
+}
+
+static const char *
+rsa_keymgmt_name(int id)
+{
+    dmsg(D_LOW, "In rsa_keymgmt_name");
+    return "RSA";
+}
+
+static const char *
+ec_keymgmt_name(int id)
+{
+    dmsg(D_LOW, "In ec_keymgmt_name");
+    return "EC";
+}
+
+static const OSSL_DISPATCH rsa_keymgmt_functions[] = {
+    {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
+    {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
+    {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
+    {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
+    {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
+    {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_keymgmt_import},
+    {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
+    {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void)) keymgmt_gettable_params},
+    {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void)) keymgmt_get_params},
+    {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void (*)(void))rsa_keymgmt_name},
+    {0, NULL }
+};
+
+static const OSSL_DISPATCH ec_keymgmt_functions[] = {
+    {OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))keymgmt_new},
+    {OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))keymgmt_free},
+    {OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))keymgmt_load},
+    {OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))keymgmt_has},
+    {OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))keymgmt_match},
+    {OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_keymgmt_import},
+    {OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))keymgmt_import_types},
+    {OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void)) keymgmt_gettable_params},
+    {OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void)) keymgmt_get_params},
+    {OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME, (void (*)(void))ec_keymgmt_name},
+    {0, NULL }
+};
+
+const OSSL_ALGORITHM keymgmts[] = {
+    {"RSA:rsaEncryption", props, rsa_keymgmt_functions},
+    {"RSA-PSS:RSASSA-PSS", props, rsa_keymgmt_functions},
+    {"EC:id-ecPublicKey", props, ec_keymgmt_functions},
+    {NULL, NULL, NULL}
+};
+
 /* main provider interface */
 
 /* provider callbacks we implement */
@@ -74,6 +424,9 @@ query_operation(void *provctx, int op, int *no_store)
             return NULL;
 
         case OSSL_OP_KEYMGMT:
+            return keymgmts;
+
+        case OSSL_OP_STORE:
             return NULL;
 
         default:
Signature · 1 line
2.20.1
author
#5 lore raw

Re: [Openvpn-devel] [PATCH 3/9] Implement keymgmt in the xkey provider

Hi,

hmm.. there is some cruft in the commit message referring to a non-existent function, in case anyone reads it. The code itself is fine.

Quoted 9 lines · On Wed, Sep 22, 2021 at 5:13 PM <[email protected]...> wrote:

From: Selva Nair <[email protected]...>

A minimal set of functions for keymgmt are implemented. No support for external key import as yet, only native keys. Support for native keys is required as all public key ops will get delegated to us once SSL_CTX is initialized in our context. This will include digest-verify using peer's public key.

Quoted 3 lines

To test key import, use the normal --key operation, change use_xkey_provider() to return true. The key

There is no use_xkey_provider() in the submitted version.

Just use --cert and --key to test and exercise the code path.

Quoted 4 lines

will import correctly but signing will fail as there is no signature interface in the provider yet. (use enable-debug for the dmsg() chatter).

Selva

#6 lore raw

PATCH 4/9 Implement provider interface for signature operations

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- DigestVerify and Sign operations for native keys are implemented.

DigestVerify ops for native keys are needed because operations on peer's public key will get delegated to us.

Sign operations on native keys are also implemented which now allows us to enable the provider in SSL_CTX with private key in a file (for testing).

To test use --key and --cert in the config. Provider function calls are logged at verb = 4 if configured with --enable-debug.

As external keys are currently supported by hooks into RSA or EVP_PKEY interface, it will continue work bypassing the provider. Even in those cases ops with peer's public key are done by our provider as those are initialized by OpenSSL using the provider interface.

Subsequent commits will move all these into the provider framework.

---
 src/openvpn/xkey_common.h   |  36 +++
 src/openvpn/xkey_provider.c | 486 +++++++++++++++++++++++++++++++++++-
 2 files changed, 521 insertions(+), 1 deletion(-)
src/openvpn/xkey_common.h +36 -0
index eb31604f..0cb76db1 100644
@@ -39,4 +39,40 @@ OSSL_provider_init_fn xkey_provider_init;
 
 #define XKEY_PROV_PROPS "provider=ovpn.xkey"
 
+/**
+ * Stuct to encapsulate signature algorithm parameters to pass
+ * to sign operation.
+ */
+typedef struct {
+   const char *padmode; /* "pkcs1", "pss" or "none" */
+   const char *mdname;  /* "SHA256" or "SHA2-256" etc. */
+   const char *saltlen; /* "digest", "auto" or "max" */
+   const char *keytype; /* "EC" or "RSA" */
+} XKEY_SIGALG;
+
+/**
+ * Callback for sign operation -- must be implemented for each backend and
+ * is used in xkey_signature_sign(), or set when loading the key.
+ * (custom key loading not yet implemented).
+ *
+ * @param handle opaque key handle provided by the backend -- could be null
+ *               or unused for management interface.
+ * @param sig    On return caller should fill this with the signature
+ * @param siglen On entry *siglen has max size of sig and on return must be
+ *               set to the actual size of the signature
+ * @param tbs    buffer to sign
+ * @param tbslen size of data in tbs buffer
+ * @sigalg       contains the signature algorithm parameters
+ *
+ * @returns 1 on success, 0 on error.
+ *
+ * The data in tbs is just the digest with no DigestInfo header added. This is
+ * unlike the deprecated RSA_sign callback which provides encoded digest.
+ * For RSA_PKCS1 signatures, the external signing function must encode the digest
+ * before signing. The digest algorithm used is passed in the sigalg structure.
+ */
+typedef int (XKEY_EXTERNAL_SIGN_fn)(void *handle, unsigned char *sig, size_t *siglen,
+                                 const unsigned char *tbs, size_t tbslen,
+                                 XKEY_SIGALG sigalg);
+
 #endif /* XKEY_PUBLIC_H_ */
src/openvpn/xkey_provider.c +485 -1

504 lines not on this page · raw message

Signature · 1 line
2.20.1
#7 lore raw

PATCH 5/9 Implement import of custom external keys

In reply to [email protected]...

From: Selva Nair <[email protected]...>

Our key object retains info about the external
key as an opaque handle to the backend. We also
need the public key as an EVP_PKEY *.

For native keys we use OpenSSL API to import data into the key. In fact the 'handle' in that case is the OpenSSL EVP_PKEY object itself.

For importing custom keys, we need to define custom parameters describing the key using OSSL_PARAM structure. We define 4 required and 1 optional parameters for loading the key:

Required params:

{.key="origin", .data_type = OSSL_PARAM_UTF8_STRING
.data = "foobar", .data_size = 0 }

Note: data_size = 0 refer to NUL terminated string in OpenSSL. This parameter is only used to identify that the key as non-native with an opaque handle. We really do not check the content of the string. Should not be NULL.

{.key="handle", .data_type = OSSL_PARAM_OCTET_PTR,
.data = &handle, .data_size = sizeof(handle)}

{.key="pubkey", .data_type = OSSL_PARAM_OCTET_STRING,
.data = &pubkey, .data_size = sizeof(pubkey)}

{.key="sign_op", .data_type = OSSL_PARAM_OCTET_PTR,
.data = &sign_op_ptr, .data_size = sizeof(sign_op_ptr)}

Optional params:

{.key="free_op", .data_type = OSSL_PARAM_OCTET_PTR,
.data = &free_op_ptr, .data_size = sizeof(free_op_ptr)}

The 'handle' is opaque to us and is retained. The caller should not free it. We will free it when no longer required by calling 'free_op()', if provided. The 'handle' should not be null as that indicates missing private key.

The 'pubkey' must be an 'EVP_PKEY *' variable, and is duplicated by us. The caller may free it when not required.

The 'sign_op' and 'free_op' function pointers should be of type 'XKEY_EXTERNAL_SIGN_fn' and 'XKEY_PRIVKEY_FREE_fn' defined in xkey_common.h

For example, for management-external-key, we really do not need any 'handle'. Pass anything that will live long and won't dereference to NULL. We do not use it for any other purpose. Pointer to a const string would be a good choice. In this case, the 'free_op' must be NULL or some harmless function. NULL is the safest choice.

The way of passing pointers via 'OSSL_PARAM' is somewhat fragile, as everything is cast into 'void *' and relies on abiding to the contract. For 'pubkey' we could pass it as a der encoded octet string, for example, and instead of function pointers we can just hard code the callbacks into our implementation. After all this is a private built-in provider.

However, no external data is involved, so the only possible bad actor is the developer herself.

A helper function to load the management key is in the next commit.

---
 src/openvpn/xkey_common.h   |   7 ++
 src/openvpn/xkey_provider.c | 166 +++++++++++++++++++++++++++++++++---
 2 files changed, 162 insertions(+), 11 deletions(-)
src/openvpn/xkey_common.h +7 -0
index 0cb76db1..466b2b8d 100644
@@ -75,4 +75,11 @@ typedef int (XKEY_EXTERNAL_SIGN_fn)(void *handle, unsigned char *sig, size_t *si
                                  const unsigned char *tbs, size_t tbslen,
                                  XKEY_SIGALG sigalg);
 
+/**
+ * Signature of private key free function callback used
+ * to free the opaque private key handle obtained from the
+ * backend. Not required for management-external-key.
+ */
+typedef void (XKEY_PRIVKEY_FREE_fn)(void *handle);
+
 #endif /* XKEY_PUBLIC_H_ */
src/openvpn/xkey_provider.c +155 -11

236 lines not on this page · raw message

Signature · 1 line
2.20.1
#8 lore raw

PATCH 6/9 A helper function to load key for management-external-key

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- A wrapper around the keymgmt import of xkey provider

- When the provider is available, use this to set SSL_CTX_use_PrivateKey for management-external-key

sign_op is not implemented yet. This will error out while signing with --management-external-key. The next commit fixes that.

---
 src/openvpn/Makefile.am   |  1 +
 src/openvpn/ssl_openssl.c | 10 ++++
 src/openvpn/xkey_common.h | 11 +++++
 src/openvpn/xkey_helper.c | 96 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+)
 create mode 100644 src/openvpn/xkey_helper.c
src/openvpn/Makefile.am +1 -0
index 432efe73..0331298b 100644
@@ -129,6 +129,7 @@ openvpn_SOURCES = \
 	tun.c tun.h \
 	vlan.c vlan.h \
 	xkey_provider.c xkey_common.h \
+	xkey_helper.c \
 	win32.h win32.c \
 	win32-util.h win32-util.c \
 	cryptoapi.h cryptoapi.c
src/openvpn/ssl_openssl.c +10 -0
index 61256620..b9453653 100644
@@ -1450,6 +1450,14 @@ tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
     EVP_PKEY *pkey = X509_get0_pubkey(cert);
     ASSERT(pkey); /* NULL before SSL_CTX_use_certificate() is called */
 
+#ifdef HAVE_XKEY_PROVIDER
+    EVP_PKEY *privkey = xkey_load_management_key(NULL, pkey);
+    if (!privkey
+        || !SSL_CTX_use_PrivateKey(ctx->ctx, privkey))
+    {
+        goto cleanup;
+    }
+#else
     if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
     {
         if (!tls_ctx_use_external_rsa_key(ctx, pkey))
@@ -1478,6 +1486,8 @@ tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
     }
 #endif /* OPENSSL_VERSION_NUMBER > 1.1.0 dev && !defined(OPENSSL_NO_EC) */
 
+#endif /* HAVE_XKEY_PROVIDER */
+
     ret = 0;
 cleanup:
     if (ret)
src/openvpn/xkey_common.h +11 -0
index 466b2b8d..751f18a0 100644
@@ -82,4 +82,15 @@ typedef int (XKEY_EXTERNAL_SIGN_fn)(void *handle, unsigned char *sig, size_t *si
  */
 typedef void (XKEY_PRIVKEY_FREE_fn)(void *handle);
 
+/**
+ * Generate an encapsulated EVP_PKEY for management-external-key
+ *
+ * @param libctx library context in which xkey provider has been loaded
+ * @param pubkey corresponding pubkey in the default provider's context
+ *
+ * @returns a new EVP_PKEY in the provider's keymgmt context.
+ * The pubkey is up-refd if retained -- the caller can free it after return
+ */
+EVP_PKEY *xkey_load_management_key(OSSL_LIB_CTX *libctx, EVP_PKEY *pubkey);
+
 #endif /* XKEY_PUBLIC_H_ */
src/openvpn/xkey_helper.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.20.1
#9 lore raw

PATCH 7/9 Enable signing via provider for management-external-key

In reply to [email protected]...

From: Selva Nair <[email protected]...>

- Add a function to set as sign_op during key import. The function passes the signature request to management interface, and returns the result to the provider.

---
 src/openvpn/xkey_common.h |  4 +++
 src/openvpn/xkey_helper.c | 68 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 69 insertions(+), 3 deletions(-)
src/openvpn/xkey_common.h +4 -0
index 751f18a0..6a00c382 100644
@@ -35,6 +35,10 @@
  */
 OSSL_provider_init_fn xkey_provider_init;
 
+#else
+
+#define OSSL_LIB_CTX void
+
 #endif /* HAVE_XKEY_PROVIDER */
 
 #define XKEY_PROV_PROPS "provider=ovpn.xkey"
src/openvpn/xkey_helper.c +65 -3

84 lines not on this page · raw message

Signature · 1 line
2.20.1
#10 lore raw

PATCH 8/9 Add a function to encode digests with PKCS1 DigestInfo wrapper

In reply to [email protected]...

From: Selva Nair <[email protected]...>

The EVP_PKEY interface as well as provider provides the raw digest to the sign() function. In case of RSA_PKCS1, our management interface expects expects an encoded hash, which has the DigestInfo header added as per PKCSv1.5 specs, unless the hash algorithm is legacy MD5_SHA1.

Fix this by
- add a function to perform the pkcs1 encoding before passing the data to sign to the management interface. The implementation is not pretty, but should work.
(Unfortunately OpenSSL does not expose a function for this).

Note:
1. cryptoki interface used by pkcs11-helper also requires this to be done before calling the Sign op. This will come handy there too.
2. We have a similar function in ssl_mbedtls.c but its not prettier, and require porting.

---
 src/openvpn/xkey_common.h |  20 ++++++
 src/openvpn/xkey_helper.c | 127 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)
src/openvpn/xkey_common.h +20 -0
index 6a00c382..e92d4868 100644
@@ -97,4 +97,24 @@ typedef void (XKEY_PRIVKEY_FREE_fn)(void *handle);
  */
 EVP_PKEY *xkey_load_management_key(OSSL_LIB_CTX *libctx, EVP_PKEY *pubkey);
 
+/**
+ * Add PKCS1 DigestInfo to tbs and return the result in *enc.
+ *
+ * @param enc           pointer to output buffer
+ * @param enc_len       capacity in bytes of output buffer
+ * @param mdname        name of the hash algorithm (SHA256, SHA1 etc.)
+ * @param tbs           pointer to digest to be encoded
+ * @param tbslen        length of data in bytes
+ *
+ * @return              false on error, true  on success
+ *
+ * On return enc_len is  set to actual size of the result.
+ * enc is NULL or enc_len is not enough to store the result, it is set
+ * to the required size and false is returned.
+ *
+ */
+bool
+encode_pkcs1(unsigned char **enc, size_t *enc_len, const char *mdname,
+             const unsigned char *tbs, size_t tbslen);
+
 #endif /* XKEY_PUBLIC_H_ */
src/openvpn/xkey_helper.c +127 -0

137 lines not on this page · raw message

Signature · 1 line
2.20.1
#11 lore raw

PATCH 9/9 Allow management client to announce pss padding support

In reply to [email protected]...

From: Selva Nair <[email protected]...>

pk-sig request from management can currently indicate support for 'nopadding' or 'pkcs1i' signatures. Add 'pss' as an option to indicate that PSS signing requests are accepted.

To match, extend the algorithm string in PK_SIGN request to include the following format:

- RSA_PKCS1_PSS_PADDING,hashlag=name,saltlen=[max|digest|auto]

Here 'name' is the short common name of the hash algorithm. E.g., SHA1, SHA256 etc.

---
 doc/man-sections/management-options.rst |  8 +++++++-
 doc/management-notes.txt                | 15 +++++++++++----
 src/openvpn/manage.h                    |  1 +
 src/openvpn/options.c                   |  7 ++++++-
 4 files changed, 25 insertions(+), 6 deletions(-)
doc/man-sections/management-options.rst +7 -1
index de0d47e7..b173a1ea 100644
@@ -90,9 +90,15 @@ server and client mode operations.
      management-external-key
      management-external-key nopadding
      management-external-key pkcs1
+     management-external-key pss
+
+  or any combination like:
+  ::
+
      management-external-key nopadding pkcs1
+     management-external-key pkcs1 pss
 
-  The optional parameters :code:`nopadding` and :code:`pkcs1` signal
+  The optional parameters :code:`nopadding` :code:`pkcs1` and :code:`pss` signal
   support for different padding algorithms. See
   :code:`doc/mangement-notes.txt` for a complete description of this
   feature.
doc/management-notes.txt +11 -4
index 84e3d04b..27163239 100644
@@ -1019,10 +1019,17 @@ can be indicated in the signing request only if the client version is > 2"
 
 The currently defined padding algorithms are:
 
- - RSA_PKCS1_PADDING  -  PKCS1 padding and RSA signature
- - RSA_NO_PADDING     -  No padding may be added for the signature
- - ECDSA              -  EC signature.
-
+ - RSA_PKCS1_PADDING            -  PKCS1 padding and RSA signature
+ - RSA_NO_PADDING               -  No padding may be added for the signature
+ - ECDSA                        -  EC signature.
+ - RSA_PKCS1_PSS_PADDING,params -  RSA signature with PSS padding
+
+   params for PSS are specified as 'digest=name,saltlen=[max|digest|auto]'.
+   The digest names are short common names such as SHA256, SHA224, etc.
+   In the case of PKCS1, when the hash algorithm is not the legacy MD5-SHA1,
+   the digest is encoded with DigestInfo header before presening to the
+   management. This is identical to CKM_RSA_PKCS in cryptoki as well as
+   what RSA_sign() provides.
 
 COMMAND -- certificate (OpenVPN 2.4 or higher)
 ----------------------------------------------
src/openvpn/manage.h +1 -0
index 04dc98d1..5ed27c0c 100644
@@ -339,6 +339,7 @@ struct management *management_init(void);
 #define MF_QUERY_REMOTE             (1<<13)
 #define MF_QUERY_PROXY              (1<<14)
 #define MF_EXTERNAL_CERT            (1<<15)
+#define MF_EXTERNAL_KEY_PSSPAD      (1<<16)
 
 bool management_open(struct management *man,
                      const char *addr,
src/openvpn/options.c +6 -1
index 26305a90..6e71563f 100644
@@ -2213,7 +2213,8 @@ options_postprocess_verify_ce(const struct options *options,
 #if  defined(ENABLE_MANAGEMENT)
     if ((tls_version_max() >= TLS_VER_1_3)
         && (options->management_flags & MF_EXTERNAL_KEY)
-        && !(options->management_flags & (MF_EXTERNAL_KEY_NOPADDING))
+        && !(options->management_flags & (MF_EXTERNAL_KEY_NOPADDING)
+             || options->management_flags & (MF_EXTERNAL_KEY_PSSPAD))
         )
     {
         msg(M_ERR, "management-external-key with OpenSSL 1.1.1 requires "
@@ -5511,6 +5512,10 @@ add_option(struct options *options,
             {
                 options->management_flags |= MF_EXTERNAL_KEY_PKCS1PAD;
             }
+            else if (streq(p[j], "pss"))
+            {
+                options->management_flags |= MF_EXTERNAL_KEY_PSSPAD;
+            }
             else
             {
                 msg(msglevel, "Unknown management-external-key flag: %s", p[j]);
Signature · 1 line
2.20.1
#12 lore raw
In reply to [email protected]...

Am 22.09.21 um 23:12 schrieb [email protected]...:

Quoted 19 lines

From: Selva Nair <[email protected]...>

The following series of patches implement a built-in provider for interfacing OpenSSL 3.0 when external keys are in use.

Essentially, to intercept the sign operation, the SSL_CTX object has to be created with properties string set to prioritize our provider. In the provider we implement only keymgmt and signature operations and specify the property string as optional. That allows all operations we do not provide to be used from the default provider.

This patch set stops at interfacing the provider with management-external-key. For pkcs11-helper, only some glue code is needed and is in the works. Same with cryptoapicert
aka CNG, but I want to cleanup the old code a bit before
hooking to the provider.

Quoted 5 lines

I haven't attempted to remove any of the deprecated interfaces. That is better done along with Arne's patches. There will be only minor, if at all any, conflicts between that and this patch set.

Great work and also extremely quick on the implementation. This currently puts myself in a bit of dilemma. We (=OpenVPN Inc) need a similar/same implementation in OpenVPN3 too. I think after I do the review I am probably too biased to finish my own implementation without running into the problem of it being derivative.

Your code is currently GPL2 only and not BSD like or you explicitly agreeing to have it under the CLA of openvpn3 library. This is completely fine. I am not arguing it should be different.

So I have two options here:

a) I finish my own implementation of the provider for OpenVPN3 to not be influenced by this implementation and review this implementation after that

b) you agree that I can take parts of your code for the OpenVPN3 implementation, then I go directly into review and then base my OpenVPN3 implementation on your xkey provider implementation.

Arne

#13 lore raw
Quoted 9 lines

So I have two options here:

a) I finish my own implementation of the provider for OpenVPN3 to not be influenced by this implementation and review this implementation after that

b) you agree that I can take parts of your code for the OpenVPN3 implementation, then I go directly into review and then base my OpenVPN3 implementation on your xkey provider implementation.

Just to clarify. Both ways are fine with me.

Arne

author
#14 lore raw
In reply to Arne Schwabe

Hi Arne,

Quoted 9 lines

So I have two options here:

a) I finish my own implementation of the provider for OpenVPN3 to not be influenced by this implementation and review this implementation after that

b) you agree that I can take parts of your code for the OpenVPN3 implementation, then I go directly into review and then base my OpenVPN3 implementation on your xkey provider implementation.

(b) is fine with me and preferred. I do not want to release it under BSD, but can agree to a CLA. I wrote it consulting OpenSSL sources (though no copy paste) which is not GPL2, so not even sure what license is most appropriate. But BSD isi not my thing.

If there is a specific wording required, I can add that to the xkey_provider.c file which contains the implementation. Or make a separate copy for you to pick for OpenVPN3 The rest is generic interfacing with OpenVPN.

Thanks,

Selva

Quoted 2 lines

Arne

#15 lore raw

Am 23.09.21 um 16:02 schrieb Selva Nair:

Quoted 15 lines

Hi Arne,

So I have two options here:

a) I finish my own implementation of the provider for OpenVPN3 to not be influenced by this implementation and review this implementation after that

b) you agree that I can take parts of your code for the OpenVPN3 implementation, then I go directly into review and then base my OpenVPN3 implementation on your xkey provider implementation.

Quoted 4 lines

(b) is fine with me and preferred. I do not want to release it under BSD, but can agree to  a CLA. I wrote it consulting OpenSSL sources (though no copy paste) which is not GPL2, so not even sure what license is most appropriate. But BSD isi not my thing.

I think a statement from you that you agree to
https://github.com/OpenVPN/openvpn3/blob/master/CLA.rst
for this patchset would be good and I think also enough.

Arne

author
#16 lore raw

Hi

Quoted 18 lines · Arne Schwabe

Am 23.09.21 um 16:02 schrieb Selva Nair:

Quoted 7 lines

Hi Arne,

So I have two options here:

a) I finish my own implementation of the provider for OpenVPN3 to

not be

Quoted 5 lines

influenced by this implementation and review this implementation after that

b) you agree that I can take parts of your code for the OpenVPN3 implementation, then I go directly into review and then base my

OpenVPN3

Quoted 3 lines

implementation on your xkey provider implementation.

Quoted 8 lines
Quoted 4 lines

(b) is fine with me and preferred. I do not want to release it under BSD, but can agree to a CLA. I wrote it consulting OpenSSL sources (though no copy paste) which is not GPL2, so not even sure what license is most appropriate. But BSD isi not my thing.

I think a statement from you that you agree to
https://github.com/OpenVPN/openvpn3/blob/master/CLA.rst
for this patchset would be good and I think also enough.

Okay, I will send you an email agreeing to that.

I'll also change the license header from "version 2" to "either version 2 of the License, or (at your option) any later version." following the FSF's wording for allowing any later version of GPL.

Selva

Quoted 2 lines

Arne

#17 lore raw
In reply to [email protected]...

Am 22.09.21 um 23:12 schrieb [email protected]...:

Quoted 13 lines

From: Selva Nair <[email protected]...>

The following series of patches implement a built-in provider for interfacing OpenSSL 3.0 when external keys are in use.

Essentially, to intercept the sign operation, the SSL_CTX object has to be created with properties string set to prioritize our provider. In the provider we implement only keymgmt and signature operations and specify the property string as optional. That allows all operations we do not provide to be used from the default provider.

Quoted 5 lines

This patch set stops at interfacing the provider with management-external-key. For pkcs11-helper, only some glue code is needed and is in the works. Same with cryptoapicert
aka CNG, but I want to cleanup the old code a bit before
hooking to the provider.

I did a quick test with my Android client to see if it works and RSA keys look good so far. I am getting a request like:

NC9t8IkYrjAQcCzc85zN0H5TvwfAUDwYkR4j2ga6fGw=,RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest

from the management interface. But I haven't found the right Signature method from java yet to actually sign it correctly:

sig = Signature.getInstance(SHA256withRSA/PSS); sig.setParameter(new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1)); sig.initSign(privkey);
sig.update(data);
signed_bytes = sig.sign();

is what I expected to be the correct signature but the server complains with OpenSSL: error:0407E068:rsa routines:RSA_verify_PKCS1_PSS_mgf1:bad signature

I will have to figure out where this goes wrong.

With an EC key somewhere in that stack, EC/RSA gets confuse as there is rsa_keymgmt_import/rsa_keymgmt_name in the stack and then later ec_keymgmt_name. I haven't digged into that as it is getting late here.

2021-09-23 22:19:56 TLS: Initial packet from
[AF_INET]192.168.188.61:1194, sid=7c606dcc fe241304
2021-09-23 22:19:56 In xkey provider query op with op = 4
2021-09-23 22:19:56 In xkey provider query op with op = 3
2021-09-23 22:19:56 In xkey provider query op with op = 10
2021-09-23 22:19:56 In xkey provider query op with op = 21
2021-09-23 22:19:56 VERIFY OK: depth=0, CN=dionysos
2021-09-23 22:19:56 In keymgmt_new
2021-09-23 22:19:56 In keydata_new
2021-09-23 22:19:56 In rsa_keymgmt_import
2021-09-23 22:19:56 In keymgmt_import
2021-09-23 22:19:56 In rsa_keymgmt_name
2021-09-23 22:19:56 In xkey signature_newctx
2021-09-23 22:19:56 In xkey digest_verify init with mdname <SHA2-256>
2021-09-23 22:19:56 In xkey digest_init_helper with mdname = <SHA2-256>
2021-09-23 22:19:56 In xkey signature_settable_ctx_params
2021-09-23 22:19:56 In signature_set_ctx_params
2021-09-23 22:19:56 xkey_sign_parameters: setting padmode to <pss>
2021-09-23 22:19:56 In xkey signature_settable_ctx_params
2021-09-23 22:19:56 In signature_set_ctx_params
2021-09-23 22:19:56 xkey_sign_parameters: setting saltlen to digest
2021-09-23 22:19:56 In xkey digest_verify
2021-09-23 22:19:56 In xkey signature_freectx
2021-09-23 22:19:56 In ec_keymgmt_name
2021-09-23 22:19:56 In xkey provider query op with op = 12
2021-09-23 22:19:56 In ec_keymgmt_name
2021-09-23 22:19:56 In xkey provider query op with op = 12
2021-09-23 22:19:56 In ec_keymgmt_name
2021-09-23 22:19:56 In xkey provider query op with op = 12

RSA for comparison:

2021-09-23 22:17:40 TLS: Initial packet from
[AF_INET]192.168.188.61:1194, sid=0e4a91a6 67f591d2
2021-09-23 22:17:40 In xkey provider query op with op = 4
2021-09-23 22:17:40 In xkey provider query op with op = 3
2021-09-23 22:17:40 In xkey provider query op with op = 10
2021-09-23 22:17:40 In xkey provider query op with op = 21
2021-09-23 22:17:40 VERIFY OK: depth=0, CN=dionysos
2021-09-23 22:17:40 In keymgmt_new
2021-09-23 22:17:40 In keydata_new
2021-09-23 22:17:40 In rsa_keymgmt_import
2021-09-23 22:17:40 In keymgmt_import
2021-09-23 22:17:40 In rsa_keymgmt_name
2021-09-23 22:17:40 In xkey signature_newctx
2021-09-23 22:17:40 In xkey digest_verify init with mdname <SHA2-256>
2021-09-23 22:17:40 In xkey digest_init_helper with mdname = <SHA2-256>
2021-09-23 22:17:40 In xkey signature_settable_ctx_params
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 xkey_sign_parameters: setting padmode to <pss>
2021-09-23 22:17:40 In xkey signature_settable_ctx_params
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 xkey_sign_parameters: setting saltlen to digest
2021-09-23 22:17:40 In xkey digest_verify
2021-09-23 22:17:40 In xkey signature_freectx
2021-09-23 22:17:40 In rsa_keymgmt_name
2021-09-23 22:17:40 In xkey signature_newctx
2021-09-23 22:17:40 In xkey digest_sign_init with mdname = SHA256>
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 In xkey signature_freectx
2021-09-23 22:17:40 In rsa_keymgmt_name
2021-09-23 22:17:40 In xkey signature_newctx
2021-09-23 22:17:40 In xkey digest_sign_init with mdname = SHA2-256>
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 In xkey signature_settable_ctx_params
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 xkey_sign_parameters: setting padmode to <pss>
2021-09-23 22:17:40 In xkey signature_settable_ctx_params
2021-09-23 22:17:40 In signature_set_ctx_params
2021-09-23 22:17:40 xkey_sign_parameters: setting saltlen to digest
2021-09-23 22:17:40 In xkey digest_sign
2021-09-23 22:17:40 In xkey digest_sign
2021-09-23 22:17:40 In xkey signature_sign with siglen = 256
2021-09-23 22:17:40 P:
2021-09-23 22:17:40 xkey management_sign: requesting sig with algorithm
<RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest>
2021-09-23 22:17:40 MANAGEMENT: CMD 'pk-sig'
2021-09-23 22:17:40 In xkey signature_freectx
author
#18 lore raw
Quoted 29 lines · Arne Schwabe

Am 22.09.21 um 23:12 schrieb [email protected]...:

Quoted 13 lines

From: Selva Nair <[email protected]...>

The following series of patches implement a built-in provider for interfacing OpenSSL 3.0 when external keys are in use.

Essentially, to intercept the sign operation, the SSL_CTX object has to be created with properties string set to prioritize our provider. In the provider we implement only keymgmt and signature operations and specify the property string as optional. That allows all operations we do not provide to be used from the default provider.

Quoted 5 lines

This patch set stops at interfacing the provider with management-external-key. For pkcs11-helper, only some glue code is needed and is in the works. Same with cryptoapicert
aka CNG, but I want to cleanup the old code a bit before
hooking to the provider.

I did a quick test with my Android client to see if it works and RSA keys look good so far. I am getting a request like:

NC9t8IkYrjAQcCzc85zN0H5TvwfAUDwYkR4j2ga6fGw=,RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest

from the management interface. But I haven't found the right Signature method from java yet to actually sign it correctly:

Quoted 7 lines

sig = Signature.getInstance(SHA256withRSA/PSS); sig.setParameter(new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1)); sig.initSign(privkey);
sig.update(data);
signed_bytes = sig.sign();

I'm not sure, but can upload my implementation of pkcs11 including PSS though it wont work yet with pkcs11-helper.. It needs my CK_MECHANISM patch that Alon has merged, but not released.

It may give you some clue as to what could be wrong.

Quoted 11 lines

is what I expected to be the correct signature but the server complains with OpenSSL: error:0407E068:rsa routines:RSA_verify_PKCS1_PSS_mgf1:bad signature

I will have to figure out where this goes wrong.

With an EC key somewhere in that stack, EC/RSA gets confuse as there is rsa_keymgmt_import/rsa_keymgmt_name in the stack and then later ec_keymgmt_name. I haven't digged into that as it is getting late here.

Yes, there is a bug in ec_keymgmt_name -- we should return the name for the op (passed as id), so when id == OSSL_OP_SIGNATURE we should return "ECDSA", not "EC". My mistake -- I testd EC key only just now.

As we do not support any other ops like ECDH key exchange we can just always return "ECDSA" in that function. I will do it v2, later. Unfortunately none of this is documented it seems.

Selva

author
#19 lore raw

Hi,

Quoted 6 lines

from the management interface. But I haven't found the right Signature

Quoted 4 lines

method from java yet to actually sign it correctly:

sig = Signature.getInstance(SHA256withRSA/PSS);

SHA256withRSA/PSS may be trying to first do Sha256 digest of the data and then pad and sign. Instead try this: "NonewithRSASSA-PSS" or "NonewithRSA/PSS"

Quoted 8 lines

sig.setParameter(new PSSParameterSpec("SHA-256", "MGF1",

Quoted 7 lines

MGF1ParameterSpec.SHA256, 32, 1));
sig.initSign(privkey);
sig.update(data);
signed_bytes = sig.sign();

You would still need SHA-256 in the PSSParameterSpec.

Selva

#20 lore raw

Am 24.09.21 um 00:54 schrieb Selva Nair:

Quoted 7 lines

Hi,

from the management interface. But I haven't found the right Signature
method from java yet to actually sign it correctly:

Quoted 6 lines

sig = Signature.getInstance(SHA256withRSA/PSS);

SHA256withRSA/PSS may be trying to first do Sha256 digest of the data and then pad and sign. Instead try this: "NonewithRSASSA-PSS" or "NonewithRSA/PSS"

Yeah, That *would* be the correct algorithm for that. Unfortunately the Android Keystore does not support that one
(https://developer.android.com/training/articles/keystore#SupportedSignatures)

Manually adding the RSA/PSS padding and then signing with "RSA/ECB/NoPadding" like I did in OpenSSL 1.1 days works. But I would like to avoid implementing RSA/PSS myself but the crypto libraries seem not be helpful in providing an implementation for that.

OpenSSL has RSA_padding_add_PKCS1_PSS_mgf1 but it is deprecated in 3.0 and the Java crypto API also does not seem to expose an API for just padding it seems.

But on the plus side, using that workaround the external key provider works with EC and RSA on Android.

Arne

author
#21 lore raw

Hi,

Quoted 15 lines · Arne Schwabe

Am 24.09.21 um 00:54 schrieb Selva Nair:

Quoted 7 lines

Hi,

from the management interface. But I haven't found the right Signature
method from java yet to actually sign it correctly:

Quoted 6 lines

sig = Signature.getInstance(SHA256withRSA/PSS);

SHA256withRSA/PSS may be trying to first do Sha256 digest of the data and then pad and sign. Instead try this: "NonewithRSASSA-PSS" or "NonewithRSA/PSS"

Quoted 6 lines

Yeah, That *would* be the correct algorithm for that. Unfortunately the Android Keystore does not support that one
(
https://developer.android.com/training/articles/keystore#SupportedSignatures
)

We can treat management-external key as special and optionally provide the digest to sign. OpenSSL 3.0 with provider always seem to call DigestSign and never Sign directly so we have the info.

I already have key->origin indication which was originally meant to distinguish between various external libraries -- in the end I simplified that a bit. So its easy to treat management external key differently from other external keys. So for management it would a digest_sign callback if "--management-external-key digest" is specified, or some such. Harder part would be to get access to options->management_flags into xkey_helper.c -- I have tried to keep a max separation between it and the core and would like to keep it that way.

Or make another ifdef for ANDROID... ahem..

I'll look into it. Probably your app is the only --management-external-key "consumer" out there. We can even change the spec of PK_SIG and no one will notice..

Manually adding the RSA/PSS padding and then signing with

Quoted 4 lines

"RSA/ECB/NoPadding" like I did in OpenSSL 1.1 days works. But I would like to avoid implementing RSA/PSS myself but the crypto libraries seem not be helpful in providing an implementation for that.

Yeah, that's not good.

Quoted 3 lines

But on the plus side, using that workaround the external key provider works with EC and RSA on Android.

Great!

Selva

author
#22 lore raw

Hi Arne,

Quoted 26 lines · Selva Nair

Hi,

Quoted 15 lines · Arne Schwabe

Am 24.09.21 um 00:54 schrieb Selva Nair:

Quoted 7 lines

Hi,

from the management interface. But I haven't found the right Signature
method from java yet to actually sign it correctly:

Quoted 6 lines

sig = Signature.getInstance(SHA256withRSA/PSS);

SHA256withRSA/PSS may be trying to first do Sha256 digest of the data and then pad and sign. Instead try this: "NonewithRSASSA-PSS" or "NonewithRSA/PSS"

Quoted 6 lines

Yeah, That *would* be the correct algorithm for that. Unfortunately the Android Keystore does not support that one
(
https://developer.android.com/training/articles/keystore#SupportedSignatures
)

Quoted 4 lines

We can treat management-external key as special and optionally provide the digest to sign. OpenSSL 3.0 with provider always seem to call DigestSign and never Sign directly so we have the info.

Turns out to be easier than I thought. I have added a patch to optionally send the undigested message to the management client. Indicate support for digesting operation in --management-external-key and you get the message to sign with data=message

See the commit message. It's in
https://github.com/selvanair/openvpn/tree/xkey-provider-v3 (only compile
tested).

Selva
PS. I'm supposed to be holidaying, but basking in LCD glow instead of sun..

#23 lore raw

Hi,

Quoted 1 line · Selva Nair

PS. I'm supposed to be holidaying, but basking in LCD glow instead of sun..

Sometimes "I finally have time for hacking!" makes great holidays :-)

(The initial IPv6 patch set was a christmas present, sort of - my wife let me hack a few days on it, without being offended :-) )

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#24 lore raw
In reply to Selva Nair
Quoted 11 lines

We can treat management-external key as special and optionally provide the digest to sign. OpenSSL 3.0 with provider always seem to call DigestSign and never Sign directly so we have the info.

Turns out to be easier than I thought. I have added a patch to optionally send the undigested message to the management client. Indicate support for digesting operation in --management-external-key and you get the message to sign with data=message

That is great. I tested it and TLS 1.3 is working nicely with "SHA256withRSA/PSS" on the java side. So that is great.

But then it went downhill from that with things unrelated to the xkey provider. I wanted to see if it works with TLS 1.1 (ie.. forcing a pkcs1 signature) and found out tls-version-max no longer work on OpenSSL 3.0 nor on my Ubuntu 20 with OpenSSL 1.1.

I decided then to replace SSLv23_client_method with TLS11_client_method, which ended with an internal SSL error in my client.

So instead I tried to see tls-version-max still works with mbed TLS only to discover that my OpenSSL 3.0 client does not connect to my mbed TLS at all and instead the server says "TLS_ERROR: read tls_read_plaintext
error: SSL - Bad input parameters to function" (Ubuntu 20/mbed TLS 2.16.0). Switching the app back to OpenSSL 1.1.1l still works.

Then tested again with OpenSSL 3.0 but forgot to reenable HAVE_XKEY_PROVIDER and now the RSA_method/EC_method works but I was fairly sure that it didn't before. Maybe there is some obscure side effect from your branch or I misremembered before ...

But for some reason having the HAVE_XKEY_PROVIDER define enabled breaks connecting to an mbed TLS 2.16 server. I can also reproduce that with my a version on my mac.

Quoted 8 lines

See the commit message. It's
in  https://github.com/selvanair/openvpn/tree/xkey-provider-v3
<https://github.com/selvanair/openvpn/tree/xkey-provider-v3>   (only compile tested).

Selva
PS. I'm supposed to be holidaying, but basking in LCD glow instead of sun..

Hehe, and I am supposed to be sleeping but instead I am testing some obscure software part that is even hard to explain to other IT (non crypto) people what it does ....

A message that is not in the archive (sent off-list or not yet received): [email protected]...

A message that is not in the archive (sent off-list or not yet received): [email protected]...

author
#27 lore raw

Hi

Here is an update on this patch set to keep all in the loop.

Arne discovered that my patch broke ECDH key exchange in some cases. This turns out to be due to the way providers are handled in OpenSSL especially when used in a TLS context. It leads to the requirement that an external provider has to handle a wide zoo of key operations including key exchange and key generation, even if all it wants to do is signing with an external key. Essentially something like: "you either export the key to me or be ready to import and handle all operations on any asymmetric key I may come across". We can't export as the key is in a protected storage in some backend, we also do not want to do all that extra work that's not in the contract, and we are not good at it either.

I have been engaging with OpenSSL developers on this and they realize this was unintended, and is a "bug/weakness" in their implementation. They are working on a patch to fix it at their end (
https://github.com/openssl/openssl/pull/16725). The eventual fix is very likely to get backported to OpenSSL 3.0, so we have to wait.

I'll submit a slightly modified v2 once their fix is finalized.

Thanks,

Selva

Quoted 1 line
author
#28 lore raw

Hi,

OpenSSL folks have merged their "fix" in the provider interface that I was waiting for. It will be in the 3.0.1 patch release. In the meantime, I have opened a matching version of this patch set as a PR for OpenVPN for comments/tests/bug-reports/nitpicks. I skipped v2 and this version is tagged v3.

Will post the patches to the list when OpenSSL 3.0.1 is released.

On top of v1 patches this also includes handling pkcs11-id and cryptoapicert options through the provider. Requires OpenSSL from either the master branch (3.1.0-dev) or 3.0 branch (3.0.1-dev) post Oct. 27.

Cheers,

Selva

Quoted 28 lines · Selva Nair

Hi

Here is an update on this patch set to keep all in the loop.

Arne discovered that my patch broke ECDH key exchange in some cases. This turns out to be due to the way providers are handled in OpenSSL especially when used in a TLS context. It leads to the requirement that an external provider has to handle a wide zoo of key operations including key exchange and key generation, even if all it wants to do is signing with an external key. Essentially something like: "you either export the key to me or be ready to import and handle all operations on any asymmetric key I may come across". We can't export as the key is in a protected storage in some backend, we also do not want to do all that extra work that's not in the contract, and we are not good at it either.

I have been engaging with OpenSSL developers on this and they realize this was unintended, and is a "bug/weakness" in their implementation. They are working on a patch to fix it at their end (
https://github.com/openssl/openssl/pull/16725). The eventual fix is very likely to get backported to OpenSSL 3.0, so we have to wait.

I'll submit a slightly modified v2 once their fix is finalized.

Thanks,

Selva

Quoted 1 line
#29 lore raw

Hi,

Quoted 11 lines · Selva Nair

OpenSSL folks have merged their "fix" in the provider interface that I was waiting for. It will be in the 3.0.1 patch release. In the meantime, I have opened a matching version of this patch set as a PR for OpenVPN for comments/tests/bug-reports/nitpicks. I skipped v2 and this version is tagged v3.

Will post the patches to the list when OpenSSL 3.0.1 is released.

On top of v1 patches this also includes handling pkcs11-id and cryptoapicert options through the provider. Requires OpenSSL from either the master branch (3.1.0-dev) or 3.0 branch (3.0.1-dev) post Oct. 27.

This sounds really nice.

I'll see if I can get Steffan, Max and Arne interested in looking at the PR tomorrow :-) - face to face discussions can be some much faster than email with asynchronous schedules...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...