From: "selvanair (Code Review)" Date: Thu, 5 Sep 2024 17:53:15 +0000 In-Reply-To: References: Reply-To: selva.nair@...277..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="he5nuszfaiY="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Interpret --key and --cert option argument as URI List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --he5nuszfaiY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos, selvanair. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/726?usp=3Demail to look at the new patch set (#3). The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied= now. Change subject: Interpret --key and --cert option argument as URI ...................................................................... Interpret --key and --cert option argument as URI OpenSSL 3 has providers which can load keys and certificates from various key stores and HSMs using a provider-specific URI. While certificates are generally exportable, and some providers support a PEM file that acts as a proxy for non-exportable private keys, not all providers are expected to do so. A generic capability to read keys and certificates from URIs appears useful. This patch does this by extending the scope of the argument for "--key" and "--cert" options to include URIs. Many of OpenSSL 3 utilities also work the same way: e.g., the "-in" option for "openssl pkey" or "openssl x509" could be a filename or URI. Other applications have started emulating this behaviour: e.g., pkcs11: URI works as an alternative to a file name for certificates and keys in apache. Even for files, this has a nice side effect that non-PEM files get transparently parsed. E.g., a pkcs12 file could be used in place of a PEM file without needing any extra options. This is backward compatible as OpenSSL falls back to treating URIs with no scheme or unrecognized scheme as file names. Parsing of inlined keys and certificates is unchanged (those should be in PEM format). Specification of URIs that OpenSSL accepts depends on the providers that support them. Some are standard URIs such as "file:/path", but providers may support non-standard URIs with arbitrary scheme names. OpenSSL by itself recognizes only file URI. However, the implementation is agnostic to the URI specification as parsing is done by the provider that supports the URI. A new URI gets automatically recognized when the provider that supports it is loaded. Below are some usage examples: Relative or absolute path to a file or as a URI "file:/absolute/path": --key mykey.pem (same as what is currently supported) --key file:/path/to/mykey.pem --cert file:/path/to/mycert.pem Other file types supported by OpenSSL would also work: --key client.p12 --cert client.p12 pkcs11-provider supports "pkcs11:" URI (RFC 7512): --key pkcs11:token=3DFoo;id=3D%01 --cert pkcs11:token=3DFoo;id=3D%01 tpm2-provider recognizes a custom URI "handle:": --key handle:0x81000000 These examples assume that required providers, if any, are loaded and configured. v2: same as PR 591 but with the fixup commit that addresses review comments= is squashed. Change-Id: I82b32d5ab472926e7889a5f4a90caba14231879a Signed-off-by: Selva Nair --- M doc/man-sections/tls-options.rst M src/openvpn/options.c M src/openvpn/ssl_openssl.c M tests/unit_tests/openvpn/test_ssl.c 4 files changed, 280 insertions(+), 27 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/26/726/3 diff --git a/doc/man-sections/tls-options.rst b/doc/man-sections/tls-option= s.rst index de74c0d..cdb8571 100644 --- a/doc/man-sections/tls-options.rst +++ b/doc/man-sections/tls-options.rst @@ -85,10 +85,17 @@ OpenVPN will log the usual warning in the logs if the relevant CRL is missing, but the connection will be allowed. ---cert file - Local peer's signed certificate in .pem format -- must be signed by a - certificate authority whose certificate is in ``--ca file``. Each peer - in an OpenVPN link running in TLS mode should have its own certificate +--cert file|uri + Local peer's signed certificate in .pem format or as a URI -- must be + signed by a certificate authority whose certificate is in ``--ca file`` + in the peer configuration. URI is supported only when built with + OpenSSL 3.0 or later and any required providers are loaded. Types + of URIs supported and their syntax depends on providers. OpenSSL has + internal support for "file:/absolute/path" URI in which case the scheme + "file:" is optional, and any file format recognized by OpenSSL (e.g., PE= M, + PKCS12) is supported. PKCS#11 URI (RFC 7512) is supported by pkcs11-prov= ider. + + Each peer in an OpenVPN link running in TLS mode should have its own cer= tificate and private key file. In addition, each certificate should have been signed by the key of a certificate authority whose public key resides in the ``--ca`` certificate authority file. You can easily make your own @@ -203,10 +210,11 @@ The ``--hand-window`` parameter also controls the amount of time that the OpenVPN client repeats the pull request until it times out. ---key file - Local peer's private key in .pem format. Use the private key which was - generated when you built your peer's certificate (see ``--cert file`` - above). +--key file|uri + Local peer's private key in .pem format or a URI. Use the private key + which was generated when you built your peer's certificate (see + ``--cert file`` above). URI is supported only when built with OpenSSL 3.0 + or later and any required providers are loaded. (See `--cert` for more d= etails). --pkcs12 file Specify a PKCS #12 file containing local private key, local certificate, diff --git a/src/openvpn/options.c b/src/openvpn/options.c index d2ef895..36105ce 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -567,10 +567,10 @@ "--dh file : File containing Diffie Hellman parameters\n" " in .pem format (for --tls-server only).\n" " Use \"openssl dhparam -out dh1024.pem 1024\" to gen= erate.\n" - "--cert file : Local certificate in .pem format -- must be signed\= n" - " by a Certificate Authority in --ca file.\n" + "--cert file : Local certificate in .pem format or a URI -- must b= e signed\n" + " by a Certificate Authority in --ca file used by the= peer.\n" "--extra-certs file : one or more PEM certs that complete the cert cha= in.\n" - "--key file : Local private key in .pem format.\n" + "--key file : Local private key in .pem format or a URI.\n" "--tls-version-min ['or-highest'] : sets the minimum TLS ver= sion we\n" " will accept from the peer. If version is unrecognized and 'or-hi= ghest'\n" " is specified, require max TLS version supported by SSL implementa= tion.\n" @@ -3851,6 +3851,7 @@ #define CHKACC_FILEXSTWR (1<<2) /** If file exists, is it writable? */ #define CHKACC_ACPTSTDIN (1<<3) /** If filename is stdin, it's allowed an= d "exists" */ #define CHKACC_PRIVATE (1<<4) /** Warn if this (private) file is group/= others accessible */ +#define CHKACC_ACCEPT_URI (1<<5) /** If filename is a URI, no check is don= e unless it starts with file: */ static bool check_file_access(const int type, const char *file, const int mode, const = char *opt) @@ -3871,6 +3872,21 @@ return false; } + /* file name is a URI if its first segment has ":" (i.e., before any = "/") + * Then no checks done if CHKACC_ACCEPT_URI is set and the URI does no= t start with "file:" + */ + if ((type & CHKACC_ACCEPT_URI) && strchr(file, ':')) + { + if (!strncmp(file, "file:", 5)) + { + file +=3D 5; + } + else if (!strchr(file, '/') || strchr(file, '/') > strchr(file, ':= ')) + { + return false; + } + } + /* Is the directory path leading to the given file accessible? */ if (type & CHKACC_DIRPATH) { @@ -4069,7 +4085,7 @@ errs |=3D check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->ca_path, R_OK, "--capath"); - errs |=3D check_file_access_inline(options->cert_file_inline, CHKACC_F= ILE, + errs |=3D check_file_access_inline(options->cert_file_inline, CHKACC_F= ILE|CHKACC_ACCEPT_URI, options->cert_file, R_OK, "--cert"); errs |=3D check_file_access_inline(options->extra_certs_file, CHKACC_F= ILE, @@ -4079,7 +4095,7 @@ if (!(options->management_flags & MF_EXTERNAL_KEY)) { errs |=3D check_file_access_inline(options->priv_key_file_inline, - CHKACC_FILE|CHKACC_PRIVATE, + CHKACC_FILE|CHKACC_PRIVATE|CHKACC= _ACCEPT_URI, options->priv_key_file, R_OK, "--= key"); } =20 diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index e8a30a3..05555a3 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -65,6 +65,12 @@ #include #endif +#if OPENSSL_VERSION_NUMBER >=3D 0x30000000L +#define HAVE_OPENSSL_STORE_API +#include +#include +#endif + #if defined(_MSC_VER) && !defined(_M_ARM64) #include #endif @@ -768,6 +774,111 @@ #endif /* OPENSSL_NO_EC */ } +#if defined(HAVE_OPENSSL_STORE_API) +/** + * A wrapper for pem_password_callback for use with OpenSSL UI_METHOD. + */ +static int +ui_reader(UI *ui, UI_STRING *uis) +{ + SSL_CTX *ctx =3D UI_get0_user_data(ui); + + if (UI_get_string_type(uis) =3D=3D UIT_PROMPT) + { + const char *prompt =3D UI_get0_output_string(uis); + + /* If pkcs#11 Use custom prompt similar to pkcs11-helper */ + if (strstr(prompt, "PKCS#11")) + { + struct user_pass up; + get_user_pass(&up, NULL, "PKCS#11 token", GET_USER_PASS_MANAGE= MENT|GET_USER_PASS_PASSWORD_ONLY); + UI_set_result(ui, uis, up.password); + purge_user_pass(&up, true); + } + else /* use our generic 'Private Key' passphrase callback */ + { + char password[64]; + pem_password_cb *cb =3D SSL_CTX_get_default_passwd_cb(ctx); + void *d =3D SSL_CTX_get_default_passwd_cb_userdata(ctx); + + cb(password, sizeof(password), 0, d); + UI_set_result(ui, uis, password); + secure_memzero(password, sizeof(password)); + } + + return 1; + } + return 0; +} +#endif /* defined(HAVE_OPENSSL_STORE_API) */ + +/** + * Load private key from OSSL_STORE URI or file + * uri : URI of object or filename + * ssl_ctx : SSL_CTX for UI prompt + * + * Return a pointer to the key or NULL if not found. + * Caller must free the key after use. + */ +static void * +load_pkey_from_uri(const char *uri, SSL_CTX *ssl_ctx) +{ + EVP_PKEY *pkey =3D NULL; + +#if !defined(HAVE_OPENSSL_STORE_API) + + /* Treat the uri as file name */ + BIO *in =3D BIO_new_file(uri, "r"); + if (!in) + { + return NULL; + } + pkey =3D PEM_read_bio_PrivateKey(in, NULL, + SSL_CTX_get_default_passwd_cb(ssl_ctx), + SSL_CTX_get_default_passwd_cb_userdata(= ssl_ctx)); + BIO_free(in); + +#else /* defined(HAVE_OPENSSL_STORE_API) */ + + OSSL_STORE_CTX *store_ctx =3D NULL; + OSSL_STORE_INFO *info =3D NULL; + + UI_METHOD *ui_method =3D UI_create_method("openvpn"); + if (!ui_method) + { + msg(M_WARN, "OpenSSL UI creation failed"); + return NULL; + } + UI_method_set_reader(ui_method, ui_reader); + + store_ctx =3D OSSL_STORE_open_ex(uri, tls_libctx, NULL, ui_method, ssl= _ctx, + NULL, NULL, NULL); + if (!store_ctx) + { + goto end; + } + if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_PKEY) !=3D 1) + { + goto end; + } + info =3D OSSL_STORE_load(store_ctx); + if (!info) + { + goto end; + } + pkey =3D OSSL_STORE_INFO_get1_PKEY(info); + OSSL_STORE_INFO_free(info); + msg(D_TLS_DEBUG_MED, "Found pkey in store using URI: %s", uri); + +end: + OSSL_STORE_close(store_ctx); + UI_destroy_method(ui_method); + +#endif /* defined(HAVE_OPENSSL_STORE_API) */ + + return pkey; +} + int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file) @@ -945,9 +1056,103 @@ } } -void -tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, - bool cert_file_inline) +static bool +cert_uri_supported(void) +{ +#if defined(HAVE_OPENSSL_STORE_API) + return 1; +#else + return 0; +#endif +} + +static void +tls_ctx_load_cert_uri(struct tls_root_ctx *tls_ctx, const char *uri) +{ +#if defined(HAVE_OPENSSL_STORE_API) + X509 *x =3D NULL; + int ret =3D 0; + OSSL_STORE_CTX *store_ctx =3D NULL; + OSSL_STORE_INFO *info =3D NULL; + + ASSERT(NULL !=3D tls_ctx); + + UI_METHOD *ui_method =3D UI_create_method("openvpn"); + if (!ui_method) + { + msg(M_WARN, "OpenSSL UI method creation failed"); + goto end; + } + UI_method_set_reader(ui_method, ui_reader); + + store_ctx =3D OSSL_STORE_open_ex(uri, tls_libctx, NULL, ui_method, tls= _ctx->ctx, + NULL, NULL, NULL); + if (!store_ctx) + { + goto end; + } + if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_CERT) !=3D 1) + { + goto end; + } + + info =3D OSSL_STORE_load(store_ctx); + if (!info) + { + goto end; + } + + x =3D OSSL_STORE_INFO_get0_CERT(info); + if (x =3D=3D NULL) + { + goto end; + } + msg(D_TLS_DEBUG_MED, "Found cert in store using URI: %s", uri); + + ret =3D SSL_CTX_use_certificate(tls_ctx->ctx, x); + if (!ret) + { + goto end; + } + OSSL_STORE_INFO_free(info); + + /* iterate through the store and add extra certificates if any to the = chain */ + info =3D OSSL_STORE_load(store_ctx); + while (info && !OSSL_STORE_eof(store_ctx)) + { + x =3D OSSL_STORE_INFO_get1_CERT(info); + if (x && SSL_CTX_add_extra_chain_cert(tls_ctx->ctx, x) !=3D 1) + { + X509_free(x); + crypto_msg(M_FATAL, "Error adding extra certificate"); + break; + } + OSSL_STORE_INFO_free(info); + info =3D OSSL_STORE_load(store_ctx); + } + +end: + if (!ret) + { + crypto_print_openssl_errors(M_WARN); + crypto_msg(M_FATAL, "Cannot load certificate from URI <%s>", uri); + } + else + { + crypto_print_openssl_errors(M_DEBUG); + } + + UI_destroy_method(ui_method); + OSSL_STORE_INFO_free(info); + OSSL_STORE_close(store_ctx); +#else /* defined(HAVE_OPENSSL_STORE_API */ + ASSERT(0); +#endif /* defined(HAVE_OPENSSL_STORE_API */ +} + +static void +tls_ctx_load_cert_pem_file(struct tls_root_ctx *ctx, const char *cert_file, + bool cert_file_inline) { BIO *in =3D NULL; X509 *x =3D NULL; @@ -961,7 +1166,7 @@ } else { - in =3D BIO_new_file(cert_file, "r"); + in =3D BIO_new_file((char *) cert_file, "r"); } if (in =3D=3D NULL) @@ -1007,6 +1212,20 @@ X509_free(x); } +void +tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, + bool cert_file_inline) +{ + if (cert_uri_supported() && !cert_file_inline) + { + tls_ctx_load_cert_uri(ctx, cert_file); + } + else + { + tls_ctx_load_cert_pem_file(ctx, cert_file, cert_file_inline); + } +} + int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline) @@ -1023,21 +1242,19 @@ if (priv_key_file_inline) { in =3D BIO_new_mem_buf((char *) priv_key_file, -1); + if (in =3D=3D NULL) + { + goto end; + } + pkey =3D PEM_read_bio_PrivateKey(in, NULL, + SSL_CTX_get_default_passwd_cb(ctx->= ctx), + SSL_CTX_get_default_passwd_cb_userd= ata(ctx->ctx)); } else { - in =3D BIO_new_file(priv_key_file, "r"); + pkey =3D load_pkey_from_uri(priv_key_file, ssl_ctx); } - if (!in) - { - goto end; - } - - pkey =3D PEM_read_bio_PrivateKey(in, NULL, - SSL_CTX_get_default_passwd_cb(ctx->ctx), - SSL_CTX_get_default_passwd_cb_userdata(= ctx->ctx)); - if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) { #ifdef ENABLE_MANAGEMENT diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index a4b2101..da50e55 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -66,6 +66,18 @@ } #endif +/* stubs for some unused functions instead of pulling in too many dependen= cies */ +bool +get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *= prefix, + const unsigned int flags, const char *auth_challenge) +{ + return false; +} +void +purge_user_pass(struct user_pass *up, bool force) +{ + return; +} const char *unittest_cert =3D "-----BEGIN CERTIFICATE-----\n" "MIIBuTCCAUCgAwIBAgIUTLtjSBzx53qZRvZ6Ur7D9kgoO= HkwCgYIKoZIzj0EAwIw\n" -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/726?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I82b32d5ab472926e7889a5f4a90caba14231879a Gerrit-Change-Number: 726 Gerrit-PatchSet: 3 Gerrit-Owner: selvanair Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-Reviewer: selvanair Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: selvanair Gerrit-MessageType: newpatchset --he5nuszfaiY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos, selvanair.

selvanair uploaded patch set #3 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/726?usp=3Demail">View Chang= e

The change is no longer submittable: checks~ChecksSubmitRule is= unsatisfied now.

Interpret --key and --cert option argument as URI

= OpenSSL 3 has providers which can load keys and certificates
from variou= s key stores and HSMs using a provider-specific URI.
While certificates = are generally exportable, and some providers
support a PEM file that act= s as a proxy for non-exportable private
keys, not all providers are expe= cted to do so. A generic capability
to read keys and certificates from U= RIs appears useful.

This patch does this by extending the scope of t= he argument for
"--key" and "--cert" options to incl= ude URIs. Many of OpenSSL 3
utilities also work the same way: e.g., the = "-in" option for
"openssl pkey" or "openssl x50= 9" could be a filename or URI.
Other applications have started emul= ating this behaviour:
e.g., pkcs11: URI works as an alternative to a fil= e name for
certificates and keys in apache. Even for files, this has a n= ice
side effect that non-PEM files get transparently parsed. E.g., a
= pkcs12 file could be used in place of a PEM file without needing
any ext= ra options.

This is backward compatible as OpenSSL falls back to tre= ating URIs
with no scheme or unrecognized scheme as file names.

P= arsing of inlined keys and certificates is unchanged (those
should be in= PEM format).

Specification of URIs that OpenSSL accepts depends on = the
providers that support them. Some are standard URIs such as
"= ;file:/path", but providers may support non-standard URIs
with arbi= trary scheme names. OpenSSL by itself recognizes
only file URI. However= , the implementation is agnostic to the
URI specification as parsing is = done by the provider that supports
the URI. A new URI gets automatically= recognized when the provider
that supports it is loaded.

Below a= re some usage examples:

Relative or absolute path to a file or as a = URI "file:/absolute/path":

--key mykey.pem (same a= s what is currently supported)
--key file:/path/to/mykey.pem
--= cert file:/path/to/mycert.pem

Other file types supported by OpenSSL = would also work:

--key client.p12
--cert client.p12

= pkcs11-provider supports "pkcs11:" URI (RFC 7512):

--ke= y pkcs11:token=3DFoo;id=3D%01
--cert pkcs11:token=3DFoo;id=3D%01
<= br>tpm2-provider recognizes a custom URI "handle:<hex>":
--key handle:0x81000000

These examples assume that required = providers, if any, are loaded
and configured.

v2: same as PR 591 = but with the fixup commit that addresses review comments is squashed.
Change-Id: I82b32d5ab472926e7889a5f4a90caba14231879a
Signed-off-by: Se= lva Nair <selva.nair@...277...>
---
M doc/man-sections/tls-opti= ons.rst
M src/openvpn/options.c
M src/openvpn/ssl_openssl.c
M test= s/unit_tests/openvpn/test_ssl.c
4 files changed, 280 insertions(+), 27 d= eletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/=
changes/26/726/3
diff --git a/doc/man-sections/tls-options.rst b/do=
c/man-sections/tls-options.rst
index de74c0d..cdb8571 10064= 4
--- a/doc/man-sections/tls-options.rst
++= + b/doc/man-sections/tls-options.rst
@@ -85,10 +85,17 @@
OpenVPN will log the usual warning in the logs if the rele= vant CRL is
missing, but the connection will be allowed.=

---cert file
- Local peer's signed certificate in .pem for= mat -- must be signed by a
- certificate authority whose certificate is in ``--ca file``. Each pe= er
- in an OpenVPN lin= k running in TLS mode should have its own certificate
+--cert file|uri
+ Local peer's signed certificate in .pem= format or as a URI -- must be
+ signed by a certificate authority whose certificate is in ``--= ca file``
+ in the p= eer configuration. URI is supported only when built with
+ OpenSSL 3.0 or later and any require= d providers are loaded. Types
+ of URIs supported and their syntax depends on providers. OpenSS= L has
+ internal sup= port for "file:/absolute/path" URI in which case the scheme
+ "file:" is op= tional, and any file format recognized by OpenSSL (e.g., PEM,
+ PKCS12) is supported. PKCS#11 U= RI (RFC 7512) is supported by pkcs11-provider.

+
+ Each peer in an OpenVPN link running in TLS mode should have its = own certificate
and private key file. In addition, each = certificate should have been
signed by the key of a cert= ificate authority whose public key resides in
the ``--ca= `` certificate authority file. You can easily make your own
@@ -203,10 +210,11 @@

The ``--hand-window`` parameter a= lso controls the amount of time that
the OpenVPN client = repeats the pull request until it times out.

---key file
- Loca= l peer's private key in .pem format. Use the private key which was
- generated when you built= your peer's certificate (see ``--cert file``
- above).
+--key file|uri
+ Local peer's private key in .pem format or a URI. Use th= e private key
+ whic= h was generated when you built your peer's certificate (see
<= span style=3D"color: hsl(120, 100%, 40%);">+ ``--cert file`` above). URI i= s supported only when built with OpenSSL 3.0

