summaryrefslogtreecommitdiffstats
path: root/tests/tls-record-size-limit.c
blob: 8346ee56d9f787b6ff57966e0691a0043e4f51dc (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * Copyright (C) 2015 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 <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <gnutls/gnutls.h>
#include "utils.h"

#define SKIP16(pos, total) { \
	uint16_t _s; \
	if (pos+2 > total) fail("error\n"); \
	_s = (msg->data[pos] << 8) | msg->data[pos+1]; \
	if ((size_t)(pos+2+_s) > total) fail("error\n"); \
	pos += 2+_s; \
	}

#define SKIP8(pos, total) { \
	uint8_t _s; \
	if (pos+1 > total) fail("error\n"); \
	_s = msg->data[pos]; \
	if ((size_t)(pos+1+_s) > total) fail("error\n"); \
	pos += 1+_s; \
	}

#define HANDSHAKE_SESSION_ID_POS 34

static size_t server_max_send_size;
static size_t client_max_send_size;

#define SERVER_PUSH_ADD if (len > server_max_send_size + 5+32) fail("max record set to %d, len: %d\n", (int)server_max_send_size, (int)len);
#include "eagain-common.h"

#include "cert-common.h"

/* This tests whether the max-record extension is respected on TLS.
 */

const char *side;

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "%s|<%d>| %s", side, level, str);
}

struct handshake_cb_data_st {
	gnutls_session_t session;
	bool found_max_record_size;
	bool found_record_size_limit;
};

static struct handshake_cb_data_st server_handshake_cb_data;
static struct handshake_cb_data_st client_handshake_cb_data;

static int ext_callback(void *ctx, unsigned tls_id, const unsigned char *data, unsigned size)
{
	struct handshake_cb_data_st *cb_data = ctx;
	if (tls_id == 1) {	/* max record size */
		cb_data->found_max_record_size = 1;
	} else if (tls_id == 28) { /* record size limit */
		cb_data->found_record_size_limit = 1;
	}
	return 0;
}

