ssl_tlsext.c revision 1.53
1/* $OpenBSD: ssl_tlsext.c,v 1.53 2020/01/22 10:36:57 tb Exp $ */
2/*
3 * Copyright (c) 2016, 2017, 2019 Joel Sing <jsing@openbsd.org>
4 * Copyright (c) 2017 Doug Hogan <doug@openbsd.org>
5 * Copyright (c) 2018-2019 Bob Beck <beck@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19#include <openssl/curve25519.h>
20#include <openssl/ocsp.h>
21
22#include "ssl_locl.h"
23
24#include "bytestring.h"
25#include "ssl_sigalgs.h"
26#include "ssl_tlsext.h"
27
28/*
29 * Supported Application-Layer Protocol Negotiation - RFC 7301
30 */
31
32int
33tlsext_alpn_client_needs(SSL *s)
34{
35	/* ALPN protos have been specified and this is the initial handshake */
36	return s->internal->alpn_client_proto_list != NULL &&
37	    S3I(s)->tmp.finish_md_len == 0;
38}
39
40int
41tlsext_alpn_client_build(SSL *s, CBB *cbb)
42{
43	CBB protolist;
44
45	if (!CBB_add_u16_length_prefixed(cbb, &protolist))
46		return 0;
47
48	if (!CBB_add_bytes(&protolist, s->internal->alpn_client_proto_list,
49	    s->internal->alpn_client_proto_list_len))
50		return 0;
51
52	if (!CBB_flush(cbb))
53		return 0;
54
55	return 1;
56}
57
58int
59tlsext_alpn_server_parse(SSL *s, CBS *cbs, int *alert)
60{
61	CBS proto_name_list, alpn;
62	const unsigned char *selected;
63	unsigned char selected_len;
64	int r;
65
66	if (!CBS_get_u16_length_prefixed(cbs, &alpn))
67		goto err;
68	if (CBS_len(&alpn) < 2)
69		goto err;
70	if (CBS_len(cbs) != 0)
71		goto err;
72
73	CBS_dup(&alpn, &proto_name_list);
74	while (CBS_len(&proto_name_list) > 0) {
75		CBS proto_name;
76
77		if (!CBS_get_u8_length_prefixed(&proto_name_list, &proto_name))
78			goto err;
79		if (CBS_len(&proto_name) == 0)
80			goto err;
81	}
82
83	if (s->ctx->internal->alpn_select_cb == NULL)
84		return 1;
85
86	r = s->ctx->internal->alpn_select_cb(s, &selected, &selected_len,
87	    CBS_data(&alpn), CBS_len(&alpn),
88	    s->ctx->internal->alpn_select_cb_arg);
89	if (r == SSL_TLSEXT_ERR_OK) {
90		free(S3I(s)->alpn_selected);
91		if ((S3I(s)->alpn_selected = malloc(selected_len)) == NULL) {
92			*alert = SSL_AD_INTERNAL_ERROR;
93			return 0;
94		}
95		memcpy(S3I(s)->alpn_selected, selected, selected_len);
96		S3I(s)->alpn_selected_len = selected_len;
97	}
98
99	return 1;
100
101 err:
102	*alert = SSL_AD_DECODE_ERROR;
103	return 0;
104}
105
106int
107tlsext_alpn_server_needs(SSL *s)
108{
109	return S3I(s)->alpn_selected != NULL;
110}
111
112int
113tlsext_alpn_server_build(SSL *s, CBB *cbb)
114{
115	CBB list, selected;
116
117	if (!CBB_add_u16_length_prefixed(cbb, &list))
118		return 0;
119
120	if (!CBB_add_u8_length_prefixed(&list, &selected))
121		return 0;
122
123	if (!CBB_add_bytes(&selected, S3I(s)->alpn_selected,
124	    S3I(s)->alpn_selected_len))
125		return 0;
126
127	if (!CBB_flush(cbb))
128		return 0;
129
130	return 1;
131}
132
133int
134tlsext_alpn_client_parse(SSL *s, CBS *cbs, int *alert)
135{
136	CBS list, proto;
137
138	if (s->internal->alpn_client_proto_list == NULL) {
139		*alert = TLS1_AD_UNSUPPORTED_EXTENSION;
140		return 0;
141	}
142
143	if (!CBS_get_u16_length_prefixed(cbs, &list))
144		goto err;
145	if (CBS_len(cbs) != 0)
146		goto err;
147
148	if (!CBS_get_u8_length_prefixed(&list, &proto))
149		goto err;
150
151	if (CBS_len(&list) != 0)
152		goto err;
153	if (CBS_len(&proto) == 0)
154		goto err;
155
156	if (!CBS_stow(&proto, &(S3I(s)->alpn_selected),
157	    &(S3I(s)->alpn_selected_len)))
158		goto err;
159
160	return 1;
161
162 err:
163	*alert = TLS1_AD_DECODE_ERROR;
164	return 0;
165}
166
167/*
168 * Supported Groups - RFC 7919 section 2
169 */
170int
171tlsext_supportedgroups_client_needs(SSL *s)
172{
173	return ssl_has_ecc_ciphers(s) ||
174	    (S3I(s)->hs_tls13.max_version >= TLS1_3_VERSION);
175}
176
177int
178tlsext_supportedgroups_client_build(SSL *s, CBB *cbb)
179{
180	const uint16_t *groups;
181	size_t groups_len;
182	CBB grouplist;
183	int i;
184
185	tls1_get_group_list(s, 0, &groups, &groups_len);
186	if (groups_len == 0) {
187		SSLerror(s, ERR_R_INTERNAL_ERROR);
188		return 0;
189	}
190
191	if (!CBB_add_u16_length_prefixed(cbb, &grouplist))
192		return 0;
193
194	for (i = 0; i < groups_len; i++) {
195		if (!CBB_add_u16(&grouplist, groups[i]))
196			return 0;
197	}
198
199	if (!CBB_flush(cbb))
200		return 0;
201
202	return 1;
203}
204
205int
206tlsext_supportedgroups_server_parse(SSL *s, CBS *cbs, int *alert)
207{
208	CBS grouplist;
209	size_t groups_len;
210
211	if (!CBS_get_u16_length_prefixed(cbs, &grouplist))
212		goto err;
213	if (CBS_len(cbs) != 0)
214		goto err;
215
216	groups_len = CBS_len(&grouplist);
217	if (groups_len == 0 || groups_len % 2 != 0)
218		goto err;
219	groups_len /= 2;
220
221	if (!s->internal->hit) {
222		uint16_t *groups;
223		int i;
224
225		if (SSI(s)->tlsext_supportedgroups != NULL)
226			goto err;
227
228		if ((groups = reallocarray(NULL, groups_len,
229		    sizeof(uint16_t))) == NULL) {
230			*alert = TLS1_AD_INTERNAL_ERROR;
231			return 0;
232		}
233
234		for (i = 0; i < groups_len; i++) {
235			if (!CBS_get_u16(&grouplist, &groups[i])) {
236				free(groups);
237				goto err;
238			}
239		}
240
241		if (CBS_len(&grouplist) != 0) {
242			free(groups);
243			goto err;
244		}
245
246		SSI(s)->tlsext_supportedgroups = groups;
247		SSI(s)->tlsext_supportedgroups_length = groups_len;
248	}
249
250	return 1;
251
252 err:
253	*alert = TLS1_AD_DECODE_ERROR;
254	return 0;
255}
256
257/* This extension is never used by the server. */
258int
259tlsext_supportedgroups_server_needs(SSL *s)
260{
261	return 0;
262}
263
264int
265tlsext_supportedgroups_server_build(SSL *s, CBB *cbb)
266{
267	return 0;
268}
269
270int
271tlsext_supportedgroups_client_parse(SSL *s, CBS *cbs, int *alert)
272{
273	/*
274	 * Servers should not send this extension per the RFC.
275	 *
276	 * However, certain F5 BIG-IP systems incorrectly send it. This bug is
277	 * from at least 2014 but as of 2017, there are still large sites with
278	 * this unpatched in production. As a result, we need to currently skip
279	 * over the extension and ignore its content:
280	 *
281	 *  https://support.f5.com/csp/article/K37345003
282	 */
283	if (!CBS_skip(cbs, CBS_len(cbs))) {
284		*alert = TLS1_AD_INTERNAL_ERROR;
285		return 0;
286	}
287
288	return 1;
289}
290
291/*
292 * Supported Point Formats Extension - RFC 4492 section 5.1.2
293 */
294static int
295tlsext_ecpf_build(SSL *s, CBB *cbb)
296{
297	CBB ecpf;
298	size_t formats_len;
299	const uint8_t *formats;
300
301	tls1_get_formatlist(s, 0, &formats, &formats_len);
302
303	if (formats_len == 0) {
304		SSLerror(s, ERR_R_INTERNAL_ERROR);
305		return 0;
306	}
307
308	if (!CBB_add_u8_length_prefixed(cbb, &ecpf))
309		return 0;
310	if (!CBB_add_bytes(&ecpf, formats, formats_len))
311		return 0;
312	if (!CBB_flush(cbb))
313		return 0;
314
315	return 1;
316}
317
318static int
319tlsext_ecpf_parse(SSL *s, CBS *cbs, int *alert)
320{
321	CBS ecpf;
322
323	if (!CBS_get_u8_length_prefixed(cbs, &ecpf))
324		goto err;
325	if (CBS_len(&ecpf) == 0)
326		goto err;
327	if (CBS_len(cbs) != 0)
328		goto err;
329
330	/* Must contain uncompressed (0) */
331	if (!CBS_contains_zero_byte(&ecpf)) {
332		SSLerror(s, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
333		goto err;
334	}
335
336	if (!s->internal->hit) {
337		if (!CBS_stow(&ecpf, &(SSI(s)->tlsext_ecpointformatlist),
338		    &(SSI(s)->tlsext_ecpointformatlist_length))) {
339			*alert = TLS1_AD_INTERNAL_ERROR;
340			return 0;
341		}
342	}
343
344	return 1;
345
346 err:
347	*alert = SSL_AD_DECODE_ERROR;
348	return 0;
349}
350
351int
352tlsext_ecpf_client_needs(SSL *s)
353{
354	return ssl_has_ecc_ciphers(s);
355}
356
357int
358tlsext_ecpf_client_build(SSL *s, CBB *cbb)
359{
360	return tlsext_ecpf_build(s, cbb);
361}
362
363int
364tlsext_ecpf_server_parse(SSL *s, CBS *cbs, int *alert)
365{
366	return tlsext_ecpf_parse(s, cbs, alert);
367}
368
369int
370tlsext_ecpf_server_needs(SSL *s)
371{
372	if (s->version == DTLS1_VERSION)
373		return 0;
374
375	return ssl_using_ecc_cipher(s);
376}
377
378int
379tlsext_ecpf_server_build(SSL *s, CBB *cbb)
380{
381	return tlsext_ecpf_build(s, cbb);
382}
383
384int
385tlsext_ecpf_client_parse(SSL *s, CBS *cbs, int *alert)
386{
387	return tlsext_ecpf_parse(s, cbs, alert);
388}
389
390/*
391 * Renegotiation Indication - RFC 5746.
392 */
393int
394tlsext_ri_client_needs(SSL *s)
395{
396	return (s->internal->renegotiate);
397}
398
399int
400tlsext_ri_client_build(SSL *s, CBB *cbb)
401{
402	CBB reneg;
403
404	if (!CBB_add_u8_length_prefixed(cbb, &reneg))
405		return 0;
406	if (!CBB_add_bytes(&reneg, S3I(s)->previous_client_finished,
407	    S3I(s)->previous_client_finished_len))
408		return 0;
409	if (!CBB_flush(cbb))
410		return 0;
411
412	return 1;
413}
414
415int
416tlsext_ri_server_parse(SSL *s, CBS *cbs, int *alert)
417{
418	CBS reneg;
419
420	if (!CBS_get_u8_length_prefixed(cbs, &reneg))
421		goto err;
422	if (CBS_len(cbs) != 0)
423		goto err;
424
425	if (!CBS_mem_equal(&reneg, S3I(s)->previous_client_finished,
426	    S3I(s)->previous_client_finished_len)) {
427		SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
428		*alert = SSL_AD_HANDSHAKE_FAILURE;
429		return 0;
430	}
431
432	S3I(s)->renegotiate_seen = 1;
433	S3I(s)->send_connection_binding = 1;
434
435	return 1;
436
437 err:
438	SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
439	*alert = SSL_AD_DECODE_ERROR;
440	return 0;
441}
442
443int
444tlsext_ri_server_needs(SSL *s)
445{
446	return (S3I(s)->send_connection_binding);
447}
448
449int
450tlsext_ri_server_build(SSL *s, CBB *cbb)
451{
452	CBB reneg;
453
454	if (!CBB_add_u8_length_prefixed(cbb, &reneg))
455		return 0;
456	if (!CBB_add_bytes(&reneg, S3I(s)->previous_client_finished,
457	    S3I(s)->previous_client_finished_len))
458		return 0;
459	if (!CBB_add_bytes(&reneg, S3I(s)->previous_server_finished,
460	    S3I(s)->previous_server_finished_len))
461		return 0;
462	if (!CBB_flush(cbb))
463		return 0;
464
465	return 1;
466}
467
468int
469tlsext_ri_client_parse(SSL *s, CBS *cbs, int *alert)
470{
471	CBS reneg, prev_client, prev_server;
472
473	/*
474	 * Ensure that the previous client and server values are both not
475	 * present, or that they are both present.
476	 */
477	if ((S3I(s)->previous_client_finished_len == 0 &&
478	    S3I(s)->previous_server_finished_len != 0) ||
479	    (S3I(s)->previous_client_finished_len != 0 &&
480	    S3I(s)->previous_server_finished_len == 0)) {
481		*alert = TLS1_AD_INTERNAL_ERROR;
482		return 0;
483	}
484
485	if (!CBS_get_u8_length_prefixed(cbs, &reneg))
486		goto err;
487	if (!CBS_get_bytes(&reneg, &prev_client,
488	    S3I(s)->previous_client_finished_len))
489		goto err;
490	if (!CBS_get_bytes(&reneg, &prev_server,
491	    S3I(s)->previous_server_finished_len))
492		goto err;
493	if (CBS_len(&reneg) != 0)
494		goto err;
495	if (CBS_len(cbs) != 0)
496		goto err;
497
498	if (!CBS_mem_equal(&prev_client, S3I(s)->previous_client_finished,
499	    S3I(s)->previous_client_finished_len)) {
500		SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
501		*alert = SSL_AD_HANDSHAKE_FAILURE;
502		return 0;
503	}
504	if (!CBS_mem_equal(&prev_server, S3I(s)->previous_server_finished,
505	    S3I(s)->previous_server_finished_len)) {
506		SSLerror(s, SSL_R_RENEGOTIATION_MISMATCH);
507		*alert = SSL_AD_HANDSHAKE_FAILURE;
508		return 0;
509	}
510
511	S3I(s)->renegotiate_seen = 1;
512	S3I(s)->send_connection_binding = 1;
513
514	return 1;
515
516 err:
517	SSLerror(s, SSL_R_RENEGOTIATION_ENCODING_ERR);
518	*alert = SSL_AD_DECODE_ERROR;
519	return 0;
520}
521
522/*
523 * Signature Algorithms - RFC 5246 section 7.4.1.4.1.
524 */
525int
526tlsext_sigalgs_client_needs(SSL *s)
527{
528	return (TLS1_get_client_version(s) >= TLS1_2_VERSION);
529}
530
531int
532tlsext_sigalgs_client_build(SSL *s, CBB *cbb)
533{
534	uint16_t *tls_sigalgs = tls12_sigalgs;
535	size_t tls_sigalgs_len = tls12_sigalgs_len;
536	CBB sigalgs;
537
538	if (TLS1_get_client_version(s) >= TLS1_3_VERSION &&
539	    S3I(s)->hs_tls13.min_version >= TLS1_3_VERSION) {
540		tls_sigalgs = tls13_sigalgs;
541		tls_sigalgs_len = tls13_sigalgs_len;
542	}
543
544	if (!CBB_add_u16_length_prefixed(cbb, &sigalgs))
545		return 0;
546
547	if (!ssl_sigalgs_build(&sigalgs, tls_sigalgs, tls_sigalgs_len))
548		return 0;
549
550	if (!CBB_flush(cbb))
551		return 0;
552
553	return 1;
554}
555
556int
557tlsext_sigalgs_server_parse(SSL *s, CBS *cbs, int *alert)
558{
559	CBS sigalgs;
560
561	if (!CBS_get_u16_length_prefixed(cbs, &sigalgs))
562		return 0;
563	if (CBS_len(&sigalgs) % 2 != 0 || CBS_len(&sigalgs) > 64)
564		return 0;
565	if (!CBS_stow(&sigalgs, &S3I(s)->hs.sigalgs, &S3I(s)->hs.sigalgs_len))
566		return 0;
567
568	return 1;
569}
570
571int
572tlsext_sigalgs_server_needs(SSL *s)
573{
574	return 0;
575}
576
577int
578tlsext_sigalgs_server_build(SSL *s, CBB *cbb)
579{
580	return 0;
581}
582
583int
584tlsext_sigalgs_client_parse(SSL *s, CBS *cbs, int *alert)
585{
586	/* As per the RFC, servers must not send this extension. */
587	return 0;
588}
589
590/*
591 * Server Name Indication - RFC 6066, section 3.
592 */
593int
594tlsext_sni_client_needs(SSL *s)
595{
596	return (s->tlsext_hostname != NULL);
597}
598
599int
600tlsext_sni_client_build(SSL *s, CBB *cbb)
601{
602	CBB server_name_list, host_name;
603
604	if (!CBB_add_u16_length_prefixed(cbb, &server_name_list))
605		return 0;
606	if (!CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name))
607		return 0;
608	if (!CBB_add_u16_length_prefixed(&server_name_list, &host_name))
609		return 0;
610	if (!CBB_add_bytes(&host_name, (const uint8_t *)s->tlsext_hostname,
611	    strlen(s->tlsext_hostname)))
612		return 0;
613	if (!CBB_flush(cbb))
614		return 0;
615
616	return 1;
617}
618
619int
620tlsext_sni_server_parse(SSL *s, CBS *cbs, int *alert)
621{
622	CBS server_name_list, host_name;
623	uint8_t name_type;
624
625	if (!CBS_get_u16_length_prefixed(cbs, &server_name_list))
626		goto err;
627
628	/*
629	 * RFC 6066 section 3 forbids multiple host names with the same type.
630	 * Additionally, only one type (host_name) is specified.
631	 */
632	if (!CBS_get_u8(&server_name_list, &name_type))
633		goto err;
634	if (name_type != TLSEXT_NAMETYPE_host_name)
635		goto err;
636
637	if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name))
638		goto err;
639	if (CBS_len(&host_name) == 0 ||
640	    CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
641	    CBS_contains_zero_byte(&host_name)) {
642		*alert = TLS1_AD_UNRECOGNIZED_NAME;
643		return 0;
644	}
645
646	if (s->internal->hit) {
647		if (s->session->tlsext_hostname == NULL) {
648			*alert = TLS1_AD_UNRECOGNIZED_NAME;
649			return 0;
650		}
651		if (!CBS_mem_equal(&host_name, s->session->tlsext_hostname,
652		    strlen(s->session->tlsext_hostname))) {
653			*alert = TLS1_AD_UNRECOGNIZED_NAME;
654			return 0;
655		}
656	} else {
657		if (s->session->tlsext_hostname != NULL)
658			goto err;
659		if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
660			*alert = TLS1_AD_INTERNAL_ERROR;
661			return 0;
662		}
663	}
664
665	if (CBS_len(&server_name_list) != 0)
666		goto err;
667	if (CBS_len(cbs) != 0)
668		goto err;
669
670	return 1;
671
672 err:
673	*alert = SSL_AD_DECODE_ERROR;
674	return 0;
675}
676
677int
678tlsext_sni_server_needs(SSL *s)
679{
680	if (s->internal->hit)
681		return 0;
682
683	return (s->session->tlsext_hostname != NULL);
684}
685
686int
687tlsext_sni_server_build(SSL *s, CBB *cbb)
688{
689	return 1;
690}
691
692int
693tlsext_sni_client_parse(SSL *s, CBS *cbs, int *alert)
694{
695	if (s->tlsext_hostname == NULL || CBS_len(cbs) != 0) {
696		*alert = TLS1_AD_UNRECOGNIZED_NAME;
697		return 0;
698	}
699
700	if (s->internal->hit) {
701		if (s->session->tlsext_hostname == NULL) {
702			*alert = TLS1_AD_UNRECOGNIZED_NAME;
703			return 0;
704		}
705		if (strcmp(s->tlsext_hostname,
706		    s->session->tlsext_hostname) != 0) {
707			*alert = TLS1_AD_UNRECOGNIZED_NAME;
708			return 0;
709		}
710	} else {
711		if (s->session->tlsext_hostname != NULL) {
712			*alert = SSL_AD_DECODE_ERROR;
713			return 0;
714		}
715		if ((s->session->tlsext_hostname =
716		    strdup(s->tlsext_hostname)) == NULL) {
717			*alert = TLS1_AD_INTERNAL_ERROR;
718			return 0;
719		}
720	}
721
722	return 1;
723}
724
725
726/*
727 *Certificate Status Request - RFC 6066 section 8.
728 */
729
730int
731tlsext_ocsp_client_needs(SSL *s)
732{
733	return (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
734	    s->version != DTLS1_VERSION);
735}
736
737int
738tlsext_ocsp_client_build(SSL *s, CBB *cbb)
739{
740	CBB respid_list, respid, exts;
741	unsigned char *ext_data;
742	size_t ext_len;
743	int i;
744
745	if (!CBB_add_u8(cbb, TLSEXT_STATUSTYPE_ocsp))
746		return 0;
747	if (!CBB_add_u16_length_prefixed(cbb, &respid_list))
748		return 0;
749	for (i = 0; i < sk_OCSP_RESPID_num(s->internal->tlsext_ocsp_ids); i++) {
750		unsigned char *respid_data;
751		OCSP_RESPID *id;
752		size_t id_len;
753
754		if ((id = sk_OCSP_RESPID_value(s->internal->tlsext_ocsp_ids,
755		    i)) ==  NULL)
756			return 0;
757		if ((id_len = i2d_OCSP_RESPID(id, NULL)) == -1)
758			return 0;
759		if (!CBB_add_u16_length_prefixed(&respid_list, &respid))
760			return 0;
761		if (!CBB_add_space(&respid, &respid_data, id_len))
762			return 0;
763		if ((i2d_OCSP_RESPID(id, &respid_data)) != id_len)
764			return 0;
765	}
766	if (!CBB_add_u16_length_prefixed(cbb, &exts))
767		return 0;
768	if ((ext_len = i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts,
769	    NULL)) == -1)
770		return 0;
771	if (!CBB_add_space(&exts, &ext_data, ext_len))
772		return 0;
773	if ((i2d_X509_EXTENSIONS(s->internal->tlsext_ocsp_exts, &ext_data) !=
774	    ext_len))
775		return 0;
776	if (!CBB_flush(cbb))
777		return 0;
778	return 1;
779}
780
781int
782tlsext_ocsp_server_parse(SSL *s, CBS *cbs, int *alert)
783{
784	int failure = SSL_AD_DECODE_ERROR;
785	CBS respid_list, respid, exts;
786	const unsigned char *p;
787	uint8_t status_type;
788	int ret = 0;
789
790	if (!CBS_get_u8(cbs, &status_type))
791		goto err;
792	if (status_type != TLSEXT_STATUSTYPE_ocsp) {
793		/* ignore unknown status types */
794		s->tlsext_status_type = -1;
795
796		if (!CBS_skip(cbs, CBS_len(cbs))) {
797			*alert = TLS1_AD_INTERNAL_ERROR;
798			return 0;
799		}
800		return 1;
801	}
802	s->tlsext_status_type = status_type;
803	if (!CBS_get_u16_length_prefixed(cbs, &respid_list))
804		goto err;
805
806	/* XXX */
807	sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, OCSP_RESPID_free);
808	s->internal->tlsext_ocsp_ids = NULL;
809	if (CBS_len(&respid_list) > 0) {
810		s->internal->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null();
811		if (s->internal->tlsext_ocsp_ids == NULL) {
812			failure = SSL_AD_INTERNAL_ERROR;
813			goto err;
814		}
815	}
816
817	while (CBS_len(&respid_list) > 0) {
818		OCSP_RESPID *id;
819
820		if (!CBS_get_u16_length_prefixed(&respid_list, &respid))
821			goto err;
822		p = CBS_data(&respid);
823		if ((id = d2i_OCSP_RESPID(NULL, &p, CBS_len(&respid))) == NULL)
824			goto err;
825		if (!sk_OCSP_RESPID_push(s->internal->tlsext_ocsp_ids, id)) {
826			failure = SSL_AD_INTERNAL_ERROR;
827			OCSP_RESPID_free(id);
828			goto err;
829		}
830	}
831
832	/* Read in request_extensions */
833	if (!CBS_get_u16_length_prefixed(cbs, &exts))
834		goto err;
835	if (CBS_len(&exts) > 0) {
836		sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts,
837		    X509_EXTENSION_free);
838		p = CBS_data(&exts);
839		if ((s->internal->tlsext_ocsp_exts = d2i_X509_EXTENSIONS(NULL,
840		    &p, CBS_len(&exts))) == NULL)
841			goto err;
842	}
843
844	/* should be nothing left */
845	if (CBS_len(cbs) > 0)
846		goto err;
847
848	ret = 1;
849 err:
850	if (ret == 0)
851		*alert = failure;
852	return ret;
853}
854
855int
856tlsext_ocsp_server_needs(SSL *s)
857{
858	return s->internal->tlsext_status_expected;
859}
860
861int
862tlsext_ocsp_server_build(SSL *s, CBB *cbb)
863{
864	return 1;
865}
866
867int
868tlsext_ocsp_client_parse(SSL *s, CBS *cbs, int *alert)
869{
870	if (s->tlsext_status_type == -1) {
871		*alert = TLS1_AD_UNSUPPORTED_EXTENSION;
872		return 0;
873	}
874	/* Set flag to expect CertificateStatus message */
875	s->internal->tlsext_status_expected = 1;
876	return 1;
877}
878
879/*
880 * SessionTicket extension - RFC 5077 section 3.2
881 */
882int
883tlsext_sessionticket_client_needs(SSL *s)
884{
885	/*
886	 * Send session ticket extension when enabled and not overridden.
887	 *
888	 * When renegotiating, send an empty session ticket to indicate support.
889	 */
890	if ((SSL_get_options(s) & SSL_OP_NO_TICKET) != 0)
891		return 0;
892
893	if (s->internal->new_session)
894		return 1;
895
896	if (s->internal->tlsext_session_ticket != NULL &&
897	    s->internal->tlsext_session_ticket->data == NULL)
898		return 0;
899
900	return 1;
901}
902
903int
904tlsext_sessionticket_client_build(SSL *s, CBB *cbb)
905{
906	/*
907	 * Signal that we support session tickets by sending an empty
908	 * extension when renegotiating or no session found.
909	 */
910	if (s->internal->new_session || s->session == NULL)
911		return 1;
912
913	if (s->session->tlsext_tick != NULL) {
914		/* Attempt to resume with an existing session ticket */
915		if (!CBB_add_bytes(cbb, s->session->tlsext_tick,
916		    s->session->tlsext_ticklen))
917			return 0;
918
919	} else if (s->internal->tlsext_session_ticket != NULL) {
920		/*
921		 * Attempt to resume with a custom provided session ticket set
922		 * by SSL_set_session_ticket_ext().
923		 */
924		if (s->internal->tlsext_session_ticket->length > 0) {
925			size_t ticklen = s->internal->tlsext_session_ticket->length;
926
927			if ((s->session->tlsext_tick = malloc(ticklen)) == NULL)
928				return 0;
929			memcpy(s->session->tlsext_tick,
930			    s->internal->tlsext_session_ticket->data,
931			    ticklen);
932			s->session->tlsext_ticklen = ticklen;
933
934			if (!CBB_add_bytes(cbb, s->session->tlsext_tick,
935			    s->session->tlsext_ticklen))
936				return 0;
937		}
938	}
939
940	if (!CBB_flush(cbb))
941		return 0;
942
943	return 1;
944}
945
946int
947tlsext_sessionticket_server_parse(SSL *s, CBS *cbs, int *alert)
948{
949	if (s->internal->tls_session_ticket_ext_cb) {
950		if (!s->internal->tls_session_ticket_ext_cb(s, CBS_data(cbs),
951		    (int)CBS_len(cbs),
952		    s->internal->tls_session_ticket_ext_cb_arg)) {
953			*alert = TLS1_AD_INTERNAL_ERROR;
954			return 0;
955		}
956	}
957
958	/* We need to signal that this was processed fully */
959	if (!CBS_skip(cbs, CBS_len(cbs))) {
960		*alert = TLS1_AD_INTERNAL_ERROR;
961		return 0;
962	}
963
964	return 1;
965}
966
967int
968tlsext_sessionticket_server_needs(SSL *s)
969{
970	return (s->internal->tlsext_ticket_expected &&
971	    !(SSL_get_options(s) & SSL_OP_NO_TICKET));
972}
973
974int
975tlsext_sessionticket_server_build(SSL *s, CBB *cbb)
976{
977	/* Empty ticket */
978	return 1;
979}
980
981int
982tlsext_sessionticket_client_parse(SSL *s, CBS *cbs, int *alert)
983{
984	if (s->internal->tls_session_ticket_ext_cb) {
985		if (!s->internal->tls_session_ticket_ext_cb(s, CBS_data(cbs),
986		    (int)CBS_len(cbs),
987		    s->internal->tls_session_ticket_ext_cb_arg)) {
988			*alert = TLS1_AD_INTERNAL_ERROR;
989			return 0;
990		}
991	}
992
993	if ((SSL_get_options(s) & SSL_OP_NO_TICKET) != 0 || CBS_len(cbs) > 0) {
994		*alert = TLS1_AD_UNSUPPORTED_EXTENSION;
995		return 0;
996	}
997
998	s->internal->tlsext_ticket_expected = 1;
999
1000	return 1;
1001}
1002
1003/*
1004 * DTLS extension for SRTP key establishment - RFC 5764
1005 */
1006
1007#ifndef OPENSSL_NO_SRTP
1008
1009int
1010tlsext_srtp_client_needs(SSL *s)
1011{
1012	return SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s) != NULL;
1013}
1014
1015int
1016tlsext_srtp_client_build(SSL *s, CBB *cbb)
1017{
1018	CBB profiles, mki;
1019	int ct, i;
1020	STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = NULL;
1021	SRTP_PROTECTION_PROFILE *prof;
1022
1023	if ((clnt = SSL_get_srtp_profiles(s)) == NULL) {
1024		SSLerror(s, SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
1025		return 0;
1026	}
1027
1028	if ((ct = sk_SRTP_PROTECTION_PROFILE_num(clnt)) < 1) {
1029		SSLerror(s, SSL_R_EMPTY_SRTP_PROTECTION_PROFILE_LIST);
1030		return 0;
1031	}
1032
1033	if (!CBB_add_u16_length_prefixed(cbb, &profiles))
1034		return 0;
1035
1036	for (i = 0; i < ct; i++) {
1037		if ((prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i)) == NULL)
1038			return 0;
1039		if (!CBB_add_u16(&profiles, prof->id))
1040			return 0;
1041	}
1042
1043	if (!CBB_add_u8_length_prefixed(cbb, &mki))
1044		return 0;
1045
1046	if (!CBB_flush(cbb))
1047		return 0;
1048
1049	return 1;
1050}
1051
1052int
1053tlsext_srtp_server_parse(SSL *s, CBS *cbs, int *alert)
1054{
1055	SRTP_PROTECTION_PROFILE *cprof, *sprof;
1056	STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = NULL, *srvr;
1057	int i, j;
1058	int ret;
1059	uint16_t id;
1060	CBS profiles, mki;
1061
1062	ret = 0;
1063
1064	if (!CBS_get_u16_length_prefixed(cbs, &profiles))
1065		goto err;
1066	if (CBS_len(&profiles) == 0 || CBS_len(&profiles) % 2 != 0)
1067		goto err;
1068
1069	if ((clnt = sk_SRTP_PROTECTION_PROFILE_new_null()) == NULL)
1070		goto err;
1071
1072	while (CBS_len(&profiles) > 0) {
1073		if (!CBS_get_u16(&profiles, &id))
1074			goto err;
1075
1076		if (!srtp_find_profile_by_num(id, &cprof)) {
1077			if (!sk_SRTP_PROTECTION_PROFILE_push(clnt, cprof))
1078				goto err;
1079		}
1080	}
1081
1082	if (!CBS_get_u8_length_prefixed(cbs, &mki) || CBS_len(&mki) != 0) {
1083		SSLerror(s, SSL_R_BAD_SRTP_MKI_VALUE);
1084		*alert = SSL_AD_DECODE_ERROR;
1085		goto done;
1086	}
1087	if (CBS_len(cbs) != 0)
1088		goto err;
1089
1090	/*
1091	 * Per RFC 5764 section 4.1.1
1092	 *
1093	 * Find the server preferred profile using the client's list.
1094	 *
1095	 * The server MUST send a profile if it sends the use_srtp
1096	 * extension.  If one is not found, it should fall back to the
1097	 * negotiated DTLS cipher suite or return a DTLS alert.
1098	 */
1099	if ((srvr = SSL_get_srtp_profiles(s)) == NULL)
1100		goto err;
1101	for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(srvr); i++) {
1102		if ((sprof = sk_SRTP_PROTECTION_PROFILE_value(srvr, i))
1103		    == NULL)
1104			goto err;
1105
1106		for (j = 0; j < sk_SRTP_PROTECTION_PROFILE_num(clnt); j++) {
1107			if ((cprof = sk_SRTP_PROTECTION_PROFILE_value(clnt, j))
1108			    == NULL)
1109				goto err;
1110
1111			if (cprof->id == sprof->id) {
1112				s->internal->srtp_profile = sprof;
1113				ret = 1;
1114				goto done;
1115			}
1116		}
1117	}
1118
1119	/* If we didn't find anything, fall back to the negotiated */
1120	ret = 1;
1121	goto done;
1122
1123 err:
1124	SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1125	*alert = SSL_AD_DECODE_ERROR;
1126
1127 done:
1128	sk_SRTP_PROTECTION_PROFILE_free(clnt);
1129	return ret;
1130}
1131
1132int
1133tlsext_srtp_server_needs(SSL *s)
1134{
1135	return SSL_IS_DTLS(s) && SSL_get_selected_srtp_profile(s) != NULL;
1136}
1137
1138int
1139tlsext_srtp_server_build(SSL *s, CBB *cbb)
1140{
1141	SRTP_PROTECTION_PROFILE *profile;
1142	CBB srtp, mki;
1143
1144	if (!CBB_add_u16_length_prefixed(cbb, &srtp))
1145		return 0;
1146
1147	if ((profile = SSL_get_selected_srtp_profile(s)) == NULL)
1148		return 0;
1149
1150	if (!CBB_add_u16(&srtp, profile->id))
1151		return 0;
1152
1153	if (!CBB_add_u8_length_prefixed(cbb, &mki))
1154		return 0;
1155
1156	if (!CBB_flush(cbb))
1157		return 0;
1158
1159	return 1;
1160}
1161
1162int
1163tlsext_srtp_client_parse(SSL *s, CBS *cbs, int *alert)
1164{
1165	STACK_OF(SRTP_PROTECTION_PROFILE) *clnt;
1166	SRTP_PROTECTION_PROFILE *prof;
1167	int i;
1168	uint16_t id;
1169	CBS profile_ids, mki;
1170
1171	if (!CBS_get_u16_length_prefixed(cbs, &profile_ids)) {
1172		SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1173		goto err;
1174	}
1175
1176	if (!CBS_get_u16(&profile_ids, &id) || CBS_len(&profile_ids) != 0) {
1177		SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1178		goto err;
1179	}
1180
1181	if (!CBS_get_u8_length_prefixed(cbs, &mki) || CBS_len(&mki) != 0) {
1182		SSLerror(s, SSL_R_BAD_SRTP_MKI_VALUE);
1183		*alert = SSL_AD_ILLEGAL_PARAMETER;
1184		return 0;
1185	}
1186
1187	if ((clnt = SSL_get_srtp_profiles(s)) == NULL) {
1188		SSLerror(s, SSL_R_NO_SRTP_PROFILES);
1189		goto err;
1190	}
1191
1192	for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) {
1193		if ((prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i))
1194		    == NULL) {
1195			SSLerror(s, SSL_R_NO_SRTP_PROFILES);
1196			goto err;
1197		}
1198
1199		if (prof->id == id) {
1200			s->internal->srtp_profile = prof;
1201			return 1;
1202		}
1203	}
1204
1205	SSLerror(s, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
1206 err:
1207	*alert = SSL_AD_DECODE_ERROR;
1208	return 0;
1209}
1210
1211#endif /* OPENSSL_NO_SRTP */
1212
1213/*
1214 * TLSv1.3 Key Share - RFC 8446 section 4.2.8.
1215 */
1216int
1217tlsext_keyshare_client_needs(SSL *s)
1218{
1219	/* XXX once this gets initialized when we get tls13_client.c */
1220	if (S3I(s)->hs_tls13.max_version == 0)
1221		return 0;
1222	return (!SSL_IS_DTLS(s) && S3I(s)->hs_tls13.max_version >=
1223	    TLS1_3_VERSION);
1224}
1225
1226int
1227tlsext_keyshare_client_build(SSL *s, CBB *cbb)
1228{
1229	uint8_t *public_key = NULL, *private_key = NULL;
1230	CBB client_shares, key_exchange;
1231
1232	/* Generate and provide key shares. */
1233	if (!CBB_add_u16_length_prefixed(cbb, &client_shares))
1234		return 0;
1235
1236	/* XXX - other groups. */
1237
1238	/* Generate X25519 key pair. */
1239	if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
1240		goto err;
1241	if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL)
1242		goto err;
1243	X25519_keypair(public_key, private_key);
1244
1245	/* Add the group and serialize the public key. */
1246	if (!CBB_add_u16(&client_shares, tls1_ec_nid2curve_id(NID_X25519)))
1247		goto err;
1248	if (!CBB_add_u16_length_prefixed(&client_shares, &key_exchange))
1249		goto err;
1250	if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH))
1251		goto err;
1252
1253	if (!CBB_flush(cbb))
1254		goto err;
1255
1256	S3I(s)->hs_tls13.x25519_public = public_key;
1257	S3I(s)->hs_tls13.x25519_private = private_key;
1258
1259	return 1;
1260
1261 err:
1262	freezero(public_key, X25519_KEY_LENGTH);
1263	freezero(private_key, X25519_KEY_LENGTH);
1264
1265	return 0;
1266}
1267
1268int
1269tlsext_keyshare_server_parse(SSL *s, CBS *cbs, int *alert)
1270{
1271	CBS client_shares;
1272	CBS key_exchange;
1273	uint16_t group;
1274	size_t out_len;
1275
1276	if (!CBS_get_u16_length_prefixed(cbs, &client_shares))
1277		goto err;
1278
1279	if (CBS_len(cbs) != 0)
1280		goto err;
1281
1282	while (CBS_len(&client_shares) > 0) {
1283
1284		/* Unpack client share. */
1285		if (!CBS_get_u16(&client_shares, &group))
1286			goto err;
1287
1288		if (!CBS_get_u16_length_prefixed(&client_shares, &key_exchange))
1289			goto err;
1290
1291		/*
1292		 * Skip this client share if not X25519
1293		 * XXX support other groups later.
1294		 * XXX enforce group can only appear once.
1295		 */
1296		if (S3I(s)->hs_tls13.x25519_peer_public != NULL ||
1297		    group != tls1_ec_nid2curve_id(NID_X25519))
1298			continue;
1299
1300		if (CBS_len(&key_exchange) != X25519_KEY_LENGTH)
1301			goto err;
1302
1303		if (!CBS_stow(&key_exchange, &S3I(s)->hs_tls13.x25519_peer_public,
1304		    &out_len))
1305			goto err;
1306	}
1307
1308	return 1;
1309
1310 err:
1311	*alert = SSL_AD_DECODE_ERROR;
1312	return 0;
1313}
1314
1315int
1316tlsext_keyshare_server_needs(SSL *s)
1317{
1318	if (SSL_IS_DTLS(s) || s->version < TLS1_3_VERSION)
1319		return 0;
1320
1321	return tlsext_extension_seen(s, TLSEXT_TYPE_key_share);
1322}
1323
1324int
1325tlsext_keyshare_server_build(SSL *s, CBB *cbb)
1326{
1327	uint8_t *public_key = NULL, *private_key = NULL;
1328	CBB key_exchange;
1329
1330	/* XXX deduplicate with client code */
1331
1332	/* X25519 */
1333	if (S3I(s)->hs_tls13.x25519_peer_public == NULL)
1334		return 0;
1335
1336	/* Generate X25519 key pair. */
1337	if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL)
1338		goto err;
1339	if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL)
1340		goto err;
1341	X25519_keypair(public_key, private_key);
1342
1343	/* Add the group and serialize the public key. */
1344	if (!CBB_add_u16(cbb, tls1_ec_nid2curve_id(NID_X25519)))
1345		goto err;
1346	if (!CBB_add_u16_length_prefixed(cbb, &key_exchange))
1347		goto err;
1348	if (!CBB_add_bytes(&key_exchange, public_key, X25519_KEY_LENGTH))
1349		goto err;
1350
1351	if (!CBB_flush(cbb))
1352		goto err;
1353
1354	S3I(s)->hs_tls13.x25519_public = public_key;
1355	S3I(s)->hs_tls13.x25519_private = private_key;
1356
1357	return 1;
1358
1359 err:
1360	freezero(public_key, X25519_KEY_LENGTH);
1361	freezero(private_key, X25519_KEY_LENGTH);
1362
1363	return 0;
1364}
1365
1366int
1367tlsext_keyshare_client_parse(SSL *s, CBS *cbs, int *alert)
1368{
1369	CBS key_exchange;
1370	uint16_t group;
1371	size_t out_len;
1372
1373	/* Unpack server share. */
1374	if (!CBS_get_u16(cbs, &group))
1375		goto err;
1376
1377	/* Handle other groups and verify that they're valid. */
1378	if (group != tls1_ec_nid2curve_id(NID_X25519))
1379		goto err;
1380
1381	if (!CBS_get_u16_length_prefixed(cbs, &key_exchange))
1382		goto err;
1383
1384	if (CBS_len(&key_exchange) != X25519_KEY_LENGTH)
1385		goto err;
1386
1387	if (!CBS_stow(&key_exchange, &S3I(s)->hs_tls13.x25519_peer_public,
1388	    &out_len))
1389		goto err;
1390
1391	return 1;
1392
1393 err:
1394	*alert = SSL_AD_DECODE_ERROR;
1395	return 0;
1396}
1397
1398/*
1399 * Supported Versions - RFC 8446 section 4.2.1.
1400 */
1401int
1402tlsext_versions_client_needs(SSL *s)
1403{
1404	if (SSL_IS_DTLS(s))
1405		return 0;
1406	return (S3I(s)->hs_tls13.max_version >= TLS1_3_VERSION);
1407}
1408
1409int
1410tlsext_versions_client_build(SSL *s, CBB *cbb)
1411{
1412	uint16_t max, min;
1413	uint16_t version;
1414	CBB versions;
1415
1416	max = S3I(s)->hs_tls13.max_version;
1417	min = S3I(s)->hs_tls13.min_version;
1418
1419	if (min < TLS1_VERSION)
1420		return 0;
1421
1422	if (!CBB_add_u8_length_prefixed(cbb, &versions))
1423		return 0;
1424
1425	/* XXX - fix, but contiguous for now... */
1426	for (version = max; version >= min; version--) {
1427		if (!CBB_add_u16(&versions, version))
1428			return 0;
1429	}
1430
1431	if (!CBB_flush(cbb))
1432		return 0;
1433
1434	return 1;
1435}
1436
1437int
1438tlsext_versions_server_parse(SSL *s, CBS *cbs, int *alert)
1439{
1440	CBS versions;
1441	uint16_t version;
1442	uint16_t max, min;
1443	uint16_t matched_version = 0;
1444
1445	max = S3I(s)->hs_tls13.max_version;
1446	min = S3I(s)->hs_tls13.min_version;
1447
1448	if (!CBS_get_u8_length_prefixed(cbs, &versions))
1449		goto err;
1450
1451	 while (CBS_len(&versions) > 0) {
1452		if (!CBS_get_u16(&versions, &version))
1453			goto err;
1454		/*
1455		 * XXX What is below implements client preference, and
1456		 * ignores any server preference entirely.
1457		 */
1458		if (matched_version == 0 && version >= min && version <= max)
1459			matched_version = version;
1460	}
1461
1462	/*
1463	 * XXX if we haven't matched a version we should
1464	 * fail - but we currently need to succeed to
1465	 * ignore this before the server code for 1.3
1466	 * is set up and initialized.
1467	 */
1468	if (max == 0)
1469		return 1; /* XXX */
1470
1471	if (matched_version != 0)  {
1472		s->version = matched_version;
1473		return 1;
1474	}
1475
1476	*alert = SSL_AD_PROTOCOL_VERSION;
1477	return 0;
1478
1479 err:
1480	*alert = SSL_AD_DECODE_ERROR;
1481	return 0;
1482}
1483
1484int
1485tlsext_versions_server_needs(SSL *s)
1486{
1487	return (!SSL_IS_DTLS(s) && s->version >= TLS1_3_VERSION);
1488}
1489
1490int
1491tlsext_versions_server_build(SSL *s, CBB *cbb)
1492{
1493	if (!CBB_add_u16(cbb, TLS1_3_VERSION))
1494		return 0;
1495	/* XXX set 1.2 in legacy version?  */
1496
1497	return 1;
1498}
1499
1500int
1501tlsext_versions_client_parse(SSL *s, CBS *cbs, int *alert)
1502{
1503	uint16_t selected_version;
1504
1505	if (!CBS_get_u16(cbs, &selected_version)) {
1506		*alert = SSL_AD_DECODE_ERROR;
1507		return 0;
1508	}
1509
1510	if (selected_version < TLS1_3_VERSION) {
1511		*alert = SSL_AD_ILLEGAL_PARAMETER;
1512		return 0;
1513	}
1514
1515	/* XXX test between min and max once initialization code goes in */
1516	S3I(s)->hs_tls13.server_version = selected_version;
1517
1518	return 1;
1519}
1520
1521
1522/*
1523 * Cookie - RFC 8446 section 4.2.2.
1524 */
1525
1526int
1527tlsext_cookie_client_needs(SSL *s)
1528{
1529	if (SSL_IS_DTLS(s))
1530		return 0;
1531	if (S3I(s)->hs_tls13.max_version < TLS1_3_VERSION)
1532		return 0;
1533	return (S3I(s)->hs_tls13.cookie_len > 0 &&
1534	    S3I(s)->hs_tls13.cookie != NULL);
1535}
1536
1537int
1538tlsext_cookie_client_build(SSL *s, CBB *cbb)
1539{
1540	CBB cookie;
1541
1542	if (!CBB_add_u16_length_prefixed(cbb, &cookie))
1543		return 0;
1544
1545	if (!CBB_add_bytes(&cookie, S3I(s)->hs_tls13.cookie,
1546	    S3I(s)->hs_tls13.cookie_len))
1547		return 0;
1548
1549	if (!CBB_flush(cbb))
1550		return 0;
1551
1552	return 1;
1553}
1554
1555int
1556tlsext_cookie_server_parse(SSL *s, CBS *cbs, int *alert)
1557{
1558	CBS cookie;
1559
1560	if (!CBS_get_u16_length_prefixed(cbs, &cookie))
1561		goto err;
1562
1563	if (CBS_len(&cookie) != S3I(s)->hs_tls13.cookie_len)
1564		goto err;
1565
1566	/*
1567	 * Check provided cookie value against what server previously
1568	 * sent - client *MUST* send the same cookie with new CR after
1569	 * a cookie is sent by the server with an HRR.
1570	 */
1571	if (!CBS_mem_equal(&cookie, S3I(s)->hs_tls13.cookie,
1572	    S3I(s)->hs_tls13.cookie_len)) {
1573		/* XXX special cookie mismatch alert? */
1574		*alert = SSL_AD_ILLEGAL_PARAMETER;
1575		return 0;
1576	}
1577
1578	return 1;
1579
1580 err:
1581	*alert = SSL_AD_DECODE_ERROR;
1582	return 0;
1583}
1584
1585int
1586tlsext_cookie_server_needs(SSL *s)
1587{
1588
1589	if (SSL_IS_DTLS(s))
1590		return 0;
1591	if (S3I(s)->hs_tls13.max_version < TLS1_3_VERSION)
1592		return 0;
1593	/*
1594	 * Server needs to set cookie value in tls13 handshake
1595	 * in order to send one, should only be sent with HRR.
1596	 */
1597	return (S3I(s)->hs_tls13.cookie_len > 0 &&
1598	    S3I(s)->hs_tls13.cookie != NULL);
1599}
1600
1601int
1602tlsext_cookie_server_build(SSL *s, CBB *cbb)
1603{
1604	CBB cookie;
1605
1606	/* XXX deduplicate with client code */
1607
1608	if (!CBB_add_u16_length_prefixed(cbb, &cookie))
1609		return 0;
1610
1611	if (!CBB_add_bytes(&cookie, S3I(s)->hs_tls13.cookie,
1612	    S3I(s)->hs_tls13.cookie_len))
1613		return 0;
1614
1615	if (!CBB_flush(cbb))
1616		return 0;
1617
1618	return 1;
1619}
1620
1621int
1622tlsext_cookie_client_parse(SSL *s, CBS *cbs, int *alert)
1623{
1624	CBS cookie;
1625
1626	/*
1627	 * XXX This currently assumes we will not get a second
1628	 * HRR from a server with a cookie to process after accepting
1629	 * one from the server in the same handshake
1630	 */
1631	if (S3I(s)->hs_tls13.cookie != NULL ||
1632	    S3I(s)->hs_tls13.cookie_len != 0) {
1633		*alert = SSL_AD_ILLEGAL_PARAMETER;
1634		return 0;
1635	}
1636
1637	if (!CBS_get_u16_length_prefixed(cbs, &cookie))
1638		goto err;
1639
1640	if (!CBS_stow(&cookie, &S3I(s)->hs_tls13.cookie,
1641	    &S3I(s)->hs_tls13.cookie_len))
1642		goto err;
1643
1644	return 1;
1645
1646 err:
1647	*alert = SSL_AD_DECODE_ERROR;
1648	return 0;
1649}
1650
1651struct tls_extension_funcs {
1652	int (*needs)(SSL *s);
1653	int (*build)(SSL *s, CBB *cbb);
1654	int (*parse)(SSL *s, CBS *cbs, int *alert);
1655};
1656
1657struct tls_extension {
1658	uint16_t type;
1659	uint16_t messages;
1660	struct tls_extension_funcs client;
1661	struct tls_extension_funcs server;
1662};
1663
1664static struct tls_extension tls_extensions[] = {
1665	{
1666		.type = TLSEXT_TYPE_supported_versions,
1667		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH |
1668		    SSL_TLSEXT_MSG_HRR,
1669		.client = {
1670			.needs = tlsext_versions_client_needs,
1671			.build = tlsext_versions_client_build,
1672			.parse = tlsext_versions_client_parse,
1673		},
1674		.server = {
1675			.needs = tlsext_versions_server_needs,
1676			.build = tlsext_versions_server_build,
1677			.parse = tlsext_versions_server_parse,
1678		},
1679	},
1680	{
1681		.type = TLSEXT_TYPE_key_share,
1682		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH |
1683		    SSL_TLSEXT_MSG_HRR,
1684		.client = {
1685			.needs = tlsext_keyshare_client_needs,
1686			.build = tlsext_keyshare_client_build,
1687			.parse = tlsext_keyshare_client_parse,
1688		},
1689		.server = {
1690			.needs = tlsext_keyshare_server_needs,
1691			.build = tlsext_keyshare_server_build,
1692			.parse = tlsext_keyshare_server_parse,
1693		},
1694	},
1695	{
1696		.type = TLSEXT_TYPE_server_name,
1697		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
1698		.client = {
1699			.needs = tlsext_sni_client_needs,
1700			.build = tlsext_sni_client_build,
1701			.parse = tlsext_sni_client_parse,
1702		},
1703		.server = {
1704			.needs = tlsext_sni_server_needs,
1705			.build = tlsext_sni_server_build,
1706			.parse = tlsext_sni_server_parse,
1707		},
1708	},
1709	{
1710		.type = TLSEXT_TYPE_renegotiate,
1711		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
1712		.client = {
1713			.needs = tlsext_ri_client_needs,
1714			.build = tlsext_ri_client_build,
1715			.parse = tlsext_ri_client_parse,
1716		},
1717		.server = {
1718			.needs = tlsext_ri_server_needs,
1719			.build = tlsext_ri_server_build,
1720			.parse = tlsext_ri_server_parse,
1721		},
1722	},
1723	{
1724		.type = TLSEXT_TYPE_status_request,
1725		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_CR |
1726		    SSL_TLSEXT_MSG_CT,
1727		.client = {
1728			.needs = tlsext_ocsp_client_needs,
1729			.build = tlsext_ocsp_client_build,
1730			.parse = tlsext_ocsp_client_parse,
1731		},
1732		.server = {
1733			.needs = tlsext_ocsp_server_needs,
1734			.build = tlsext_ocsp_server_build,
1735			.parse = tlsext_ocsp_server_parse,
1736		},
1737	},
1738	{
1739		.type = TLSEXT_TYPE_ec_point_formats,
1740		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
1741		.client = {
1742			.needs = tlsext_ecpf_client_needs,
1743			.build = tlsext_ecpf_client_build,
1744			.parse = tlsext_ecpf_client_parse,
1745		},
1746		.server = {
1747			.needs = tlsext_ecpf_server_needs,
1748			.build = tlsext_ecpf_server_build,
1749			.parse = tlsext_ecpf_server_parse,
1750		},
1751	},
1752	{
1753		.type = TLSEXT_TYPE_supported_groups,
1754		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
1755		.client = {
1756			.needs = tlsext_supportedgroups_client_needs,
1757			.build = tlsext_supportedgroups_client_build,
1758			.parse = tlsext_supportedgroups_client_parse,
1759		},
1760		.server = {
1761			.needs = tlsext_supportedgroups_server_needs,
1762			.build = tlsext_supportedgroups_server_build,
1763			.parse = tlsext_supportedgroups_server_parse,
1764		},
1765	},
1766	{
1767		.type = TLSEXT_TYPE_session_ticket,
1768		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH,
1769		.client = {
1770			.needs = tlsext_sessionticket_client_needs,
1771			.build = tlsext_sessionticket_client_build,
1772			.parse = tlsext_sessionticket_client_parse,
1773		},
1774		.server = {
1775			.needs = tlsext_sessionticket_server_needs,
1776			.build = tlsext_sessionticket_server_build,
1777			.parse = tlsext_sessionticket_server_parse,
1778		},
1779	},
1780	{
1781		.type = TLSEXT_TYPE_signature_algorithms,
1782		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_CR,
1783		.client = {
1784			.needs = tlsext_sigalgs_client_needs,
1785			.build = tlsext_sigalgs_client_build,
1786			.parse = tlsext_sigalgs_client_parse,
1787		},
1788		.server = {
1789			.needs = tlsext_sigalgs_server_needs,
1790			.build = tlsext_sigalgs_server_build,
1791			.parse = tlsext_sigalgs_server_parse,
1792		},
1793	},
1794	{
1795		.type = TLSEXT_TYPE_application_layer_protocol_negotiation,
1796		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_EE,
1797		.client = {
1798			.needs = tlsext_alpn_client_needs,
1799			.build = tlsext_alpn_client_build,
1800			.parse = tlsext_alpn_client_parse,
1801		},
1802		.server = {
1803			.needs = tlsext_alpn_server_needs,
1804			.build = tlsext_alpn_server_build,
1805			.parse = tlsext_alpn_server_parse,
1806		},
1807	},
1808	{
1809		.type = TLSEXT_TYPE_cookie,
1810		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_HRR,
1811		.client = {
1812			.needs = tlsext_cookie_client_needs,
1813			.build = tlsext_cookie_client_build,
1814			.parse = tlsext_cookie_client_parse,
1815		},
1816		.server = {
1817			.needs = tlsext_cookie_server_needs,
1818			.build = tlsext_cookie_server_build,
1819			.parse = tlsext_cookie_server_parse,
1820		},
1821	},
1822#ifndef OPENSSL_NO_SRTP
1823	{
1824		.type = TLSEXT_TYPE_use_srtp,
1825		.messages = SSL_TLSEXT_MSG_CH | SSL_TLSEXT_MSG_SH /* XXX */ |
1826		    SSL_TLSEXT_MSG_EE,
1827		.client = {
1828			.needs = tlsext_srtp_client_needs,
1829			.build = tlsext_srtp_client_build,
1830			.parse = tlsext_srtp_client_parse,
1831		},
1832		.server = {
1833			.needs = tlsext_srtp_server_needs,
1834			.build = tlsext_srtp_server_build,
1835			.parse = tlsext_srtp_server_parse,
1836		},
1837	}
1838#endif /* OPENSSL_NO_SRTP */
1839};
1840
1841#define N_TLS_EXTENSIONS (sizeof(tls_extensions) / sizeof(*tls_extensions))
1842
1843/* Ensure that extensions fit in a uint32_t bitmask. */
1844CTASSERT(N_TLS_EXTENSIONS <= (sizeof(uint32_t) * 8));
1845
1846struct tls_extension *
1847tls_extension_find(uint16_t type, size_t *tls_extensions_idx)
1848{
1849	size_t i;
1850
1851	for (i = 0; i < N_TLS_EXTENSIONS; i++) {
1852		if (tls_extensions[i].type == type) {
1853			*tls_extensions_idx = i;
1854			return &tls_extensions[i];
1855		}
1856	}
1857
1858	return NULL;
1859}
1860
1861int
1862tlsext_extension_seen(SSL *s, uint16_t type)
1863{
1864	size_t idx;
1865
1866	if (tls_extension_find(type, &idx) == NULL)
1867		return 0;
1868	return ((S3I(s)->hs.extensions_seen & (1 << idx)) != 0);
1869}
1870
1871static struct tls_extension_funcs *
1872tlsext_funcs(struct tls_extension *tlsext, int is_server)
1873{
1874	if (is_server)
1875		return &tlsext->server;
1876
1877	return &tlsext->client;
1878}
1879
1880static int
1881tlsext_build(SSL *s, CBB *cbb, int is_server, uint16_t msg_type)
1882{
1883	struct tls_extension_funcs *ext;
1884	struct tls_extension *tlsext;
1885	CBB extensions, extension_data;
1886	int extensions_present = 0;
1887	size_t i;
1888	uint16_t version;
1889
1890	if (is_server)
1891		version = s->version;
1892	else
1893		version = TLS1_get_client_version(s);
1894
1895	if (!CBB_add_u16_length_prefixed(cbb, &extensions))
1896		return 0;
1897
1898	for (i = 0; i < N_TLS_EXTENSIONS; i++) {
1899		tlsext = &tls_extensions[i];
1900		ext = tlsext_funcs(tlsext, is_server);
1901
1902		/* RFC 8446 Section 4.2 */
1903		if (version >= TLS1_3_VERSION &&
1904		    !(tlsext->messages & msg_type))
1905			continue;
1906
1907		if (!ext->needs(s))
1908			continue;
1909
1910		if (!CBB_add_u16(&extensions, tlsext->type))
1911			return 0;
1912		if (!CBB_add_u16_length_prefixed(&extensions, &extension_data))
1913			return 0;
1914
1915		if (!ext->build(s, &extension_data))
1916			return 0;
1917
1918		extensions_present = 1;
1919	}
1920
1921	if (!extensions_present)
1922		CBB_discard_child(cbb);
1923
1924	if (!CBB_flush(cbb))
1925		return 0;
1926
1927	return 1;
1928}
1929
1930static int
1931tlsext_parse(SSL *s, CBS *cbs, int *alert, int is_server, uint16_t msg_type)
1932{
1933	struct tls_extension_funcs *ext;
1934	struct tls_extension *tlsext;
1935	CBS extensions, extension_data;
1936	uint16_t type;
1937	size_t idx;
1938	uint16_t version;
1939	int alert_desc;
1940
1941	S3I(s)->hs.extensions_seen = 0;
1942
1943	if (is_server)
1944		version = s->version;
1945	else
1946		version = TLS1_get_client_version(s);
1947
1948	/* An empty extensions block is valid. */
1949	if (CBS_len(cbs) == 0)
1950		return 1;
1951
1952	alert_desc = SSL_AD_DECODE_ERROR;
1953
1954	if (!CBS_get_u16_length_prefixed(cbs, &extensions))
1955		goto err;
1956
1957	while (CBS_len(&extensions) > 0) {
1958		if (!CBS_get_u16(&extensions, &type))
1959			goto err;
1960		if (!CBS_get_u16_length_prefixed(&extensions, &extension_data))
1961			goto err;
1962
1963		if (s->internal->tlsext_debug_cb != NULL)
1964			s->internal->tlsext_debug_cb(s, is_server, type,
1965			    (unsigned char *)CBS_data(&extension_data),
1966			    CBS_len(&extension_data),
1967			    s->internal->tlsext_debug_arg);
1968
1969		/* Unknown extensions are ignored. */
1970		if ((tlsext = tls_extension_find(type, &idx)) == NULL)
1971			continue;
1972
1973		/* RFC 8446 Section 4.2 */
1974		if (version >= TLS1_3_VERSION &&
1975		    !(tlsext->messages & msg_type)) {
1976			alert_desc = SSL_AD_ILLEGAL_PARAMETER;
1977			goto err;
1978		}
1979
1980		/* Check for duplicate known extensions. */
1981		if ((S3I(s)->hs.extensions_seen & (1 << idx)) != 0)
1982			goto err;
1983		S3I(s)->hs.extensions_seen |= (1 << idx);
1984
1985		ext = tlsext_funcs(tlsext, is_server);
1986		if (!ext->parse(s, &extension_data, &alert_desc))
1987			goto err;
1988
1989		if (CBS_len(&extension_data) != 0)
1990			goto err;
1991	}
1992
1993	return 1;
1994
1995 err:
1996	*alert = alert_desc;
1997
1998	return 0;
1999}
2000
2001static void
2002tlsext_server_reset_state(SSL *s)
2003{
2004	s->internal->servername_done = 0;
2005	s->tlsext_status_type = -1;
2006	S3I(s)->renegotiate_seen = 0;
2007	free(S3I(s)->alpn_selected);
2008	S3I(s)->alpn_selected = NULL;
2009	s->internal->srtp_profile = NULL;
2010}
2011
2012int
2013tlsext_server_build(SSL *s, CBB *cbb, uint16_t msg_type)
2014{
2015	return tlsext_build(s, cbb, 1, msg_type);
2016}
2017
2018int
2019tlsext_server_parse(SSL *s, CBS *cbs, int *alert, uint16_t msg_type)
2020{
2021	/* XXX - this possibly should be done by the caller... */
2022	tlsext_server_reset_state(s);
2023
2024	return tlsext_parse(s, cbs, alert, 1, msg_type);
2025}
2026
2027static void
2028tlsext_client_reset_state(SSL *s)
2029{
2030	S3I(s)->renegotiate_seen = 0;
2031	free(S3I(s)->alpn_selected);
2032	S3I(s)->alpn_selected = NULL;
2033}
2034
2035int
2036tlsext_client_build(SSL *s, CBB *cbb, uint16_t msg_type)
2037{
2038	return tlsext_build(s, cbb, 0, msg_type);
2039}
2040
2041int
2042tlsext_client_parse(SSL *s, CBS *cbs, int *alert, uint16_t msg_type)
2043{
2044	/* XXX - this possibly should be done by the caller... */
2045	tlsext_client_reset_state(s);
2046
2047	return tlsext_parse(s, cbs, alert, 0, msg_type);
2048}
2049