summaryrefslogtreecommitdiff
path: root/regress/lib/libssl/handshake/handshake_table.c
blob: 494f72fd27c765c505a93a8d219515a94ee17c92 (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
/*	$OpenBSD: handshake_table.c,v 1.11 2019/04/05 20:25:25 tb Exp $	*/
/*
 * Copyright (c) 2019 Theo Buehler <tb@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "tls13_handshake.h"

/*
 * From RFC 8446:
 *
 * Appendix A.  State Machine
 *
 *    This appendix provides a summary of the legal state transitions for
 *    the client and server handshakes.  State names (in all capitals,
 *    e.g., START) have no formal meaning but are provided for ease of
 *    comprehension.  Actions which are taken only in certain circumstances
 *    are indicated in [].  The notation "K_{send,recv} = foo" means "set
 *    the send/recv key to the given key".
 *
 * A.1.  Client
 *
 *                               START <----+
 *                Send ClientHello |        | Recv HelloRetryRequest
 *           [K_send = early data] |        |
 *                                 v        |
 *            /                 WAIT_SH ----+
 *            |                    | Recv ServerHello
 *            |                    | K_recv = handshake
 *        Can |                    V
 *       send |                 WAIT_EE
 *      early |                    | Recv EncryptedExtensions
 *       data |           +--------+--------+
 *            |     Using |                 | Using certificate
 *            |       PSK |                 v
 *            |           |            WAIT_CERT_CR
 *            |           |        Recv |       | Recv CertificateRequest
 *            |           | Certificate |       v
 *            |           |             |    WAIT_CERT
 *            |           |             |       | Recv Certificate
 *            |           |             v       v
 *            |           |              WAIT_CV
 *            |           |                 | Recv CertificateVerify
 *            |           +> WAIT_FINISHED <+
 *            |                  | Recv Finished
 *            \                  | [Send EndOfEarlyData]
 *                               | K_send = handshake
 *                               | [Send Certificate [+ CertificateVerify]]
 *     Can send                  | Send Finished
 *     app data   -->            | K_send = K_recv = application
 *     after here                v
 *                           CONNECTED
 *
 *    Note that with the transitions as shown above, clients may send
 *    alerts that derive from post-ServerHello messages in the clear or
 *    with the early data keys.  If clients need to send such alerts, they
 *    SHOULD first rekey to the handshake keys if possible.
 *
 */

struct child {
	enum tls13_message_type	mt;
	uint8_t			flag;
	uint8_t			forced;
	uint8_t			illegal;
};

#define DEFAULT			0x00

static struct child stateinfo[][TLS13_NUM_MESSAGE_TYPES] = {
	[CLIENT_HELLO] = {
		{SERVER_HELLO, DEFAULT, 0, 0},
	},
	[SERVER_HELLO] = {
		{SERVER_ENCRYPTED_EXTENSIONS, DEFAULT, 0, 0},
		{CLIENT_HELLO_RETRY, WITH_HRR, 0, 0},
	},
	[CLIENT_HELLO_RETRY] = {
		{SERVER_HELLO_RETRY, DEFAULT, 0, 0},
	},
	[SERVER_HELLO_RETRY] = {
		{SERVER_ENCRYPTED_EXTENSIONS, DEFAULT, 0, 0},
	},
	[SERVER_ENCRYPTED_EXTENSIONS] = {
		{SERVER_CERTIFICATE_REQUEST, DEFAULT, 0, 0},
		{SERVER_CERTIFICATE, WITHOUT_CR, 0, 0},
		{SERVER_FINISHED, WITH_PSK, 0, 0},
	},
	[SERVER_CERTIFICATE_REQUEST] = {
		{SERVER_CERTIFICATE, DEFAULT, 0, 0},
	},
	[SERVER_CERTIFICATE] = {
		{SERVER_CERTIFICATE_VERIFY, DEFAULT, 0, 0},
	},
	[SERVER_CERTIFICATE_VERIFY] = {
		{SERVER_FINISHED, DEFAULT, 0, 0},
	},
	[SERVER_FINISHED] = {
		{CLIENT_FINISHED, DEFAULT, WITHOUT_CR | WITH_PSK, 0},
		{CLIENT_CERTIFICATE, DEFAULT, 0, WITHOUT_CR | WITH_PSK},
	},
	[CLIENT_CERTIFICATE] = {
		{CLIENT_FINISHED, DEFAULT, 0, 0},
		{CLIENT_CERTIFICATE_VERIFY, WITH_CCV, 0, 0},
	},
	[CLIENT_CERTIFICATE_VERIFY] = {
		{CLIENT_FINISHED, DEFAULT, 0, 0},
	},
	[CLIENT_FINISHED] = {
		{APPLICATION_DATA, DEFAULT, 0, 0},
	},
	[APPLICATION_DATA] = {
		{0, DEFAULT, 0, 0},
	},
};

const size_t	 stateinfo_count = sizeof(stateinfo) / sizeof(stateinfo[0]);

void		 build_table(enum tls13_message_type
		     table[UINT8_MAX][TLS13_NUM_MESSAGE_TYPES],
		     struct child current, struct child end,
		     struct child path[], uint8_t flags, unsigned int depth);
size_t		 count_handshakes(void);
void		 edge(enum tls13_message_type start,
		     enum tls13_message_type end, uint8_t flag);
const char	*flag2str(uint8_t flag);
void		 flag_label(uint8_t flag);
void		 forced_edges(enum tls13_message_type start,
		     enum tls13_message_type end, uint8_t forced);
int		 generate_graphics(void);
void		 fprint_entry(FILE *stream,
		     enum tls13_message_type path[TLS13_NUM_MESSAGE_TYPES],
		     uint8_t flags);
void		 fprint_flags(FILE *stream, uint8_t flags);
const char	*mt2str(enum tls13_message_type mt);
__dead void	 usage(void);
int		 verify_table(enum tls13_message_type
		     table[UINT8_MAX][TLS13_NUM_MESSAGE_TYPES], int print);

const char *
flag2str(uint8_t flag)
{
	const char *ret;

	if (flag & (flag - 1))
		errx(1, "more than one bit is set");

	switch (flag) {
	case INITIAL:
		ret = "INITIAL";
		break;
	case NEGOTIATED:
		ret = "NEGOTIATED";
		break;
	case WITHOUT_CR:
		ret = "WITHOUT_CR";
		break;
	case WITH_HRR:
		ret = "WITH_HRR";
		break;
	case WITH_PSK:
		ret = "WITH_PSK";
		break;
	case WITH_CCV:
		ret = "WITH_CCV";
		break;
	case WITH_0RTT:
		ret = "WITH_0RTT";
		break;
	default:
		ret = "UNKNOWN";
	}

	return ret;
}

const char *
mt2str(enum tls13_message_type mt)
{
	const char *ret;

	switch (mt) {
	case INVALID:
		ret = "INVALID";
		break;
	case CLIENT_HELLO:
		ret = "CLIENT_HELLO";
		break;
	case CLIENT_HELLO_RETRY:
		ret = "CLIENT_HELLO_RETRY";
		break;
	case CLIENT_END_OF_EARLY_DATA:
		ret = "CLIENT_END_OF_EARLY_DATA";
		break;
	case CLIENT_CERTIFICATE:
		ret = "CLIENT_CERTIFICATE";
		break;
	case CLIENT_CERTIFICATE_VERIFY:
		ret = "CLIENT_CERTIFICATE_VERIFY";
		break;
	case CLIENT_FINISHED:
		ret = "CLIENT_FINISHED";
		break;
	case CLIENT_KEY_UPDATE:
		ret = "CLIENT_KEY_UPDATE";
		break;
	case SERVER_HELLO:
		ret = "SERVER_HELLO";
		break;
	case SERVER_HELLO_RETRY:
		ret = "SERVER_HELLO_RETRY";
		break;
	case SERVER_NEW_SESSION_TICKET:
		ret = "SERVER_NEW_SESSION_TICKET";
		break;
	case SERVER_ENCRYPTED_EXTENSIONS:
		ret = "SERVER_ENCRYPTED_EXTENSIONS";
		break;
	case SERVER_CERTIFICATE:
		ret = "SERVER_CERTIFICATE";
		break;
	case SERVER_CERTIFICATE_VERIFY:
		ret = "SERVER_CERTIFICATE_VERIFY";
		break;
	case SERVER_CERTIFICATE_REQUEST:
		ret = "SERVER_CERTIFICATE_REQUEST";
		break;
	case SERVER_FINISHED:
		ret = "SERVER_FINISHED";
		break;
	case APPLICATION_DATA:
		ret = "APPLICATION_DATA";
		break;
	case TLS13_NUM_MESSAGE_TYPES:
		ret = "TLS13_NUM_MESSAGE_TYPES";
		break;
	default:
		ret = "UNKNOWN";
		break;
	}

	return ret;
}

void
fprint_flags(FILE *stream, uint8_t flags)
{
	int first = 1, i;

	if (flags == 0) {
		fprintf(stream, "%s", flag2str(flags));
		return;
	}

	for (i = 0; i < 8; i++) {
		uint8_t set = flags & (1U << i);

		if (set) {
			fprintf(stream, "%s%s", first ? "" : " | ",
			    flag2str(set));
			first = 0;
		}
	}
}

void
fprint_entry(FILE *stream,
    enum tls13_message_type path[TLS13_NUM_MESSAGE_TYPES], uint8_t flags)
{
	int i;

	fprintf(stream, "\t[");
	fprint_flags(stream, flags);
	fprintf(stream, "] = {\n");

	for (i = 0; i < TLS13_NUM_MESSAGE_TYPES; i++) {
		if (path[i] == 0)
			break;
		fprintf(stream, "\t\t%s,\n", mt2str(path[i]));
	}
	fprintf(stream, "\t},\n");
}

void
edge(enum tls13_message_type start, enum tls13_message_type end,
    uint8_t flag)
{
	printf("\t%s -> %s", mt2str(start), mt2str(end));
	flag_label(flag);
	printf(";\n");
}

void
flag_label(uint8_t flag)
{
	if (flag)
		printf(" [label=\"%s\"]", flag2str(flag));
}

void
forced_edges(enum tls13_message_type start, enum tls13_message_type end,
    uint8_t forced)
{
	uint8_t	forced_flag, i;

	if (forced == 0)
		return;

	for (i = 0; i < 8; i++) {
		forced_flag = forced & (1U << i);
		if (forced_flag)
			edge(start, end, forced_flag);
	}
}

int
generate_graphics(void)
{
	enum tls13_message_type	start, end;
	unsigned int		child;
	uint8_t			flag;
	uint8_t			forced;

	printf("digraph G {\n");
	printf("\t%s [shape=box];\n", mt2str(CLIENT_HELLO));
	printf("\t%s [shape=box];\n", mt2str(APPLICATION_DATA));

	for (start = CLIENT_HELLO; start < APPLICATION_DATA; start++) {
		for (child = 0; stateinfo[start][child].mt != 0; child++) {
			end = stateinfo[start][child].mt;
			flag = stateinfo[start][child].flag;
			forced = stateinfo[start][child].forced;

			if (forced == 0)
				edge(start, end, flag);
			else
				forced_edges(start, end, forced);
		}
	}

	printf("}\n");
	return 0;
}

extern enum tls13_message_type	handshakes[][TLS13_NUM_MESSAGE_TYPES];
extern size_t			handshake_count;

size_t
count_handshakes(void)
{
	size_t	ret = 0, i;

	for (i = 0; i < handshake_count; i++) {
		if (handshakes[i][0] != INVALID)
			ret++;
	}

	return ret;
}

void
build_table(enum tls13_message_type table[UINT8_MAX][TLS13_NUM_MESSAGE_TYPES],
    struct child current, struct child end, struct child path[], uint8_t flags,
    unsigned int depth)
{
	unsigned int i;

	if (depth >= TLS13_NUM_MESSAGE_TYPES - 1)
		errx(1, "recursed too deeply");

	/* Record current node. */
	path[depth++] = current;
	flags |= current.flag;

	/* If we haven't reached the end, recurse over the children. */
	if (current.mt != end.mt) {
		for (i = 0; stateinfo[current.mt][i].mt != 0; i++) {
			struct child child = stateinfo[current.mt][i];
			int forced = stateinfo[current.mt][i].forced;
			int illegal = stateinfo[current.mt][i].illegal;

			if ((forced == 0 || (forced & flags)) &&
			    (illegal == 0 || !(illegal & flags)))
				build_table(table, child, end, path, flags,
				    depth);
		}
		return;
	}

	if (flags == 0)
		errx(1, "path does not set flags");

	if (table[flags][0] != 0)
		errx(1, "path traversed twice");

	for (i = 0; i < depth; i++)
		table[flags][i] = path[i].mt;
}

int
verify_table(enum tls13_message_type table[UINT8_MAX][TLS13_NUM_MESSAGE_TYPES],
    int print)
{
	int	success = 1, i;
	size_t	num_valid, num_found = 0;
	uint8_t	flags = 0;

	do {
		if (table[flags][0] == 0)
			continue;

		num_found++;

		for (i = 0; i < TLS13_NUM_MESSAGE_TYPES; i++) {
			if (table[flags][i] != handshakes[flags][i]) {
				fprintf(stderr,
				    "incorrect entry %d of handshake ", i);
				fprint_flags(stderr, flags);
				fprintf(stderr, "\n");
				success = 0;
			}
		}

		if (print)
			fprint_entry(stdout, table[flags], flags);
	} while(++flags != 0);

	num_valid = count_handshakes();
	if (num_valid != num_found) {
		fprintf(stderr,
		    "incorrect number of handshakes: want %zu, got %zu.\n",
		    num_valid, num_found);
		success = 0;
	}

	return success;
}

__dead void
usage(void)
{
	fprintf(stderr, "usage: handshake_table [-C | -g]\n");
	exit(1);
}

int
main(int argc, char *argv[])
{
	static enum tls13_message_type
	    hs_table[UINT8_MAX][TLS13_NUM_MESSAGE_TYPES] = {
		[INITIAL] = {
			CLIENT_HELLO,
			SERVER_HELLO,
		},
	};
	struct child	start = {
		CLIENT_HELLO, DEFAULT, 0, 0,
	};
	struct child	end = {
		APPLICATION_DATA, DEFAULT, 0, 0,
	};
	struct child	path[TLS13_NUM_MESSAGE_TYPES] = {{0}};
	uint8_t		flags = NEGOTIATED;
	unsigned int	depth = 0;
	int		ch, graphviz = 0, print = 0;

	while ((ch = getopt(argc, argv, "Cg")) != -1) {
		switch (ch) {
		case 'C':
			print = 1;
			break;
		case 'g':
			graphviz = 1;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 0)
		usage();

	if (graphviz && print)
		usage();

	if (graphviz)
		return generate_graphics();

	build_table(hs_table, start, end, path, flags, depth);
	if (!verify_table(hs_table, print))
		return 1;

	if (!print)
		printf("SUCCESS\n");

	return 0;
}