summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2018-07-11 17:55:28 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2018-07-11 17:55:28 +0000
commit40b5e30494230f1e87d1622f14cf65cce5ba3bc9 (patch)
tree8224be45162d19619ffa38bced2c344b28df5545
parent4cca24907374505de223f36b363cd903d502381b (diff)
parentb9320a8b251129ee54cc943b748af3a85a53b229 (diff)
downloadgnutls-40b5e30494230f1e87d1622f14cf65cce5ba3bc9.tar.gz
Merge branch 'tmp-limit-ticket-age' into 'master'
limit the age of session tickets Closes #476 See merge request gnutls/gnutls!697
-rw-r--r--doc/cha-intro-tls.texi3
-rw-r--r--lib/constate.c2
-rw-r--r--lib/ext/pre_shared_key.c12
-rw-r--r--lib/tls13/session_ticket.c31
-rw-r--r--tests/Makefile.am6
-rw-r--r--tests/handshake-timeout.c (renamed from tests/mini-handshake-timeout.c)10
-rw-r--r--tests/resume-lifetime.c282
-rw-r--r--tests/session-tickets-ok.c24
-rw-r--r--tests/virt-time.h56
9 files changed, 389 insertions, 37 deletions
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
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/ext/pre_shared_key.c b/lib/ext/pre_shared_key.c
index b12d853af8..dc56d1dc49 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;
@@ -340,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;
@@ -470,7 +476,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);
diff --git a/lib/tls13/session_ticket.c b/lib/tls13/session_ticket.c
index dfe4992669..77edbcda91 100644
--- a/lib/tls13/session_ticket.c
+++ b/lib/tls13/session_ticket.c
@@ -177,9 +177,23 @@ 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 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)
@@ -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 1 day (86400 seconds) */
- 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..2ad3b527a4 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,9 +143,9 @@ 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-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/mini-handshake-timeout.c b/tests/handshake-timeout.c
index b4d1c2c962..c7ff20ca9e 100644
--- a/tests/mini-handshake-timeout.c
+++ b/tests/handshake-timeout.c
@@ -15,9 +15,8 @@
* 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
+ * 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
@@ -46,6 +45,7 @@ int main()
#include <gnutls/gnutls.h>
#include <gnutls/dtls.h>
#include <signal.h>
+#include <virt-time.h>
#include "utils.h"
@@ -156,7 +156,7 @@ static void server(int fd, int wait)
gnutls_transport_set_int(session, fd);
if (wait) {
- sec_sleep(25);
+ virt_sec_sleep(25);
} else {
do {
ret = gnutls_handshake(session);
@@ -217,6 +217,8 @@ static void ch_handler(int sig)
void doit(void)
{
+ virt_time_init();
+
signal(SIGCHLD, ch_handler);
signal(SIGPIPE, SIG_IGN);
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 <http://www.gnu.org/licenses/>
+ */
+
+#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 "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/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);
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 <http://www.gnu.org/licenses/>
+ */
+
+#ifndef VIRT_TIME_H
+#define VIRT_TIME_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <time.h>
+#include <gnutls/gnutls.h>
+
+/* 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