Skip to contentSkip to conversation

add engine keys keys

PATCH Superseded James Bottomley 9 participants 19 messages Oct 28, 2017 – Nov 8, 2017

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover add engine keys keys

Engine keys are an openssl concept for a key file which can only be understood by an engine (usually because it's been wrapped by the engine itself).  We use this for TPM engine keys, so you can either generate them within your TPM or wrap them from existing private keys. Once wrapped, the keys will only function in the TPM that generated them, so it means the VPN keys are tied to the physical platform, which is very useful.  Engine keys have to be loaded via a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

James

---

James Bottomley (1):
  openssl: add engine method for loading the key

 src/openvpn/crypto_backend.h | 13 ++++++++++++ src/openvpn/crypto_openssl.c | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/openvpn/ssl_openssl.c    |  6 +++++-

 3 files changed, 67 insertions(+), 1 deletion(-)

--
2.12.3

#1 lore raw

PATCH 1/1 openssl: add engine method for loading the key

As well as doing crypto acceleration, engines can also be used to load key files. If the engine is set, and the private key loading fails for bio methods, this patch makes openvpn try to get the engine to load the key. If that succeeds, we end up using an engine based key. This can be used with the openssl tpm engines to make openvpn use a TPM wrapped key file.

---
 src/openvpn/crypto_backend.h | 13 ++++++++++++
 src/openvpn/crypto_openssl.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/ssl_openssl.c    |  6 +++++-
 3 files changed, 67 insertions(+), 1 deletion(-)
src/openvpn/crypto_backend.h +13 -0
index 567fd9b2..0b4a9ce9 100644
@@ -669,4 +669,17 @@ const char *translate_cipher_name_from_openvpn(const char *cipher_name);
  */
 const char *translate_cipher_name_to_openvpn(const char *cipher_name);
 
+/**
+ * Load a key file from an engine
+ *
+ * @param file	The engine file to load
+ * @param ui	The UI method for the password prompt
+ * @param data	The data to pass to the UI method
+ *
+ * @return	The private key if successful or NULL if not
+ */
+EVP_PKEY *
+engine_load_key(const char *file, SSL_CTX *ctx);
+
+
 #endif /* CRYPTO_BACKEND_H_ */
src/openvpn/crypto_openssl.c +49 -0

53 lines not on this page · raw message

src/openvpn/ssl_openssl.c +5 -1
index 92a662b5..52e9a869 100644
@@ -839,7 +839,11 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
                                    SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx));
     if (!pkey)
     {
-        goto end;
+        pkey = engine_load_key(priv_key_file, ctx->ctx);
+        if (!pkey)
+        {
+            goto end;
+        }
     }
 
     if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
Signature · 1 line
2.12.3
#2 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

2017-10-28 17:03 GMT+05:00 James Bottomley <
[email protected]...>:

Quoted 7 lines

As well as doing crypto acceleration, engines can also be used to load key files. If the engine is set, and the private key loading fails for bio methods, this patch makes openvpn try to get the engine to load the key. If that succeeds, we end up using an engine based key. This can be used with the openssl tpm engines to make openvpn use a TPM wrapped key file.

it fails on mbedtls and openssl-1.1.0

https://travis-ci.org/chipitsine/openvpn/builds/294429659

Quoted 121 lines
---
 src/openvpn/crypto_backend.h | 13 ++++++++++++
 src/openvpn/crypto_openssl.c | 49 ++++++++++++++++++++++++++++++
++++++++++++++
 src/openvpn/ssl_openssl.c    |  6 +++++-
 3 files changed, 67 insertions(+), 1 deletion(-)
src/openvpn/crypto_backend.h +13 -0
index 567fd9b2..0b4a9ce9 100644
@@ -669,4 +669,17 @@ const char *translate_cipher_name_from_openvpn(const
 char *cipher_name);
  */
 const char *translate_cipher_name_to_openvpn(const char *cipher_name);
 
+/**
+ * Load a key file from an engine
+ *
+ * @param file The engine file to load
+ * @param ui   The UI method for the password prompt
+ * @param data The data to pass to the UI method
+ *
+ * @return     The private key if successful or NULL if not
+ */
+EVP_PKEY *
+engine_load_key(const char *file, SSL_CTX *ctx);
+
+