static int handshake_callback(gnutls_session_t session, unsigned int htype,
	unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
{
	int ret;
	unsigned pos;
	gnutls_datum_t mmsg;

	if (!post)
		return 0;

	switch (htype) {
	case GNUTLS_HANDSHAKE_CLIENT_HELLO:
		assert(msg->size >= HANDSHAKE_SESSION_ID_POS);
		pos = HANDSHAKE_SESSION_ID_POS;
		SKIP8(pos, msg->size);
		SKIP16(pos, msg->size);
		SKIP8(pos, msg->size);

		mmsg.data = &msg->data[pos];
		mmsg.size = msg->size - pos;
		ret = gnutls_ext_raw_parse(&server_handshake_cb_data, ext_callback, &mmsg, 0);
		assert(ret >= 0);
		break;
	case GNUTLS_HANDSHAKE_ENCRYPTED_EXTENSIONS:
		ret = gnutls_ext_raw_parse(&client_handshake_cb_data, ext_callback, msg, 0);
		assert(ret >= 0);
		break;
	case GNUTLS_HANDSHAKE_SERVER_HELLO:
		assert(msg->size >= HANDSHAKE_SESSION_ID_POS);
		pos = HANDSHAKE_SESSION_ID_POS;
		SKIP8(pos, msg->size);
		pos += 3;

		mmsg.data = &msg->data[pos];
		mmsg.size = msg->size - pos;
		ret = gnutls_ext_raw_parse(&client_handshake_cb_data, ext_callback, &mmsg, 0);
		assert(ret >= 0);
		break;
	default:
		break;
	}
	return 0;
}

#define MAX_BUF 16384
static char buffer[MAX_BUF];

struct test_exp_st {
	int error;
	size_t size;
	bool max_record_size;
	bool record_size_limit;
};

struct test_st {
	const char *prio;
	size_t server_max_size;
	size_t client_max_size;

	struct test_exp_st server_exp;
	struct test_exp_st client_exp;
};

static void check_exts(const struct test_exp_st *exp,
		       struct handshake_cb_data_st *data)
{
	if (exp->max_record_size && !data->found_max_record_size)
		fail("%s: didn't see max_record_size\n", side);
	if (!exp->max_record_size && data->found_max_record_size)
		fail("%s: did see max_record_size\n", side);

	if (exp->record_size_limit && !data->found_record_size_limit)
		fail("%s: didn't see record_size_limit\n", side);
	if (!exp->record_size_limit && data->found_record_size_limit)
		fail("%s: did see record_size_limit\n", side);
}

static void start(const struct test_st *test)
{
	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;

	memset(&server_handshake_cb_data, 0, sizeof(server_handshake_cb_data));
	memset(&client_handshake_cb_data, 0, sizeof(client_handshake_cb_data));

	global_init();

	/* General init. */
	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(6);

	/* Init server */
	gnutls_certificate_allocate_credentials(&serverx509cred);
	gnutls_certificate_set_x509_key_mem(serverx509cred,
					    &server2_cert, &server2_key,
					    GNUTLS_X509_FMT_PEM);

	gnutls_init(&server, GNUTLS_SERVER);
	gnutls_credentials_set(server, GNUTLS_CRD_CERTIFICATE,
				serverx509cred);

	gnutls_priority_set_direct(server, test->prio, NULL);

	ret = gnutls_record_set_max_size(server, test->server_max_size);
	if (ret != test->server_exp.error)
		fail("server: unexpected error from gnutls_record_set_max_size()");
	if (ret == 0)
		server_max_send_size = test->server_max_size;
	else
		server_max_send_size = MAX_BUF;

	gnutls_transport_set_push_function(server, server_push);
	gnutls_transport_set_pull_function(server, server_pull);
	gnutls_transport_set_pull_timeout_function(server,
						   server_pull_timeout_func);
	gnutls_transport_set_ptr(server, server);

	server_handshake_cb_data.session = server;
	gnutls_handshake_set_hook_function(server,
					   GNUTLS_HANDSHAKE_CLIENT_HELLO,
					   GNUTLS_HOOK_POST,
					   handshake_callback);


	/* Init client */
	ret = gnutls_certificate_allocate_credentials(&clientx509cred);
	if (ret < 0)
		exit(1);

	ret = gnutls_certificate_set_x509_trust_mem(clientx509cred, &ca2_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);

	ret = gnutls_priority_set_direct(client, test->prio, NULL);
	if (ret < 0)
		exit(1);

	ret = gnutls_record_set_max_size(client, test->client_max_size);
	if (ret != test->client_exp.error)
		fail("client: unexpected error from gnutls_record_set_max_size()");
	if (ret == 0)
		client_max_send_size = test->client_max_size;
	else
		client_max_send_size = MAX_BUF;

	gnutls_transport_set_push_function(client, client_push);
	gnutls_transport_set_pull_function(client, client_pull);
	gnutls_transport_set_pull_timeout_function(client,
						   client_pull_timeout_func);
	gnutls_transport_set_ptr(client, client);

	client_handshake_cb_data.session = client;
	gnutls_handshake_set_hook_function(client,
					   GNUTLS_HANDSHAKE_ANY,
					   GNUTLS_HOOK_POST,
					   handshake_callback);

	HANDSHAKE(client, server);

	memset(buffer, 1, sizeof(buffer));
	ret = gnutls_record_send(server, buffer, server_max_send_size + 1);
	if (ret < 0) {
		gnutls_perror(ret);
		exit(1);
	}
	if (ret != (int)test->server_exp.size)
		fail("server: unexpected record size sent: %d (%d)\n",
		     ret, (int)test->server_exp.size);
	success("server: did not send a %d-byte packet\n", (int)server_max_send_size + 1);

	ret = gnutls_record_send(server, buffer, server_max_send_size);
	if (ret < 0) {
		gnutls_perror(ret);
		exit(1);
	}
	success("server: did send a %d-byte packet\n", (int)server_max_send_size);

	ret = gnutls_record_send(client, buffer, client_max_send_size + 1);
	if (ret < 0) {
		gnutls_perror(ret);
		exit(1);
	}
	if (ret != (int)test->client_exp.size)
		fail("client: unexpected record size sent: %d (%d)\n",
		     ret, (int)test->client_exp.size);
	success("client: did not send a %d-byte packet\n", (int)client_max_send_size + 1);

	ret = gnutls_record_send(client, buffer, client_max_send_size);
	if (ret < 0) {
		gnutls_perror(ret);
		exit(1);
	}
	success("client: did send a %d-byte packet\n", (int)client_max_send_size);

	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();

	reset_buffers();

	check_exts(&test->server_exp,
		   &server_handshake_cb_data);
	check_exts(&test->client_exp,
		   &client_handshake_cb_data);
}

static const struct test_st tests[] = {
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.2",
		.server_max_size = 511,
		.client_max_size = 511,
		.server_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.2",
		.server_max_size = 512,
		.client_max_size = 512,
		.server_exp = {
			.error = 0,
			.size = 512,
			.max_record_size = 1,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 512,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.2",
		.server_max_size = 8192,
		.client_max_size = 8192,
		.server_exp = {
			.error = 0,
			.size = 8192,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 8192,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.2",
		.server_max_size = 16384,
		.client_max_size = 16384,
		.server_exp = {
			.error = 0,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.2",
		.server_max_size = 16385,
		.client_max_size = 16385,
		.server_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},

	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 511,
		.client_max_size = 511,
		.server_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 512,
		.client_max_size = 512,
		.server_exp = {
			.error = 0,
			.size = 512,
			.max_record_size = 1,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 512,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 8192,
		.client_max_size = 8192,
		.server_exp = {
			.error = 0,
			.size = 8192,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 8192,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 16384,
		.client_max_size = 16384,
		.server_exp = {
			.error = 0,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 16383,
		.client_max_size = 16384,
		.server_exp = {
			.error = 0,
			.size = 16383,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = 0,
			.size = 16383,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	},
	{
		.prio = "NORMAL:-VERS-ALL:+VERS-TLS1.3",
		.server_max_size = 16385,
		.client_max_size = 16385,
		.server_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		},
		.client_exp = {
			.error = GNUTLS_E_INVALID_REQUEST,
			.size = 16384,
			.max_record_size = 0,
			.record_size_limit = 1
		}
	}
};

void doit(void)
{
	size_t i;
	for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++)
		start(&tests[i]);
}