+ or later and any required providers are loaded. = (See `--cert` for more details).

--pkcs1= 2 file
Specify a PKCS #12 file containing local private = key, local certificate,
diff --git a/src/openvpn/options.c = b/src/openvpn/options.c
index d2ef895..36105ce 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/op= tions.c
@@ -567,10 +567,10 @@
"--= dh file : File containing Diffie Hellman parameters\n" " in .pem format (for --tls-server only).= \n"
" Use \"openssl dh= param -out dh1024.pem 1024\" to generate.\n"
- "--cert file : Local certific= ate in .pem format -- must be signed\n"
- " by a Certificate Authori= ty in --ca file.\n"
+ "--cert file : Local certificate in .pem format or a URI = -- must be signed\n"
+ " by a Certificate Authority in --ca file u= sed by the peer.\n"
"--extra-certs file : on= e or more PEM certs that complete the cert chain.\n"
- "--key file : Local priva= te key in .pem format.\n"
+ "--key file : Local private key in .pem format or = a URI.\n"
"--tls-version-min <version>= ['or-highest'] : sets the minimum TLS version we\n" " will accept from the peer. If version is unrecognize= d and 'or-highest'\n"
" is specif= ied, require max TLS version supported by SSL implementation.\n"
@@ -3851,6 +3851,7 @@
#define CHKACC_FILEXSTWR = (1<<2) /** If file exists, is it writable? */
#defi= ne CHKACC_ACPTSTDIN (1<<3) /** If filename is stdin, it's allowe= d and "exists" */
#define CHKACC_PRIVATE (1<&= lt;4) /** Warn if this (private) file is group/others accessible */
+#define CHKACC_ACCEPT_UR= I (1<<5) /** If filename is a URI, no check is done unless it starts = with file: */