#endif /* CRYPTO_BACKEND_H_ */

src/openvpn/crypto_openssl.c +49 -0
index 0134e55d..ee16a496 100644
@@ -969,4 +969,53 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
     HMAC_Final(ctx, dst, &in_hmac_len);
 }
 
+static int
+ui_read(UI *ui, UI_STRING *uis)
+{
+    SSL_CTX *ctx = UI_get0_user_data(ui);
+
+    if (UI_get_string_type(uis) == UIT_PROMPT) {
+        pem_password_cb *cb = SSL_CTX_get_default_passwd_cb(ctx);
+        void *d = SSL_CTX_get_default_passwd_cb_userdata(ctx);
+        char password[64];
+
+        cb(password, sizeof(password), 0, d);
+        UI_set_result(ui, uis, password);
+
+        return 1;
+    }
+    return 0;
+}
+
+EVP_PKEY *
+engine_load_key(const char *file, SSL_CTX *ctx)
+{
+    UI_METHOD *ui;
+    EVP_PKEY *pkey;
+
+    if (!engine_persist)
+        return NULL;
+
+    ui = UI_create_method("openvpn");
+
+    if (!ui)
+        return NULL;
+
+    UI_method_set_reader(ui, ui_read);
+
+    ERR_clear_error();         /* BIO read failure */
+    if (!ENGINE_init(engine_persist)) {
+       ERR_print_errors_fp(stderr);
+       pkey = NULL;
+       goto out;
+    }
+    pkey = ENGINE_load_private_key(engine_persist, file, ui, ctx);
+    ENGINE_finish(engine_persist);
+    if (!pkey)
+       ERR_print_errors_fp(stderr);
+ out:
+    UI_destroy_method(ui);
+    return pkey;
+}
+
 #endif /* ENABLE_CRYPTO && ENABLE_CRYPTO_OPENSSL */
src/openvpn/ssl_openssl.c +5 -1
index 92a662b5..52e9a869 100644
@@ -839,7 +839,11 @@ tls_ctx_load_priv_file(struct tls_root_ctx *ctx,
 const char *priv_key_file,
                                    SSL_CTX_get_default_passwd_cb_
 userdata(ctx->ctx));
     if (!pkey)
     {
-        goto end;
+        pkey = engine_load_key(priv_key_file, ctx->ctx);
+        if (!pkey)
+        {
+            goto end;
+        }
     }

if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
--
2.12.3

------------------------------------------------------------
------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#3 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

Quoted 15 lines · Илья Шипицин

2017-10-28 17:03 GMT+05:00 James Bottomley <
[email protected]...>:

Quoted 10 lines

As well as doing crypto acceleration, engines can also be used to load
key files.  If the engine is set, and the private key loading fails for bio methods, this patch makes openvpn try to get the engine to load the key.  If that succeeds, we end up using an engine based key.
This can be used with the openssl tpm engines to make openvpn use a TPM wrapped key file.

Quoted 3 lines

it fails on mbedtls and openssl-1.1.0

https://travis-ci.org/chipitsine/openvpn/builds/294429659

It looks like it needs better config guarding; incremental attached below.

However, it exposes an openvpn problem: engines aren't built with openssl-1.1 because the configure.ac check for ENGINE_cleanup doesn't find the function (it became a #define).  I'll see if I can fix that.

The mbedtls one looks like the function def needs to be in crypto_openssl.h; I've moved it but can't compile check

James

---

src/openvpn/crypto_backend.h +0 -12
index 0b4a9ce9..cc8f138f 100644
@@ -669,17 +669,5 @@ const char *translate_cipher_name_from_openvpn(const char *cipher_name);
  */
 const char *translate_cipher_name_to_openvpn(const char *cipher_name);
 
-/**
- * Load a key file from an engine
- *
- * @param file	The engine file to load
- * @param ui	The UI method for the password prompt
- * @param data	The data to pass to the UI method
- *
- * @return	The private key if successful or NULL if not
- */
-EVP_PKEY *
-engine_load_key(const char *file, SSL_CTX *ctx);
-
 
 #endif /* CRYPTO_BACKEND_H_ */
src/openvpn/crypto_openssl.c +6 -0
index ee16a496..1fcb80a6 100644
@@ -969,6 +969,7 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
     HMAC_Final(ctx, dst, &in_hmac_len);
 }
 
