From 2ffcfe5e45738e7d7b8df34676c59eaa1960c239 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 2 Jul 2018 16:19:35 +0200 Subject: generate_session_ticket: fixed comment Signed-off-by: Nikos Mavrogiannopoulos --- lib/tls13/session_ticket.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tls13/session_ticket.c b/lib/tls13/session_ticket.c index dfe4992669..213e6a8970 100644 --- a/lib/tls13/session_ticket.c +++ b/lib/tls13/session_ticket.c @@ -188,7 +188,7 @@ generate_session_ticket(gnutls_session_t session, tls13_ticket_t *ticket) if ((ret = gnutls_rnd(GNUTLS_RND_NONCE, &ticket->age_add, sizeof(uint32_t))) < 0) return gnutls_assert_val(ret); - /* Set ticket lifetime to 1 day (86400 seconds) */ + /* Set ticket lifetime to the default expiration time */ ticket->lifetime = session->internals.expire_time; ticket->prf = session->security_parameters.prf; -- cgit v1.2.1 From 2a4bc165bf62353316bb3d842cd4b24833468979 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 2 Jul 2018 16:22:04 +0200 Subject: pre_shared_key: use time_t type for ticket_age variable This is guarranteed to allow negative values, and also be 32-bits. Signed-off-by: Nikos Mavrogiannopoulos --- lib/ext/pre_shared_key.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ext/pre_shared_key.c b/lib/ext/pre_shared_key.c index b12d853af8..60ed86f742 100644 --- a/lib/ext/pre_shared_key.c +++ b/lib/ext/pre_shared_key.c @@ -201,8 +201,7 @@ client_send_params(gnutls_session_t session, unsigned next_idx; const mac_entry_st *prf_res = NULL; const mac_entry_st *prf_psk = NULL; - time_t cur_time; - int ticket_age; + time_t cur_time, ticket_age; uint32_t ob_ticket_age; int free_username = 0; psk_auth_info_t info = NULL; @@ -470,7 +469,7 @@ static int server_recv_params(gnutls_session_t session, struct psk_st psk; psk_auth_info_t info; tls13_ticket_t ticket_data; - int ticket_age; + time_t ticket_age; bool resuming; ret = _gnutls13_psk_ext_parser_init(&psk_parser, data, len); -- cgit v1.2.1 From 6317bdaebfa656b90af58d121de9aae2297b4684 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 2 Jul 2018 16:29:04 +0200 Subject: generate_session_ticket: use a 4-byte nonce by default It is not necessary to use large nonces. Signed-off-by: Nikos Mavrogiannopoulos --- lib/tls13/session_ticket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tls13/session_ticket.c b/lib/tls13/session_ticket.c index 213e6a8970..ca11fd78a6 100644 --- a/lib/tls13/session_ticket.c +++ b/lib/tls13/session_ticket.c @@ -178,8 +178,8 @@ generate_session_ticket(gnutls_session_t session, tls13_ticket_t *ticket) gnutls_datum_t packed = { NULL, 0 }; tls13_ticket_t ticket_data; - /* Generate a random 128-bit ticket nonce */ - ticket->nonce_size = 16; + /* Generate a random 32-bit ticket nonce */ + ticket->nonce_size = 4; if ((ret = gnutls_rnd(GNUTLS_RND_NONCE, ticket->nonce, ticket->nonce_size)) < 0) -- cgit v1.2.1 From 9b69b3e9690021456ab1086aeb1125a9753f9ae0 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 3 Jul 2018 10:22:04 +0200 Subject: doc: mention session ticket behavior under TLS1.3 Signed-off-by: Nikos Mavrogiannopoulos --- doc/cha-intro-tls.texi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/cha-intro-tls.texi b/doc/cha-intro-tls.texi index 0c82f0853b..b95abc6b81 100644 --- a/doc/cha-intro-tls.texi +++ b/doc/cha-intro-tls.texi @@ -464,6 +464,9 @@ regularly. Since version 3.1.3 GnuTLS clients transparently support session tickets, unless forward secrecy is explicitly requested (with the PFS priority string). +Under TLS 1.3 session tickets are mandatory for session resumption, and they +do not share the forward secrecy concerns as with TLS 1.2 or earlier. + @node HeartBeat @subsection HeartBeat @cindex TLS extensions -- cgit v1.2.1 From c8224ea701cd470bb4643e8d8d86115f2befaa66 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 2 Jul 2018 20:25:40 +0200 Subject: tests: corrected priority strings in session-tickets-ok and other cleanups Signed-off-by: Nikos Mavrogiannopoulos --- tests/session-tickets-ok.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/tests/session-tickets-ok.c b/tests/session-tickets-ok.c index f33e1967c4..56aae5063c 100644 --- a/tests/session-tickets-ok.c +++ b/tests/session-tickets-ok.c @@ -74,7 +74,8 @@ static int handshake_callback(gnutls_session_t session, unsigned int htype, if (htype != GNUTLS_HANDSHAKE_NEW_SESSION_TICKET) return 0; - success("sent session ticket\n"); + if (debug) + success("sent session ticket\n"); sent = 1; return 0; } @@ -86,7 +87,6 @@ static void client(int fd, const char *prio) int ret; gnutls_certificate_credentials_t x509_cred; gnutls_session_t session; - /* Need to enable anonymous KX specifically. */ global_init(); @@ -97,31 +97,19 @@ static void client(int fd, const char *prio) gnutls_certificate_allocate_credentials(&x509_cred); - /* Initialize TLS session - */ - gnutls_init(&session, GNUTLS_CLIENT); + assert(gnutls_init(&session, GNUTLS_CLIENT)>=0); - /* Use default priorities */ - gnutls_priority_set_direct(session, "NORMAL:-KX-ALL:+ECDHE-RSA", NULL); + assert(gnutls_priority_set_direct(session, prio, NULL)>=0); - /* put the anonymous credentials to the current session - */ gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); gnutls_transport_set_int(session, fd); - /* Perform the TLS handshake - */ do { ret = gnutls_handshake(session); } while (ret < 0 && gnutls_error_is_fatal(ret) == 0); - if (ret == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) { - /* success */ - goto end; - } - if (ret < 0) { fail("client: Handshake failed: %s\n", gnutls_strerror(ret)); terminate(); @@ -137,8 +125,6 @@ static void client(int fd, const char *prio) gnutls_bye(session, GNUTLS_SHUT_WR); - end: - close(fd); gnutls_deinit(session); @@ -193,7 +179,7 @@ static void server(int fd, const char *prio) /* avoid calling all the priority functions, since the defaults * are adequate. */ - assert(gnutls_priority_set_direct(session, "NORMAL", NULL)>=0); + assert(gnutls_priority_set_direct(session, prio, NULL)>=0); gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); -- cgit v1.2.1 From 6d6d9c1ba8d1f73e82ddb2b1698b0c67d45174d3 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 3 Jul 2018 18:42:01 +0200 Subject: pre_shared_key: do not send extension when no identities are present Signed-off-by: Nikos Mavrogiannopoulos --- lib/ext/pre_shared_key.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/ext/pre_shared_key.c b/lib/ext/pre_shared_key.c index 60ed86f742..dc56d1dc49 100644 --- a/lib/ext/pre_shared_key.c +++ b/lib/ext/pre_shared_key.c @@ -339,6 +339,13 @@ client_send_params(gnutls_session_t session, binders_len += 1 + _gnutls_mac_get_algo_len(prf_psk); } + /* if no tickets or identities to be sent */ + if (psk_id_len == 0) { + /* reset extensions buffer */ + extdata->length = spos; + return 0; + } + _gnutls_write_uint16(psk_id_len, &extdata->data[spos]); binders_pos = extdata->length-spos; -- cgit v1.2.1 From bd87a9ffbf286f802c171f92b87b5f4edfcdfeb7 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Mon, 2 Jul 2018 16:28:28 +0200 Subject: generate_session_ticket: tickets cannot extend the original session time That is, on a resumed session the server would not issue new tickets that would have extended the lifetime of the originally issued ticket. Resolves #476 Signed-off-by: Nikos Mavrogiannopoulos --- lib/constate.c | 2 +- lib/tls13/session_ticket.c | 27 ++++- tests/Makefile.am | 4 +- tests/resume-lifetime.c | 282 +++++++++++++++++++++++++++++++++++++++++++++ tests/virt-time.h | 56 +++++++++ 5 files changed, 363 insertions(+), 8 deletions(-) create mode 100644 tests/resume-lifetime.c create mode 100644 tests/virt-time.h diff --git a/lib/constate.c b/lib/constate.c index b1086fd838..8f15990ad6 100644 --- a/lib/constate.c +++ b/lib/constate.c @@ -669,8 +669,8 @@ int _gnutls_epoch_set_keys(gnutls_session_t session, uint16_t epoch, hs_stage_t dst->pversion = src->pversion; \ memcpy( dst->session_id, src->session_id, GNUTLS_MAX_SESSION_ID_SIZE); \ dst->session_id_size = src->session_id_size; \ - dst->timestamp = src->timestamp; \ } \ + dst->timestamp = src->timestamp; \ dst->cert_type = src->cert_type; \ dst->client_auth_type = src->client_auth_type; \ dst->server_auth_type = src->server_auth_type diff --git a/lib/tls13/session_ticket.c b/lib/tls13/session_ticket.c index ca11fd78a6..77edbcda91 100644 --- a/lib/tls13/session_ticket.c +++ b/lib/tls13/session_ticket.c @@ -177,6 +177,20 @@ generate_session_ticket(gnutls_session_t session, tls13_ticket_t *ticket) int ret; gnutls_datum_t packed = { NULL, 0 }; tls13_ticket_t ticket_data; + time_t now = gnutls_time(0); + + if (session->internals.resumed != RESUME_FALSE) { + /* If we are resuming ensure that we don't extend the lifetime + * of the ticket past the original session expiration time */ + if (now >= session->security_parameters.timestamp + session->internals.expire_time) + return GNUTLS_E_INT_RET_0; /* don't send ticket */ + else + ticket->lifetime = session->security_parameters.timestamp + + session->internals.expire_time - now; + } else { + /* Set ticket lifetime to the default expiration time */ + ticket->lifetime = session->internals.expire_time; + } /* Generate a random 32-bit ticket nonce */ ticket->nonce_size = 4; @@ -188,8 +202,6 @@ generate_session_ticket(gnutls_session_t session, tls13_ticket_t *ticket) if ((ret = gnutls_rnd(GNUTLS_RND_NONCE, &ticket->age_add, sizeof(uint32_t))) < 0) return gnutls_assert_val(ret); - /* Set ticket lifetime to the default expiration time */ - ticket->lifetime = session->internals.expire_time; ticket->prf = session->security_parameters.prf; @@ -239,11 +251,16 @@ int _gnutls13_send_session_ticket(gnutls_session_t session, unsigned again) if (again == 0) { memset(&ticket, 0, sizeof(tls13_ticket_t)); - ret = _gnutls_buffer_init_handshake_mbuffer(&buf); - if (ret < 0) + ret = generate_session_ticket(session, &ticket); + if (ret < 0) { + if (ret == GNUTLS_E_INT_RET_0) { + return gnutls_assert_val(0); + } + return gnutls_assert_val(ret); + } - ret = generate_session_ticket(session, &ticket); + ret = _gnutls_buffer_init_handshake_mbuffer(&buf); if (ret < 0) { gnutls_assert(); goto cleanup; diff --git a/tests/Makefile.am b/tests/Makefile.am index c12887d4eb..5d6a306445 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -32,7 +32,7 @@ SUBDIRS += suite endif EXTRA_DIST = suppressions.valgrind eagain-common.h cert-common.h test-chains.h \ - ocsp-common.h cmocka-common.h \ + ocsp-common.h cmocka-common.h virt-time.h \ certs/ca-cert-ecc.pem certs/cert-ecc256.pem certs/cert-ecc521.pem \ certs/cert-rsa-2432.pem certs/ecc384.pem certs/ecc.pem hex.h \ certs/ca-ecc.pem certs/cert-ecc384.pem certs/cert-ecc.pem certs/ecc256.pem \ @@ -143,7 +143,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei mini-emsgsize-dtls chainverify-unsorted mini-overhead tls12-ffdhe \ mini-dtls-heartbeat mini-x509-callbacks key-openssl priorities priorities-groups \ gnutls_x509_privkey_import gnutls_x509_crt_list_import \ - sign-verify-ext4 tls-neg-ext4-key \ + sign-verify-ext4 tls-neg-ext4-key resume-lifetime \ mini-dtls-srtp rsa-encrypt-decrypt mini-loss-time gnutls-strcodes \ mini-record mini-dtls-record mini-handshake-timeout mini-record-range \ mini-cert-status fips-mode-pthread rsa-psk global-init sec-params sign-verify-data \ diff --git a/tests/resume-lifetime.c b/tests/resume-lifetime.c new file mode 100644 index 0000000000..99c9c5b346 --- /dev/null +++ b/tests/resume-lifetime.c @@ -0,0 +1,282 @@ +/* + * Copyright (C) 2016-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 + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include "utils.h" +#include "virt-time.h" +#include "eagain-common.h" +#include "cert-common.h" + +/* This test checks whether the lifetime of a resumed session can + * be extended past the designated one */ + +const char *side; + +static void tls_log_func(int level, const char *str) +{ + fprintf(stderr, "%s|<%d>| %s", side, level, str); +} + +struct hsk_st { + unsigned sent_nst; /* whether the new session ticket was sent */ + unsigned sent_psk; /* whether the PSK extension was sent */ + unsigned sleep_at_finished; /* how long to wait at finished message reception */ + +}; + +static int ext_hook_func(void *ctx, unsigned tls_id, + const unsigned char *data, unsigned size) +{ + if (tls_id == 41) { + struct hsk_st *h = ctx; + h->sent_psk = 1; + } + return 0; +} + +static int handshake_callback(gnutls_session_t session, unsigned int htype, + unsigned post, unsigned int incoming, const gnutls_datum_t *msg) +{ + struct hsk_st *h = gnutls_session_get_ptr(session); + + if (htype == GNUTLS_HANDSHAKE_FINISHED && incoming) { + if (h->sleep_at_finished) + virt_sec_sleep(h->sleep_at_finished); + return 0; + } else if (htype == GNUTLS_HANDSHAKE_CLIENT_HELLO) { + gnutls_ext_raw_parse(h, ext_hook_func, msg, + GNUTLS_EXT_RAW_FLAG_TLS_CLIENT_HELLO); + } + + if (htype != GNUTLS_HANDSHAKE_NEW_SESSION_TICKET) + return 0; + + if (h) + h->sent_nst = 1; + return 0; +} + +/* Returns true if resumed */ +static unsigned handshake(const char *prio, unsigned t, const gnutls_datum_t *sdata, + gnutls_datum_t *ndata, + gnutls_datum_t *skey, + struct hsk_st *h) +{ + 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; + char buf[128]; + + gnutls_global_set_log_function(tls_log_func); + if (debug) + gnutls_global_set_log_level(6); + + assert(gnutls_certificate_allocate_credentials(&serverx509cred)>=0); + assert(gnutls_certificate_set_x509_key_mem(serverx509cred, + &server_cert, &server_key, + GNUTLS_X509_FMT_PEM)>=0); + + assert(gnutls_init(&server, GNUTLS_SERVER)>=0); + gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE, + serverx509cred); + assert(gnutls_priority_set_direct(server, prio, NULL)>=0); + gnutls_transport_set_push_function(server, server_push); + gnutls_transport_set_pull_function(server, server_pull); + gnutls_transport_set_ptr(server, server); + gnutls_session_set_ptr(server, h); + + gnutls_db_set_cache_expiration(server, t); + assert(gnutls_session_ticket_enable_server(server, skey) >= 0); + + gnutls_handshake_set_hook_function(server, -1, + GNUTLS_HOOK_POST, + handshake_callback); + + assert(gnutls_certificate_allocate_credentials(&clientx509cred)>=0); + assert(gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca_cert, GNUTLS_X509_FMT_PEM)>=0); + assert(gnutls_init(&client, GNUTLS_CLIENT)>=0); + + assert(gnutls_credentials_set(client, GNUTLS_CRD_CERTIFICATE, + clientx509cred)>=0); + + assert(gnutls_priority_set_direct(client, prio, NULL)>=0); + gnutls_transport_set_push_function(client, client_push); + gnutls_transport_set_pull_function(client, client_pull); + gnutls_transport_set_ptr(client, client); + + if (sdata) { + assert(gnutls_session_set_data(client, sdata->data, sdata->size)>=0); + } + + memset(buf, 0, sizeof(buf)); + ret = gnutls_session_set_data(client, buf, sizeof(buf)); + if (ret != GNUTLS_E_DB_ERROR) { + fail("unexpected error: %s\n", gnutls_strerror(ret)); + } + + HANDSHAKE(client, server); + + gnutls_record_recv(client, buf, sizeof(buf)); + + if (ndata) { + ret = gnutls_session_get_data2(client, ndata); + if (ret < 0) { + fail("unexpected error: %s\n", gnutls_strerror(ret)); + } + } + + ret = gnutls_session_is_resumed(client); + + gnutls_deinit(server); + gnutls_deinit(client); + + gnutls_certificate_free_credentials(serverx509cred); + gnutls_certificate_free_credentials(clientx509cred); + + reset_buffers(); + return ret; +} + +/* @t is the lifetime of the first ticket, @s is the + * time to wait before asking for a ticket the last try */ +static void start(const char *name, const char *prio, unsigned t, unsigned s) +{ + gnutls_datum_t sdata, ndata, skey; + unsigned ret; + struct hsk_st h; + memset(&h, 0, sizeof(h)); + + success("trying %s\n", name); + + assert(gnutls_session_ticket_key_generate(&skey)>=0); + + /* step1: get a fresh ticket */ + ret = handshake(prio, t, NULL, &sdata, &skey, &h); + assert(ret == 0); + assert(h.sent_nst != 0); + memset(&h, 0, sizeof(h)); + + if (debug) + success("completed first handshake\n"); + + if (s) + virt_sec_sleep(s); + + /* step2: get a ticket from the resumed session of the first */ + ret = handshake(prio, t, &sdata, &ndata, &skey, &h); + assert(ret != 0); + assert(h.sent_nst != 0); + memset(&h, 0, sizeof(h)); + + /* wait until the ticket we got in step1 is invalid, although + * the ticket we got in step2 is valid */ + + if (debug) + success("completed second handshake\n"); + + if (s) + virt_sec_sleep(s); + + ret = handshake(prio, t, &ndata, NULL, &skey, &h); + + if (s) { + if (ret != 0) + fail("server resumed session even if ticket expired!\n"); + + /* we shouldn't have sent the PSK extension as the ticket was expired */ + assert(h.sent_psk == 0); + } + + gnutls_free(ndata.data); + gnutls_free(sdata.data); + gnutls_free(skey.data); +} + +/* @t is the lifetime of the first ticket, @s is the + * time to wait before asking for a ticket the last try + * + * This makes the ticket to expire during handshake (after resumtion), + * but before the client receives the new session ticket. In that + * case the server shouldn't send a session ticket. + */ +static void start2(const char *name, const char *prio, unsigned t, unsigned s) +{ + gnutls_datum_t sdata, ndata, skey; + unsigned ret; + struct hsk_st h; + memset(&h, 0, sizeof(h)); + + success("trying %s\n", name); + + assert(gnutls_session_ticket_key_generate(&skey)>=0); + + /* step1: get a fresh ticket */ + ret = handshake(prio, t, NULL, &sdata, &skey, &h); + assert(ret == 0); + assert(h.sent_nst != 0); + memset(&h, 0, sizeof(h)); + + /* step2: get a ticket from the resumed session of the first */ + ret = handshake(prio, t, &sdata, &ndata, &skey, &h); + assert(ret != 0); + assert(h.sent_nst != 0); + memset(&h, 0, sizeof(h)); + + /* wait until the ticket we got in step1 is invalid, although + * the ticket we got in step2 is valid */ + + if (s) + h.sleep_at_finished = s; + + ret = handshake(prio, t, &ndata, NULL, &skey, &h); + + assert(ret != 0); + if (h.sent_nst != 0) + fail("server sent session ticket even if ticket expired!\n"); + + gnutls_free(ndata.data); + gnutls_free(sdata.data); + gnutls_free(skey.data); +} + +void doit(void) +{ + virt_time_init(); + + start("TLS1.3 sanity", "NORMAL:-VERS-ALL:+VERS-TLS1.3", 64, 0); + start("TLS1.3 ticket extension", "NORMAL:-VERS-ALL:+VERS-TLS1.3", 5, 3); + start2("TLS1.3 ticket extension - expires at handshake", "NORMAL:-VERS-ALL:+VERS-TLS1.3", 2, 3); +} diff --git a/tests/virt-time.h b/tests/virt-time.h new file mode 100644 index 0000000000..02c8cdb147 --- /dev/null +++ b/tests/virt-time.h @@ -0,0 +1,56 @@ +/* + * 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 + */ + +#ifndef VIRT_TIME_H +#define VIRT_TIME_H + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +/* virtualize time in a test. This freezes the time in the test, except for + * the advances due to calls to virt_sleep_sec(). This makes the test + * independent of the test system load, and avoids any long delays. + * + * This only affects the parts of the library that utilize gnutls_time(), + * not the higher precision gettime */ +static time_t _now = 0; + +#define virt_sec_sleep(s) _now += s + +#define virt_time_init() { \ + _now = time(0); \ + gnutls_global_set_time_function(mytime); \ + } + + +static time_t mytime(time_t * t) +{ + if (t) + *t = _now; + + return _now; +} + +#endif -- cgit v1.2.1 From b9320a8b251129ee54cc943b748af3a85a53b229 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Wed, 4 Jul 2018 07:42:44 +0200 Subject: tests: handshake-timeout: use virt_sec_sleep() to avoid long delays in test Signed-off-by: Nikos Mavrogiannopoulos --- tests/Makefile.am | 2 +- tests/handshake-timeout.c | 232 +++++++++++++++++++++++++++++++++++++++++ tests/mini-handshake-timeout.c | 230 ---------------------------------------- 3 files changed, 233 insertions(+), 231 deletions(-) create mode 100644 tests/handshake-timeout.c delete mode 100644 tests/mini-handshake-timeout.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 5d6a306445..2ad3b527a4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -145,7 +145,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei gnutls_x509_privkey_import gnutls_x509_crt_list_import \ sign-verify-ext4 tls-neg-ext4-key resume-lifetime \ mini-dtls-srtp rsa-encrypt-decrypt mini-loss-time gnutls-strcodes \ - mini-record mini-dtls-record mini-handshake-timeout mini-record-range \ + mini-record mini-dtls-record handshake-timeout mini-record-range \ mini-cert-status fips-mode-pthread rsa-psk global-init sec-params sign-verify-data \ fips-test fips-override-test mini-global-load name-constraints x509-extensions \ long-session-id mini-x509-callbacks-intr mini-dtls-lowmtu set_x509_key_file-late \ diff --git a/tests/handshake-timeout.c b/tests/handshake-timeout.c new file mode 100644 index 0000000000..c7ff20ca9e --- /dev/null +++ b/tests/handshake-timeout.c @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2012 Free Software Foundation, 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 + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#if defined(_WIN32) + +int main() +{ + exit(77); +} + +#else + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils.h" + +/* This program tests whether the handshake timeout value is enforced. + */ + +static void server_log_func(int level, const char *str) +{ + fprintf(stderr, "server|<%d>| %s", level, str); +} + +static void client_log_func(int level, const char *str) +{ + fprintf(stderr, "client|<%d>| %s", level, str); +} + +/* A very basic TLS client, with anonymous authentication. + */ + +static void client(int fd, int wait) +{ + int ret; + gnutls_anon_client_credentials_t anoncred; + gnutls_session_t session; + /* Need to enable anonymous KX specifically. */ + + global_init(); + + if (debug) { + gnutls_global_set_log_function(client_log_func); + gnutls_global_set_log_level(4711); + } + + gnutls_anon_allocate_client_credentials(&anoncred); + + /* Initialize TLS session + */ + gnutls_init(&session, GNUTLS_CLIENT); + gnutls_handshake_set_timeout(session, 20 * 1000); + + /* Use default priorities */ + gnutls_priority_set_direct(session, "NORMAL:+ANON-ECDH:-VERS-ALL:+VERS-TLS1.2", NULL); + + /* put the anonymous credentials to the current session + */ + gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred); + + gnutls_transport_set_int(session, fd); + + /* Perform the TLS handshake + */ + do { + ret = gnutls_handshake(session); + } + while (ret < 0 && gnutls_error_is_fatal(ret) == 0); + + gnutls_deinit(session); + gnutls_anon_free_client_credentials(anoncred); + gnutls_global_deinit(); + + if (ret < 0) { + if (ret != GNUTLS_E_TIMEDOUT || wait == 0) { + if (debug) + fail("client: unexpected error: %s\n", + gnutls_strerror(ret)); + exit(1); + } + if (debug) + success("client: expected timeout occurred\n"); + return; + } else { + if (wait != 0) { + fail("client: handshake was completed unexpectedly\n"); + gnutls_perror(ret); + exit(1); + } + } + + return; +} + +static void server(int fd, int wait) +{ + int ret; + gnutls_session_t session; + gnutls_anon_server_credentials_t anoncred; + + /* this must be called once in the program + */ + global_init(); + + if (debug) { + gnutls_global_set_log_function(server_log_func); + gnutls_global_set_log_level(4711); + } + + gnutls_anon_allocate_server_credentials(&anoncred); + + gnutls_init(&session, GNUTLS_SERVER); + + /* avoid calling all the priority functions, since the defaults + * are adequate. + */ + gnutls_priority_set_direct(session, "NORMAL:+ANON-ECDH:-VERS-ALL:+VERS-TLS1.2", NULL); + + gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred); + + gnutls_transport_set_int(session, fd); + + if (wait) { + virt_sec_sleep(25); + } else { + do { + ret = gnutls_handshake(session); + } + while (ret < 0 && gnutls_error_is_fatal(ret) == 0); + + if (ret == 0) + gnutls_bye(session, GNUTLS_SHUT_RDWR); + } + + gnutls_deinit(session); + gnutls_anon_free_server_credentials(anoncred); + gnutls_global_deinit(); +} + +static void start(int wait) +{ + int fd[2]; + int ret; + pid_t child; + + if (debug && wait) + fprintf(stderr, "\nWill test timeout\n"); + + ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); + if (ret < 0) { + perror("socketpair"); + exit(1); + } + + child = fork(); + if (child < 0) { + perror("fork"); + fail("fork"); + exit(1); + } + + if (child) { + /* parent */ + close(fd[1]); + server(fd[0], wait); + close(fd[0]); + } else { + close(fd[0]); + client(fd[1], wait); + close(fd[1]); + exit(0); + } +} + +static void ch_handler(int sig) +{ + int status = 0; + wait(&status); + check_wait_status(status); + return; +} + +void doit(void) +{ + virt_time_init(); + + signal(SIGCHLD, ch_handler); + signal(SIGPIPE, SIG_IGN); + + /* make sure that normal handshake occurs */ + start(0); + + /* check the handshake with an expected timeout */ + start(1); +} + +#endif /* _WIN32 */ diff --git a/tests/mini-handshake-timeout.c b/tests/mini-handshake-timeout.c deleted file mode 100644 index b4d1c2c962..0000000000 --- a/tests/mini-handshake-timeout.c +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Copyright (C) 2012 Free Software Foundation, 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 -#endif - -#include -#include -#include - -#if defined(_WIN32) - -int main() -{ - exit(77); -} - -#else - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "utils.h" - -/* This program tests whether the handshake timeout value is enforced. - */ - -static void server_log_func(int level, const char *str) -{ - fprintf(stderr, "server|<%d>| %s", level, str); -} - -static void client_log_func(int level, const char *str) -{ - fprintf(stderr, "client|<%d>| %s", level, str); -} - -/* A very basic TLS client, with anonymous authentication. - */ - -static void client(int fd, int wait) -{ - int ret; - gnutls_anon_client_credentials_t anoncred; - gnutls_session_t session; - /* Need to enable anonymous KX specifically. */ - - global_init(); - - if (debug) { - gnutls_global_set_log_function(client_log_func); - gnutls_global_set_log_level(4711); - } - - gnutls_anon_allocate_client_credentials(&anoncred); - - /* Initialize TLS session - */ - gnutls_init(&session, GNUTLS_CLIENT); - gnutls_handshake_set_timeout(session, 20 * 1000); - - /* Use default priorities */ - gnutls_priority_set_direct(session, "NORMAL:+ANON-ECDH:-VERS-ALL:+VERS-TLS1.2", NULL); - - /* put the anonymous credentials to the current session - */ - gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred); - - gnutls_transport_set_int(session, fd); - - /* Perform the TLS handshake - */ - do { - ret = gnutls_handshake(session); - } - while (ret < 0 && gnutls_error_is_fatal(ret) == 0); - - gnutls_deinit(session); - gnutls_anon_free_client_credentials(anoncred); - gnutls_global_deinit(); - - if (ret < 0) { - if (ret != GNUTLS_E_TIMEDOUT || wait == 0) { - if (debug) - fail("client: unexpected error: %s\n", - gnutls_strerror(ret)); - exit(1); - } - if (debug) - success("client: expected timeout occurred\n"); - return; - } else { - if (wait != 0) { - fail("client: handshake was completed unexpectedly\n"); - gnutls_perror(ret); - exit(1); - } - } - - return; -} - -static void server(int fd, int wait) -{ - int ret; - gnutls_session_t session; - gnutls_anon_server_credentials_t anoncred; - - /* this must be called once in the program - */ - global_init(); - - if (debug) { - gnutls_global_set_log_function(server_log_func); - gnutls_global_set_log_level(4711); - } - - gnutls_anon_allocate_server_credentials(&anoncred); - - gnutls_init(&session, GNUTLS_SERVER); - - /* avoid calling all the priority functions, since the defaults - * are adequate. - */ - gnutls_priority_set_direct(session, "NORMAL:+ANON-ECDH:-VERS-ALL:+VERS-TLS1.2", NULL); - - gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred); - - gnutls_transport_set_int(session, fd); - - if (wait) { - sec_sleep(25); - } else { - do { - ret = gnutls_handshake(session); - } - while (ret < 0 && gnutls_error_is_fatal(ret) == 0); - - if (ret == 0) - gnutls_bye(session, GNUTLS_SHUT_RDWR); - } - - gnutls_deinit(session); - gnutls_anon_free_server_credentials(anoncred); - gnutls_global_deinit(); -} - -static void start(int wait) -{ - int fd[2]; - int ret; - pid_t child; - - if (debug && wait) - fprintf(stderr, "\nWill test timeout\n"); - - ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); - if (ret < 0) { - perror("socketpair"); - exit(1); - } - - child = fork(); - if (child < 0) { - perror("fork"); - fail("fork"); - exit(1); - } - - if (child) { - /* parent */ - close(fd[1]); - server(fd[0], wait); - close(fd[0]); - } else { - close(fd[0]); - client(fd[1], wait); - close(fd[1]); - exit(0); - } -} - -static void ch_handler(int sig) -{ - int status = 0; - wait(&status); - check_wait_status(status); - return; -} - -void doit(void) -{ - signal(SIGCHLD, ch_handler); - signal(SIGPIPE, SIG_IGN); - - /* make sure that normal handshake occurs */ - start(0); - - /* check the handshake with an expected timeout */ - start(1); -} - -#endif /* _WIN32 */ -- cgit v1.2.1