static bool
check_file_access(const int type, const char *file, const int mode, cons= t char *opt)

@@ -3871,6 +3872,21 @@
= return false;
}

+ /* file name is a URI if its first= segment has ":" (i.e., before any "/")
+ * Then no checks done if CHKA= CC_ACCEPT_URI is set and the URI does not start with "file:"
+ */
+ if ((type & CHKACC_ACCEPT_UR= I) && strchr(file, ':'))
+ {
+ if (!strncmp(file, "file:", 5))
+ {
+ file +=3D 5;
+ }
+ else if (!strchr(file, '/') || strc= hr(file, '/') > strchr(file, ':'))
+ {
+ return false;
+ }
+ }
+
/* Is the directory path leading to the given f= ile accessible? */
if (type & CHKACC_DIRPATH)
{
@@ -4069,7 +4085,7 @@
= errs |=3D check_file_access_chroot(options->chroot_dir, CHKACC_FILE,<= /span>
options->ca_path, = R_OK, "--capath");

- errs |=3D check_file_access_inline(options->= ;cert_file_inline, CHKACC_FILE,
+ errs |=3D check_file_access_inline(options->cert_file_in= line, CHKACC_FILE|CHKACC_ACCEPT_URI,
= options->cert_file, R_OK, "--cert");
errs |=3D check_file_access_inline(options-&g= t;extra_certs_file, CHKACC_FILE,
@@ -4079,7 +4095,7 @@
if (!(options->management_flags & MF_EXTERNAL_KEY))=
{
errs |=3D check_file_acces= s_inline(options->priv_key_file_inline,
- CHKACC_FILE|C= HKACC_PRIVATE,
+ = CHKACC_FILE|CHKACC_PRIVATE|CHKACC_ACCEP= T_URI,
options-&g= t;priv_key_file, R_OK, "--key");
}
diff --git a/src/openvpn/ssl_openssl.c b/src/openv= pn/ssl_openssl.c
index e8a30a3..05555a3 100644
--- a/src/openvpn/ssl_openssl.c

+++ b/src/openvpn/ssl_o= penssl.c
@@ -65,6 +65,12 @@
#include <o= penssl/ec.h>
#endif

+#if OPENSSL_VERSION_NUMBER >=3D 0x= 30000000L
+#define HA= VE_OPENSSL_STORE_API
= +#include <openssl/ui.h>
+#include <openssl/store.h>
+#endif
+
#if defined(_MSC_VER) && !defined(_M_A= RM64)
#include <openssl/applink.c>
= #endif
@@ -768,6 +774,111 @@
#endif /* OPE= NSSL_NO_EC */
}

+#if defined(HAVE_OPENSSL_STORE_API)+/**
+ * A wrapper for pem_password_callback for u= se with OpenSSL UI_METHOD.
+ */
+static in= t
+ui_reader(UI *ui, = UI_STRING *uis)
+{

+ SSL_CTX *ctx =3D U= I_get0_user_data(ui);
+
+ if (UI_get_st= ring_type(uis) =3D=3D UIT_PROMPT)
+ {
+= const char *prompt =3D UI_get0_output_string(uis);
+
+ /* If pkcs#11 Use custom prompt similar to pkcs= 11-helper */
+ = if (strstr(prompt, "PKCS#11"))
+ {
+ struct user_pass up;
+ get_user_pass(&up, NULL, "PKC= S#11 token", GET_USER_PASS_MANAGEMENT|GET_USER_PASS_PASSWORD_ONLY);
+ UI_set_res= ult(ui, uis, up.password);
+ purge_user_pass(&up, true);
+ }
+ else /* use our generic 'Private Key' = passphrase callback */
+ = char password[64];
+ pem_password_cb *cb =3D SSL_CTX_get_default_passwd_cb(ctx);=
+ void *d= =3D SSL_CTX_get_default_passwd_cb_userdata(ctx);
+
+ cb(password, sizeof(password), 0, d);
+ UI_set_result(ui, uis,= password);

+ = secure_memzero(password, sizeof(password));
+ }
+
+ = return 1;
+ = }
+ return 0;
+}
+#endif /* defined(HAVE_OPENSSL_STORE_API)= */
+
+/**

+ * Load private key from OSSL_STORE URI or file
+ * uri : URI of object = or filename
+ * ssl_c= tx : SSL_CTX for UI prompt
+ *
+ * Return = a pointer to the key or NULL if not found.
+ * Caller must free the key after use.
+ */

+static void *
+load_pkey_from_uri(const char *uri, SSL_CTX *ssl_ctx)
+{
+ EVP_PKEY *pkey =3D NULL;
= +
+#if !defined(HAVE_OPENSSL_STORE_API)
+

+ /* Treat the uri as file name */
+ BIO *in =3D BIO_new_file(uri, = "r");
+ = if (!in)
+ {
+ return NULL;

+ }
+ pkey =3D PEM_read_bio_PrivateKey(i= n, NULL,
+ = SSL_CTX_get_default_passwd_cb(ssl_ctx),
<= span style=3D"color: hsl(120, 100%, 40%);">+ = SSL_CTX_get_default_passwd_cb_userdata(ssl_ctx));
+ BIO_free(in);
+
+#else /* defined(HAVE_OPENSSL_STORE_API) */
+
+ OSSL_STORE_CTX *store_ctx =3D NULL;
+ OSSL_STORE_INFO *info =3D NULL;=
+
+ UI_METHOD *ui_method =3D UI_create= _method("openvpn");
+ if (!ui_method)
+ {
+ = msg(M_WARN, "OpenSSL UI creation failed");
+ return NULL;
+ }
+ UI_method_set_reader(ui_method, ui_reader);
+
+ store_ctx =3D OSSL_STORE_open_ex(uri,= tls_libctx, NULL, ui_method, ssl_ctx,
+ NULL, NULL, NULL);
+ if (!store_ctx)
+ {
+ goto end;
+ }
+ if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO= _PKEY) !=3D 1)
+ {=
+ goto end;
+ }
+ info =3D OSSL_STORE_load(store_= ctx);

