add engine keys keys
v3 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.
Adding a unit test for this type of key proved particularly
problematic: there's apparently no simple engine you can use to check the functionality, so after a bit of googling, I just wrote one as part of the test. You can see that the unit test converts an existing key to engine format (which is simply changing the PEM guards), tries to start openvpn with the key and verifies that the engine methods are called and the password correctly retrieved. To make the test simple, it relies on openssl detecting a mismatch between the certificate and the key after the key has been loaded rather than going on to bring up an openvpn loop, but I think that's sufficient to test out the engine patch fully.
James Bottomley (2):
openssl: add engine method for loading the key
Add unit tests for engine keys
configure.ac | 2 + src/openvpn/crypto_openssl.c | 55 ++++++++++++ src/openvpn/crypto_openssl.h | 12 +++ src/openvpn/ssl_openssl.c | 6 +- tests/unit_tests/Makefile.am | 6 +- tests/unit_tests/engine-key/Makefile.am | 14 ++++ tests/unit_tests/engine-key/check_engine_keys.sh | 30 +++++++ tests/unit_tests/engine-key/libtestengine.c | 102 +++++++++++++++++++++++ tests/unit_tests/engine-key/openssl.cnf | 12 +++ 9 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 tests/unit_tests/engine-key/Makefile.am create mode 100755 tests/unit_tests/engine-key/check_engine_keys.sh create mode 100644 tests/unit_tests/engine-key/libtestengine.c create mode 100644 tests/unit_tests/engine-key/openssl.cnf
Signature · 1 line
2.12.3
[Openvpn-devel] PATCH v3 1/2] 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.
---
v2: add better configuration guarding
--- src/openvpn/crypto_openssl.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/openvpn/crypto_openssl.h | 12 ++++++++++ src/openvpn/ssl_openssl.c | 6 ++++- 3 files changed, 72 insertions(+), 1 deletion(-)
src/openvpn/crypto_openssl.c +55 -0
@@ -969,4 +969,59 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)HMAC_Final (ctx, dst, &in_hmac_len); } +#if 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); +char password[64 ]; + +cb (password,sizeof (password),0 , d); +UI_set_result (ui, uis, password); + +return 1 ; + } +return 0 ; +} +#endif + +EVP_PKEY * +engine_load_key (const char *file,SSL_CTX *ctx) +{ +#if HAVE_OPENSSL_ENGINE +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; +#else +return NULL ; +#endif +} +#endif /* ENABLE_CRYPTO_OPENSSL */
src/openvpn/crypto_openssl.h +12 -0
@@ -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_ */
src/openvpn/ssl_openssl.c +5 -1
@@ -907,7 +907,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
Re: [Openvpn-devel] PATCH v3 1/2] openssl: add engine method for loading the key
Am 26.01.18 um 21:30 schrieb James Bottomley:
Quoted 23 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.
Signed-off-by James Bottomley <[email protected]...>---
v2: add better configuration guarding
--- src/openvpn/crypto_openssl.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ src/openvpn/crypto_openssl.h | 12 ++++++++++ src/openvpn/ssl_openssl.c | 6 ++++- 3 files changed, 72 insertions(+), 1 deletion(-)src/openvpn/crypto_openssl.c +0
-0
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 974
Quoted 6 lines
src/openvpn/crypto_openssl.c +3
-0@@ -969,4 +969,59 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)HMAC_Final (ctx, dst, &in_hmac_len); } +#if HAVE_OPENSSL_ENGINE +static int +ui_read (UI *ui,UI_STRING *uis)
I would rather a bit more verbose method name here. ui_read seems a bit too generic.
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 980
Quoted 6 lines
src/openvpn/crypto_openssl.c +6
-0+{ +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);
This pointer is never used and the function also seem to have no side effects. So seem like this can be removed.
Quoted 21 lines
src/openvpn/crypto_openssl.c +21
-0+char password[64 ]; + +cb (password,sizeof (password),0 , d); +UI_set_result (ui, uis, password); + + return 1; + } +return 0 ; +} +#endif + +EVP_PKEY * +engine_load_key (const char *file,SSL_CTX *ctx) +{ +#if HAVE_OPENSSL_ENGINE +UI_METHOD *ui; +EVP_PKEY *pkey; + +if (!engine_persist) +return NULL ; +
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 1011
Quoted 10 lines
src/openvpn/crypto_openssl.c +10
-0+ 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);
This should use our standard openssl error reporting. Not directly writing to stderr.
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 1018
Quoted 7 lines
src/openvpn/crypto_openssl.c +7
-0+ 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);
Same as above.
Quoted 7 lines
src/openvpn/crypto_openssl.c +7
-0+ out: +UI_destroy_method (ui); +return pkey; +#else +return NULL ; +#endif +}
Re: [Openvpn-devel] PATCH v3 1/2] openssl: add engine method for loading the key
Quoted 22 lines · Arne Schwabe
Am 26.01.18 um 21:30 schrieb James Bottomley:
Quoted 21 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.
Signed-off-by James Bottomley <[email protected]...om>
---
v2: add better configuration guarding
---
src/openvpn/crypto_openssl.c | 55
++++++++++++++++++++++++++++++++++++++++++++
src/openvpn/crypto_openssl.h | 12 ++++++++++
src/openvpn/ssl_openssl.c | 6 ++++-3 files changed, 72 insertions(+), 1 deletion(-)
Quoted 15 lines
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 974
Quoted 12 lines
src/openvpn/crypto_openssl.c +0
-0@@ -969,4 +969,59 @@ hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) HMAC_Final(ctx, dst, &in_hmac_len); }src/openvpn/crypto_openssl.c +3
-0+#if HAVE_OPENSSL_ENGINE +static int +ui_read (UI *ui,UI_STRING *uis)I would rather a bit more verbose method name here.
ui_readseems a bit too generic.
I can call it anything you like, but the naming scheme in the file seems to be <openssl structure>_<function>, like md_ctx_init() etc, EVP_MD_CTX is the structure and init the function. Here UI is the structure and read the function. Would you prefer UI_reader to match the method set name?
Quoted 9 lines
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 980
Quoted 6 lines
src/openvpn/crypto_openssl.c +6
-0+{ + 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);This pointer is never used and the function also seem to have no side effects. So seem like this can be removed.
It's used two lines below:
Quoted 4 lines
Quoted 4 lines
+ char password[64];
+
+ cb(password, sizeof(password), 0, d);
Here -----------------------------^
If you mean it can be removed because the openssl password callback data pointer is never used in the openvpn implementation then, yes, that's true, but I think following the openssl standard practice is better otherwise engine keys would break if someone ever found a future openvpn use for the data pointer.
Quoted 23 lines
Quoted 23 lines
+ UI_set_result(ui, uis, password);
src/openvpn/crypto_openssl.c +22
-0+ + return 1; + } + return 0; +} +#endif + +EVP_PKEY * +engine_load_key (const char *file,SSL_CTX *ctx) +{ +#if HAVE_OPENSSL_ENGINE + UI_METHOD *ui; + EVP_PKEY *pkey; + + if (!engine_persist) + return NULL; + + ui = UI_create_method("openvpn"); + + if (!ui) + return NULL; +
Quoted 8 lines
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 1011
Quoted 5 lines
+ UI_method_set_reader(ui,
ui_read);src/openvpn/crypto_openssl.c +4
-0+ + ERR_clear_error(); /* BIO read failure */ + if (!ENGINE_init(engine_persist)) { +ERR_print_errors_fp (stderr);This should use our standard openssl error reporting. Not directly writing to stderr.
Agreed, I'll make them crypto_msg() calls.
James
Quoted 20 lines
src/openvpn/crypto_openssl.c hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst) · line 1018
Quoted 8 lines
src/openvpn/crypto_openssl.c +7
-0+ 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);Same as above.
Quoted 8 lines
+ out:
+ UI_destroy_method(ui);
+ return pkey;src/openvpn/crypto_openssl.c +4
-0+#else + return NULL; +#endif +}
PATCH v3 2/2 Add unit tests for engine keys
In reply to James BottomleyTesting engines is problematic, so one of the prerequisites built for the tests is a simple openssl engine that reads a non-standard PEM guarded key. The test is simply can we run a client/server configuration with the usual sample key replaced by an engine key. The trivial engine prints out some operations and we check for these in the log to make sure the engine was used to load the key and that it correctly got the password.
--- configure.ac | 2 + tests/unit_tests/Makefile.am | 6 +- tests/unit_tests/engine-key/Makefile.am | 14 ++++ tests/unit_tests/engine-key/check_engine_keys.sh | 30 +++++++ tests/unit_tests/engine-key/libtestengine.c | 102 +++++++++++++++++++++++ tests/unit_tests/engine-key/openssl.cnf | 12 +++ 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 tests/unit_tests/engine-key/Makefile.am create mode 100755 tests/unit_tests/engine-key/check_engine_keys.sh create mode 100644 tests/unit_tests/engine-key/libtestengine.c create mode 100644 tests/unit_tests/engine-key/openssl.cnf
configure.ac +2 -0
@@ -1360,6 +1360,7 @@ AM_CONDITIONAL([GIT_CHECKOUT], [test "${GIT_CHECKOUT}" = "yes"]) AM_CONDITIONAL([ENABLE_PLUGIN_AUTH_PAM], [test "${enable_plugin_auth_pam}" = "yes"]) AM_CONDITIONAL([ENABLE_PLUGIN_DOWN_ROOT], [test "${enable_plugin_down_root}" = "yes"]) AM_CONDITIONAL([HAVE_LD_WRAP_SUPPORT], [test "${have_ld_wrap_support}" = "yes"]) +AM_CONDITIONAL([OPENSSL_ENGINE], [test "${have_openssl_engine}" = "yes"]) sampledir="\$(docdir)/sample" AC_SUBST([plugindir])
@@ -1424,6 +1425,7 @@ AC_CONFIG_FILES([ tests/unit_tests/openvpn/Makefile tests/unit_tests/plugins/Makefile tests/unit_tests/plugins/auth-pam/Makefile + tests/unit_tests/engine-key/Makefile vendor/Makefile sample/Makefile ])
tests/unit_tests/Makefile.am +5 -1
@@ -1,5 +1,9 @@ AUTOMAKE_OPTIONS = foreign +SUBDIRS = if CMOCKA_INITIALIZED -SUBDIRS = example_test openvpn plugins +SUBDIRS += example_test openvpn plugins +endif +if OPENSSL_ENGINE +SUBDIRS += engine-key endif
tests/unit_tests/engine-key/Makefile.am new file +14 -0
@@ -0,0 +1,14 @@ +AUTOMAKE_OPTIONS = foreign + +check_LTLIBRARIES = libtestengine.la + +TESTS_ENVIRONMENT = srcdir="$(abs_srcdir)"; \ + top_builddir="$(top_builddir)"; \ + top_srcdir="$(top_srcdir)"; \ + export srcdir top_builddir top_srcdir; + +TESTS = check_engine_keys.sh + +libtestengine_la_SOURCES = libtestengine.c +libtestengine_la_LDFLAGS = -rpath /lib -avoid-version +
tests/unit_tests/engine-key/check_engine_keys.sh new file +30 -0
@@ -0,0 +1,30 @@ +#!/bin/sh + +OPENSSL_CONF="${srcdir}/openssl.cnf" +export OPENSSL_CONF + +password='AT3S4PASSWD' + +key="${srcdir}/client.key" +pwdfile="${srcdir}/passwd" + +# create an engine key for us +sed 's/PRIVATE KEY/TEST ENGINE KEY/' < ${top_srcdir}/sample/sample-keys/client.key > ${key} +echo "$password" > $pwdfile + +# note here we've induced a mismatch in the client key and the server +# cert which openvpn should report and die. Check that it does. Note +# also that this mismatch depends on openssl not openvpn, so it is +# somewhat fragile +${top_builddir}/src/openvpn/openvpn --cd ${top_srcdir}/sample --config sample-config-files/loopback-server --engine testengine --key ${key} --askpass $pwdfile > log.txt 2>&1 + +# first off check we died because of a key mismatch. If this doesn't +# pass, suspect openssl of returning different messages and update the +# test accordingly +grep -q 'X509_check_private_key:key values mismatch' log.txt || { echo "Key mismatch not detected"; exit 1; } + +# now look for the engine prints (these are under our control) +grep -q 'ENGINE: engine_init called' log.txt || { echo "Engine initialization not detected"; exit 1; } +grep -q 'ENGINE: engine_load_key called' log.txt || { echo "Key was not loaded from engine"; exit 1; } +grep -q "ENGINE: engine_load_key got password ${password}" log.txt || { echo "Key password was not retrieved by the engine"; exit 1; } +exit 0
tests/unit_tests/engine-key/libtestengine.c new file +102 -0
102 lines not on this page · raw message
tests/unit_tests/engine-key/openssl.cnf new file +12 -0
@@ -0,0 +1,12 @@ +HOME = . +openssl_conf = openssl_init + +[req] +[openssl_init] +engines = engines_section + +[engines_section] +testengine = testengine_section + +[testengine_section] +dynamic_path = $ENV::srcdir/.libs/libtestengine.so
Signature · 1 line
2.12.3