summaryrefslogtreecommitdiff
path: root/regress/lib/libssl/unit/tls_ext_alpn.c
blob: e4ad2c5fd4f225e6e24e4380099a0c805f53c474 (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
/*	$OpenBSD: tls_ext_alpn.c,v 1.2 2016/12/18 13:54:15 jsing Exp $	*/
/*
 * Copyright (c) 2015 Doug Hogan <doug@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.
 */

/*
 * Test TLS extension Application-Layer Protocol Negotiation (RFC 7301).
 */
#include <stdio.h>
#include <openssl/ssl.h>

#include "tests.h"

extern int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p,
    unsigned char *d, int n, int *al);
extern int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p,
    int n, int *al);

/*
 * In the ProtocolNameList, ProtocolNames must not include empty strings and
 * byte strings must not be truncated.
 *
 * This uses some of the IANA approved protocol names from:
 * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
 */

/* Valid for client and server since it only has one name. */
static uint8_t proto_single[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0f, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x0b, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x09, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x08, /* len */
	0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31
};

/* Valid for client, but NOT server.  Server must have exactly one name. */
static uint8_t proto_multiple1[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x19, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x15, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x13, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x08, /* len */
	0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
	/* opaque ProtocolName<1..2^8-1> -- 'stun.nat' */
	0x09, /* len */
	0x73, 0x74, 0x75, 0x6e, 0x2e, 0x74, 0x75, 0x72, 0x6e
};

/* Valid for client, but NOT server.  Server must have exactly one name. */
static uint8_t proto_multiple2[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x1c, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x18, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x16, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x08, /* len */
	0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
	/* opaque ProtocolName<1..2^8-1> -- 'h2' */
	0x02, /* len */
	0x68, 0x32,
	/* opaque ProtocolName<1..2^8-1> -- 'stun.nat' */
	0x09, /* len */
	0x73, 0x74, 0x75, 0x6e, 0x2e, 0x74, 0x75, 0x72, 0x6e
};

/* Valid for client, but NOT server.  Server must have exactly one name. */
static uint8_t proto_multiple3[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x20, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x1c, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x1a, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x08, /* len */
	0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
	/* opaque ProtocolName<1..2^8-1> -- 'h2' */
	0x02, /* len */
	0x68, 0x32,
	/* opaque ProtocolName<1..2^8-1> -- 'stun.nat' */
	0x09, /* len */
	0x73, 0x74, 0x75, 0x6e, 0x2e, 0x74, 0x75, 0x72, 0x6e,
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};

static uint8_t proto_empty[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions. */
	0x00, 0x00, /* none present. */
};