+#ifdef HAVE_OPENSSL_ENGINE
 static int
 ui_read(UI *ui, UI_STRING *uis)
 {
@@ -986,10 +987,12 @@ ui_read(UI *ui, UI_STRING *uis)
     }
     return 0;
 }
+#endif
 
 EVP_PKEY *
 engine_load_key(const char *file, SSL_CTX *ctx)
 {
+#ifdef HAVE_OPENSSL_ENGINE
     UI_METHOD *ui;
     EVP_PKEY *pkey;
 
@@ -1016,6 +1019,9 @@ engine_load_key(const char *file, SSL_CTX *ctx)
  out:
     UI_destroy_method(ui);
     return pkey;
+#else
+    return NULL;
+#endif
 }
 
 #endif /* ENABLE_CRYPTO && ENABLE_CRYPTO_OPENSSL */
src/openvpn/crypto_openssl.h +12 -0
index 60a28123..759dc927 100644
@@ -101,5 +101,17 @@ void crypto_print_openssl_errors(const unsigned int flags);
         msg((flags), __VA_ARGS__); \
     } while (false)
 
+/**
+ * Load a key file from an engine
+ *
+ * @param file	The engine file to load
+ * @param ui	The UI method for the password prompt
+ * @param data	The data to pass to the UI method
+ *
+ * @return	The private key if successful or NULL if not
+ */
+EVP_PKEY *
+engine_load_key(const char *file, SSL_CTX *ctx);
+
 
 #endif /* CRYPTO_OPENSSL_H_ */
#4 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

James,

could you please resend a full patch, so to have a better overview of the whole change?

Thanks!

Quoted 122 lines · James Bottomley
Quoted 15 lines · Илья Шипицин

2017-10-28 17:03 GMT+05:00 James Bottomley <
[email protected]...>:

Quoted 10 lines

As well as doing crypto acceleration, engines can also be used to load
key files.  If the engine is set, and the private key loading fails for bio methods, this patch makes openvpn try to get the engine to load the key.  If that succeeds, we end up using an engine based key.
This can be used with the openssl tpm engines to make openvpn use a TPM wrapped key file.

Quoted 3 lines

it fails on mbedtls and openssl-1.1.0

https://travis-ci.org/chipitsine/openvpn/builds/294429659

It looks like it needs better config guarding; incremental attached below.

However, it exposes an openvpn problem: engines aren't built with openssl-1.1 because the configure.ac check for ENGINE_cleanup doesn't find the function (it became a #define).  I'll see if I can fix that.

The mbedtls one looks like the function def needs to be in crypto_openssl.h; I've moved it but can't compile check

James

---

src/openvpn/crypto_backend.h +0 -12
index 0b4a9ce9..cc8f138f 100644
@@ -669,17 +669,5 @@ const char *translate_cipher_name_from_openvpn(const char *cipher_name);
  */
 const char *translate_cipher_name_to_openvpn(const char *cipher_name);
 
-/**
- * Load a key file from an engine
- *
- * @param file	The engine file to load
- * @param ui	The UI method for the password prompt
- * @param data	The data to pass to the UI method
- *
- * @return	The private key if successful or NULL if not
- */
-EVP_PKEY *
-engine_load_key(const char *file, SSL_CTX *ctx);
-
 
 #endif /* CRYPTO_BACKEND_H_ */
src/openvpn/crypto_openssl.c +6 -0
index ee16a496..1fcb80a6 100644
@@ -969,6 +969,7 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
     HMAC_Final(ctx, dst, &in_hmac_len);
 }
 
+#ifdef HAVE_OPENSSL_ENGINE
 static int
 ui_read(UI *ui, UI_STRING *uis)
 {
@@ -986,10 +987,12 @@ ui_read(UI *ui, UI_STRING *uis)
     }
     return 0;
 }
+#endif
 
 EVP_PKEY *
 engine_load_key(const char *file, SSL_CTX *ctx)
 {
+#ifdef HAVE_OPENSSL_ENGINE
     UI_METHOD *ui;
     EVP_PKEY *pkey;
 
@@ -1016,6 +1019,9 @@ engine_load_key(const char *file, SSL_CTX *ctx)
  out:
     UI_destroy_method(ui);
     return pkey;
+#else
+    return NULL;
+#endif
 }
 
 #endif /* ENABLE_CRYPTO && ENABLE_CRYPTO_OPENSSL */
