diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2018-05-12 06:55:32 +0000 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2018-05-12 06:55:32 +0000 |
commit | 058b550a302a609ea6934f4ab99b95741025d1ba (patch) | |
tree | a788ee06cd1b95df6344eafff33877721f609b0b | |
parent | 01574b9c01024ad6a0e83cf06412a3816d360469 (diff) | |
parent | bf7ffd14294e99eca34bdf0a3094bb9917342ea5 (diff) | |
download | gnutls-058b550a302a609ea6934f4ab99b95741025d1ba.tar.gz |
Merge branch 'master' into 'master'
New C++ interfaces for passing flags while construction
Closes #438
See merge request gnutls/gnutls!637
-rw-r--r-- | .gitlab-ci.yml | 8 | ||||
-rw-r--r-- | lib/gnutlsxx.cpp | 10 | ||||
-rw-r--r-- | lib/includes/gnutls/gnutlsxx.h | 2 | ||||
-rw-r--r-- | tests/Makefile.am | 18 | ||||
-rw-r--r-- | tests/cert-common.h | 76 | ||||
-rw-r--r-- | tests/sanity-cpp.cpp | 236 |
6 files changed, 306 insertions, 44 deletions
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c10c3bed1c..7a2070b2a8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -204,7 +204,7 @@ asan.Fedora.x86_64: script: - git submodule update --init --no-fetch - make autoreconf - - CFLAGS="-fsanitize=address -g -O2" LDFLAGS="-static-libasan" + - CFLAGS="-fsanitize=address -g -O2" CXXFLAGS=$CFLAGS LDFLAGS="-static-libasan" dash ./configure --disable-gcc-warnings --disable-doc --cache-file cache/config.cache --disable-non-suiteb-curves --disable-guile - make -j$(nproc) - LSAN_OPTIONS="suppressions=$(pwd)/fuzz/lsan.supp" make check -j$(nproc) @@ -212,7 +212,7 @@ asan.Fedora.x86_64: - LSAN_OPTIONS="suppressions=$(pwd)/fuzz/lsan.supp" make -C fuzz check -j$(nproc) GNUTLS_CPUID_OVERRIDE=0x2 - LSAN_OPTIONS="suppressions=$(pwd)/fuzz/lsan.supp" make -C fuzz check -j$(nproc) GNUTLS_CPUID_OVERRIDE=0x4 - LSAN_OPTIONS="suppressions=$(pwd)/fuzz/lsan.supp" make -C fuzz check -j$(nproc) GNUTLS_CPUID_OVERRIDE=0x8 - - CFLAGS="-fsanitize=address -g -O2" LDFLAGS="-static-libasan" + - CFLAGS="-fsanitize=address -g -O2" CXXFLAGS=$CFLAGS LDFLAGS="-static-libasan" dash ./configure --cache-file cache/config.cache --disable-doc --with-default-trust-store-pkcs11="pkcs11:" --disable-guile - make -j$(nproc) - make -C tests check -j$(nproc) TESTS="trust-store p11-kit-load.sh" SUBDIRS=. @@ -438,7 +438,7 @@ ubsan-Werror.Fedora.x86_64: script: - git submodule update --init --no-fetch - make autoreconf - - CFLAGS="-fsanitize=undefined -fsanitize=bool -fsanitize=alignment -fsanitize=null -fsanitize=bounds-strict -fsanitize=enum -fno-sanitize-recover -g -O2" LDFLAGS="-static-libubsan" dash ./configure + - CFLAGS="-fsanitize=undefined -fsanitize=bool -fsanitize=alignment -fsanitize=null -fsanitize=bounds-strict -fsanitize=enum -fno-sanitize-recover -g -O2" CXXFLAGS=$CFLAGS LDFLAGS="-static-libubsan" dash ./configure --cache-file cache/config.cache --disable-non-suiteb-curves --disable-guile --disable-full-test-suite --disable-doc - make -j$(nproc) -C gl - make -j$(nproc) -C lib CFLAGS="-Werror -O2 -g" @@ -447,7 +447,7 @@ ubsan-Werror.Fedora.x86_64: - make -j$(nproc) -C src CFLAGS="-Werror -O2 -g -fsanitize=undefined -Wno-error=parentheses -Wno-error=unused-macros" - make -j$(nproc) - make check -j$(nproc) - - CFLAGS="-fsanitize=undefined -fsanitize=bool -fsanitize=alignment -fsanitize=null -fsanitize=bounds-strict -fsanitize=enum -fno-sanitize-recover -g -O2" LDFLAGS="-static-libubsan" dash ./configure + - CFLAGS="-fsanitize=undefined -fsanitize=bool -fsanitize=alignment -fsanitize=null -fsanitize=bounds-strict -fsanitize=enum -fno-sanitize-recover -g -O2" CXXFLAGS=$CFLAGS LDFLAGS="-static-libubsan" dash ./configure --cache-file cache/config.cache --disable-non-suiteb-curves --disable-guile --disable-doc --disable-full-test-suite --with-default-trust-store-pkcs11="pkcs11:" - make -j$(nproc) - make -C tests check -j$(nproc) TESTS="trust-store p11-kit-load.sh" SUBDIRS=. diff --git a/lib/gnutlsxx.cpp b/lib/gnutlsxx.cpp index b91c994436..6a3de6af16 100644 --- a/lib/gnutlsxx.cpp +++ b/lib/gnutlsxx.cpp @@ -60,6 +60,11 @@ namespace gnutls { } + server_session::server_session (int flags):session (GNUTLS_SERVER | + (flags & ~GNUTLS_CLIENT)) + { + } + server_session::~server_session () { } @@ -277,6 +282,11 @@ namespace gnutls { } + client_session::client_session (int flags):session (GNUTLS_CLIENT | + (flags & ~GNUTLS_SERVER)) + { + } + client_session::~client_session () { } diff --git a/lib/includes/gnutls/gnutlsxx.h b/lib/includes/gnutls/gnutlsxx.h index 1ed83fbd44..561dcaca1f 100644 --- a/lib/includes/gnutls/gnutlsxx.h +++ b/lib/includes/gnutls/gnutlsxx.h @@ -237,6 +237,7 @@ namespace gnutls { class server_session:public session { public: server_session(); + server_session(int flags); ~server_session(); void db_remove() const; @@ -261,6 +262,7 @@ namespace gnutls { class client_session:public session { public: client_session(); + client_session(int flags); ~client_session(); void set_server_name(gnutls_server_name_type_t type, diff --git a/tests/Makefile.am b/tests/Makefile.am index 18e1b7a939..99d6f933c8 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -421,8 +421,22 @@ TESTS_ENVIRONMENT += WINDOWS=1 endif -check_PROGRAMS = $(ctests) $(indirect_tests) -TESTS = $(ctests) $(dist_check_SCRIPTS) +cpptests = +if ENABLE_CXX +if HAVE_CMOCKA + +cpptests += sanity-cpp + +sanity_cpp_SOURCES = sanity-cpp.cpp +sanity_cpp_LDADD = $(CMOCKA_LDADD) ../lib/libgnutlsxx.la +sanity_cpp_CXXFLAGS = $(AM_CPPFLAGS) \ + -I$(top_srcdir)/gl \ + -I$(top_builddir)/gl +endif +endif + +check_PROGRAMS = $(cpptests) $(ctests) $(indirect_tests) +TESTS = $(cpptests) $(ctests) $(dist_check_SCRIPTS) TESTS_ENVIRONMENT += \ CC="$(CC)" \ diff --git a/tests/cert-common.h b/tests/cert-common.h index d8106641b2..7f312988ff 100644 --- a/tests/cert-common.h +++ b/tests/cert-common.h @@ -71,11 +71,11 @@ static char ecc_cert[] = "-----END CERTIFICATE-----\n"; const gnutls_datum_t server_ecc_cert = - {(void *) ecc_cert, sizeof(ecc_cert)-1}; + {(unsigned char*) ecc_cert, sizeof(ecc_cert)-1}; const gnutls_datum_t server_ecc_key = - {(void *) ecc_key, sizeof(ecc_key)-1}; + {(unsigned char*) ecc_key, sizeof(ecc_key)-1}; /* A cert-key pair */ static char pem1_cert[] = @@ -111,11 +111,11 @@ static char pem1_key[] = "-----END RSA PRIVATE KEY-----\n"; const gnutls_datum_t cert_dat = - {(void *) pem1_cert, sizeof(pem1_cert)-1}; + {(unsigned char*) pem1_cert, sizeof(pem1_cert)-1}; const gnutls_datum_t key_dat = - {(void *) pem1_key, sizeof(pem1_key)-1}; + {(unsigned char*) pem1_key, sizeof(pem1_key)-1}; /* A server cert/key pair with CA */ @@ -398,7 +398,7 @@ static char dsa_key_pem[] = "jlvXN8gyPpbCPvRD2n2RAg+3vPjvj/dBAF6W3w8IltzqsukGgq/SLwIUS5/r/2ya\n" "AoNBXjeBjgCGMei2m8E=\n" "-----END DSA PRIVATE KEY-----\n"; -const gnutls_datum_t dsa_key = { (void*)dsa_key_pem, +const gnutls_datum_t dsa_key = { (unsigned char*)dsa_key_pem, sizeof(dsa_key_pem)-1 }; @@ -471,11 +471,11 @@ static char ca3_key_pem[] = "xkwfZG91IiOdKlKEddraZb3OppP1j7HsiyaYmwIMtsPc9wa2EsU=\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t ca3_key = { (void*)ca3_key_pem, +const gnutls_datum_t ca3_key = { (unsigned char*)ca3_key_pem, sizeof(ca3_key_pem)-1 }; -const gnutls_datum_t ca3_cert = { (void*)ca3_cert_pem, +const gnutls_datum_t ca3_cert = { (unsigned char*)ca3_cert_pem, sizeof(ca3_cert_pem)-1 }; @@ -547,11 +547,11 @@ static char subca3_key_pem[] = "CLo/c/+F0N4e0F7P+haq+Ccj6MNM99HnuJALc1Ke9971YxrNfniGvA==\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t subca3_key = { (void*)subca3_key_pem, +const gnutls_datum_t subca3_key = { (unsigned char*)subca3_key_pem, sizeof(subca3_key_pem)-1 }; -const gnutls_datum_t subca3_cert = { (void*)subca3_cert_pem, +const gnutls_datum_t subca3_cert = { (unsigned char*)subca3_cert_pem, sizeof(subca3_cert_pem)-1 }; @@ -675,15 +675,15 @@ static char cli_ca3_key_pem[] = "uiyti3689G0RW9DM/F+NeJkoEo0D15JweVkSfDcsVTdvNsbeA1Pzzds=\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t cli_ca3_key = { (void*)cli_ca3_key_pem, +const gnutls_datum_t cli_ca3_key = { (unsigned char*)cli_ca3_key_pem, sizeof(cli_ca3_key_pem)-1 }; -const gnutls_datum_t cli_ca3_cert = { (void*)cli_ca3_cert_pem, +const gnutls_datum_t cli_ca3_cert = { (unsigned char*)cli_ca3_cert_pem, sizeof(cli_ca3_cert_pem)-1 }; -const gnutls_datum_t cli_ca3_cert_chain = { (void*)cli_ca3_cert_chain_pem, +const gnutls_datum_t cli_ca3_cert_chain = { (unsigned char*)cli_ca3_cert_chain_pem, sizeof(cli_ca3_cert_chain_pem)-1 }; @@ -728,11 +728,11 @@ static char clidsa_ca3_key_pem[] = "FgAdB5hhtag7oTw45a72\n" "-----END DSA PRIVATE KEY-----\n"; -const gnutls_datum_t clidsa_ca3_key = { (void*)clidsa_ca3_key_pem, +const gnutls_datum_t clidsa_ca3_key = { (unsigned char*)clidsa_ca3_key_pem, sizeof(clidsa_ca3_key_pem)-1 }; -const gnutls_datum_t clidsa_ca3_cert = { (void*)clidsa_ca3_cert_pem, +const gnutls_datum_t clidsa_ca3_cert = { (unsigned char*)clidsa_ca3_cert_pem, sizeof(clidsa_ca3_cert_pem)-1 }; @@ -743,7 +743,7 @@ static char server_ca3_ecc_key_pem[] = "GK04pl/ReivZAwibv+85lpT4sm/9RBVhLZM=\n" "-----END EC PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_ecc_key = { (void*)server_ca3_ecc_key_pem, +const gnutls_datum_t server_ca3_ecc_key = { (unsigned char*)server_ca3_ecc_key_pem, sizeof(server_ca3_ecc_key_pem)-1 }; @@ -788,7 +788,7 @@ static char server_ca3_key_pem[] = "r6wql68K+fJ1W38b+ENQ46bZZMvAh8z4MZyzBvS8M/grD0WBBwrWLA==\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_key = { (void*)server_ca3_key_pem, +const gnutls_datum_t server_ca3_key = { (unsigned char*)server_ca3_key_pem, sizeof(server_ca3_key_pem)-1 }; @@ -821,7 +821,7 @@ static char server_ca3_rsa_pss_key_pem[] = "ODwZlaKK2fjp9xr2dNpYjRqyEb1gkC9FJMaxab9OAf+AoQifxncv\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_rsa_pss_key = { (void*)server_ca3_rsa_pss_key_pem, +const gnutls_datum_t server_ca3_rsa_pss_key = { (unsigned char*)server_ca3_rsa_pss_key_pem, sizeof(server_ca3_rsa_pss_key_pem)-1 }; @@ -851,7 +851,7 @@ static char server_ca3_rsa_pss_cert_pem[] = "dPVrVe13NnAP8tV4a8PisLaz9fHLnA==\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_rsa_pss_cert = { (void*)server_ca3_rsa_pss_cert_pem, +const gnutls_datum_t server_ca3_rsa_pss_cert = { (unsigned char*)server_ca3_rsa_pss_cert_pem, sizeof(server_ca3_rsa_pss_cert_pem)-1 }; @@ -886,7 +886,7 @@ static char server_ca3_rsa_pss2_key_pem[] = "nQyUF7m3FUJjavm46KJIhw==\n" "-----END PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_rsa_pss2_key = { (void*)server_ca3_rsa_pss2_key_pem, +const gnutls_datum_t server_ca3_rsa_pss2_key = { (unsigned char*)server_ca3_rsa_pss2_key_pem, sizeof(server_ca3_rsa_pss2_key_pem)-1 }; @@ -915,7 +915,7 @@ static char server_ca3_rsa_pss2_cert_pem[] = "D8aQbKGJzzih56a2wwc0ZqA0ilGm\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_rsa_pss2_cert = { (void*)server_ca3_rsa_pss2_cert_pem, +const gnutls_datum_t server_ca3_rsa_pss2_cert = { (unsigned char*)server_ca3_rsa_pss2_cert_pem, sizeof(server_ca3_rsa_pss2_cert_pem)-1 }; @@ -945,7 +945,7 @@ static char cli_ca3_rsa_pss_cert_pem[] = "vXOeamGsi2jyiC5LbreWecbMnzi3vQ==\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t cli_ca3_rsa_pss_cert = { (void*)cli_ca3_rsa_pss_cert_pem, +const gnutls_datum_t cli_ca3_rsa_pss_cert = { (unsigned char*)cli_ca3_rsa_pss_cert_pem, sizeof(cli_ca3_rsa_pss_cert_pem)-1 }; @@ -957,7 +957,7 @@ static char server_ca3_eddsa_key_pem[] = "MC4CAQAwBQYDK2VwBCIEIBypI9w1qP3WLaiYuWB7zhA99GTG5UsKZVZqPHNlUaIv\n" "-----END PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_eddsa_key = { (void*)server_ca3_eddsa_key_pem, +const gnutls_datum_t server_ca3_eddsa_key = { (unsigned char*)server_ca3_eddsa_key_pem, sizeof(server_ca3_eddsa_key_pem)-1 }; @@ -971,7 +971,7 @@ static char server_ca3_eddsa_cert_pem[] = "KKn/PNiAq4fPNEupyzC3AzE1xLzKLRArAFFDDUjPCwy3OR4js3MF\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_eddsa_cert = { (void*)server_ca3_eddsa_cert_pem, +const gnutls_datum_t server_ca3_eddsa_cert = { (unsigned char*)server_ca3_eddsa_cert_pem, sizeof(server_ca3_eddsa_cert_pem)-1 }; @@ -1055,13 +1055,13 @@ static char server_localhost6_ca3_cert_chain_pem[] = "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_localhost6_cert = { (void*)server_localhost6_ca3_cert_pem, +const gnutls_datum_t server_ca3_localhost6_cert = { (unsigned char*)server_localhost6_ca3_cert_pem, sizeof(server_localhost6_ca3_cert_pem)-1 }; const gnutls_datum_t server_ca3_localhost6_cert_chain = { - (void*)server_localhost6_ca3_cert_chain_pem, + (unsigned char*)server_localhost6_ca3_cert_chain_pem, sizeof(server_localhost6_ca3_cert_chain_pem)-1 }; @@ -1143,13 +1143,13 @@ static char server_ipaddr_ca3_cert_chain_pem[] = "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_ipaddr_cert = { (void*)server_ipaddr_ca3_cert_pem, +const gnutls_datum_t server_ca3_ipaddr_cert = { (unsigned char*)server_ipaddr_ca3_cert_pem, sizeof(server_ipaddr_ca3_cert_pem)-1 }; const gnutls_datum_t server_ca3_ipaddr_cert_chain = { - (void*)server_ipaddr_ca3_cert_chain_pem, + (unsigned char*)server_ipaddr_ca3_cert_chain_pem, sizeof(server_ipaddr_ca3_cert_chain_pem)-1 }; @@ -1182,7 +1182,7 @@ static char server_localhost_utf8_ca3_cert_pem[] = "PexWtASNmu1xcO13LdgN4Oa1OL4P4U9TQVwoCpkjlDSVNLqBC0N5kPmGkOY=\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_localhost_utf8_cert = { (void*)server_localhost_utf8_ca3_cert_pem, +const gnutls_datum_t server_ca3_localhost_utf8_cert = { (unsigned char*)server_localhost_utf8_ca3_cert_pem, sizeof(server_localhost_utf8_ca3_cert_pem)-1 }; @@ -1214,7 +1214,7 @@ static char server_localhost_inv_utf8_ca3_cert_pem[] = "Efwe6rFJaNbKv9C9tWpPIPHRk/YkUIe29VUQR2m7UUpToBca\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_localhost_inv_utf8_cert = { (void*)server_localhost_inv_utf8_ca3_cert_pem, +const gnutls_datum_t server_ca3_localhost_inv_utf8_cert = { (unsigned char*)server_localhost_inv_utf8_ca3_cert_pem, sizeof(server_localhost_inv_utf8_ca3_cert_pem)-1 }; @@ -1239,7 +1239,7 @@ static char server_localhost_ca3_ecc_cert_pem[] = "Li9LPzU70EyX6WF+9FM45E4/Gt9Oh8btrYyjbyH/K2VI8qPRz5cW\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_localhost_ecc_cert = { (void*)server_localhost_ca3_ecc_cert_pem, +const gnutls_datum_t server_ca3_localhost_ecc_cert = { (unsigned char*)server_localhost_ca3_ecc_cert_pem, sizeof(server_localhost_ca3_ecc_cert_pem)-1}; /* shares server_ca3 key */ @@ -1379,17 +1379,17 @@ static char server_localhost_ca3_cert_chain_pem[] = #define server_ca3_cert server_ca3_localhost_cert #define server_ca3_cert_chain server_ca3_localhost_cert_chain -const gnutls_datum_t server_ca3_localhost_cert = { (void*)server_localhost_ca3_cert_pem, +const gnutls_datum_t server_ca3_localhost_cert = { (unsigned char*)server_localhost_ca3_cert_pem, sizeof(server_localhost_ca3_cert_pem)-1}; -const gnutls_datum_t server_ca3_localhost_rsa_decrypt_cert = { (void*)server_localhost_ca3_rsa_decrypt_cert_pem, +const gnutls_datum_t server_ca3_localhost_rsa_decrypt_cert = { (unsigned char*)server_localhost_ca3_rsa_decrypt_cert_pem, sizeof(server_localhost_ca3_rsa_decrypt_cert_pem)-1}; -const gnutls_datum_t server_ca3_localhost_rsa_sign_cert = { (void*)server_localhost_ca3_rsa_sign_cert_pem, +const gnutls_datum_t server_ca3_localhost_rsa_sign_cert = { (unsigned char*)server_localhost_ca3_rsa_sign_cert_pem, sizeof(server_localhost_ca3_rsa_sign_cert_pem)-1}; const gnutls_datum_t server_ca3_localhost_cert_chain = { - (void*)server_localhost_ca3_cert_chain_pem, + (unsigned char*)server_localhost_ca3_cert_chain_pem, sizeof(server_localhost_ca3_cert_chain_pem)-1 }; @@ -1415,7 +1415,7 @@ static char server_localhost_insecure_ca3_cert_pem[] = "F92lL9akoGYmyehqQHeRQsrVRKcCOiv8lgVF\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t server_ca3_localhost_insecure_cert = { (void*)server_localhost_insecure_ca3_cert_pem, +const gnutls_datum_t server_ca3_localhost_insecure_cert = { (unsigned char*)server_localhost_insecure_ca3_cert_pem, sizeof(server_localhost_insecure_ca3_cert_pem)-1 }; @@ -1433,7 +1433,7 @@ static char server_ca3_localhost_insecure_key_pem[] = "dD2lxHfq5Q1QxCSjl8EnBnjnbFJN9WmK9ztkK00Avg==\n" "-----END RSA PRIVATE KEY-----\n"; -const gnutls_datum_t server_ca3_localhost_insecure_key = { (void*)server_ca3_localhost_insecure_key_pem, +const gnutls_datum_t server_ca3_localhost_insecure_key = { (unsigned char*)server_ca3_localhost_insecure_key_pem, sizeof(server_ca3_localhost_insecure_key_pem)-1 }; @@ -1462,7 +1462,7 @@ static char unknown_ca_cert_pem[] = "nva6xGbu1R8UP4+fXCOFdiVixHEQR5k+mqd65vGGxovALAfY\n" "-----END CERTIFICATE-----\n"; -const gnutls_datum_t unknown_ca_cert = { (void*)unknown_ca_cert_pem, +const gnutls_datum_t unknown_ca_cert = { (unsigned char*)unknown_ca_cert_pem, sizeof(unknown_ca_cert_pem)-1 }; @@ -1563,7 +1563,7 @@ static const char server_ca3_pkcs12_pem[] = "BAjXfJCHoHZI2QICCAA=\n" "-----END PKCS12-----\n"; -const gnutls_datum_t server_ca3_pkcs12 = { (void*)server_ca3_pkcs12_pem, +const gnutls_datum_t server_ca3_pkcs12 = { (unsigned char*)server_ca3_pkcs12_pem, sizeof(server_ca3_pkcs12_pem)-1 }; diff --git a/tests/sanity-cpp.cpp b/tests/sanity-cpp.cpp new file mode 100644 index 0000000000..70f9e55d9b --- /dev/null +++ b/tests/sanity-cpp.cpp @@ -0,0 +1,236 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. + * + * Author: Nikos Mavrogiannopoulos + * + * This file is part of GnuTLS. + * + * GnuTLS is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuTLS 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 Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/> + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <gnutls/gnutls.h> +#include <gnutls/gnutlsxx.h> +#include <iostream> + +extern "C" { +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <assert.h> +#include "cert-common.h" +#include <setjmp.h> +#include <cmocka.h> +#include <minmax.h> +} + +/* This is a basic test for C++ API */ +static void tls_log_func(int level, const char *str) +{ + std::cerr << level << "| " << str << "\n"; +} + +static char to_server[64 * 1024]; +static size_t to_server_len = 0; + +static char to_client[64 * 1024]; +static size_t to_client_len = 0; + +static ssize_t +client_push(gnutls_transport_ptr_t tr, const void *data, size_t len) +{ + size_t newlen; + + len = MIN(len, sizeof(to_server) - to_server_len); + + newlen = to_server_len + len; + memcpy(to_server + to_server_len, data, len); + to_server_len = newlen; + + return len; +} + +static ssize_t +client_pull(gnutls_transport_ptr_t tr, void *data, size_t len) +{ + if (to_client_len == 0) { + errno = EAGAIN; + return -1; + } + + len = MIN(len, to_client_len); + + memcpy(data, to_client, len); + + memmove(to_client, to_client + len, to_client_len - len); + to_client_len -= len; + return len; +} + +static ssize_t +server_pull(gnutls_transport_ptr_t tr, void *data, size_t len) +{ + if (to_server_len == 0) { + errno = EAGAIN; + return -1; + } + + len = MIN(len, to_server_len); + memcpy(data, to_server, len); + + memmove(to_server, to_server + len, to_server_len - len); + to_server_len -= len; + + return len; +} + +static ssize_t +server_push(gnutls_transport_ptr_t tr, const void *data, size_t len) +{ + size_t newlen; + + len = MIN(len, sizeof(to_client) - to_client_len); + + newlen = to_client_len + len; + memcpy(to_client + to_client_len, data, len); + to_client_len = newlen; + + return len; +} + +inline static void reset_buffers(void) +{ + to_server_len = 0; + to_client_len = 0; +} + +#define MSG "test message" +static void test_handshake(void **glob_state, const char *prio, + gnutls::server_session& server, gnutls::client_session& client) +{ + gnutls::certificate_credentials serverx509cred; + int sret, cret; + gnutls::certificate_credentials clientx509cred; + char buffer[64]; + int ret; + + /* General init. */ + reset_buffers(); + gnutls_global_set_log_function(tls_log_func); + + try { + serverx509cred.set_x509_key(server_cert, server_key, GNUTLS_X509_FMT_PEM); + server.set_credentials(serverx509cred); + + server.set_priority(prio, NULL); + + server.set_transport_push_function(server_push); + server.set_transport_pull_function(server_pull); + + client.set_priority(prio, NULL); + client.set_credentials(clientx509cred); + + client.set_transport_push_function(client_push); + client.set_transport_pull_function(client_pull); + } + catch (std::exception &ex) { + std::cerr << "Exception caught: " << ex.what() << std::endl; + } + + sret = cret = GNUTLS_E_AGAIN; + + do { + if (cret == GNUTLS_E_AGAIN) { + try { + cret = client.handshake(); + } catch(gnutls::exception &ex) { + if (ex.get_code() == GNUTLS_E_INTERRUPTED || ex.get_code() == GNUTLS_E_AGAIN) + cret = GNUTLS_E_AGAIN; + } + } + if (sret == GNUTLS_E_AGAIN) { + try { + sret = server.handshake(); + } catch(gnutls::exception &ex) { + if (ex.get_code() == GNUTLS_E_INTERRUPTED || ex.get_code() == GNUTLS_E_AGAIN) + sret = GNUTLS_E_AGAIN; + } + } + } + while ((cret == GNUTLS_E_AGAIN || (cret == 0 && sret == GNUTLS_E_AGAIN)) && + (sret == GNUTLS_E_AGAIN || (sret == 0 && cret == GNUTLS_E_AGAIN))); + + try { + client.send(MSG, sizeof(MSG)-1); + ret = server.recv(buffer, sizeof(buffer)); + + assert(ret == sizeof(MSG)-1); + assert(memcmp(buffer, MSG, sizeof(MSG)-1) == 0); + + client.bye(GNUTLS_SHUT_WR); + server.bye(GNUTLS_SHUT_WR); + } + catch (std::exception &ex) { + std::cerr << "Exception caught: " << ex.what() << std::endl; + } + + return; +} + +static void tls_handshake(void **glob_state) +{ + gnutls::server_session server; + gnutls::client_session client; + + test_handshake(glob_state, "NORMAL", server, client); +} + +static void tls_handshake_alt(void **glob_state) +{ + gnutls::server_session server(0); + gnutls::client_session client(0); + + test_handshake(glob_state, "NORMAL", server, client); +} + +static void tls12_handshake(void **glob_state) +{ + gnutls::server_session server; + gnutls::client_session client; + + test_handshake(glob_state, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.2", server, client); +} + +static void tls13_handshake(void **glob_state) +{ + gnutls::server_session server; + gnutls::client_session client; + + test_handshake(glob_state, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3", server, client); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(tls_handshake), + cmocka_unit_test(tls_handshake_alt), + cmocka_unit_test(tls13_handshake), + cmocka_unit_test(tls12_handshake) + }; + return cmocka_run_group_tests(tests, NULL, NULL); +} |