diff options
author | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-12-19 11:01:10 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@redhat.com> | 2016-12-19 11:02:38 +0100 |
commit | d81cfbd8fb522b9c21246bdaf47613e5b7c6de47 (patch) | |
tree | 55de8e8bdd8a525acce6ee0049a343fbf7fa3959 /tests | |
parent | aaf49747f981f6c17cdc9ea7495a8948a5015ae2 (diff) | |
download | gnutls-d81cfbd8fb522b9c21246bdaf47613e5b7c6de47.tar.gz |
tests: added check for multiple calls to gnutls_priority_set_direct()
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/priority-mix.c | 172 |
2 files changed, 173 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 4128560f67..2e37ad01f7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -120,7 +120,7 @@ ctests = mini-record-2 simple gc set_pkcs12_cred cert certuniqueid \ set_known_dh_params_anon set_known_dh_params_psk session-tickets-ok \ session-tickets-missing set_x509_key_file_legacy status-request-ext \ rng-no-onload dtls1-2-mtu-check crl_apis cert_verify_inv_utf8 \ - hostname-check-utf8 pkcs8-key-decode-encrypted + hostname-check-utf8 pkcs8-key-decode-encrypted priority-mix if HAVE_SECCOMP_TESTS ctests += dtls-with-seccomp tls-with-seccomp dtls-client-with-seccomp tls-client-with-seccomp diff --git a/tests/priority-mix.c b/tests/priority-mix.c new file mode 100644 index 0000000000..ff9393738e --- /dev/null +++ b/tests/priority-mix.c @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2008-2012 Free Software Foundation, Inc. + * Copyright (C) 2016 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 General Public License + * along with GnuTLS; 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <gnutls/gnutls.h> +#include <assert.h> +#include "utils.h" +#include "eagain-common.h" +#include "cert-common.h" + +const char *side; + +static void tls_log_func(int level, const char *str) +{ + fprintf(stderr, "%s|<%d>| %s", side, level, str); +} + +static time_t mytime(time_t * t) +{ + time_t then = 1461671166; + + if (t) + *t = then; + + return then; +} + +/* Tests whether multiple calls to gnutls_priority_set_direct() would work + * as intended. */ + +void doit(void) +{ + int exit_code = EXIT_SUCCESS; + int ret; + /* Server stuff. */ + gnutls_certificate_credentials_t serverx509cred; + gnutls_session_t server; + int sret = GNUTLS_E_AGAIN; + /* Client stuff. */ + gnutls_certificate_credentials_t clientx509cred; + gnutls_session_t client; + int cret = GNUTLS_E_AGAIN; + + /* General init. */ + global_init(); + gnutls_global_set_log_function(tls_log_func); + if (debug) + gnutls_global_set_log_level(6); + + gnutls_global_set_time_function(mytime); + + /* Init server */ + gnutls_certificate_allocate_credentials(&serverx509cred); + gnutls_certificate_set_x509_key_mem(serverx509cred, + &server_cert, &server_key, + GNUTLS_X509_FMT_PEM); + + gnutls_init(&server, GNUTLS_SERVER); + gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, + serverx509cred); + assert(gnutls_priority_set_direct(server, + "SECURE256", + NULL) >= 0); + assert(gnutls_priority_set_direct(server, + "NORMAL", + NULL) >= 0); + gnutls_transport_set_push_function(server, server_push); + gnutls_transport_set_pull_function(server, server_pull); + gnutls_transport_set_ptr(server, server); + + /* Init client */ + ret = gnutls_certificate_allocate_credentials(&clientx509cred); + if (ret < 0) + exit(1); + + ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca_cert, GNUTLS_X509_FMT_PEM); + if (ret < 0) + exit(1); + + ret = gnutls_init(&client, GNUTLS_CLIENT); + if (ret < 0) + exit(1); + + ret = gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, + clientx509cred); + if (ret < 0) + exit(1); + + assert(gnutls_priority_set_direct(client, + "PFS:%PROFILE_ULTRA", + NULL) >= 0); + assert(gnutls_priority_set_direct(client, + "NORMAL", + NULL) >= 0); + gnutls_transport_set_push_function(client, client_push); + gnutls_transport_set_pull_function(client, client_pull); + gnutls_transport_set_ptr(client, client); + + HANDSHAKE(client, server); + + /* check gnutls_certificate_get_ours() - client side */ + { + const gnutls_datum_t *mcert; + + mcert = gnutls_certificate_get_ours(client); + if (mcert != NULL) { + fail("gnutls_certificate_get_ours(): failed\n"); + exit(1); + } + } + + /* check the number of certificates received and verify */ + { + unsigned status; + + ret = gnutls_certificate_verify_peers3(client, "localhost", &status); + if (ret < 0) { + fail("could not verify certificate: %s\n", gnutls_strerror(ret)); + exit(1); + } + + if (status != 0) { + fail("could not verify certificate: %.4x\n", status); + exit(1); + } + } + + gnutls_bye(client, GNUTLS_SHUT_RDWR); + gnutls_bye(server, GNUTLS_SHUT_RDWR); + + gnutls_deinit(client); + gnutls_deinit(server); + + gnutls_certificate_free_credentials(serverx509cred); + gnutls_certificate_free_credentials(clientx509cred); + + gnutls_global_deinit(); + + if (debug > 0) { + if (exit_code == 0) + puts("Self-test successful"); + else + puts("Self-test failed"); + } +} |