From: "plaisthos (Code Review)" Date: Tue, 16 Jan 2024 13:58:13 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="lHcSvL06OIE="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Add test_ssl unit test and test export of PEM to file List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: cron2 , flichtenheld Cc: openvpn-devel --lHcSvL06OIE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos. Hello cron2, flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/471?usp=3Demail to look at the new patch set (#6). The following approvals got outdated and were removed: Code-Review+2 by flichtenheld, Code-Review-1 by cron2 The change is no longer submittable: Code-Review and checks~ChecksSubmitRul= e are unsatisfied now. Change subject: Add test_ssl unit test and test export of PEM to file ...................................................................... Add test_ssl unit test and test export of PEM to file This introduces a number of mock function to be able to compile ssl_verify_*.c and ssl_mbedtls.c/ssl_openssl.c into a unit and adds quite a number of files to that unit. But it allows similar unit tests (in term of dependencies) to be added in the future. Change-Id: Ie248d35d063bb6878f3dd42840c77ba0d6fa3381 Signed-off-by: Arne Schwabe --- M .github/workflows/build.yaml M CMakeLists.txt M tests/unit_tests/openvpn/Makefile.am A tests/unit_tests/openvpn/mock_management.c A tests/unit_tests/openvpn/mock_ssl_dependencies.c A tests/unit_tests/openvpn/test_ssl.c 6 files changed, 325 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/71/471/6 diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 51100c3..cc98813 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -85,7 +85,7 @@ fail-fast: false matrix: arch: [x86, x64] - test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, pac= ket_id, pkt, provider, tls_crypt] + test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, pac= ket_id, pkt, provider, ssl, tls_crypt] runs-on: windows-latest name: "mingw unittest ${{ matrix.test }} - ${{ matrix.arch }} - OSSL" diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f370c3..b38e6ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -595,6 +595,7 @@ "test_packet_id" "test_pkt" "test_provider" + "test_ssl" ) if (WIN32) @@ -699,6 +700,33 @@ src/openvpn/mss.c ) + target_sources(test_ssl PRIVATE + tests/unit_tests/openvpn/mock_management.c + tests/unit_tests/openvpn/mock_ssl_dependencies.c + tests/unit_tests/openvpn/mock_win32_execve.c + src/openvpn/argv.c + src/openvpn/base64.c + src/openvpn/block_dns.c + src/openvpn/crypto.c + src/openvpn/crypto_mbedtls.c + src/openvpn/crypto_openssl.c + src/openvpn/cryptoapi.c + src/openvpn/env_set.c + src/openvpn/mss.c + src/openvpn/mtu.c + src/openvpn/options_util.c + src/openvpn/otime.c + src/openvpn/packet_id.c + src/openvpn/run_command.c + src/openvpn/ssl_mbedtls.c + src/openvpn/ssl_openssl.c + src/openvpn/ssl_util.c + src/openvpn/ssl_verify_mbedtls.c + src/openvpn/ssl_verify_openssl.c + src/openvpn/xkey_helper.c + src/openvpn/xkey_provider.c + ) + target_sources(test_misc PRIVATE tests/unit_tests/openvpn/mock_get_random.c src/openvpn/options_util.c diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index cecf4dc..35e25bb 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -9,7 +9,8 @@ endif test_binaries +=3D crypto_testdriver packet_id_testdriver auth_token_testd= river ncp_testdriver misc_testdriver \ - pkt_testdriver + pkt_testdriver ssl_testdriver + if HAVE_LD_WRAP_SUPPORT if !WIN32 test_binaries +=3D tls_crypt_testdriver @@ -69,6 +70,42 @@ $(top_srcdir)/src/openvpn/win32-util.c \ $(top_srcdir)/src/openvpn/mss.c +ssl_testdriver_CFLAGS =3D @TEST_CFLAGS@ \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn +ssl_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ $(OPTIONAL_CRYPTO_LIBS) +ssl_testdriver_SOURCES =3D test_ssl.c mock_msg.c mock_msg.h \ + mock_management.c mock_ssl_dependencies.c mock_win32_execve.c \ + $(top_srcdir)/src/openvpn/argv.c \ + $(top_srcdir)/src/openvpn/base64.c \ + $(top_srcdir)/src/openvpn/block_dns.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/compat/compat-strsep.c \ + $(top_srcdir)/src/openvpn/crypto.c \ + $(top_srcdir)/src/openvpn/cryptoapi.c \ + $(top_srcdir)/src/openvpn/crypto_mbedtls.c \ + $(top_srcdir)/src/openvpn/crypto_openssl.c \ + $(top_srcdir)/src/openvpn/env_set.c \ + $(top_srcdir)/src/openvpn/mss.c \ + $(top_srcdir)/src/openvpn/mtu.c \ + $(top_srcdir)/src/openvpn/otime.c \ + $(top_srcdir)/src/openvpn/options_util.c \ + $(top_srcdir)/src/openvpn/packet_id.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/run_command.c \ + $(top_srcdir)/src/openvpn/ssl_openssl.c \ + $(top_srcdir)/src/openvpn/ssl_mbedtls.c \ + $(top_srcdir)/src/openvpn/ssl_util.c \ + $(top_srcdir)/src/openvpn/ssl_verify_mbedtls.c \ + $(top_srcdir)/src/openvpn/ssl_verify_openssl.c \ + $(top_srcdir)/src/openvpn/xkey_helper.c \ + $(top_srcdir)/src/openvpn/xkey_provider.c \ + $(top_srcdir)/src/openvpn/win32-util.c + + +if WIN32 + ssl_testdriver_LDADD =3D -lcrypt32 -lncrypt -lfwpuclnt -liphlpapi -lw= s2_32 +endif + packet_id_testdriver_CFLAGS =3D @TEST_CFLAGS@ \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn packet_id_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ diff --git a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests/= openvpn/mock_management.c new file mode 100644 index 0000000..8936c3a --- /dev/null +++ b/tests/unit_tests/openvpn/mock_management.c @@ -0,0 +1,51 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2023 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* Minimal set of mocked management function/globals to get unit tests to + * compile */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include "manage.h" + +#ifdef ENABLE_MANAGEMENT + +struct management *management; /* GLOBAL */ + +void +management_auth_failure(struct management *man, const char *type, const ch= ar *reason) +{ + ASSERT(false); +} + +char * +management_query_pk_sig(struct management *man, const char *b64_data, + const char *algorithm) +{ + return NULL; +} +#endif diff --git a/tests/unit_tests/openvpn/mock_ssl_dependencies.c b/tests/unit_= tests/openvpn/mock_ssl_dependencies.c new file mode 100644 index 0000000..9231d65 --- /dev/null +++ b/tests/unit_tests/openvpn/mock_ssl_dependencies.c @@ -0,0 +1,67 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2023 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* Minimal set of mocked function/globals to get unit tests to + * compile that use the ssl_* files */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include + + +#include "ssl.h" +#include "ssl_verify.h" + +int +parse_line(const char *line, char **p, const int n, const char *file, + const int line_num, int msglevel, struct gc_arena *gc) +{ + /* Dummy function to get the linker happy, should never be called */ + assert_true(false); + return 0; +} + + +int +pem_password_callback(char *buf, int size, int rwflag, void *u) +{ + return 0; +} + +void +cert_hash_remember(struct tls_session *session, const int cert_depth, + const struct buffer *cert_hash) +{ + assert_false(true); +} + +result_t +verify_cert(struct tls_session *session, openvpn_x509_cert_t *cert, int ce= rt_depth) +{ + return FAILURE; +} diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c new file mode 100644 index 0000000..fd2049f --- /dev/null +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -0,0 +1,140 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2023 OpenVPN Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include +#include +#include +#include +#include + +#include "crypto.h" +#include "options.h" +#include "ssl_backend.h" +#include "options_util.h" + +#include "mock_msg.h" +#include "mss.h" +#include "ssl_verify_backend.h" +#include "win32.h" + +/* Mock function to be allowed to include win32.c which is required for + * getting the temp directory */ +#ifdef _WIN32 +struct signal_info siginfo_static; /* GLOBAL */ + +const char * +strerror_win32(DWORD errnum, struct gc_arena *gc) +{ + ASSERT(false); +} + +void +throw_signal(const int signum) +{ + ASSERT(false); +} +#endif + + +const char *unittest_cert =3D "-----BEGIN CERTIFICATE-----\n" + "MIIBuTCCAUCgAwIBAgIUTLtjSBzx53qZRvZ6Ur7D9kgoO= HkwCgYIKoZIzj0EAwIw\n" + "EzERMA8GA1UEAwwIdW5pdHRlc3QwIBcNMjMxMTIxMDk1N= DQ3WhgPMjA3ODA4MjQw\n" + "OTU0NDdaMBMxETAPBgNVBAMMCHVuaXR0ZXN0MHYwEAYHK= oZIzj0CAQYFK4EEACID\n" + "YgAEHYB2hn2xx3f4lClXDtdi36P19pMZA+kI1Dkv/Vn10= vBZ/j9oa+P99T8duz/e\n" + "QlPeHpesNJO4fX8iEDj6+vMeWejOT7jAQ4MmG5EZjpcBK= xCfwFooEvzu8bVujUcu\n" + "wTQEo1MwUTAdBgNVHQ4EFgQUPcgBEVXjF5vYfDsInoE3d= F6UfQswHwYDVR0jBBgw\n" + "FoAUPcgBEVXjF5vYfDsInoE3dF6UfQswDwYDVR0TAQH/B= AUwAwEB/zAKBggqhkjO\n" + "PQQDAgNnADBkAjBLPAGrQAyinigqiu0RomoV8TVaknVLF= Sq6H6A8jgvzfsFCUK1O\n" + "dvNZhFPM6idKB+oCME2JLOBANCSV8o7aJzq7SYHKwPyb1= J4JFlwKe/0Jpv7oh9b1\n" + "IJbuaM9Z/VSKbrIXGg=3D=3D\n" + "-----END CERTIFICATE-----\n"; + +static const char * +get_tmp_dir() +{ + const char *ret; +#ifdef _WIN32 + ret =3D win_get_tempdir(); +#else + ret =3D "/tmp"; +#endif + assert_non_null(ret); + return ret; +} + +static void +crypto_pem_encode_certificate(void **state) +{ + struct gc_arena gc =3D gc_new(); + + struct tls_root_ctx ctx =3D { 0 }; + tls_ctx_client_new(&ctx); + tls_ctx_load_cert_file(&ctx, unittest_cert, true); + + openvpn_x509_cert_t *cert =3D NULL; + + /* we do not have methods to fetch certificates from ssl contexts, use + * internal TLS library methods for the unit test */ +#ifdef ENABLE_CRYPTO_OPENSSL + cert =3D SSL_CTX_get0_certificate(ctx.ctx); +#elif defined(ENABLE_CRYPTO_MBEDTLS) + cert =3D ctx.crt_chain; +#endif + + const char *tmpfile =3D platform_create_temp_file(get_tmp_dir(), "ut_p= em", &gc); + backend_x509_write_pem(cert, tmpfile); + + struct buffer exported_pem =3D buffer_read_from_file(tmpfile, &gc); + assert_string_equal(BSTR(&exported_pem), unittest_cert); + + tls_ctx_free(&ctx); + unlink(tmpfile); + gc_free(&gc); +} + +int +main(void) +{ + const struct CMUnitTest tests[] =3D { + cmocka_unit_test(crypto_pem_encode_certificate) + }; + +#if defined(ENABLE_CRYPTO_OPENSSL) + OpenSSL_add_all_algorithms(); +#endif + + int ret =3D cmocka_run_group_tests_name("crypto tests", tests, NULL, N= ULL); + +#if defined(ENABLE_CRYPTO_OPENSSL) + EVP_cleanup(); +#endif + + return ret; +} -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/471?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: Ie248d35d063bb6878f3dd42840c77ba0d6fa3381 Gerrit-Change-Number: 471 Gerrit-PatchSet: 6 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --lHcSvL06OIE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

plaisthos uploaded patch set #6 to this change.

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

The following approvals got outdated and were removed: Code-Review+2 by flichtenheld, Code-Review-1 by cron2

The change is n= o longer submittable: Code-Review and checks~ChecksSubmitRule are unsatisfi= ed now.

Add test_ssl unit test and test export of PEM to file

This i= ntroduces a number of mock function to be able to compile
ssl_verify_*.c= and ssl_mbedtls.c/ssl_openssl.c into a unit and adds
quite a number of = files to that unit. But it allows similar unit tests
(in term of depende= ncies) to be added in the future.

Change-Id: Ie248d35d063bb6878f3dd4= 2840c77ba0d6fa3381
Signed-off-by: Arne Schwabe <arne@...1227...>---
M .github/workflows/build.yaml
M CMakeLists.txt
M tests/unit= _tests/openvpn/Makefile.am
A tests/unit_tests/openvpn/mock_management.c<= br>A tests/unit_tests/openvpn/mock_ssl_dependencies.c
A tests/unit_tests= /openvpn/test_ssl.c
6 files changed, 325 insertions(+), 2 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/71/47=
1/6
diff --git a/.github/workflows/build.yaml b/.github/workflows/b=
uild.yaml
index 51100c3..cc98813 100644
---= a/.github/workflows/build.yaml
+++ b/.github/workflows/bui= ld.yaml
@@ -85,7 +85,7 @@
fail-fast:= false
matrix:
arch: [x86, = x64]
- test: [ar= gv, auth_token, buffer, cryptoapi, crypto, misc, ncp, packet_id, pkt, provi= der, tls_crypt]
+ = test: [argv, auth_token, buffer, cryptoapi, crypto, misc, ncp, packet_i= d, pkt, provider, ssl, tls_crypt]

ru= ns-on: windows-latest
name: "mingw unittest ${{ m= atrix.test }} - ${{ matrix.arch }} - OSSL"
diff --git = a/CMakeLists.txt b/CMakeLists.txt
index 6f370c3..b38e6ab 10= 0644
--- a/CMakeLists.txt
+++ b/CMakeLists.= txt
@@ -595,6 +595,7 @@
"test= _packet_id"
"test_pkt"
"test_provider"
+ "test_ssl"
)<= /span>