+ if (!info)=
+ {
+ goto end;

+ }

+ pkey =3D OSSL_STORE_INFO_get1_PKEY(info);
+ OSSL_STORE_INFO_fre= e(info);
+ msg(D_T= LS_DEBUG_MED, "Found pkey in store using URI: %s", uri);+
+end:
+ OSSL_STORE_close(store_ctx);
+ UI_destroy_method(ui_method);
+

+#endif /* defined(HAVE_OPENSSL_STORE_API) */+
+ return pkey;
+}
+
int
tls_ctx_load_pkcs12(struct tls= _root_ctx *ctx, const char *pkcs12_file,
= bool pkcs12_file_inline, bool load_ca_file)
@@ -945,9 +1= 056,103 @@
}
}

-void
-tls_ctx_load_cert_file(struct tls_root_ctx = *ctx, const char *cert_file,
- bool cert_file_inline)
+static bool
+cert_uri_supported(void)
+{
+#if defined(HAVE_OPENSSL_STORE_API)
+ return 1;
+#else
+ return 0;
+}+
+static void
+tls_ctx_load_cert_uri(struct tls_root_ctx *tls_ctx, con= st char *uri)
+{
+#if defined(HAVE_OPENSSL= _STORE_API)
+ X509= *x =3D NULL;
+ in= t ret =3D 0;
+ OSS= L_STORE_CTX *store_ctx =3D NULL;
+ OSSL_STORE_INFO *info =3D NULL;
+
+ ASSERT(NULL !=3D tls_ctx);
+
+ UI_METHOD *ui_method =3D UI_create_method("openvpn");

+ if (!ui_method)
+ {
+ msg(M_WARN, "OpenSSL UI me= thod creation failed");
+ goto end;
+ }
+ UI_= method_set_reader(ui_method, ui_reader);
+
+ store_ctx =3D OSSL_STORE_open_ex(uri, tls_libctx, NULL, ui_method, tl= s_ctx->ctx,
+ = NULL, NULL, NULL);
+ if (!store_ctx)
+ {
+ goto end;
+ }
+ = if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_CERT) !=3D 1)+ {
+ goto end;
+ }
+
= + info =3D OSSL_STORE_load(store_ctx);
+ if (!info)
+ {
= + }
+
+ x =3D OSSL_STORE_INFO_get0_C= ERT(info);

