From patchwork Sun Aug 15 12:25:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Selva Nair X-Patchwork-Id: 1913 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id ePFMNdGUGWEENQAAIUCqbw (envelope-from ) for ; Sun, 15 Aug 2021 18:27:29 -0400 Received: from proxy14.mail.iad3b.rsapps.net ([172.31.255.6]) by director11.mail.ord1d.rsapps.net with LMTP id OLwmNdGUGWHQAgAAvGGmqA (envelope-from ) for ; Sun, 15 Aug 2021 18:27:29 -0400 Received: from smtp24.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy14.mail.iad3b.rsapps.net with LMTPS id CEDdL9GUGWF5cQAA+7ETDg (envelope-from ) for ; Sun, 15 Aug 2021 18:27:29 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp24.gate.iad3b.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=gmail.com; dmarc=fail (p=none; dis=none) header.from=gmail.com X-Suspicious-Flag: YES X-Classification-ID: f96e04d6-fe17-11eb-be6a-525400892b35-1-1 Received: from [216.105.38.7] ([216.105.38.7:45404] helo=lists.sourceforge.net) by smtp24.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id B7/0D-05708-1D499116; Sun, 15 Aug 2021 18:27:29 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.92.3) (envelope-from ) id 1mFOaA-0000Qy-Hx; Sun, 15 Aug 2021 22:26:18 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) (envelope-from ) id 1mFOa9-0000Qr-I1 for openvpn-devel@lists.sourceforge.net; Sun, 15 Aug 2021 22:26:17 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=rNqMEg8LodVzV3JpZBV887Rxe78fov7qoS1Vho6d9FE=; b=PIs03teG5gKatBbyp3IENXNHy+ KEHtqKuYbKted2bHx/89TUTIfKfDaN/wfaPfOGKpmVer+FJZyM5wcVzTZp2WKwFgIGcoJpxGiicHs K2WkcVryptDVDq1pb7IEh2Nf+XfZkF5n3toTOzMtn8GhDL0CKeMC5j0lAkIVHplSWTXw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=rNqMEg8LodVzV3JpZBV887Rxe78fov7qoS1Vho6d9FE=; b=iYA+ptQEmm+1WhcYDiZ+jc8Wc0 SkdiGv9ixWEDJVMXY+jLD6PkzQKxCAjh/GeBf0tFoAMp06DgF3y9qRryx35ZJ7O1CP2ElXD1p6awi xQsx38HxqfEf+SQ8iT03ogdKe2FY9xvJLHpjD8cTVzEUq6M4FR8wqwo7xZUmhbA504uI=; Received: from mail-io1-f43.google.com ([209.85.166.43]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) id 1mFOa5-0005yA-JG for openvpn-devel@lists.sourceforge.net; Sun, 15 Aug 2021 22:26:17 +0000 Received: by mail-io1-f43.google.com with SMTP id t13so7058973ioj.7 for ; Sun, 15 Aug 2021 15:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rNqMEg8LodVzV3JpZBV887Rxe78fov7qoS1Vho6d9FE=; b=JlqEE2IB5D+nCYeSq0p7jd8IMZPz9brFysImU84VwT1oOfwHSRwhJulJobxM8RHicB i9Lm37tYsBZE643ACeXq2jAicuIi7qJ1Z/tu5VpljsIO1o41fyRN3wFFP96FR+M/y8eG HGg8oAeWcVQhuI5L+EztzGinwmTnR363KLxZzXAoF0xDlDuIdjpWzeJFnXvF6a61z8XD feDtuFRcExQq1veLJQNrG6Xq8jo8R8w0p0vgXogyNxkzfjpMlpoproX7GGjAccNnYZHQ /7cZRfd47x2ZB5DyymfL8la1f0O7gnzS5iQYgEcUvQMpWcbIhzTNxJragGJkN75bjoYy NLdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rNqMEg8LodVzV3JpZBV887Rxe78fov7qoS1Vho6d9FE=; b=EYV3Wg5db+ehgJF6JJkjwp+y4aP1WfA57DcEyI//jxnczXCqgqBdwdt7u787Jmgyln ig4wTmG8R9udQbCUQSTBaUPjosZerxaj6xmuhYhmvIeZPtuyfYOtpKdJR0HOB+CSIF9a rUzLZfucQK4YALXP44rnP+KCoDXtlVPGqTkOqJ/4S5KHNjwJ5OdtK0lI9HrKi2ID2OZV yJpH+In2rspxq9GoViJ3UCF/gYJqW4w4F07A7O1loXjrl56CARkAsTULembuChh/LQuL uHbotlU0sK5QLfbjqzPtQWhdny9hRhxkpxQt6zF2tKvyf2/RXiDoiNFcpzvOtsr/Otks puPA== X-Gm-Message-State: AOAM530MyaM60vwHGdteOZxwolet0VE/E9iwgzSfb4OWQfRw8vQPzI76 e5hHbj3NQkl40JIpwxRXWiFMqm2qpTuO/A== X-Google-Smtp-Source: ABdhPJwGyzxGyB19kyLProFuaLEosewy06WcCA6pvY0Aq6orjGoO9b5hEBHgKhGw58/7SFVIs14dzw== X-Received: by 2002:a02:cb46:: with SMTP id k6mr12782332jap.15.1629066367693; Sun, 15 Aug 2021 15:26:07 -0700 (PDT) Received: from scala.nanotech.utoronto.ca (scala.nanotech.utoronto.ca. [128.100.226.29]) by smtp.gmail.com with ESMTPSA id d4sm4507703ilo.29.2021.08.15.15.26.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Aug 2021 15:26:07 -0700 (PDT) From: selva.nair@gmail.com To: openvpn-devel@lists.sourceforge.net Date: Sun, 15 Aug 2021 18:25:58 -0400 Message-Id: <20210815222558.22502-1-selva.nair@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210510013239.18087-1-selva.nair@gmail.com> References: <20210510013239.18087-1-selva.nair@gmail.com> MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (selva.nair[at]gmail.com) -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.166.43 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.166.43 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1mFOa5-0005yA-JG Subject: [Openvpn-devel] [PATCH v3] Allow PKCS#11 uri to be used as --cert and --key file names X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Selva Nair v2 changes - do not allow so-path embedded in cert and key uri - add --pkcs11-engine option to optionally specify the engine and provider module to use v3: rebase to master If either --cert or --key is specified as a PKCS#11 uri, try to load the certificate and key from any accessible PKCS#11 device. This does not require linking with any pkcs11 library, but needs pkcs11 engine to be available on the target machine. In its simplest form, just have --cert 'pkcs11:id=%01' Either do not specify --key, or use the same uri for --key. Do not include type=cert or type=private in the uri as the same uri is used for both certificate and private key. That's all what is required if pkcs11 engine is installed in the right location and optionally set up to load any necessary provider libraries (e.g., via openssl.cnf or via PKCS11_MODULE_PATH). If both cert and key are specified, the last entry takes precedence and is used to locate both the certificate and key. Use of different uri's for the cert and key are not supported. Specifying --cert as a file and --key as a uri or vice versa is treated as a usage error. If the engine cannot be automatically loaded, or a custom engine object has to be loaded, the engine name or shared library may be specified using the newly added option --pkcs11-engine engine [module_path] Here engine may the the engine-id that OpenSSL is configured to locate, or the path to a shared object. The optional 'module_path' specifies any provider module that must be loaded. It must be given as a path. Use full path or relative path for these shared objects based on the target system setup. Requires building with OpenSSL engine support although the pkcs11 or a compatible engine, and provider libraries are required only at run time. Signed-off-by: Selva Nair --- Changes.rst | 6 + doc/man-sections/tls-options.rst | 30 +++++ src/openvpn/options.c | 68 +++++++++++- src/openvpn/options.h | 7 ++ src/openvpn/ssl.c | 15 ++- src/openvpn/ssl_backend.h | 10 ++ src/openvpn/ssl_openssl.c | 183 ++++++++++++++++++++++++++++++- 7 files changed, 315 insertions(+), 4 deletions(-) diff --git a/Changes.rst b/Changes.rst index 0323a7f7..a556de44 100644 --- a/Changes.rst +++ b/Changes.rst @@ -4,6 +4,12 @@ Overview of changes in 2.6 New features ------------ +Specification of private key and certificates as PKCS#11 URI + ``--cert`` and ``--key`` options can take RFC7512 PKCS#11 + URI's pointing to certificate and key in a token. Both cert + and key must use the same URI. Requires OpenSSL with engine + support and pkcs11 (or compatible) engine installed. + Keying Material Exporters (RFC 5705) based key generation As part of the cipher negotiation OpenVPN will automatically prefer the RFC5705 based key material generation to the current custom diff --git a/doc/man-sections/tls-options.rst b/doc/man-sections/tls-options.rst index 00ea063a..ccc06a2e 100644 --- a/doc/man-sections/tls-options.rst +++ b/doc/man-sections/tls-options.rst @@ -116,6 +116,19 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa authority functions, you must set up the files :code:`index.txt` (may be empty) and :code:`serial` (initialize to :code:`01`). +--cert pkcs11-uri + The local peer's certificate in a PKCS#11 token specified as a RFC 7512 + uri. Cannot be used with ``--key file``. ``--key`` must be left unspecified + or point to the same uri. All other requrements for the certificate + described under ``--cert file`` applies. + + Requires OpenSSL with pkcs11 engine installed and configured. Also see + the option ``--pkcs11-engine``. + + As the same uri is used for certificate and private key, do not include type + attribute (e.g., :code: `type=cert;` or :code: `type=private;` should not + be included) + --crl-verify args Check peer certificate against a Certificate Revocation List. @@ -208,11 +221,28 @@ certificates and keys: https://github.com/OpenVPN/easy-rsa generated when you built your peer's certificate (see ``--cert file`` above). +--key pkcs11-uri + See ``--cert pkcs11-uri`` above. + --pkcs12 file Specify a PKCS #12 file containing local private key, local certificate, and root CA certificate. This option can be used instead of ``--ca``, ``--cert``, and ``--key``. Not available with mbed TLS. +--pkcs11-engine engine-name [module-path] + Specifiy the pkcs11-engine and the provider module to load when + certificate and private key are given as a pkcs11 URI. + + If the option is unspecified, and a pkcs11 URI is used for cert/key, + :code:`pkcs11` engine is loaded, if it can be automatically found by + OpenSSL. + + If specified, the cert/key must be given as a pkcs11 URI. + + The engine name could be a valid engine-id or path to a shared object. + The module-path should be the path to a shared object. Objects in + non-standard locations would need to be specified as full paths. + --remote-cert-eku oid Require that peer certificate was signed with an explicit *extended key usage*. diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 7e146db9..4933546c 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -530,6 +530,11 @@ static const char usage_message[] = " nonce_secret_len=nsl. Set alg=none to disable PRNG.\n" #ifndef ENABLE_CRYPTO_MBEDTLS "--engine [name] : Enable OpenSSL hardware crypto engine functionality.\n" +#endif +#ifdef HAVE_OPENSSL_ENGINE + "--pkcs11-engine name [module-path] : PKCS11 engine and provider module to use\n" + " for cert and key specified as a pkcs11 URI.\n" + " name could be an engine-id or a path.\n" #endif "--no-replay : (DEPRECATED) Disable replay protection.\n" "--mute-replay-warnings : Silence the output of replay warnings to log file.\n" @@ -913,6 +918,12 @@ struct pull_filter_list struct pull_filter *tail; }; +static bool +is_pkcs11_uri(const char *uri) +{ + return (uri && !strncmp(uri, "pkcs11:", 7)); +} + static const char * pull_filter_type_name(int type) { @@ -2606,6 +2617,17 @@ options_postprocess_verify_ce(const struct options *options, if (options->tls_server || options->tls_client) { check_ca_required(options); +#ifdef HAVE_OPENSSL_ENGINE + if (is_pkcs11_uri(options->cert_file) != is_pkcs11_uri(options->priv_key_file)) + { + msg(M_USAGE, "Use of PKCS#11 uri for --cert or --key and file name for the other is not supported"); + } + else + if (options->pkcs11_engine && !is_pkcs11_uri(options->cert_file)) + { + msg(M_USAGE, "Use of --pkcs11-engine expects --cert to be specified as a pkcs11: URI"); + } +#endif #ifdef ENABLE_PKCS11 if (options->pkcs11_providers[0]) { @@ -3445,8 +3467,11 @@ options_postprocess_filechecks(struct options *options) errs |= check_file_access_chroot(options->chroot_dir, CHKACC_FILE, options->ca_path, R_OK, "--capath"); - errs |= check_file_access_inline(options->cert_file_inline, CHKACC_FILE, + if (!is_pkcs11_uri(options->cert_file)) + { + errs |= check_file_access_inline(options->cert_file_inline, CHKACC_FILE, options->cert_file, R_OK, "--cert"); + } errs |= check_file_access_inline(options->extra_certs_file, CHKACC_FILE, options->extra_certs_file, R_OK, @@ -3456,9 +3481,12 @@ options_postprocess_filechecks(struct options *options) if (!(options->management_flags & MF_EXTERNAL_KEY)) #endif { - errs |= check_file_access_inline(options->priv_key_file_inline, + if (!is_pkcs11_uri(options->priv_key_file)) + { + errs |= check_file_access_inline(options->priv_key_file_inline, CHKACC_FILE|CHKACC_PRIVATE, options->priv_key_file, R_OK, "--key"); + } } errs |= check_file_access_inline(options->pkcs12_file_inline, @@ -8078,6 +8106,14 @@ add_option(struct options *options, } } #endif /* ENABLE_CRYPTO_MBEDTLS */ +#ifdef HAVE_OPENSSL_ENGINE + else if (streq(p[0], "pkcs11-engine") && p[1] && !p[3]) + { + VERIFY_PERMISSION(OPT_P_GENERAL); + options->pkcs11_engine = p[1]; + options->pkcs11_engine_module = p[2]; /* may be NULL */ + } +#endif /* HAVE_OPENSSL_ENGINE */ #ifdef ENABLE_PREDICTION_RESISTANCE else if (streq(p[0], "use-prediction-resistance") && !p[1]) { @@ -8137,6 +8173,20 @@ add_option(struct options *options, VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE); options->cert_file = p[1]; options->cert_file_inline = is_inline; + if (is_pkcs11_uri(p[1])) + { +#ifndef HAVE_OPENSSL_ENGINE + msg(msglevel, "Use of PKCS11 uri as cert and key file names requires OpenSSL " + "ENGINE support which is missing in this binary.") +#else + options->priv_key_file = p[1]; + options->cert_file_is_pkcs11_uri = true; + } + else + { + options->cert_file_is_pkcs11_uri = false; +#endif /* HAVE_OPENSSL_ENGINE */ + } } else if (streq(p[0], "extra-certs") && p[1] && !p[2]) { @@ -8219,6 +8269,20 @@ add_option(struct options *options, VERIFY_PERMISSION(OPT_P_GENERAL|OPT_P_INLINE); options->priv_key_file = p[1]; options->priv_key_file_inline = is_inline; + if (is_pkcs11_uri(p[1])) + { +#ifndef HAVE_OPENSSL_ENGINE + msg(msglevel, "Use of PKCS11 uri as cert and key file names requires OpenSSL " + "ENGINE support which is missing in this binary.") +#else + options->cert_file = p[1]; + options->cert_file_is_pkcs11_uri = true; + } + else + { + options->cert_file_is_pkcs11_uri = false; +#endif /* HAVE_OPENSSL_ENGINE */ + } } else if (streq(p[0], "tls-version-min") && p[1] && !p[3]) { diff --git a/src/openvpn/options.h b/src/openvpn/options.h index b0e40cb7..19911b01 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -658,6 +658,13 @@ struct options /* data channel crypto flags set by push/pull. Reuses the CO_* crypto_flags */ unsigned int data_channel_crypto_flags; + +#ifdef HAVE_OPENSSL_ENGINE + const char *pkcs11_engine; + const char *pkcs11_engine_module; + /* flag to indicate cert and key files are specified as pkcs11 uri */ + bool cert_file_is_pkcs11_uri; +#endif }; #define streq(x, y) (!strcmp((x), (y))) diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index b2dc48be..7e8940c6 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -647,6 +647,19 @@ init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_ch goto err; } } +#ifdef HAVE_OPENSSL_ENGINE + else if (options->cert_file_is_pkcs11_uri) + { + if (!tls_ctx_use_pkcs11_engine(new_ctx, options->cert_file, + options->pkcs11_engine, + options->pkcs11_engine_module)) + { + msg(M_WARN, "Cannot load certificate \"%s\" using PKCS#11 engine", + options->cert_file); + goto err; + } + } +#endif #ifdef ENABLE_PKCS11 else if (options->pkcs11_providers[0]) { @@ -678,7 +691,7 @@ init_ssl(const struct options *options, struct tls_root_ctx *new_ctx, bool in_ch tls_ctx_load_cert_file(new_ctx, options->cert_file, options->cert_file_inline); } - if (options->priv_key_file) + if (options->priv_key_file && !options->cert_file_is_pkcs11_uri) { if (0 != tls_ctx_load_priv_file(new_ctx, options->priv_key_file, options->priv_key_file_inline)) diff --git a/src/openvpn/ssl_backend.h b/src/openvpn/ssl_backend.h index c877e194..dbab721e 100644 --- a/src/openvpn/ssl_backend.h +++ b/src/openvpn/ssl_backend.h @@ -569,4 +569,14 @@ void get_highest_preference_tls_cipher(char *buf, int size); */ const char *get_ssl_library_version(void); +/** + * Load certificate and key into TLS context using pkcs11 engine + * @param ctx TLS context + * @param cert_id ceritificate and proivate key spec as pkcs11 URI + * @param engine id or path of OpenSSL pkcs11 engine object (default: pkcs11) + * @param module path of optional provider module to load with the engine + */ +int tls_ctx_use_pkcs11_engine(struct tls_root_ctx *tls_ctx, const char *cert_id, + const char *engine, const char *module); + #endif /* SSL_BACKEND_H_ */ diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index 241206fb..137234a5 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -2251,4 +2251,185 @@ get_ssl_library_version(void) return OpenSSL_version(OPENSSL_VERSION); } -#endif /* defined(ENABLE_CRYPTO_OPENSSL) */ +#if HAVE_OPENSSL_ENGINE +#include +#include + +/* Call back method for user interface with pkcs11 engine + * used for PIN prompt and possibly token insertion request. + */ +static int +ui_reader(UI *ui, UI_STRING *uis) +{ + struct user_pass token_pass; + int ret = 0; + + const char *uri = UI_get0_user_data(ui); + const char *prompt = UI_get0_output_string(uis); + + token_pass.defined = false; + token_pass.nocache = true; + + switch(UI_get_string_type(uis)) + { + case UIT_PROMPT: + case UIT_VERIFY: + if (get_user_pass(&token_pass, NULL, prompt, + GET_USER_PASS_MANAGEMENT|GET_USER_PASS_PASSWORD_ONLY + |GET_USER_PASS_NOFATAL)) + { + ret = 1; + UI_set_result(ui, uis, token_pass.password); + } + break; + case UIT_BOOLEAN: + if (get_user_pass(&token_pass, NULL, UI_get0_output_string(uis), + GET_USER_PASS_MANAGEMENT|GET_USER_PASS_NEED_OK + |GET_USER_PASS_NOFATAL)) + { + ret = (strcmp(token_pass.password, "ok") == 0); + UI_set_result(ui, uis, token_pass.password); + } + case UIT_INFO: + msg(M_INFO, "INFO prompt from token: <%s>", prompt); + break; + case UIT_ERROR: + msg(M_INFO, "ERROR prompt from token: <%s>", prompt); + break; + default: + break; + } + + return ret; +} + +static char * +ui_prompt_constructor(UI *ui, const char *desc, const char *name) +{ + int len = strlen(desc) + strlen(name) + 6; + char *s = malloc(len); + openvpn_snprintf(s, len, "%s for %s", desc, name); + return s; +} + +static ENGINE * +load_pkcs11_engine(const char *engine_id) +{ + ENGINE *e = ENGINE_by_id(engine_id); + + if (e) { + return e; + } + + /* try dynamic engine with engine-id as path to the engine shared object */ + e = ENGINE_by_id("dynamic"); + if (e) + { + if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine_id, 0) + || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0)) + { + ENGINE_free(e); + e = NULL; + } + } + if (!e) + { + msg(M_WARN, "PKCS11 engine <%s> not available", engine_id); + } + return e; +} + +static ENGINE * +setup_pkcs11_engine(const char *engine_id, const char *module_path, UI_METHOD *ui) +{ + if (!engine_id) + { + engine_id = "pkcs11"; + } + + msg(D_SHOW_PKCS11, "Loading pkcs11 engine <%s> with module <%s>", + engine_id, (module_path ? module_path : "unspecified")); + + ENGINE *e = load_pkcs11_engine(engine_id); + + if (e) + { + if (module_path) + { + ENGINE_ctrl_cmd_string(e, "MODULE_PATH", module_path, 0); + } + ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui, NULL, 0); + } + + return e; +} + +/** + * Load certificate and key into TLS context using pkcs11 engine + * @param ctx TLS context + * @param cert_id ceritificate and proivate key spec as pkcs11 URI + * @param engine id or path of OpenSSL pkcs11 engine object (default: pkcs11) + * @param module path of optional provider module to load with the engine + */ +int +tls_ctx_use_pkcs11_engine(struct tls_root_ctx *tls_ctx, const char *cert_id, + const char *engine, const char *module) +{ + int ret = 0; + EVP_PKEY *pkey = NULL; + + UI_METHOD *ui = UI_create_method("openvpn"); + if (!ui) + { + msg(M_WARN, "Failed to setup UI callback for engine"); + return ret; + } + UI_method_set_reader(ui, ui_reader); + UI_method_set_prompt_constructor(ui, ui_prompt_constructor); + + struct + { + const char *cert_id; + X509* cert; + } params = {cert_id, NULL}; + + ENGINE *e = setup_pkcs11_engine(engine, module, ui); + if (!e || !ENGINE_init(e)) + { + goto cleanup; + } + ENGINE_ctrl_cmd(e, "SET_CALLBACK_DATA", 0, (void *)cert_id, NULL, 0); + + msg (D_SHOW_PKCS11, "Loading certificate <%s> using engine", params.cert_id); + + if (!ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, ¶ms, NULL, 0) + || !params.cert || !SSL_CTX_use_certificate(tls_ctx->ctx, params.cert)) + { + msg (M_WARN, "Failed to load certificate <%s>", cert_id); + goto finish; + } + + msg (D_SHOW_PKCS11, "Loading private key <%s> using engine", params.cert_id); + + pkey = ENGINE_load_private_key(e, cert_id, ui, (void *)cert_id); + if (!pkey || !SSL_CTX_use_PrivateKey(tls_ctx->ctx, pkey)) + { + msg (M_WARN, "Failed to set private key <%s> using engine", cert_id); + goto finish; + } + ret = 1; + +finish: + ENGINE_finish(e); + +cleanup: + ENGINE_free(e); + X509_free(params.cert); + UI_destroy_method(ui); + + return ret; +} + +#endif /* HAVE_OPENSSL_ENGINE */ + +#endif /* ENABLE_CRYPTO_OPENSSL */