src/openvpn/crypto_openssl.h +12 -1
index 60a28123..759dc927 100644
@@ -101,5 +101,17 @@ void crypto_print_openssl_errors(const unsigned int flags);
         msg((flags), __VA_ARGS__); \
     } while (false)
 
+/**
+ * Load a key file from an engine
+ *
+ * @param file	The engine file to load
+ * @param ui	The UI method for the password prompt
+ * @param data	The data to pass to the UI method
+ *
+ * @return	The private key if successful or NULL if not
+ */
+EVP_PKEY *
+engine_load_key(const char *file, SSL_CTX *ctx);
+
 
 #endif /* CRYPTO_OPENSSL_H_ */
 
------------------------------------------------------------------------------

Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Signature · 1 line
Antonio Quartulli
#11 lore raw
In reply to James Bottomley

Hi,

Quoted 9 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be understood by an engine (usually because it's been wrapped by the engine itself).  We use this for TPM engine keys, so you can either generate them within your TPM or wrap them from existing private keys. Once wrapped, the keys will only function in the TPM that generated them, so it means the VPN keys are tied to the physical platform, which is very useful.  Engine keys have to be loaded via a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

How does this work in an OpenVPN context, as in, what do I have to do to make TPM keys work on client and server?

Do we need a new abstraction layer here somewhere, given that this seems to do something similar to using the windows crypto layer to access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

I see more #ifdef in the code and this is usually a sign of "it will increase testing requirement, and we can't even test the abstraction if none of us has the hardware".

But I leave the more specific discussion to Steffan and Antonio :-)

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#5 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

In reply to Antonio Quartulli
Quoted 4 lines · Antonio Quartulli

James,

could you please resend a full patch, so to have a better overview of the whole change?

Sure thing.  It's below.

James

---

#12 lore raw
In reply to Gert Doering
Quoted 14 lines · Gert Doering

Hi,

Quoted 10 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be understood by an engine (usually because it's been wrapped by the engine itself).  We use this for TPM engine keys, so you can either generate them within your TPM or wrap them from existing private keys.  Once wrapped, the keys will only function in the TPM that generated them, so it means the VPN keys are tied to the physical platform, which is very useful.  Engine keys have to be loaded via a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

Quoted 2 lines

How does this work in an OpenVPN context, as in, what do I have to do to make TPM keys work on client and server?

If you have an existing private key, you convert it to a TPM key using the create_tpm[2]_key binary that comes with openssl_tpm[2]_engine. The key you create is then locked to the TPM of the system you create it on (so save an offline copy of the non-TPM private key).

Quoted 4 lines

Do we need a new abstraction layer here somewhere, given that this seems to do something similar to using the windows crypto layer to access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

I don't really think so.  The engine keys are a bit of an openssl
brainfart: there's no reason why PEM_read_bio_PrivateKey couldn't actually check for engine keys and have the whole thing transparent to all applications, except that openssl didn't want to do it that way.

These keys are real files, so they're not like tokens, which is the pkcs11 abstraction.

Quoted 5 lines

I see more #ifdef in the code and this is usually a sign of "it will increase testing requirement, and we can't even test the abstraction if none of us has the hardware".

But I leave the more specific discussion to Steffan and Antonio :-)

Testing is possible.  If you look at the build repository of openssl_tpm2_engine:

https://build.opensuse.org/project/show/home:jejb1:TPM

You'll see there are tests in there that fire up a software tpm to run. However, the software TPM is a bit capricious and certainly only runs on openssl 1.0.2, so I can't run the tests on Fedora 26 for instance without rewriting the swtpm2 package.

James

#18 lore raw
In reply to Gert Doering

Am 29.10.17 um 16:24 schrieb Gert Doering:

Quoted 21 lines

Hi,

Quoted 9 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be understood by an engine (usually because it's been wrapped by the engine itself).  We use this for TPM engine keys, so you can either generate them within your TPM or wrap them from existing private keys. Once wrapped, the keys will only function in the TPM that generated them, so it means the VPN keys are tied to the physical platform, which is very useful.  Engine keys have to be loaded via a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

How does this work in an OpenVPN context, as in, what do I have to do to make TPM keys work on client and server?

Do we need a new abstraction layer here somewhere, given that this seems to do something similar to using the windows crypto layer to access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

Quoted 5 lines

I see more #ifdef in the code and this is usually a sign of "it will increase testing requirement, and we can't even test the abstraction if none of us has the hardware".

But I leave the more specific discussion to Steffan and Antonio :-)

