From patchwork Fri Sep 6 10:37:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Lichtenheld X-Patchwork-Id: 3798 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:6bd4:b0:5b9:581e:f939 with SMTP id c20csp63337max; Fri, 6 Sep 2024 03:56:34 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV1iyzfDT3Zf7auPD8e/upF6QR03y9DrveZj3lMi3TyYiNcoPVgyKpD2cXhrA5yDEIrqpViHaRD1HU=@openvpn.net X-Google-Smtp-Source: AGHT+IHlGNfunb1umgFv5/AKJbdG/E1KoM0VLQxJSpjlkf7rHyHBTRfT1mD0RfQInnVCJ04jwMU0 X-Received: by 2002:a05:6870:ecac:b0:25e:1382:864d with SMTP id 586e51a60fabf-27b82fb784cmr2574162fac.30.1725620194662; Fri, 06 Sep 2024 03:56:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1725620194; cv=none; d=google.com; s=arc-20240605; b=I+J2RCstyTl+9+5+5sDQZFFWD3t3YYWgXsc5NhVXP8LoSgeDxp/AgJOisdRE0dw+o+ CZWZ3sEpG+qWdyWtPiWt9WYK69KvBOc7DDaUM3QMVFMSaFGL7Xvv5urqXyYzr2hN/XsE BKaKrIi2YGq0h6nLF88snzyWGo7rDm6wybcgQ8to2FzVDvpNUMN7NeoBl2oWep0EAZ0C EWTFkxxfUAyjs42WVGaTQJjupyAyAO/XxfI6mykeK8rAVakxT2xjsoqpdo+Jf6etUD2F k9DX1tto1uRg1IZeylPFo8yUDAEpnrDV6QVgeQuvf+MXYxGPDhEUzTS4yHAJBBAJ7XrP RDDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=Y1Q7A8Cql7yHVXV+n7DJikb/Qm7OTLuPGKUO5KyMFSE=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=bbfTNiV0pebf+5W6q225Dq0Yf6aJvYmfg4hkQM7raRkWVGBnU7uZLbj4W0t3k2WUtx bd7n4L8DbMxX493CDTVY8/i/S/s0RPm05Kvy2U1VgUydlLJgipczxDL8lO2LDhGTaq+A 0dZ6OND2UHp3bxZOgqa1fvNqfY2Ylci80qJkFnaSv357iE7IUqBHy16q4aMMQL2QoX+4 vtC0lTG9zRaQ+pIhH8K5f7uw9DxlnVg0N9ZQcn3DQKX5Q9hibYVr+w782jGsODdHE1nI AeAaHJLclfCEriDelh6gu0OqtVzeil0zCEKTXsJgfxpePdC2BlZ15Qg0DIXG1m911dIn auow==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=ZUgUJ5Xm; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YxuspriY; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=xHTXa+OU; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id d2e1a72fcca58-71797766779si2588604b3a.331.2024.09.06.03.56.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2024 03:56:34 -0700 (PDT) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=ZUgUJ5Xm; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=YxuspriY; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=xHTXa+OU; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net Received: from [127.0.0.1] (helo=sfs-ml-3.v29.lw.sourceforge.com) by sfs-ml-3.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1smWdB-0005bi-LZ; Fri, 06 Sep 2024 10:55:57 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-3.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1smWd9-0005bV-Al for openvpn-devel@lists.sourceforge.net; Fri, 06 Sep 2024 10:55:55 +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=lw/TRGxBrivksZwdoQFKOfOQgJzVFJoBUaInpoVjY2A=; b=ZUgUJ5XmKQ2rejOp8BUA/morN1 tsSMjiZqpP6tXzcmukhYV0jFraFsW56fWGZGGmRubI2sELl0wVRWB4QErQ9PE4nl8lmH8kXysMC/R cgbFhDUYJjWqlSpkzijSI5/3qx+Y1U6kZQFvcb6/sxl+LxhuljkSJp3leOVifBi5FhaA=; 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=lw/TRGxBrivksZwdoQFKOfOQgJzVFJoBUaInpoVjY2A=; b=YxuspriYEiH8quIJaL2kRy5sbL xOGncPmdal1nInMzMBgd73ABdjybyn9mz6E42yrbkkdorFcySNCYsRim+4iDkvkagXqX485EQVrED xtDjPvpjF39fJTj3oAOQiIzpJ4EMO+z/u+7nWktOo1Z9nE4WV6IvpesyViVGF7lMvbPQ=; Received: from mout-p-101.mailbox.org ([80.241.56.151]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1smWd6-0004ZS-5j for openvpn-devel@lists.sourceforge.net; Fri, 06 Sep 2024 10:55:54 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4X0Xjw53CCz9tHc; Fri, 6 Sep 2024 12:37:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lichtenheld.com; s=MBO0001; t=1725619060; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lw/TRGxBrivksZwdoQFKOfOQgJzVFJoBUaInpoVjY2A=; b=xHTXa+OUe9F/9xMcYXXE3J/PiNC/YCH4WIGq7Jn1pEqhg6wVC7arIhiQV8ZjvGSQhTMeAO 3bTTAiy2n9jteEMCeywKNo+/2Rabb4QtlDSlq7IJ927kps0Iaav0mALfRfMnkwTzgrmQbe mYmyemkOT/DiZ4JsPOgjYN2+PR7joIiOHqFflZLDg+0ELIQeM6hX44y8KVdrQXvrLOdqx9 tBiLqkW16tGd11cOGJax/GSpsCKnAeByVbtUntgizLsygC7OlEQNApjKflGAxbsarU93pD JJ83s8P6l9pXBRTsGuuGJkgUhFuFEpEEORVLRvznY2gDVQjVz+PaYQ3FCTnrnw== From: Frank Lichtenheld To: openvpn-devel@lists.sourceforge.net Date: Fri, 6 Sep 2024 12:37:34 +0200 Message-Id: <20240906103734.36633-1-frank@lichtenheld.com> In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: -0.9 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Selva Nair 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 P [...] Content analysis details: (-0.9 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [80.241.56.151 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [80.241.56.151 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_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -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 X-Headers-End: 1smWd6-0004ZS-5j Subject: [Openvpn-devel] [PATCH v3] Interpret --key and --cert option argument as URI 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 X-GMAIL-THRID: =?utf-8?q?1809443921313455786?= X-GMAIL-MSGID: =?utf-8?q?1809443921313455786?= From: Selva Nair 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=Foo;id=%01 --cert pkcs11:token=Foo;id=%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 Acked-by: Frank Lichtenheld --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/726 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Frank Lichtenheld diff --git a/doc/man-sections/tls-options.rst b/doc/man-sections/tls-options.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., PEM, + PKCS12) is supported. PKCS#11 URI (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 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 details). --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 generate.\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 be 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 chain.\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 version we\n" " will accept from the peer. If version is unrecognized and 'or-highest'\n" " is specified, require max TLS version supported by SSL implementation.\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 and "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 done 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 not start with "file:" + */ + if ((type & CHKACC_ACCEPT_URI) && strchr(file, ':')) + { + if (!strncmp(file, "file:", 5)) + { + file += 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 |= 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, + errs |= check_file_access_inline(options->cert_file_inline, CHKACC_FILE|CHKACC_ACCEPT_URI, options->cert_file, R_OK, "--cert"); errs |= check_file_access_inline(options->extra_certs_file, CHKACC_FILE, @@ -4079,7 +4095,7 @@ if (!(options->management_flags & MF_EXTERNAL_KEY)) { errs |= 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"); } 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 >= 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 = UI_get0_user_data(ui); + + if (UI_get_string_type(uis) == UIT_PROMPT) + { + const char *prompt = 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_MANAGEMENT|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 = SSL_CTX_get_default_passwd_cb(ctx); + void *d = 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 = NULL; + +#if !defined(HAVE_OPENSSL_STORE_API) + + /* Treat the uri as file name */ + BIO *in = BIO_new_file(uri, "r"); + if (!in) + { + return NULL; + } + pkey = 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 = NULL; + OSSL_STORE_INFO *info = NULL; + + UI_METHOD *ui_method = 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 = 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) != 1) + { + goto end; + } + info = OSSL_STORE_load(store_ctx); + if (!info) + { + goto end; + } + pkey = 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 = NULL; + int ret = 0; + OSSL_STORE_CTX *store_ctx = NULL; + OSSL_STORE_INFO *info = NULL; + + ASSERT(NULL != tls_ctx); + + UI_METHOD *ui_method = 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 = 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) != 1) + { + goto end; + } + + info = OSSL_STORE_load(store_ctx); + if (!info) + { + goto end; + } + + x = OSSL_STORE_INFO_get0_CERT(info); + if (x == NULL) + { + goto end; + } + msg(D_TLS_DEBUG_MED, "Found cert in store using URI: %s", uri); + + ret = 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 = OSSL_STORE_load(store_ctx); + while (info && !OSSL_STORE_eof(store_ctx)) + { + x = OSSL_STORE_INFO_get1_CERT(info); + if (x && SSL_CTX_add_extra_chain_cert(tls_ctx->ctx, x) != 1) + { + X509_free(x); + crypto_msg(M_FATAL, "Error adding extra certificate"); + break; + } + OSSL_STORE_INFO_free(info); + info = 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 = NULL; X509 *x = NULL; @@ -961,7 +1166,7 @@ } else { - in = BIO_new_file(cert_file, "r"); + in = BIO_new_file((char *) cert_file, "r"); } if (in == 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 = BIO_new_mem_buf((char *) priv_key_file, -1); + if (in == NULL) + { + goto end; + } + pkey = PEM_read_bio_PrivateKey(in, NULL, + SSL_CTX_get_default_passwd_cb(ctx->ctx), + SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx)); } else { - in = BIO_new_file(priv_key_file, "r"); + pkey = load_pkey_from_uri(priv_key_file, ssl_ctx); } - if (!in) - { - goto end; - } - - pkey = 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 dependencies */ +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 = "-----BEGIN CERTIFICATE-----\n" "MIIBuTCCAUCgAwIBAgIUTLtjSBzx53qZRvZ6Ur7D9kgoOHkwCgYIKoZIzj0EAwIw\n"