if (WIN32)
@@ -699,6= +700,33 @@
src/openvpn/mss.c
= )

+ target_sources(test_ssl PRIVATE
+ tests/unit_tests/openvpn/mock_management.c=
+ tests/u= nit_tests/openvpn/mock_ssl_dependencies.c
+ tests/unit_tests/openvpn/mock_win32_execv= e.c
+ src/= openvpn/argv.c
+ = src/openvpn/base64.c
+ src/openvpn/block_dns.c
+ src/openvpn/crypto.c
+ src/openvpn/crypto_mbedtls= .c
+ src/o= penvpn/crypto_openssl.c
+ src/openvpn/cryptoapi.c
+ src/openvpn/env_set.c
+ src/openvpn/mss.c
<= span style=3D"color: hsl(120, 100%, 40%);">+ src/openvpn/mtu.c
+ src/openv= pn/options_util.c
+ = src/openvpn/otime.c
+ src/openvpn/packet_id.c
+ src/openvpn/run_command.c
<= span style=3D"color: hsl(120, 100%, 40%);">+ src/openvpn/ssl_mbe= dtls.c

+ s= rc/openvpn/ssl_openssl.c
+ src/openvpn/ssl_util.c
+ src/openvpn/ssl_verify_mbedtls.c
= + src/openvpn/ssl_ve= rify_openssl.c
+ = src/openvpn/xkey_helper.c
+ src/openvpn/xkey_provider.c
+ )
+
target_sources(test_misc PRIVATE<= /span>
tests/unit_tests/openvpn/mock_get_random.c<= br> src/openvpn/options_util.c
diff --git a/t= ests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am<= /span>
index cecf4dc..35e25bb 100644
--- a/tests/u= nit_tests/openvpn/Makefile.am
+++ b/tests/unit_tests/openvp= n/Makefile.am
@@ -9,7 +9,8 @@
endif=

test_binaries +=3D crypto_testdriver packet_id= _testdriver auth_token_testdriver ncp_testdriver misc_testdriver \- pkt_testdriver
+ pkt_testdriver ssl_testdriver
+
if HA= VE_LD_WRAP_SUPPORT
if !WIN32
test_binarie= s +=3D tls_crypt_testdriver
@@ -69,6 +70,42 @@
$(top_srcdir)/src/openvpn/win32-util.c \

$(top_srcd= ir)/src/openvpn/mss.c

+ssl_testdriver_CFLAGS =3D @TEST_CFLAGS@ \
+ -I$(top_srcdir)/include -I$(top= _srcdir)/src/compat -I$(top_srcdir)/src/openvpn

+ssl_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ $(OP= TIONAL_CRYPTO_LIBS)
+= ssl_testdriver_SOURCES =3D test_ssl.c mock_msg.c mock_msg.h \
+ mock_management.c mock_ssl_depen= dencies.c mock_win32_execve.c \
+ $(top_srcdir)/src/openvpn/argv.c \
+ $(top_srcdir)/src/openvpn/base64.c \+ $(top_srcdir)/src/openvpn= /block_dns.c \
+ $(to= p_srcdir)/src/openvpn/buffer.c \
+ $(top_srcdir)/src/compat/compat-strsep.c \
+ $(top_srcdir)/src/openvpn/crypto.c \<= /span>
+ $(top_srcdir)/src/o= penvpn/cryptoapi.c \
= + $(top_srcdir)/src/openvpn/crypto_mbedtls.c \
+ $(top_srcdir)/src/openvpn/crypto_openssl.c \

+ $(top_srcdir)/src/ope= nvpn/env_set.c \
+ = $(top_srcdir)/src/openvpn/mss.c \
+ $(top_srcdir)/src/openvpn/mtu.c \
+ $(top_srcdir)/src/openvpn/otime.c \
+ $(top_srcdir)/src/openv= pn/options_util.c \
+= $(top_srcdir)/src/openvpn/packet_id.c \
+ $(top_srcdir)/src/openvpn/platform.c \
+ $(top_srcdir)/src/openvpn/run_com= mand.c \

+ $(top_srcd= ir)/src/openvpn/ssl_openssl.c \
+ $(top_srcdir)/src/openvpn/ssl_mbedtls.c \
+ $(top_srcdir)/src/openvpn/ssl_util.c= \
+ $(top_srcdir)/sr= c/openvpn/ssl_verify_mbedtls.c \
+ $(top_srcdir)/src/openvpn/ssl_verify_openssl.c \+ $(top_srcdir)/src/openvpn= /xkey_helper.c \
+ = $(top_srcdir)/src/openvpn/xkey_provider.c \
+ $(top_srcdir)/src/openvpn/win32-util.c<= br>+
+
+if WIN32
+ = ssl_testdriver_LDADD =3D -lcrypt32 -lncrypt -lfwpuclnt -liphlpapi -lws2= _32
+endif
= +
packet_id_te= stdriver_CFLAGS =3D @TEST_CFLAGS@ \
-I$(top_srcdir)/incl= ude -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn
= packet_id_testdriver_LDFLAGS =3D @TEST_LDFLAGS@
diff --git= a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests/openvpn/mo= ck_management.c
new file mode 100644
index = 0000000..8936c3a
--- /dev/null
+++ b/tests/= unit_tests/openvpn/mock_management.c
@@ -0,0 +1,51 @@
+/*
+ * OpenVPN -- An application to securely= tunnel IP networks
+= * over a single UDP port, with support for SSL/TLS-based
+ * session au= thentication and key exchange,
+ * packet encryption, packet authentication, and
+ * packet c= ompression.
+ *
+ * Copyright (C) 2002-20= 23 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it and/or modify<= /span>
+ * it under the ter= ms of the GNU General Public License version 2
+ * as published by the Free Software Foundation= .
+ *
+ * This program is distributed in = the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warrant= y of
+ * MERCHANTABI= LITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more deta= ils.
+ *
+ * You should have received a c= opy of the GNU General Public License along

+ * with this program; if not, write to the Free So= ftware Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.=
+ */
+
+/* Minimal set of mocked management function/globals to get = unit tests to
+ * com= pile */
+
<= span style=3D"color: hsl(120, 100%, 40%);">+#ifdef HAVE_CONFIG_H

= +#include "config.h"<= /span>
+#endif
+

+#include "syshead.h"
+
+#include "manage.h"
+
+#ifdef ENABLE_MANAGEMENT
+
+struct m= anagement *management; /* GLOBAL */
+
+voi= d
+management_auth_fa= ilure(struct management *man, const char *type, const char *reason)<= br>+{
+ ASSERT(false);
+}
+
+char *
+management_query_pk_s= ig(struct management *man, const char *b64_data,
+ const char *algorithm)=
+{
+ return NULL;
+}
+#endif
diff --git a/tests/unit_tests/openv= pn/mock_ssl_dependencies.c b/tests/unit_tests/openvpn/mock_ssl_dependencies= .c
new file mode 100644
index 0000000..9231= d65
--- /dev/null
+++ b/tests/unit_tests/op= envpn/mock_ssl_dependencies.c
@@ -0,0 +1,67 @@
+/*

+ * OpenVPN -- An application to securely tunnel = IP networks
+ * = over a single UDP port, with support for SSL/TLS-based
+ * session authentica= tion and key exchange,

+ * packet compressi= on.
+ *
+ * Copyright (C) 2002-2023 OpenV= PN Inc <sales@...515...>

+ *
+ * Th= is program is free software; you can redistribute it and/or modify+ * it under the terms of th= e GNU General Public License version 2
+ * as published by the Free Software Foundation.=
+ *
+ * This program is distributed in the ho= pe that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY o= r FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of= the GNU General Public License along
+ * with this program; if not, write to the Free Software= Foundation, Inc.,
+ = * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */

+
+/* Minimal set of mocked function/globals to get unit tests to
+ * compile that use the = ssl_* files */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h= "
+#endif=
+
+#include "syshead.h"
+

+#include <setjmp.h>
+#include <cmocka.h>
+
+
+#inc= lude "ssl.h"
+
+in= t
+parse_line(const c= har *line, char **p, const int n, const char *file,
+ const int line_num, int msglev= el, struct gc_arena *gc)
+{
+ /* Dummy = function to get the linker happy, should never be called */
+ assert_true(false);

<= span style=3D"color: hsl(120, 100%, 40%);">+ return 0;

+}
+
= +
+int
+pem_password_callback(char *buf, i= nt size, int rwflag, void *u)

+{
+ retu= rn 0;
+}
+
+void
+cert_hash_remember(struct tls_session *session, const int cert_dep= th,
+ = const struct buffer *cert_hash)
+{
+ = assert_false(true);
= +}
+
+result_t
+verify_cert(struct tls_session *session, openvp= n_x509_cert_t *cert, int cert_depth)
+{
+ = return FAILURE;
+}=
diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/u= nit_tests/openvpn/test_ssl.c
new file mode 100644index 0000000..fd2049f
--- /dev/null
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -0,0 +1,140 @= @
+/*
+ * OpenVPN -- An application to se= curely tunnel IP networks
+ * over a single UDP port, with support for SSL/TLS-based=
+ * sess= ion authentication and key exchange,
+ * packet encryption, packet authentication, a= nd
+ * pa= cket compression.
+ *=
+ * Copyright (C) 20= 23 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it and/or modify<= /span>
+ * it under the ter= ms of the GNU General Public License version 2
+ * as published by the Free Software Foundation= .
+ *
+ * This program is distributed in = the hope that it will be useful,

+ * but WITHOUT ANY WARRANTY; without even the implied warrant= y of
+ * MERCHANTABI= LITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more deta= ils.
+ *
+ * You should have received a c= opy of the GNU General Public License along
+ * with this program; if not, write to the Free So= ftware Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.=
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#inclu= de "syshead.h"
+
+#include <s= tdio.h>
+#include = <stdlib.h>
+#in= clude <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "crypto.h"
+#include "options.h"
+#include "ssl_backend.h"
+#include "options_util.h"
+
+#include "mock_msg.h"+#include "mss.h"
+#include "ssl_ve= rify_backend.h"
= +#include "win32.h"
+
+/* Mock f= unction to be allowed to include win32.c which is required for
+ * getting the temp directory */=

+#ifdef _WIN32

+struct signal_info siginf= o_static; /* GLOBAL */
+const char *
+strerror_win32(DWORD er= rnum, struct gc_arena *gc)
+{
+ ASSERT(= false);
+}
= +
+void
+throw_signal(const int signum)
+{
+ ASSERT(false);
+#endif+
+
+const char *unittest_cert =3D "-----BEGIN CERTIFICATE-----\n&= quot;
+ = "MIIBuTCCAUCgAwIBAgIUTLtjSBzx53qZRvZ6Ur7D9kgoOHkwCgYIKoZ= Izj0EAwIw\n"
+ = "EzERMA8GA1UEAwwIdW5pdHRlc3QwIBcNMjMxMTIxMDk= 1NDQ3WhgPMjA3ODA4MjQw\n"
+ "OTU0NDdaMBMxETAPBgNVBAMMCHVuaXR= 0ZXN0MHYwEAYHKoZIzj0CAQYFK4EEACID\n"
+ "YgAEHYB2hn2xx3f4lCl= XDtdi36P19pMZA+kI1Dkv/Vn10vBZ/j9oa+P99T8duz/e\n"
+ "QlPeHpe= sNJO4fX8iEDj6+vMeWejOT7jAQ4MmG5EZjpcBKxCfwFooEvzu8bVujUcu\n"+ &= quot;wTQEo1MwUTAdBgNVHQ4EFgQUPcgBEVXjF5vYfDsInoE3dF6UfQswHwYDVR0jBBgw\n&quo= t;
+ = "FoAUPcgBEVXjF5vYfDsInoE3dF6UfQswDwYDVR0TAQH/BAUwAwEB/zAKBg= gqhkjO\n"
+ = "PQQDAgNnADBkAjBLPAGrQAyinigqiu0RomoV8TVaknVLFS= q6H6A8jgvzfsFCUK1O\n"
+ "dvNZhFPM6idKB+oCME2JLOBANCSV8o7aJz= q7SYHKwPyb1J4JFlwKe/0Jpv7oh9b1\n"
+ "IJbuaM9Z/VSKbrIXGg=3D= =3D\n"
+ = "-----END CERTIFICATE-----\n";
+

+static const char *
+get_tmp_dir()
+{
+ = const char *ret;
+#= ifdef _WIN32
+ ret= =3D win_get_tempdir();
+#else
+ ret = =3D "/tmp";
+#endif
+ assert_= non_null(ret);
+ r= eturn ret;
+}<= br>+
+static void
+crypto_pem_encode_certificate(void **state)
= +{
+ struct gc_arena gc =3D gc_new();
<= span style=3D"color: hsl(120, 100%, 40%);">+
+ struct tls_root_ctx ctx =3D { 0 };
<= span style=3D"color: hsl(120, 100%, 40%);">+ tls_ctx_client_new(&ctx= );
+ tls_ctx_load_= cert_file(&ctx, unittest_cert, true);
+
+
+ /* we do not have methods to fetch certificates from ssl contexts, = use
+ * internal = TLS library methods for the unit test */
+#ifdef ENABLE_CRYPTO_OPENSSL
+ cert =3D SSL_CTX_get0_certificate(ctx.ctx= );
+#elif defined(ENA= BLE_CRYPTO_MBEDTLS)
+= cert =3D ctx.crt_chain;
+#endif
+
+ const char *tmpfile = =3D platform_create_temp_file(get_tmp_dir(), "ut_pem", &gc);<= /span>
+ backend_x509_wri= te_pem(cert, tmpfile);
+ struct buffe= r exported_pem =3D buffer_read_from_file(tmpfile, &gc);
+ assert_string_equal(BSTR(&e= xported_pem), unittest_cert);

+
+ tls_c= tx_free(&ctx);
+ = unlink(tmpfile);
+= gc_free(&gc);
+}
+
+int

+main(void)
+{
+ = const struct CMUnitTest tests[] =3D {
+ cmocka_unit_test(crypto_pem_encode_certificate)<= /span>
+ };
+

+#if defined(ENABLE_CRYPTO_OPENSSL)
+ OpenSSL_add_all_algorithms();
+#endif
+
+ int ret =3D cmocka_run_group_tests_name("crypt= o tests", tests, NULL, NULL);
+
+#if = defined(ENABLE_CRYPTO_OPENSSL)
+ EVP_cleanup();
+#endif
+

+ return ret;=
+}
<= br>

To view, visit change 471. To unsubscribe, or for help writing mail = filters, visit settings.=

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ie248d35d063bb6878f3dd42840c= 77ba0d6fa3381
Gerrit-Change-Number: 471
Gerrit-PatchSet: 6
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--lHcSvL06OIE=--