As a sidenote, OpenVPN itself implements its own "engine" (with the older pre engine RSA_method) to allow management-external-keys.

Unifying that would be nice but is probably a lot of work, especially since OpenVPN engine is not so straightforward to implement.

Arne

#13 lore raw
In reply to James Bottomley

Hi,

Quoted 28 lines · James Bottomley
Quoted 14 lines · Gert Doering

Hi,

Quoted 10 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be understood by an engine (usually because it's been wrapped by the engine itself). We use this for TPM engine keys, so you can either generate them within your TPM or wrap them from existing private keys. Once wrapped, the keys will only function in the TPM that generated them, so it means the VPN keys are tied to the physical platform, which is very useful. Engine keys have to be loaded via a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

Quoted 2 lines

How does this work in an OpenVPN context, as in, what do I have to do to make TPM keys work on client and server?

If you have an existing private key, you convert it to a TPM key using the create_tpm[2]_key binary that comes with openssl_tpm[2]_engine.
The key you create is then locked to the TPM of the system you create
it on (so save an offline copy of the non-TPM private key).

Quoted 4 lines

Do we need a new abstraction layer here somewhere, given that this seems to do something similar to using the windows crypto layer to access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

Quoted 7 lines

I don't really think so. The engine keys are a bit of an openssl
brainfart: there's no reason why PEM_read_bio_PrivateKey couldn't actually check for engine keys and have the whole thing transparent to all applications, except that openssl didn't want to do it that way.

These keys are real files, so they're not like tokens, which is the pkcs11 abstraction.

That means, if one has the tpm_engine for openssl installed and TPM-wrapped keys, this should just work, is it? And, I suppose, the patch for openvpn is required only because openssl does not transparently use available engines.

If so, this looks like a easy way to provide TPM support to openssl users on some platforms. What about Windows? Is there an openssl TPM engine and tools to convert the key to a wrapped TSS blob on Windows?

I would like to see new features transparently supported on Windows as well without the need for too much extra code and associated maintenance burden. Our 'cryptoapicert' implementation is already in need of a major re-write to support TLS 1.2 and newer.

From that point of view, instead of file-based wrapped keys, if a pkcs11 compatible API can be used to access TPM (that's possible isn't it?) TPM could be more widely usable without the need of any additional support in openssl or openvpn.

Selva

#14 lore raw
Quoted 57 lines · Selva

Hi,

Quoted 52 lines · James Bottomley
Quoted 24 lines · Gert Doering

Hi,

Quoted 11 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be
understood by an engine (usually because it's been wrapped by the
engine itself).  We use this for TPM engine keys, so you can either
generate them within your TPM or wrap them from existing private
keys.  Once wrapped, the keys will only function in the TPM

Quoted 8 lines

that
generated them, so it means the VPN keys are tied to the physical
platform, which is very useful.  Engine keys have to be loaded via
a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

Quoted 3 lines

How does this work in an OpenVPN context, as in, what do I have to do
to make TPM keys work on client and server?

If you have an existing private key, you convert it to a TPM key using
the create_tpm[2]_key binary that comes with openssl_tpm[2]_engine. The key you create is then locked to the TPM of the system you create
it on (so save an offline copy of the non-TPM private key).

Quoted 7 lines

Do we need a new abstraction layer here somewhere, given that this
seems to do something similar to using the windows crypto layer to
access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

I don't really think so.  The engine keys are a bit of an openssl
brainfart: there's no reason why PEM_read_bio_PrivateKey couldn't actually check for engine keys and have the whole thing transparent to
all applications, except that openssl didn't want to do it that way.

Quoted 8 lines
Quoted 2 lines

These keys are real files, so they're not like tokens, which is the pkcs11 abstraction.

That means, if one has the tpm_engine for openssl installed and TPM-wrapped keys, this should just work, is it? And, I suppose, the patch for openvpn is required only because openssl does not transparently use available engines.

Yes, and no.  By the time openssl sees the load request, it has a bio, rather than a file name, so there's a bit of additional glue openssl would need for transparent engine support.  I proposed a patch a while ago to the openssl list, but it required an additional engine callback and wasn't very enthusiastically received.

Quoted 4 lines

If so, this looks like a easy way to provide TPM support to openssl users on some platforms. What about Windows? Is there an openssl TPM engine and tools to convert the key to a wrapped TSS blob on Windows?

No idea, I'm afraid.  I would suspect not because windows isn't a great consumer of openssl, so no-one on the windows development side probably thought to write an openssl engine for their TPMs.

Quoted 4 lines

I would like to see new features transparently supported on Windows as well without the need for too much extra code and associated maintenance burden. Our 'cryptoapicert' implementation is already in need of a major re-write to support TLS 1.2 and newer.

Well, windows supports engines (via dlls), so the engine code will work for Windows, just not for my use case (their being no engine) and MS CryptoAPI is one of the supported engines.

Quoted 4 lines

From that point of view, instead of file-based wrapped keys, if a pkcs11 compatible API can be used to access TPM (that's possible isn't it?) TPM could be more widely usable without the need of any additional support in openssl or openvpn.

For TPM 1.2 it is possible to load the key into the TPM and then use the TPM as a loaded token via PKCS11 but the usual key format is file based not already present.  For TPM 2, the design is different and the key has to be loaded each time from offline storage (the file) because the space in the TPM is so tiny, so treating it as a token is a bit more difficult.  Obviously with enough pressure, even a square peg will fit in a round hole, it's just that it won't be elegant.

James

#15 lore raw

Hi James,

Quoted 80 lines · James Bottomley
Quoted 46 lines · Selva
Quoted 43 lines · James Bottomley
Quoted 19 lines · Gert Doering
Quoted 10 lines · James Bottomley

Engine keys are an openssl concept for a key file which can only be
understood by an engine (usually because it's been wrapped by the
engine itself).  We use this for TPM engine keys, so you can either
generate them within your TPM or wrap them from existing private
keys.  Once wrapped, the keys will only function in the TPM

Quoted 8 lines

that
generated them, so it means the VPN keys are tied to the physical
platform, which is very useful.  Engine keys have to be loaded via
a specific callback, so use this as a fallback in openvpn if an engine is specified and if the PEM read of the private key fails.

Quoted 3 lines

How does this work in an OpenVPN context, as in, what do I have to do
to make TPM keys work on client and server?

If you have an existing private key, you convert it to a TPM key using
the create_tpm[2]_key binary that comes with openssl_tpm[2]_engine.
 The key you create is then locked to the TPM of the system you
create
it on (so save an offline copy of the non-TPM private key).

Quoted 6 lines

Do we need a new abstraction layer here somewhere, given that this
seems to do something similar to using the windows crypto layer to
access "private keys hidden in windows" (--cryptoapicert) and/or pkcs11?

I don't really think so.  The engine keys are a bit of an openssl
brainfart: there's no reason why PEM_read_bio_PrivateKey couldn't actually check for engine keys and have the whole thing transparent to
all applications, except that openssl didn't want to do it that way.

Quoted 7 lines
Quoted 2 lines

These keys are real files, so they're not like tokens, which is the pkcs11 abstraction.

That means, if one has the tpm_engine for openssl installed and TPM-wrapped keys, this should just work, is it? And, I suppose, the patch for openvpn is required only because openssl does not transparently use available engines.

Yes, and no.  By the time openssl sees the load request, it has a bio, rather than a file name, so there's a bit of additional glue openssl would need for transparent engine support.  I proposed a patch a while ago to the openssl list, but it required an additional engine callback and wasn't very enthusiastically received.

Quoted 4 lines

If so, this looks like a easy way to provide TPM support to openssl users on some platforms. What about Windows? Is there an openssl TPM engine and tools to convert the key to a wrapped TSS blob on Windows?

No idea, I'm afraid.  I would suspect not because windows isn't a great consumer of openssl, so no-one on the windows development side probably thought to write an openssl engine for their TPMs.

Quoted 4 lines

I would like to see new features transparently supported on Windows as well without the need for too much extra code and associated maintenance burden. Our 'cryptoapicert' implementation is already in need of a major re-write to support TLS 1.2 and newer.

Well, windows supports engines (via dlls), so the engine code will work for Windows, just not for my use case (their being no engine) and MS CryptoAPI is one of the supported engines.

Quoted 4 lines

From that point of view, instead of file-based wrapped keys, if a
pkcs11 compatible API can be used to access TPM (that's possible isn't it?) TPM could be more widely usable without the need of any additional support in openssl or openvpn.

Quoted 9 lines

For TPM 1.2 it is possible to load the key into the TPM and then use the TPM as a loaded token via PKCS11 but the usual key format is file based not already present.  For TPM 2, the design is different and the key has to be loaded each time from offline storage (the file) because the space in the TPM is so tiny, so treating it as a token is a bit more difficult.  Obviously with enough pressure, even a square peg will fit in a round hole, it's just that it won't be elegant.

I'm quite interested in using a TPM to store keys - do you have a set of instructions on how to load a key into the TPM and then sign data using that key?  Most of the implementations I've found using something like trousers , which does NOT encrypt data using the TPM itself, but uses the TPM as a master key to sign other keys with.

thx,

JJK

#16 lore raw
In reply to Selva

Hi,

Quoted 4 lines · Selva

I would like to see new features transparently supported on Windows as well without the need for too much extra code and associated maintenance burden. Our 'cryptoapicert' implementation is already in need of a major re-write to support TLS 1.2 and newer.

Fully agree. Since cryptoapicert is windows-specific, I actually think it would be better to add a 'CNG'[0] implementation to the windows wrapper, and make that use management-external-key. That would probably improve UX a lot too, showing users a drop-down to select a key, etc. We can then remove the whole deprecated cryptoapi implementation from the openvpn core.

Quoted 4 lines

From that point of view, instead of file-based wrapped keys, if a pkcs11 compatible API can be used to access TPM (that's possible isn't it?) TPM could be more widely usable without the need of any additional support in openssl or openvpn.

Since this one is transparent, and works as long as the user loads the right engine, I don't see any limitations to include this patch.

-Steffan

[0]
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376210(v=vs.85).aspx

#6 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

In reply to James Bottomley

Hi,

Quoted 7 lines · James Bottomley
Quoted 4 lines · Antonio Quartulli

James,

could you please resend a full patch, so to have a better overview of the whole change?

Sure thing.  It's below.

Feature makes sense, so feature-ACK. An early question though:

Quoted 15 lines
src/openvpn/crypto_openssl.c +9 -0
@@ -969,4 +969,59 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
     HMAC_Final(ctx, dst, &in_hmac_len);
 }
 
