diff options
author | Tom Vrancken <dev@tomvrancken.nl> | 2018-08-15 18:29:32 +0200 |
---|---|---|
committer | Tom Vrancken <dev@tomvrancken.nl> | 2018-08-20 17:08:01 +0200 |
commit | 07180a416731749883234f931ac18831ff38abbb (patch) | |
tree | 0b9d13b9ad394477d566f01ba8e279c33711cc7c /tests | |
parent | a42db538c3f01aa76e2c1a2affc39237840c2522 (diff) | |
download | gnutls-07180a416731749883234f931ac18831ff38abbb.tar.gz |
Implemented RFC7250 certificate type negotiation extensions.
Signed-off-by: Tom Vrancken <dev@tomvrancken.nl>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 5 | ||||
-rw-r--r-- | tests/crt_type-neg-common.c | 209 | ||||
-rw-r--r-- | tests/tls-crt_type-neg.c | 303 |
3 files changed, 515 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 3a42b90bf9..c0ecfed3f2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -67,7 +67,8 @@ EXTRA_DIST = suppressions.valgrind eagain-common.h cert-common.h test-chains.h \ data/pkcs7-cat-ca.pem data/long.crl data/long.pem data/large-cert.pem \ testpkcs11.pkcs15 testpkcs11.softhsm testpkcs11.sc-hsm testpkcs11-certs/ca.crt testpkcs11-certs/ca-tmpl \ testpkcs11-certs/client.key testpkcs11-certs/server.crt testpkcs11-certs/server-tmpl \ - testpkcs11-certs/ca.key testpkcs11-certs/client.crt testpkcs11-certs/client-tmpl testpkcs11-certs/server.key + testpkcs11-certs/ca.key testpkcs11-certs/client.crt testpkcs11-certs/client-tmpl testpkcs11-certs/server.key \ + crt_type-neg-common.c AM_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS) AM_CPPFLAGS = \ @@ -202,7 +203,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei ip-check mini-x509-ipaddr trust-store base64-raw random-art dhex509self \ dss-sig-val sign-pk-api tls-session-ext-override record-pad \ tls13-server-kx-neg gnutls_ext_raw_parse_dtls key-export-pkcs8 \ - null_retrieve_function tls-record-size-limit + null_retrieve_function tls-record-size-limit tls-crt_type-neg if HAVE_SECCOMP_TESTS ctests += dtls-with-seccomp tls-with-seccomp dtls-client-with-seccomp tls-client-with-seccomp diff --git a/tests/crt_type-neg-common.c b/tests/crt_type-neg-common.c new file mode 100644 index 0000000000..ac99e20984 --- /dev/null +++ b/tests/crt_type-neg-common.c @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2017 - 2018 ARPA2 project + * + * Author: Tom Vrancken + * + * 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/> + */ +#include <assert.h> +#include <gnutls/gnutls.h> +#include <gnutls/abstract.h> +#include <stdbool.h> + +// Credential type flags +#define CRED_EMPTY 1<<0 +#define CRED_X509 1<<1 +#define CRED_RAWPK 1<<2 + +// Test case definition +typedef struct test_case_st { + const char *name; + const char *client_prio; + const char *server_prio; + const char set_cli_creds; + const char set_srv_creds; + gnutls_certificate_type_t expected_cli_ctype; + gnutls_certificate_type_t expected_srv_ctype; + int client_err; + int server_err; + bool enable_cert_type_neg_cli; + bool enable_cert_type_neg_srv; +} test_case_st; + + +static void try(test_case_st * test) +{ + int sret, cret; // Needed for HANDSHAKE macro + /* To hold negotiated certificate types */ + gnutls_certificate_type_t srv_srv_ctype, srv_cli_ctype; + gnutls_certificate_type_t cli_srv_ctype, cli_cli_ctype; + /* To hold certificate credentials */ + gnutls_certificate_credentials_t client_creds = NULL; + gnutls_certificate_credentials_t server_creds = NULL; + + gnutls_session_t server, client; + gnutls_pubkey_t rawpk = NULL; // For RawPubKey tmp + gnutls_privkey_t privkey = NULL; // For RawPubKey tmp + + sret = cret = GNUTLS_E_AGAIN; + + // Initialize creds + assert(gnutls_certificate_allocate_credentials(&client_creds) >= 0); + assert(gnutls_certificate_allocate_credentials(&server_creds) >= 0); + + // Print test + success("Running %s...\n", test->name); + + // Init client/server + if(test->enable_cert_type_neg_cli) { + assert(gnutls_init(&client, GNUTLS_CLIENT | GNUTLS_ENABLE_CERT_TYPE_NEG) >= 0); + } else { + assert(gnutls_init(&client, GNUTLS_CLIENT) >= 0); + } + + if (test->enable_cert_type_neg_srv) { + assert(gnutls_init(&server, GNUTLS_SERVER | GNUTLS_ENABLE_CERT_TYPE_NEG) >= 0); + } else { + assert(gnutls_init(&server, GNUTLS_SERVER) >= 0); + } + + /* Set up our credentials for this handshake */ + // Test for using empty cli credentials + if (test->set_cli_creds == CRED_EMPTY) { + gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, client_creds); + } else { + // Test for using X509 cli credentials + if (test->set_cli_creds & CRED_X509) { + assert(gnutls_certificate_set_x509_key_mem + (client_creds, &cli_ca3_cert, &cli_ca3_key, GNUTLS_X509_FMT_PEM) >= 0); + } + + // Test for using RawPubKey cli credentials + if (test->set_cli_creds & CRED_RAWPK) { + // TODO set client RawPubKey when support is ready + } + + // -- Add extra ctype creds here in the future -- + + // Finally set the credentials + gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, client_creds); + } + + // Test for using empty srv credentials + if (test->set_srv_creds == CRED_EMPTY) { + gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, server_creds); + } else { + // Test for using X509 srv credentials + if (test->set_srv_creds & CRED_X509) { + assert(gnutls_certificate_set_x509_key_mem + (server_creds, &server_ca3_localhost_rsa_decrypt_cert, + &server_ca3_key, GNUTLS_X509_FMT_PEM) >= 0); + assert(gnutls_certificate_set_x509_key_mem + (server_creds, &server_ca3_localhost_ecc_cert, + &server_ca3_ecc_key, GNUTLS_X509_FMT_PEM) >= 0); + assert(gnutls_certificate_set_x509_key_mem + (server_creds, &server_ca3_localhost_rsa_sign_cert, + &server_ca3_key, GNUTLS_X509_FMT_PEM) >= 0); + gnutls_certificate_set_known_dh_params(server_creds, + GNUTLS_SEC_PARAM_MEDIUM); + } + + // Test for using RawPubKey srv credentials + if( test->set_srv_creds & CRED_RAWPK ) { + //TODO when RawPK support is finished + } + + // -- Add extra ctype creds here in the future -- + + // Finally set the credentials + gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, server_creds); + } + + // Server settings + gnutls_transport_set_push_function(server, server_push); + gnutls_transport_set_pull_function(server, server_pull); + gnutls_transport_set_ptr(server, server); + assert(gnutls_priority_set_direct(server, test->server_prio, 0) >= 0); + + // Client settings + gnutls_transport_set_push_function(client, client_push); + gnutls_transport_set_pull_function(client, client_pull); + gnutls_transport_set_ptr(client, client); + assert(gnutls_priority_set_direct(client, test->client_prio, 0) >= 0); + + // Try handshake + if (test->client_err && test->server_err) { + /* We expect errors during the handshake and don't check + * any negotiated certificate types */ + HANDSHAKE_EXPECT(client, server, test->client_err, test->server_err); + } else { + /* We expect a handshake without errors and check the negotiated + * certificate types */ + HANDSHAKE(client, server); + + /* Get the negotiated certificate types */ + srv_srv_ctype = + gnutls_certificate_type_get2(server, GNUTLS_CTYPE_SERVER); + srv_cli_ctype = + gnutls_certificate_type_get2(server, GNUTLS_CTYPE_CLIENT); + cli_srv_ctype = + gnutls_certificate_type_get2(client, GNUTLS_CTYPE_SERVER); + cli_cli_ctype = + gnutls_certificate_type_get2(client, GNUTLS_CTYPE_CLIENT); + + /* Check whether the API functions return the correct cert types for OURS and PEERS */ + assert(srv_srv_ctype == gnutls_certificate_type_get2(server, GNUTLS_CTYPE_OURS)); + assert(srv_srv_ctype == gnutls_certificate_type_get2(client, GNUTLS_CTYPE_PEERS)); + assert(cli_cli_ctype == gnutls_certificate_type_get2(server, GNUTLS_CTYPE_PEERS)); + assert(cli_cli_ctype == gnutls_certificate_type_get2(client, GNUTLS_CTYPE_OURS)); + + // For debugging + if (debug) { + success("Srv srv ctype: %s\n", gnutls_certificate_type_get_name(srv_srv_ctype)); + success("Srv cli ctype: %s\n", gnutls_certificate_type_get_name(srv_cli_ctype)); + success("Cli srv ctype: %s\n", gnutls_certificate_type_get_name(cli_srv_ctype)); + success("Cli srv ctype: %s\n", gnutls_certificate_type_get_name(cli_cli_ctype)); + } + + /* Check whether the negotiated certificate types match the expected results */ + // Matching server ctype + if (srv_srv_ctype != cli_srv_ctype) { + fail("%s: client negotiated different server ctype than server (%s, %s)!\n", test->name, gnutls_certificate_type_get_name(cli_srv_ctype), gnutls_certificate_type_get_name(srv_srv_ctype)); + } + // Matching client ctype + if (srv_cli_ctype != cli_cli_ctype) { + fail("%s: client negotiated different client ctype than server (%s, %s)!\n", test->name, gnutls_certificate_type_get_name(cli_cli_ctype), gnutls_certificate_type_get_name(srv_cli_ctype)); + } + // Matching expected server ctype + if (srv_srv_ctype != test->expected_srv_ctype) { + fail("%s: negotiated server ctype diffs the expected (%s, %s)!\n", test->name, gnutls_certificate_type_get_name(srv_srv_ctype), gnutls_certificate_type_get_name(test->expected_srv_ctype)); + } + // Matching expected client ctype + if (srv_cli_ctype != test->expected_cli_ctype) { + fail("%s: negotiated server ctype diffs the expected (%s, %s)!\n", test->name, gnutls_certificate_type_get_name(srv_cli_ctype), gnutls_certificate_type_get_name(test->expected_cli_ctype)); + } + } + + // Cleanup + gnutls_deinit(server); + gnutls_deinit(client); + gnutls_certificate_free_credentials(client_creds); + gnutls_certificate_free_credentials(server_creds); + gnutls_pubkey_deinit(rawpk); + gnutls_privkey_deinit(privkey); + + reset_buffers(); +} diff --git a/tests/tls-crt_type-neg.c b/tests/tls-crt_type-neg.c new file mode 100644 index 0000000000..ff5aa08885 --- /dev/null +++ b/tests/tls-crt_type-neg.c @@ -0,0 +1,303 @@ +/* + * Copyright (C) 2017 - 2018 ARPA2 project + * + * Author: Tom Vrancken + * + * 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 + +/* This program tests the certificate type negotiation mechnism for + * the handshake as specified in RFC7250 */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <gnutls/gnutls.h> +#include "utils.h" +#include "cert-common.h" +#include "eagain-common.h" +#include "crt_type-neg-common.c" + +test_case_st tests[] = { + /* Tests with only a single credential set for client/server. + * Tests for X.509 cases. + */ + { + /* Default case A + * + * Priority cli: NORMAL + * Priority srv: NORMAL + * Certificate negotiation mechanism: disabled + * Cli creds: None + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: cert types should default to X.509 + */ + .name = "Default case A. Neg off (default). Creds set (CLI/SRV): None/X509.", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_EMPTY, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = false, + .enable_cert_type_neg_srv = false}, + { + /* Default case B + * + * Priority: NORMAL + * Certificate negotiation mechanism: disabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: cert types should default to X.509 + */ + .name = "Default case B. Neg off (default). Creds set (CLI/SRV): X509/X509.", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = false, + .enable_cert_type_neg_srv = false}, + { + /* No server credentials + * + * Priority: NORMAL + * Certificate negotiation mechanism: disabled + * Cli creds: None + * Srv creds: None + * Handshake: results in errors + * Negotiation: cert types are not evaluated + */ + .name = "No server creds. Creds set (CLI/SRV): None/None.", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_EMPTY, + .set_srv_creds = CRED_EMPTY, + .client_err = GNUTLS_E_AGAIN, + .server_err = GNUTLS_E_NO_CIPHER_SUITES, + .enable_cert_type_neg_cli = false, + .enable_cert_type_neg_srv = false}, + { + /* Client can negotiate, server not + * + * Priority: NORMAL + * Certificate negotiation mechanism (cli/srv): enabled/disabled + * Cli creds: None + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: cert types should default to X.509 + */ + .name = "Client can negotiate, server not", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_EMPTY, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = false}, + { + /* Server can negotiate, client not + * + * Priority: NORMAL + * Certificate negotiation mechanism (cli/srv): disabled/enabled + * Cli creds: None + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: cert types should default to X.509 + */ + .name = "Server can negotiate, client not", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_EMPTY, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = false, + .enable_cert_type_neg_srv = true}, + { + /* Client and server can negotiate + * + * Priority: NORMAL + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: None + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: cert types should default to X.509 + */ + .name = "Client and server can negotiate", + .client_prio = "NORMAL", + .server_prio = "NORMAL", + .set_cli_creds = CRED_EMPTY, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* Negotiate both, cli creds x509, srv creds x509 + * + * Priority: NORMAL + request x509 for cli and srv + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate CLI X.509 + SRV X.509, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-CLI-X509:+CTYPE-SRV-X509", + .server_prio = "NORMAL:+CTYPE-CLI-X509:+CTYPE-SRV-X509", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* Negotiate cli x509, cli creds x509, srv creds x509 + * + * Priority: NORMAL + request x509 for cli + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate CLI X.509, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-CLI-X509", + .server_prio = "NORMAL:+CTYPE-CLI-X509", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* Negotiate srv x509, cli creds x509, srv creds x509 + * + * Priority: NORMAL + request x509 for srv + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate SRV X.509, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-SRV-X509", + .server_prio = "NORMAL:+CTYPE-SRV-X509", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* All types allowed for CLI, cli creds x509, srv creds x509 + * + * Priority: NORMAL + allow all client cert types + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate CLI all, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-CLI-ALL", + .server_prio = "NORMAL:+CTYPE-CLI-ALL", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* All types allowed for SRV, cli creds x509, srv creds x509 + * + * Priority: NORMAL + allow all server cert types + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate SRV all, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-SRV-ALL", + .server_prio = "NORMAL:+CTYPE-SRV-ALL", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true}, + { + /* All types allowed for CLI/SRV, cli creds x509, srv creds x509 + * + * Priority: NORMAL + allow all client and server cert types + * Certificate negotiation mechanism (cli/srv): enabled/enabled + * Cli creds: X.509 + * Srv creds: X.509 + * Handshake: should complete without errors + * Negotiation: Fallback to default cli X.509, srv X.509 because + * we advertise with only the cert type defaults. + */ + .name = "Negotiate CLI/SRV all, cli/srv X.509 creds set", + .client_prio = "NORMAL:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL", + .server_prio = "NORMAL:+CTYPE-CLI-ALL:+CTYPE-SRV-ALL", + .set_cli_creds = CRED_X509, + .set_srv_creds = CRED_X509, + .expected_cli_ctype = GNUTLS_CRT_X509, + .expected_srv_ctype = GNUTLS_CRT_X509, + .enable_cert_type_neg_cli = true, + .enable_cert_type_neg_srv = true} + + /* Tests with only a single credential set for client/server. + * Tests for Raw public-key cases. + */ + //TODO implement when Raw public key support is finished + + /* Tests with only a single credential set for client/server. + * Tests for KDH cases. + */ + //TODO implement when KDH support is finished + + /* Tests with multiple credentials set for client/server. */ + //TODO implement when support for more cert types is ready +}; + +void doit(void) +{ + unsigned i; + global_init(); + + for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + try(&tests[i]); + } + + gnutls_global_deinit(); +} |