summaryrefslogtreecommitdiffstats
path: root/tests/resume-lifetime.c
blob: 3e720393274ee0e5bab173528386bb17fadde911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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 <https://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);
}