From: "selvanair (Code Review)" Date: Sun, 1 Oct 2023 19:06:43 +0000 In-Reply-To: References: Reply-To: selva.nair@...277..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <7e9aa940e06c40ecf0ef4cac23904686d1f59df8-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="PHmN5OEK58Q="; charset=UTF-8 Subject: [Openvpn-devel] [S] Change in openvpn[master]: Log OpenSSL errors on failure to set certificate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --PHmN5OEK58Q= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld,=20 I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/363?usp=3Demail to look at the new patch set (#2). Change subject: Log OpenSSL errors on failure to set certificate ...................................................................... Log OpenSSL errors on failure to set certificate Currently we log a bogus error message saying private key password verifica= tion failed when SSL_CTX_use_cert_and_key() fails in pkcs11_openssl.c. Instead p= rint OpenSSL error queue and exit promptly. Also log OpenSSL errors when SSL_CTX_use_certiifcate() fails in cryptoapi.c and elsewhere. Such logging could be useful especially when the ceritficate= is rejected by OpenSSL due to stricter security restrictions in recent versions of the library. Change-Id: Ic7ec25ac0503a91d5869b8da966d0065f264af22 Signed-off-by: Selva Nair --- M src/openvpn/cryptoapi.c M src/openvpn/pkcs11_openssl.c M src/openvpn/ssl_openssl.c M tests/unit_tests/openvpn/test_cryptoapi.c M tests/unit_tests/openvpn/test_pkcs11.c 5 files changed, 32 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/63/363/2 diff --git a/src/openvpn/cryptoapi.c b/src/openvpn/cryptoapi.c index 3b92e48..f7e5b67 100644 --- a/src/openvpn/cryptoapi.c +++ b/src/openvpn/cryptoapi.c @@ -51,6 +51,7 @@ #include "openssl_compat.h" #include "win32.h" #include "xkey_common.h" +#include "crypto_openssl.h" #ifndef HAVE_XKEY_PROVIDER @@ -505,6 +506,7 @@ if (SSL_CTX_use_certificate(ssl_ctx, cert) && SSL_CTX_use_PrivateKey(ssl_ctx, privkey)) { + crypto_print_openssl_errors(M_WARN); ret =3D 1; } diff --git a/src/openvpn/pkcs11_openssl.c b/src/openvpn/pkcs11_openssl.c index 40080ef..aa0819f 100644 --- a/src/openvpn/pkcs11_openssl.c +++ b/src/openvpn/pkcs11_openssl.c @@ -302,7 +302,8 @@ if (!SSL_CTX_use_cert_and_key(ctx->ctx, x509, pkey, NULL, 0)) { - msg(M_WARN, "PKCS#11: Failed to set cert and private key for OpenS= SL"); + crypto_print_openssl_errors(M_WARN); + msg(M_FATAL, "PKCS#11: Failed to set cert and private key for Open= SSL"); goto cleanup; } ret =3D 1; @@ -369,7 +370,8 @@ if (!SSL_CTX_use_certificate(ssl_ctx->ctx, x509)) { - msg(M_WARN, "PKCS#11: Cannot set certificate for openssl"); + crypto_print_openssl_errors(M_WARN); + msg(M_FATAL, "PKCS#11: Cannot set certificate for openssl"); goto cleanup; } ret =3D 0; diff --git a/src/openvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c index 0b310de..b5cc9a7 100644 --- a/src/openvpn/ssl_openssl.c +++ b/src/openvpn/ssl_openssl.c @@ -857,6 +857,7 @@ /* Load Certificate */ if (!SSL_CTX_use_certificate(ctx->ctx, cert)) { + crypto_print_openssl_errors(M_WARN); crypto_msg(M_FATAL, "Cannot use certificate"); } @@ -1007,6 +1008,7 @@ end: if (!ret) { + crypto_print_openssl_errors(M_WARN); if (cert_file_inline) { crypto_msg(M_FATAL, "Cannot load inline certificate file"); diff --git a/tests/unit_tests/openvpn/test_cryptoapi.c b/tests/unit_tests/o= penvpn/test_cryptoapi.c index 008f41c..d90bfc3 100644 --- a/tests/unit_tests/openvpn/test_cryptoapi.c +++ b/tests/unit_tests/openvpn/test_cryptoapi.c @@ -58,6 +58,17 @@ return NULL; } +/* replacement for crypto_print_openssl_errors() */ +void +crypto_print_openssl_errors(const unsigned int flags) +{ + unsigned long e; + while ((e =3D ERR_get_error())) + { + msg(flags, "OpenSSL error %lu: %s\n", e, ERR_error_string(e, NULL)= ); + } +} + /* tls_libctx is defined in ssl_openssl.c which we do not want to compile = in */ OSSL_LIB_CTX *tls_libctx; diff --git a/tests/unit_tests/openvpn/test_pkcs11.c b/tests/unit_tests/open= vpn/test_pkcs11.c index 235cc43..870ab93 100644 --- a/tests/unit_tests/openvpn/test_pkcs11.c +++ b/tests/unit_tests/openvpn/test_pkcs11.c @@ -44,6 +44,19 @@ struct management *management; /* global */ +/* replacement for crypto_print_openssl_errors() */ +#if defined (ENABLE_CRYPTO_OPENSSL) +void +crypto_print_openssl_errors(const unsigned int flags) +{ + unsigned long e; + while ((e =3D ERR_get_error())) + { + msg(flags, "OpenSSL error %lu: %s\n", e, ERR_error_string(e, NULL)= ); + } +} +#endif + /* stubs for some unused functions instead of pulling in too many dependen= cies */ int parse_line(const char *line, char **p, const int n, const char *file, -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/363?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: Ic7ec25ac0503a91d5869b8da966d0065f264af22 Gerrit-Change-Number: 363 Gerrit-PatchSet: 2 Gerrit-Owner: selvanair Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --PHmN5OEK58Q= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

selvanair uploaded patch set #2 to this change.

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

Log OpenSSL errors on failure to set certificate

Currently we = log a bogus error message saying private key password verification
faile= d when SSL_CTX_use_cert_and_key() fails in pkcs11_openssl.c. Instead print<= br>OpenSSL error queue and exit promptly.

Also log OpenSSL errors wh= en SSL_CTX_use_certiifcate() fails in cryptoapi.c
and elsewhere. Such lo= gging could be useful especially when the ceritficate is
rejected by Ope= nSSL due to stricter security restrictions in recent versions
of the lib= rary.

Change-Id: Ic7ec25ac0503a91d5869b8da966d0065f264af22
Signed= -off-by: Selva Nair <selva.nair@...277...>
---
M src/openvpn/cr= yptoapi.c
M src/openvpn/pkcs11_openssl.c
M src/openvpn/ssl_openssl.c<= br>M tests/unit_tests/openvpn/test_cryptoapi.c
M tests/unit_tests/openvp= n/test_pkcs11.c
5 files changed, 32 insertions(+), 2 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/63/363/2
diff --git a/src/openvpn/cryptoapi.c b/src/openvpn/cryptoapi.c
index 3b92e48..f7e5b67 100644
--- a/src/openvpn/= cryptoapi.c
+++ b/src/openvpn/cryptoapi.c
@= @ -51,6 +51,7 @@
#include "openssl_compat.h"
#include "win32.h"
#include "= xkey_common.h"
+= #include "crypto_openssl.h"

#i= fndef HAVE_XKEY_PROVIDER

@@ -505,6 +506,7= @@
if (SSL_CTX_use_certificate(ssl_ctx, cert)<= br> && SSL_CTX_use_PrivateKey(ssl_ctx, privkey))
{
+ = crypto_print_openssl_errors(M_WARN);
ret = =3D 1;
}

diff --git = a/src/openvpn/pkcs11_openssl.c b/src/openvpn/pkcs11_openssl.c
index 40080ef..aa0819f 100644

--- a/src/openvpn/pkcs11_o= penssl.c
+++ b/src/openvpn/pkcs11_openssl.c
@@ -302,7 +302,8 @@


if (!SSL_CTX_us= e_cert_and_key(ctx->ctx, x509, pkey, NULL, 0))
{
- msg(M_WARN, &quo= t;PKCS#11: Failed to set cert and private key for OpenSSL");+ crypto_print_openssl_= errors(M_WARN);
+ = msg(M_FATAL, "PKCS#11: Failed to set cert and private key for Open= SSL");
goto cleanup;
}
ret =3D 1;
@@ -369,7 +370,8 @@
if (!SSL_CTX_use_certificate(ssl_ctx->ctx= , x509))
{
- msg(M_WARN, "PKCS#11: Cannot set certificate for ope= nssl");
+ = crypto_print_openssl_errors(M_WARN);
+ msg(M_FATAL, "PKCS#11: Cannot set certificat= e for openssl");
goto cleanup;
}

ret =3D 0;
diff --git a/src/o= penvpn/ssl_openssl.c b/src/openvpn/ssl_openssl.c
index 0b31= 0de..b5cc9a7 100644
--- a/src/openvpn/ssl_openssl.c<= br>+++ b/src/openvpn/ssl_openssl.c
@@ -857,6 +857,7 @= @
/* Load Certificate */
if (!SSL= _CTX_use_certificate(ctx->ctx, cert))
{
<= span style=3D"color: hsl(120, 100%, 40%);">+ crypto_print_openssl_er= rors(M_WARN);

crypto_msg(M_FATAL, "Cannot use= certificate");
}

@@ -1007,6 +1008,7 @@

end:
if (!re= t)
{
+ crypto_print_openssl_errors(M_WARN);
= if (cert_file_inline)
{
= crypto_msg(M_FATAL, "Cannot load inline certificate file");
diff --git a/tests/unit_tests/openvpn/test_cryptoapi.c b/tes= ts/unit_tests/openvpn/test_cryptoapi.c
index 008f41c..d90bf= c3 100644
--- a/tests/unit_tests/openvpn/test_cryptoapi.c
+++ b/tests/unit_tests/openvpn/test_cryptoapi.c
@@ -58,6 +58,17 @@

return NULL;
}=

+/= * replacement for crypto_print_openssl_errors() */
+void
+crypto_print_openssl_errors(const unsigned int flags)
+{
+ unsigned long e;
+ while ((e =3D ERR_get_error()))
+ {
+ msg(flags, "OpenSSL error= %lu: %s\n", e, ERR_error_string(e, NULL));
+ }
+}
+
/* tls_libctx is defined in ssl_openssl.c which we do not wa= nt to compile in */
OSSL_LIB_CTX *tls_libctx;

diff --git a/tests/unit_tests/openvpn/test_pkcs11.c b= /tests/unit_tests/openvpn/test_pkcs11.c
index 235cc43..870a= b93 100644
--- a/tests/unit_tests/openvpn/test_pkcs11.c
+++ b/tests/unit_tests/openvpn/test_pkcs11.c
@= @ -44,6 +44,19 @@

struct management *man= agement; /* global */

+/* replacement for crypto_print_openssl_errors() */
+#if defined (ENABLE_CRY= PTO_OPENSSL)
+void
+crypto_print_openssl_e= rrors(const unsigned int flags)
+{
+ un= signed long e;
+ w= hile ((e =3D ERR_get_error()))
+ {
+ = msg(flags, "OpenSSL error %lu: %s\n", e, ERR_error_string(e,= NULL));
+ }
+}
+#endif
+
/* stubs for some unused functions i= nstead of pulling in too many dependencies */
int parse_line(const char *line, char **p, const int n, const char *fi= le,

To view, visit change 363. To unsubscrib= e, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ic7ec25ac0503a91d5869b8da966= d0065f264af22
Gerrit-Change-Number: 363
Gerrit-PatchSet: 2
Gerrit-Owner: selvanair <selva.nair@...1354...= ..>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--PHmN5OEK58Q=--