+#ifdef HAVE_OPENSSL_ENGINE
+static int
+ui_read(UI *ui, UI_STRING *uis)
+{
+    SSL_CTX *ctx = UI_get0_user_data(ui);
+
+    if (UI_get_string_type(uis) == UIT_PROMPT) {
+        pem_password_cb *cb = SSL_CTX_get_default_passwd_cb(ctx);
+        void *d = SSL_CTX_get_default_passwd_cb_userdata(ctx);

src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 990

Quoted 10 lines
src/openvpn/crypto_openssl.c +10 -0
+        char password[64];
+
+        cb(password, sizeof(password), 0, d);
+        UI_set_result(ui, uis, password);
+
+        return 1;
+    }
+    return 0;
+}
+#endif

This looks like it should use our user query wrappers from (e.g.) console.h. David, you're the expert here, what should James use to query for passwords?

-Steffan

#17 lore raw
In reply to Steffan Karger

Hi,

Quoted 15 lines · Steffan Karger

Hi,

Quoted 4 lines · Selva

I would like to see new features transparently supported on Windows as well without the need for too much extra code and associated maintenance burden. Our 'cryptoapicert' implementation is already in need of a major re-write to support TLS 1.2 and newer.

Fully agree. Since cryptoapicert is windows-specific, I actually think it would be better to add a 'CNG'[0] implementation to the windows wrapper, and make that use management-external-key. That would probably improve UX a lot too, showing users a drop-down to select a key, etc. We can then remove the whole deprecated cryptoapi implementation from the openvpn core.