+ if (x= =3D=3D NULL)
+ {<= /span>
+ goto end;
+ }
+ msg(D_TLS_DEBUG_MED, "Found= cert in store using URI: %s", uri);
+
+ if (!ret)
+ {
+ goto end;
+ }
+ OSSL_STORE_INFO_free(info);
+
+ = /* iterate through the store and add extra certificates if any to the chai= n */
+ info =3D OS= SL_STORE_load(store_ctx);
+ while (info && !OSSL_STORE_eof(store_ctx))
+ {

+ x =3D OSSL_STORE_INFO_get1_CERT(info);
+ if (x &&a= mp; SSL_CTX_add_extra_chain_cert(tls_ctx->ctx, x) !=3D 1)
+ {

+ X509_free(x);
+ crypto_msg(M_FATAL, "= Error adding extra certificate");
+ break;
+ }
+ OSSL_STORE_INFO_free(info);
+ info =3D OSSL_STORE_load(store_ctx);=
+ }
+
+end:
= + if (!ret)
+ {=
+ crypto_prin= t_openssl_errors(M_WARN);
+ crypto_msg(M_FATAL, "Cannot load certificate from URI &l= t;%s>", uri);
+ }
+ else
+ {
+ crypto_print_openssl_errors(M= _DEBUG);
+ }
+
+ UI_destroy_method(ui_method);
= + OSSL_STORE_INFO_free(info)= ;
+ OSSL_STORE_clo= se(store_ctx);
+#else= /* defined(HAVE_OPENSSL_STORE_API */
+ ASSERT(0);
+#endif /* defined(HAVE_OPENSSL_STORE_API */
+}
+
+= static void
+tls_ctx_= load_cert_pem_file(struct tls_root_ctx *ctx, const char *cert_file,<= br>+ = bool cert_file_inline)
{
BIO *in =3D = NULL;
X509 *x =3D NULL;
@@ -961,7 +116= 6,7 @@
}
else
= {
- in =3D BIO= _new_file(cert_file, "r");
+ in =3D BIO_new_file((char *) cert_file, "r&qu= ot;);
}

if (in = =3D=3D NULL)
@@ -1007,6 +1212,20 @@
X5= 09_free(x);
}