/* Invalid for both client and server.  Length is wrong. */
static uint8_t proto_invalid_len1[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x04, /* XXX len too large */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len2[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x02, /* XXX len too small */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len3[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x03, /* XXX len too small */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len4[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x06, /* XXX len too large */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len5[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x01, 0x08, /* XXX len too large */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len6[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x05, /* XXX len too small */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len7[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x06, /* XXX len too small */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};
static uint8_t proto_invalid_len8[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0b, /* XXX len too large */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	0x03, /* len */
	0x68, 0x32, 0x63
};

/* Invalid for client and server since it is missing data. */
static uint8_t proto_invalid_missing1[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x06, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x04, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'h2c' */
	/* XXX missing */
};
static uint8_t proto_invalid_missing2[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x00, /* XXX missing name list */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
};
static uint8_t proto_invalid_missing3[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x02, /* XXX size is sufficient but missing data for name list */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
};
static uint8_t proto_invalid_missing4[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x0a, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	/* XXX missing */
};
static uint8_t proto_invalid_missing5[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x1c, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x18, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x16, /* len of all names */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x08, /* len */
	0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
	/* opaque ProtocolName<1..2^8-1> -- 'h2' */
	0x02, /* len */
	0x68, 0x32,
	/* XXX missing name */
};
static uint8_t proto_invalid_missing6[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x07, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x03, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x01, /* XXX len must be at least 2 */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x00, /* XXX len cannot be 0 */
};
static uint8_t proto_invalid_missing7[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x07, /* len */
	/* ExtensionType extension_type */
	0x00, 0x10, /* ALPN */
	/* opaque extension_data<0..2^16-1> */
	0x00, 0x03, /* len */
	/* ProtocolName protocol_name_list<2..2^16-1> -- ALPN names */
	0x00, 0x02, /* XXX len is at least 2 but not correct. */
	/* opaque ProtocolName<1..2^8-1> -- 'http/1.1' */
	0x00, /* XXX len cannot be 0 */
};
static uint8_t proto_invalid_missing8[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x00, 0x01, /* len */
	/* ExtensionType extension_type */
	0x00, /* XXX need a 2 byte type */
};
static uint8_t proto_invalid_missing9[] = {
	/* Extension extensions<0..2^16-1> -- All TLS extensions */
	0x0a, /* XXX need a 2 byte len */
};


#define CHECK_BOTH(c_val,s_val,proto) do {				\
	{								\
		unsigned char *p = proto;				\
		int al;							\
		CHECK(c_val == ssl_parse_clienthello_tlsext(s, &p,	\
		    proto, sizeof(proto), &al));			\
		p = proto;						\
		CHECK(s_val == ssl_parse_serverhello_tlsext(s, &p,	\
		    sizeof(proto), &al));				\
	}								\
} while (0)

static int dummy_alpn_cb(SSL *ssl, const unsigned char **out,
    unsigned char *outlen, const unsigned char *in, unsigned int inlen,
    void *arg);

static int
check_valid_alpn(SSL *s)
{
	const uint8_t str[] = {
		0x08, /* len */
		0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 /* http/1.1 */
	};

	/* Setup in order to test ALPN. */
	CHECK(! SSL_set_alpn_protos(s, str, 9));
	SSL_CTX_set_alpn_select_cb(s->ctx, dummy_alpn_cb, NULL);

	/* Prerequisites to test these. */
	CHECK(s->alpn_client_proto_list != NULL);
	CHECK(s->ctx->alpn_select_cb != NULL);
	CHECK(s->s3->tmp.finish_md_len == 0);

	CHECK_BOTH(1, 1, proto_single);
	CHECK_BOTH(1, 1, proto_empty);

	/* Multiple protocol names are only valid for client */
	CHECK_BOTH(1, 0, proto_multiple1);
	CHECK_BOTH(1, 0, proto_multiple2);
	CHECK_BOTH(1, 0, proto_multiple3);

	return 1;
}

/*
 * Some of the IANA approved IDs from:
 * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
 */
static int
check_invalid_alpn(SSL *s)
{
	const uint8_t str[] = {
		0x08, /* len */
		0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31 /* http/1.1 */
	};

	/* Setup in order to test ALPN. */
	CHECK(! SSL_set_alpn_protos(s, str, 9));
	SSL_CTX_set_alpn_select_cb(s->ctx, dummy_alpn_cb, NULL);

	/* Prerequisites to test these. */
	CHECK(s->alpn_client_proto_list != NULL);
	CHECK(s->ctx->alpn_select_cb != NULL);
	CHECK(s->s3->tmp.finish_md_len == 0);

	/* None of these are valid for client or server */
	CHECK_BOTH(0, 0, proto_invalid_len1);
	CHECK_BOTH(0, 0, proto_invalid_len2);
	CHECK_BOTH(0, 0, proto_invalid_len3);
	CHECK_BOTH(0, 0, proto_invalid_len4);
	CHECK_BOTH(0, 0, proto_invalid_len5);
	CHECK_BOTH(0, 0, proto_invalid_len6);
	CHECK_BOTH(0, 0, proto_invalid_len7);
	CHECK_BOTH(0, 0, proto_invalid_len8);
	CHECK_BOTH(0, 0, proto_invalid_missing1);
	CHECK_BOTH(0, 0, proto_invalid_missing2);
	CHECK_BOTH(0, 0, proto_invalid_missing3);
	CHECK_BOTH(0, 0, proto_invalid_missing4);
	CHECK_BOTH(0, 0, proto_invalid_missing5);
	CHECK_BOTH(0, 0, proto_invalid_missing6);
	CHECK_BOTH(0, 0, proto_invalid_missing7);
	CHECK_BOTH(0, 0, proto_invalid_missing8);
	CHECK_BOTH(0, 0, proto_invalid_missing9);

	return 1;
}

int
dummy_alpn_cb(SSL *ssl __attribute__((unused)), const unsigned char **out,
    unsigned char *outlen, const unsigned char *in, unsigned int inlen,
    void *arg __attribute__((unused)))
{
	*out = in;
	*outlen = (unsigned char)inlen;

	return 0;
}

int
main(void)
{
	SSL_CTX *ctx = NULL;
	SSL *s = NULL;
	int rv = 1;

	SSL_library_init();

	CHECK_GOTO((ctx = SSL_CTX_new(TLSv1_2_client_method())) != NULL);
	CHECK_GOTO((s = SSL_new(ctx)) != NULL);

	if (!check_valid_alpn(s))
		goto err;
	if (!check_invalid_alpn(s))
		goto err;

	rv = 0;

err:
	SSL_CTX_free(ctx);
	SSL_free(s);

	if (!rv)
		printf("PASS %s\n", __FILE__);
	return rv;
}