Quoted 8 lines
Quoted 4 lines

From that point of view, instead of file-based wrapped keys, if a pkcs11 compatible API can be used to access TPM (that's possible isn't it?) TPM could be more widely usable without the need of any additional support in openssl or openvpn.

Since this one is transparent, and works as long as the user loads the right engine, I don't see any limitations to include this patch.

Agreed this is a simple patch that could be used right away. Let's address 'Cryptography Next Gen' (CNG) and general use of management-external-key as a separate topic.

I too feel the management interface client and/or plugins is a better place to add support for platform specific cert/key storage etc., instead of polluting
openvpn core.

Simon was planning to add support for TLS 1.2+ in eduVPN for keys stored in Windows certstore -- I suppose that would involve CNG.

Selva

#7 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

In reply to Steffan Karger

Hi,

Quoted 3 lines · Steffan Karger

This looks like it should use our user query wrappers from (e.g.) console.h. David, you're the expert here, what should James use to query for passwords?

The mechanics are "query_user_...()", most conveniently

/**
* A plain "make Gert happy" wrapper. Same arguments as @query_user_add */
static inline bool
query_user_SINGLE(char *prompt, size_t prompt_len,
char *resp, size_t resp_len,
bool echo)

(console.h)

... most notably this is important because it can use "other mechanisms" if console input is not available, for example, systemd querying.

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#8 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