+void
+tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *= cert_file,
+ = bool cert_file_inline)
+{
+ = if (cert_uri_supported() && !cert_file_inline)
+ {
+ tls_ctx_load_cert_uri(ctx, cert_file);
+ }
+ else
+ {
+ tls_ctx_load_cert_pem_file(ctx, cert_file, cert_file_in= line);
+ }<= br>+}
+
int
tls_ct= x_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
bool priv_key_file_inline)
@@ -1023,21 +1242,19 @@

if (priv_key_file_inline)
{
in =3D BIO_new_mem_buf((char= *) priv_key_file, -1);
+ if (in =3D=3D NULL)
+ {
+ goto end;
+ }
+ = pkey =3D PEM_read_bio_PrivateKey(in, NULL,
+ SSL_CTX_get= _default_passwd_cb(ctx->ctx),
+ SSL_CTX_get_default_pas= swd_cb_userdata(ctx->ctx));
}
= else
{
- in =3D BIO_new_file(priv_key_file, "r");
= + pkey =3D load_pkey_fro= m_uri(priv_key_file, ssl_ctx);
}

- if (!in)
- {

- goto end;
- }
-
- pkey =3D PEM= _read_bio_PrivateKey(in, NULL,
- SSL_CTX_get_default_passwd_cb(c= tx->ctx),
- = SSL_CTX_get_default_passwd_cb_userdata(ctx->ct= x));
-
= if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
= {
#ifdef ENABLE_MANAGEMENT
diff --git = a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c=
index a4b2101..da50e55 100644
--- a/tests/= unit_tests/openvpn/test_ssl.c
+++ b/tests/unit_tests/openvp= n/test_ssl.c
@@ -66,6 +66,18 @@
} #endif

+/* stubs for some unused functions instead of pulling in too = many dependencies */
= +bool
+get_user_pass_= cr(struct user_pass *up, const char *auth_file, const char *prefix,<= br>+ const unsi= gned int flags, const char *auth_challenge)
+{
+ return false;
+}
+void+purge_user_pass(struct user_p= ass *up, bool force)
= +{
+ return;
+}

const char *unittest_cert =3D "-----BEGIN CERTIFICATE-----= \n"
"MIIBuTCCAUCgAwI= BAgIUTLtjSBzx53qZRvZ6Ur7D9kgoOHkwCgYIKoZIzj0EAwIw\n"
<= /span>

To view, visit change 726. To unsubscribe, or for help writin= g mail filters, visit settin= gs.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I82b32d5ab472926e7889a5f4a90= caba14231879a
Gerrit-Change-Number: 726
Gerrit-PatchSet: 3
Gerrit-Owner: selvanair <selva.nair@...1354...= ..>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-Reviewer: selvanair <selva.nair@...1665...= 77...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: selvanair <selva.nair@...493...= 277...>
Gerrit-MessageType: newpatchset
--he5nuszfaiY=--