Quoted 17 lines · Gert Doering

Hi,

Quoted 3 lines · Steffan Karger

This looks like it should use our user query wrappers from (e.g.) console.h. David, you're the expert here, what should James use to query for passwords?

The mechanics are "query_user_...()", most conveniently

/**
* A plain "make Gert happy" wrapper. Same arguments as @query_user_add */
static inline bool
query_user_SINGLE(char *prompt, size_t prompt_len,
char *resp, size_t resp_len,
bool echo)

Quoted 5 lines

(console.h)

... most notably this is important because it can use "other mechanisms" if console input is not available, for example, systemd querying.

Shouldn't this happen automatically in this particular case as the patch uses
SSL_CTX_get_default_passwd_cb_userdata() which would result in openssl using to the password callback previously set in ssl_openssl.c ? And that callback is
get_userpass() which should know whether to query the management, console or something else.

Selva

#9 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

Quoted 28 lines · Selva

On Sat, Nov 4, 2017 at 1:58 PM, Gert Doering <[email protected]... <mailto:[email protected]...>> wrote:

Hi,

On Wed, Nov 01, 2017 at 07:24:02PM +0100, Steffan Karger wrote:
> This looks like it should use our user query wrappers from (e.g.) > console.h.  David, you're the expert here, what should James use to > query for passwords?

The mechanics are "query_user_...()", most conveniently

/**
 * A plain "make Gert happy" wrapper.  Same arguments as @query_user_add */
static inline bool
query_user_SINGLE(char *prompt, size_t prompt_len,
                  char *resp, size_t resp_len,
                  bool echo)

(console.h)

... most notably this is important because it can use "other mechanisms" if console input is not available, for example, systemd querying.

Quoted 7 lines

Shouldn't this  happen automatically in this particular case as the patch uses
SSL_CTX_get_default_passwd_cb_userdata() which would result in openssl using to the password callback previously set in ssl_openssl.c ? And that callback is
get_userpass() which should know whether to query the management, console or something else.

All the hoops, loops and twists we take to retrieve a simple password from a user ....

TL;DR: Yes, Selva is correct.

#10 lore raw

Re: [Openvpn-devel] [PATCH 1/1] openssl: add engine method for loading the key

Quoted 3 lines · David Sommerseth

I need to spend a bit more time to fully grasp the UI get/set calls and the related implementation.  But what is done in regards to password retrieving in ui_read() makes sense to me.

How to use UI methods is (unsurprisingly) badly documented in openssl. I picked up my knowledge of it by giving up on the docs and reading the source code. Basically it's a huge overkill interface for reading and verifying a passphrase.  There are about six overrideable methods:
opener, closer, reader, writer, flusher and prompt constructor.  The interface seems to be designed to give control of all aspects of user interaction, from passwords, inputs and error outputs.  However, it only ever seems to be used for passwords.

The only thing I need from it is the reader method (that's what takes input from the user) and of the five types UIT_PROMPT/VERIFY/BOOLEAN/INPUT/ERROR I only need UIT_PROMPT because that's asking the user for input, which duplicates what the PEM password prompt does.

James