]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/extensions.c
Fix CertificateCompressionAlgorithm to be read as 2-octet-wide
[thirdparty/openssl.git] / ssl / statem / extensions.c
CommitLineData
6b473aca 1/*
fecb3aae 2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
6b473aca 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
6b473aca
MC
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
650c6687
RB
10#if defined(__TANDEM) && defined(_SPT_MODEL_)
11# include <spthread.h>
12# include <spt_extensions.h> /* timeval */
13#endif
14
f6370040 15#include <string.h>
677963e5 16#include "internal/nelem.h"
88050dd1 17#include "internal/cryptlib.h"
706457b7
DMSP
18#include "../ssl_local.h"
19#include "statem_local.h"
6b473aca 20
38b051a1
TM
21static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent);
22static int init_server_name(SSL_CONNECTION *s, unsigned int context);
23static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent);
24static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
25 int sent);
26static int init_session_ticket(SSL_CONNECTION *s, unsigned int context);
8f8c11d8 27#ifndef OPENSSL_NO_OCSP
38b051a1 28static int init_status_request(SSL_CONNECTION *s, unsigned int context);
8f8c11d8 29#endif
805a2e9e 30#ifndef OPENSSL_NO_NEXTPROTONEG
38b051a1 31static int init_npn(SSL_CONNECTION *s, unsigned int context);
805a2e9e 32#endif
38b051a1
TM
33static int init_alpn(SSL_CONNECTION *s, unsigned int context);
34static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent);
35static int init_sig_algs_cert(SSL_CONNECTION *s, unsigned int context);
36static int init_sig_algs(SSL_CONNECTION *s, unsigned int context);
37static int init_certificate_authorities(SSL_CONNECTION *s,
38 unsigned int context);
39static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
40 WPACKET *pkt,
b186a592
MC
41 unsigned int context,
42 X509 *x,
f63a17d6 43 size_t chainidx);
38b051a1 44static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
45615c5f 45 unsigned int context, X509 *x,
f63a17d6 46 size_t chainidx);
805a2e9e 47#ifndef OPENSSL_NO_SRP
38b051a1 48static int init_srp(SSL_CONNECTION *s, unsigned int context);
805a2e9e 49#endif
38b051a1
TM
50static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context);
51static int init_etm(SSL_CONNECTION *s, unsigned int context);
52static int init_ems(SSL_CONNECTION *s, unsigned int context);
53static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent);
54static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context);
55static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent);
805a2e9e 56#ifndef OPENSSL_NO_SRTP
38b051a1 57static int init_srtp(SSL_CONNECTION *s, unsigned int context);
805a2e9e 58#endif
38b051a1
TM
59static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent);
60static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent);
61static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
62 int sent);
63static int init_post_handshake_auth(SSL_CONNECTION *s, unsigned int context);
64static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent);
b67cb09f
TS
65static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context);
66static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
67 unsigned int context,
68 X509 *x, size_t chainidx);
69static int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt,
70 unsigned int context,
71 X509 *x, size_t chainidx);
9d75dce3 72
70af3d8e 73/* Structure to define a built-in extension */
1266eefd
MC
74typedef struct extensions_definition_st {
75 /* The defined type for the extension */
6b473aca 76 unsigned int type;
1266eefd
MC
77 /*
78 * The context that this extension applies to, e.g. what messages and
79 * protocol versions
80 */
81 unsigned int context;
68db4dda 82 /*
805a2e9e
MC
83 * Initialise extension before parsing. Always called for relevant contexts
84 * even if extension not present
68db4dda 85 */
38b051a1 86 int (*init)(SSL_CONNECTION *s, unsigned int context);
1266eefd 87 /* Parse extension sent from client to server */
38b051a1
TM
88 int (*parse_ctos)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
89 X509 *x, size_t chainidx);
1266eefd 90 /* Parse extension send from server to client */
38b051a1
TM
91 int (*parse_stoc)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context,
92 X509 *x, size_t chainidx);
1266eefd 93 /* Construct extension sent from server to client */
38b051a1
TM
94 EXT_RETURN (*construct_stoc)(SSL_CONNECTION *s, WPACKET *pkt,
95 unsigned int context,
f63a17d6 96 X509 *x, size_t chainidx);
1266eefd 97 /* Construct extension sent from client to server */
38b051a1
TM
98 EXT_RETURN (*construct_ctos)(SSL_CONNECTION *s, WPACKET *pkt,
99 unsigned int context,
f63a17d6 100 X509 *x, size_t chainidx);
68db4dda 101 /*
805a2e9e
MC
102 * Finalise extension after parsing. Always called where an extensions was
103 * initialised even if the extension was not present. |sent| is set to 1 if
104 * the extension was seen, or 0 otherwise.
68db4dda 105 */
38b051a1 106 int (*final)(SSL_CONNECTION *s, unsigned int context, int sent);
6b473aca
MC
107} EXTENSION_DEFINITION;
108
4b299b8e 109/*
70af3d8e 110 * Definitions of all built-in extensions. NOTE: Changes in the number or order
bd91e3c8 111 * of these extensions should be mirrored with equivalent changes to the
706457b7 112 * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
ac44deaf
TS
113 * Extensions should be added to test/ext_internal_test.c as well, as that
114 * tests the ordering of the extensions.
115 *
70af3d8e
MC
116 * Each extension has an initialiser, a client and
117 * server side parser and a finaliser. The initialiser is called (if the
118 * extension is relevant to the given context) even if we did not see the
119 * extension in the message that we received. The parser functions are only
120 * called if we see the extension in the message. The finalisers are always
121 * called if the initialiser was called.
122 * There are also server and client side constructor functions which are always
123 * called during message construction if the extension is relevant for the
124 * given context.
125 * The initialisation, parsing, finalisation and construction functions are
126 * always called in the order defined in this list. Some extensions may depend
127 * on others having been processed first, so the order of this list is
128 * significant.
129 * The extension context is defined by a series of flags which specify which
130 * messages the extension is relevant to. These flags also specify whether the
3e6c1da8 131 * extension is relevant to a particular protocol or protocol version.
a1448c26 132 *
10ed1b72
TS
133 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
134 * the end, keep these extensions before signature_algorithm.
4b299b8e 135 */
ac44deaf 136#define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, NULL, NULL, NULL, NULL, NULL, NULL }
6b473aca
MC
137static const EXTENSION_DEFINITION ext_defs[] = {
138 {
139 TLSEXT_TYPE_renegotiate,
fe874d27
MC
140 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
141 | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
142 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
143 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
144 final_renegotiate
6b473aca
MC
145 },
146 {
147 TLSEXT_TYPE_server_name,
fe874d27
MC
148 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
149 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
1266eefd
MC
150 init_server_name,
151 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
152 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
153 final_server_name
6b473aca 154 },
cf72c757
F
155 {
156 TLSEXT_TYPE_max_fragment_length,
157 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
158 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
159 NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
160 tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
161 final_maxfragmentlen
162 },
6b473aca
MC
163#ifndef OPENSSL_NO_SRP
164 {
165 TLSEXT_TYPE_srp,
fe874d27 166 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd 167 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
6b473aca 168 },
0785274c
MC
169#else
170 INVALID_EXTENSION,
6b473aca 171#endif
6b473aca
MC
172 {
173 TLSEXT_TYPE_ec_point_formats,
fe874d27
MC
174 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
175 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
3f987381 176 init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
1266eefd
MC
177 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
178 final_ec_pt_formats
6b473aca
MC
179 },
180 {
7bc2bddb
BK
181 /*
182 * "supported_groups" is spread across several specifications.
183 * It was originally specified as "elliptic_curves" in RFC 4492,
184 * and broadened to include named FFDH groups by RFC 7919.
185 * Both RFCs 4492 and 7919 do not include a provision for the server
186 * to indicate to the client the complete list of groups supported
187 * by the server, with the server instead just indicating the
188 * selected group for this connection in the ServerKeyExchange
189 * message. TLS 1.3 adds a scheme for the server to indicate
190 * to the client its list of supported groups in the
191 * EncryptedExtensions message, but none of the relevant
192 * specifications permit sending supported_groups in the ServerHello.
193 * Nonetheless (possibly due to the close proximity to the
194 * "ec_point_formats" extension, which is allowed in the ServerHello),
195 * there are several servers that send this extension in the
196 * ServerHello anyway. Up to and including the 1.1.0 release,
197 * we did not check for the presence of nonpermitted extensions,
198 * so to avoid a regression, we must permit this extension in the
199 * TLS 1.2 ServerHello as well.
200 *
201 * Note that there is no tls_parse_stoc_supported_groups function,
202 * so we do not perform any additional parsing, validation, or
203 * processing on the server's group list -- this is just a minimal
204 * change to preserve compatibility with these misbehaving servers.
205 */
6b473aca 206 TLSEXT_TYPE_supported_groups,
7bc2bddb
BK
207 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
208 | SSL_EXT_TLS1_2_SERVER_HELLO,
1266eefd 209 NULL, tls_parse_ctos_supported_groups, NULL,
6af87546 210 tls_construct_stoc_supported_groups,
1266eefd 211 tls_construct_ctos_supported_groups, NULL
6b473aca 212 },
6b473aca
MC
213 {
214 TLSEXT_TYPE_session_ticket,
fe874d27
MC
215 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
216 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
217 init_session_ticket, tls_parse_ctos_session_ticket,
218 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
219 tls_construct_ctos_session_ticket, NULL
6b473aca 220 },
ab83e314 221#ifndef OPENSSL_NO_OCSP
6b473aca
MC
222 {
223 TLSEXT_TYPE_status_request,
fe874d27 224 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
5de683d2 225 | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
1266eefd
MC
226 init_status_request, tls_parse_ctos_status_request,
227 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
f63e4288 228 tls_construct_ctos_status_request, NULL
6b473aca 229 },
0785274c
MC
230#else
231 INVALID_EXTENSION,
ab83e314 232#endif
6b473aca
MC
233#ifndef OPENSSL_NO_NEXTPROTONEG
234 {
235 TLSEXT_TYPE_next_proto_neg,
fe874d27
MC
236 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
237 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
238 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
239 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
6b473aca 240 },
0785274c
MC
241#else
242 INVALID_EXTENSION,
6b473aca
MC
243#endif
244 {
02f0274e
MC
245 /*
246 * Must appear in this list after server_name so that finalisation
247 * happens after server_name callbacks
248 */
6b473aca 249 TLSEXT_TYPE_application_layer_protocol_negotiation,
fe874d27
MC
250 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
251 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
1266eefd 252 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
630369d9 253 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
6b473aca 254 },
7da160b0 255#ifndef OPENSSL_NO_SRTP
6b473aca
MC
256 {
257 TLSEXT_TYPE_use_srtp,
fe874d27
MC
258 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
259 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
1266eefd
MC
260 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
261 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
6b473aca 262 },
0785274c
MC
263#else
264 INVALID_EXTENSION,
7da160b0 265#endif
6b473aca
MC
266 {
267 TLSEXT_TYPE_encrypt_then_mac,
fe874d27
MC
268 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
269 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
270 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
271 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
6b473aca 272 },
6dd083fd 273#ifndef OPENSSL_NO_CT
6b473aca
MC
274 {
275 TLSEXT_TYPE_signed_certificate_timestamp,
fe874d27 276 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
5de683d2 277 | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
68db4dda 278 NULL,
6b473aca
MC
279 /*
280 * No server side support for this, but can be provided by a custom
281 * extension. This is an exception to the rule that custom extensions
282 * cannot override built in ones.
283 */
1266eefd 284 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
6b473aca 285 },
0785274c
MC
286#else
287 INVALID_EXTENSION,
6dd083fd 288#endif
6b473aca
MC
289 {
290 TLSEXT_TYPE_extended_master_secret,
fe874d27
MC
291 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
292 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
293 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
294 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
6b473aca 295 },
c589c34e
BK
296 {
297 TLSEXT_TYPE_signature_algorithms_cert,
298 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
299 init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
300 tls_parse_ctos_sig_algs_cert,
301 /* We do not generate signature_algorithms_cert at present. */
302 NULL, NULL, NULL
303 },
9d75dce3
TS
304 {
305 TLSEXT_TYPE_post_handshake_auth,
306 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
307 init_post_handshake_auth,
308 tls_parse_ctos_post_handshake_auth, NULL,
309 NULL, tls_construct_ctos_post_handshake_auth,
310 NULL,
311 },
10ed1b72
TS
312 {
313 TLSEXT_TYPE_signature_algorithms,
314 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
315 init_sig_algs, tls_parse_ctos_sig_algs,
316 tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
317 tls_construct_ctos_sig_algs, final_sig_algs
318 },
6b473aca
MC
319 {
320 TLSEXT_TYPE_supported_versions,
27e462f1
MC
321 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
322 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY,
68db4dda 323 NULL,
6b473aca 324 /* Processed inline as part of version selection */
88050dd1
MC
325 NULL, tls_parse_stoc_supported_versions,
326 tls_construct_stoc_supported_versions,
327 tls_construct_ctos_supported_versions, NULL
6b473aca 328 },
b2f7e8c0 329 {
b2f7e8c0 330 TLSEXT_TYPE_psk_kex_modes,
fe874d27
MC
331 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
332 | SSL_EXT_TLS1_3_ONLY,
b2f7e8c0
MC
333 init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
334 tls_construct_ctos_psk_kex_modes, NULL
335 },
6b473aca 336 {
70af3d8e
MC
337 /*
338 * Must be in this list after supported_groups. We need that to have
339 * been parsed before we do this one.
340 */
6b473aca 341 TLSEXT_TYPE_key_share,
fe874d27
MC
342 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
343 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
344 | SSL_EXT_TLS1_3_ONLY,
1266eefd 345 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
f4bbb37c
MC
346 tls_construct_stoc_key_share, tls_construct_ctos_key_share,
347 final_key_share
7da160b0 348 },
cfef5027 349 {
97ea1e7f 350 /* Must be after key_share */
cfef5027 351 TLSEXT_TYPE_cookie,
fe874d27
MC
352 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
353 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
43054d3d
MC
354 NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
355 tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL
cfef5027 356 },
7da160b0
MC
357 {
358 /*
359 * Special unsolicited ServerHello extension only used when
23fed8ba
MC
360 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
361 * ignore it.
7da160b0
MC
362 */
363 TLSEXT_TYPE_cryptopro_bug,
23fed8ba
MC
364 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
365 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd 366 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
ab83e314 367 },
b67cb09f
TS
368 {
369 TLSEXT_TYPE_compress_certificate,
370 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
371 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
372 tls_init_compress_certificate,
373 tls_parse_compress_certificate, tls_parse_compress_certificate,
374 tls_construct_compress_certificate, tls_construct_compress_certificate,
375 NULL
376 },
38df5a45
MC
377 {
378 TLSEXT_TYPE_early_data,
fe874d27 379 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
6e99ae58 380 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
38df5a45
MC
381 NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
382 tls_construct_stoc_early_data, tls_construct_ctos_early_data,
383 final_early_data
384 },
45615c5f
DSH
385 {
386 TLSEXT_TYPE_certificate_authorities,
fe874d27
MC
387 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
388 | SSL_EXT_TLS1_3_ONLY,
45615c5f
DSH
389 init_certificate_authorities,
390 tls_parse_certificate_authorities, tls_parse_certificate_authorities,
391 tls_construct_certificate_authorities,
392 tls_construct_certificate_authorities, NULL,
393 },
ab83e314 394 {
ec15acb6 395 /* Must be immediately before pre_shared_key */
ab83e314 396 TLSEXT_TYPE_padding,
fe874d27 397 SSL_EXT_CLIENT_HELLO,
68db4dda 398 NULL,
ab83e314 399 /* We send this, but don't read it */
1266eefd 400 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
ec15acb6
MC
401 },
402 {
403 /* Required by the TLSv1.3 spec to always be the last extension */
404 TLSEXT_TYPE_psk,
fe874d27
MC
405 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
406 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
0247086d 407 NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
efe0f315 408 tls_construct_ctos_psk, final_psk
6b473aca
MC
409 }
410};
411
ac44deaf
TS
412/* Returns a TLSEXT_TYPE for the given index */
413unsigned int ossl_get_extension_type(size_t idx)
414{
415 size_t num_exts = OSSL_NELEM(ext_defs);
416
417 if (idx >= num_exts)
418 return TLSEXT_TYPE_out_of_range;
419
420 return ext_defs[idx].type;
421}
422
43ae5eed 423/* Check whether an extension's context matches the current context */
38b051a1
TM
424static int validate_context(SSL_CONNECTION *s, unsigned int extctx,
425 unsigned int thisctx)
43ae5eed
MC
426{
427 /* Check we're allowed to use this extension in this context */
428 if ((thisctx & extctx) == 0)
429 return 0;
430
38b051a1 431 if (SSL_CONNECTION_IS_DTLS(s)) {
43ae5eed
MC
432 if ((extctx & SSL_EXT_TLS_ONLY) != 0)
433 return 0;
434 } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
435 return 0;
436 }
437
438 return 1;
439}
440
38b051a1
TM
441int tls_validate_all_contexts(SSL_CONNECTION *s, unsigned int thisctx,
442 RAW_EXTENSION *exts)
88050dd1
MC
443{
444 size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
445 RAW_EXTENSION *thisext;
446 unsigned int context;
447 ENDPOINT role = ENDPOINT_BOTH;
448
449 if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
450 role = ENDPOINT_SERVER;
451 else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
452 role = ENDPOINT_CLIENT;
453
454 /* Calculate the number of extensions in the extensions list */
455 num_exts = builtin_num + s->cert->custext.meths_count;
456
457 for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
458 if (!thisext->present)
459 continue;
460
461 if (i < builtin_num) {
462 context = ext_defs[i].context;
463 } else {
464 custom_ext_method *meth = NULL;
465
466 meth = custom_ext_find(&s->cert->custext, role, thisext->type,
467 &offset);
468 if (!ossl_assert(meth != NULL))
469 return 0;
470 context = meth->context;
471 }
472
473 if (!validate_context(s, context, thisctx))
474 return 0;
475 }
476
477 return 1;
478}
479
6b473aca
MC
480/*
481 * Verify whether we are allowed to use the extension |type| in the current
482 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
70af3d8e 483 * indicate the extension is not allowed. If returning 1 then |*found| is set to
69687aa8 484 * the definition for the extension we found.
6b473aca 485 */
38b051a1
TM
486static int verify_extension(SSL_CONNECTION *s, unsigned int context,
487 unsigned int type, custom_ext_methods *meths,
488 RAW_EXTENSION *rawexlist, RAW_EXTENSION **found)
6b473aca
MC
489{
490 size_t i;
70af3d8e 491 size_t builtin_num = OSSL_NELEM(ext_defs);
d270de32 492 const EXTENSION_DEFINITION *thisext;
6b473aca 493
1266eefd
MC
494 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
495 if (type == thisext->type) {
43ae5eed 496 if (!validate_context(s, thisext->context, context))
6b473aca
MC
497 return 0;
498
1266eefd 499 *found = &rawexlist[i];
6b473aca
MC
500 return 1;
501 }
502 }
503
70af3d8e
MC
504 /* Check the custom extensions */
505 if (meths != NULL) {
43ae5eed 506 size_t offset = 0;
787d9ec7 507 ENDPOINT role = ENDPOINT_BOTH;
43ae5eed
MC
508 custom_ext_method *meth = NULL;
509
510 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
787d9ec7 511 role = ENDPOINT_SERVER;
43ae5eed 512 else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
787d9ec7 513 role = ENDPOINT_CLIENT;
43ae5eed 514
787d9ec7 515 meth = custom_ext_find(meths, role, type, &offset);
43ae5eed
MC
516 if (meth != NULL) {
517 if (!validate_context(s, meth->context, context))
518 return 0;
519 *found = &rawexlist[offset + builtin_num];
520 return 1;
6b473aca
MC
521 }
522 }
523
70af3d8e 524 /* Unknown extension. We allow it */
1266eefd 525 *found = NULL;
70af3d8e 526 return 1;
6b473aca
MC
527}
528
70af3d8e
MC
529/*
530 * Check whether the context defined for an extension |extctx| means whether
531 * the extension is relevant for the current context |thisctx| or not. Returns
532 * 1 if the extension is relevant for this context, and 0 otherwise
533 */
38b051a1
TM
534int extension_is_relevant(SSL_CONNECTION *s, unsigned int extctx,
535 unsigned int thisctx)
805a2e9e 536{
a2b97bdf
MC
537 int is_tls13;
538
539 /*
540 * For HRR we haven't selected the version yet but we know it will be
541 * TLSv1.3
542 */
543 if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
544 is_tls13 = 1;
545 else
38b051a1 546 is_tls13 = SSL_CONNECTION_IS_TLS13(s);
a2b97bdf 547
38b051a1 548 if ((SSL_CONNECTION_IS_DTLS(s)
fe874d27 549 && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
805a2e9e 550 || (s->version == SSL3_VERSION
fe874d27 551 && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
ee36b963
BK
552 /*
553 * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
554 * which is never true when generating the ClientHello.
555 * However, version negotiation *has* occurred by the time the
556 * ClientHello extensions are being parsed.
557 * Be careful to allow TLS 1.3-only extensions when generating
558 * the ClientHello.
559 */
a2b97bdf 560 || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
ee36b963
BK
561 || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
562 && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
563 || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
43ae5eed 564 || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
805a2e9e 565 return 0;
805a2e9e
MC
566 return 1;
567}
568
6b473aca
MC
569/*
570 * Gather a list of all the extensions from the data in |packet]. |context|
70af3d8e 571 * tells us which message this extension is for. The raw extension data is
29bfd5b7
MC
572 * stored in |*res| on success. We don't actually process the content of the
573 * extensions yet, except to check their types. This function also runs the
574 * initialiser functions for all known extensions if |init| is nonzero (whether
575 * we have collected them or not). If successful the caller is responsible for
576 * freeing the contents of |*res|.
6b473aca
MC
577 *
578 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
579 * more than one extension of the same type in a ClientHello or ServerHello.
580 * This function returns 1 if all extensions are unique and we have parsed their
581 * types, and 0 if the extensions contain duplicates, could not be successfully
1266eefd 582 * found, or an internal error occurred. We only check duplicates for
70af3d8e 583 * extensions that we know about. We ignore others.
6b473aca 584 */
38b051a1
TM
585int tls_collect_extensions(SSL_CONNECTION *s, PACKET *packet,
586 unsigned int context,
f63a17d6 587 RAW_EXTENSION **res, size_t *len, int init)
6b473aca
MC
588{
589 PACKET extensions = *packet;
d270de32 590 size_t i = 0;
fc5ece2e 591 size_t num_exts;
43ae5eed 592 custom_ext_methods *exts = &s->cert->custext;
6b473aca 593 RAW_EXTENSION *raw_extensions = NULL;
d270de32 594 const EXTENSION_DEFINITION *thisexd;
6b473aca 595
ecc2f938
MC
596 *res = NULL;
597
70af3d8e
MC
598 /*
599 * Initialise server side custom extensions. Client side is done during
600 * construction of extensions for the ClientHello.
601 */
43ae5eed
MC
602 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
603 custom_ext_init(&s->cert->custext);
70af3d8e 604
fc5ece2e
BK
605 num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
606 raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
70af3d8e 607 if (raw_extensions == NULL) {
e077455e 608 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB);
70af3d8e
MC
609 return 0;
610 }
611
193b5d76 612 i = 0;
6b473aca 613 while (PACKET_remaining(&extensions) > 0) {
b186a592 614 unsigned int type, idx;
6b473aca 615 PACKET extension;
1266eefd 616 RAW_EXTENSION *thisex;
6b473aca
MC
617
618 if (!PACKET_get_net_2(&extensions, &type) ||
619 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
c48ffbcc 620 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
6b473aca
MC
621 goto err;
622 }
70af3d8e
MC
623 /*
624 * Verify this extension is allowed. We only check duplicates for
652a6b7e
MC
625 * extensions that we recognise. We also have a special case for the
626 * PSK extension, which must be the last one in the ClientHello.
70af3d8e 627 */
1266eefd 628 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
652a6b7e
MC
629 || (thisex != NULL && thisex->present == 1)
630 || (type == TLSEXT_TYPE_psk
fe874d27 631 && (context & SSL_EXT_CLIENT_HELLO) != 0
652a6b7e 632 && PACKET_remaining(&extensions) != 0)) {
c48ffbcc 633 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
6b473aca
MC
634 goto err;
635 }
b186a592
MC
636 idx = thisex - raw_extensions;
637 /*-
638 * Check that we requested this extension (if appropriate). Requests can
639 * be sent in the ClientHello and CertificateRequest. Unsolicited
640 * extensions can be sent in the NewSessionTicket. We only do this for
641 * the built-in extensions. Custom extensions have a different but
642 * similar check elsewhere.
643 * Special cases:
644 * - The HRR cookie extension is unsolicited
645 * - The renegotiate extension is unsolicited (the client signals
646 * support via an SCSV)
647 * - The signed_certificate_timestamp extension can be provided by a
648 * custom extension or by the built-in version. We let the extension
649 * itself handle unsolicited response checks.
650 */
651 if (idx < OSSL_NELEM(ext_defs)
652 && (context & (SSL_EXT_CLIENT_HELLO
653 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
654 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
655 && type != TLSEXT_TYPE_cookie
656 && type != TLSEXT_TYPE_renegotiate
657 && type != TLSEXT_TYPE_signed_certificate_timestamp
673e0bbb
DB
658 && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
659#ifndef OPENSSL_NO_GOST
660 && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
661 && type == TLSEXT_TYPE_cryptopro_bug)
662#endif
dd6b2706 663 ) {
f63a17d6 664 SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
c48ffbcc 665 SSL_R_UNSOLICITED_EXTENSION);
b186a592
MC
666 goto err;
667 }
1266eefd
MC
668 if (thisex != NULL) {
669 thisex->data = extension;
670 thisex->present = 1;
671 thisex->type = type;
193b5d76 672 thisex->received_order = i++;
b93a295a 673 if (s->ext.debug_cb)
38b051a1
TM
674 s->ext.debug_cb(SSL_CONNECTION_GET_SSL(s), !s->server,
675 thisex->type, PACKET_data(&thisex->data),
b93a295a
TS
676 PACKET_remaining(&thisex->data),
677 s->ext.debug_arg);
6b473aca
MC
678 }
679 }
680
735d5b59
TT
681 if (init) {
682 /*
683 * Initialise all known extensions relevant to this context,
684 * whether we have found them or not
685 */
686 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
687 i++, thisexd++) {
bf5c84f5
TT
688 if (thisexd->init != NULL && (thisexd->context & context) != 0
689 && extension_is_relevant(s, thisexd->context, context)
690 && !thisexd->init(s, context)) {
f63a17d6 691 /* SSLfatal() already called */
735d5b59
TT
692 goto err;
693 }
68db4dda
MC
694 }
695 }
696
6b473aca 697 *res = raw_extensions;
fc5ece2e
BK
698 if (len != NULL)
699 *len = num_exts;
6b473aca
MC
700 return 1;
701
702 err:
703 OPENSSL_free(raw_extensions);
704 return 0;
705}
706
68db4dda 707/*
70af3d8e
MC
708 * Runs the parser for a given extension with index |idx|. |exts| contains the
709 * list of all parsed extensions previously collected by
710 * tls_collect_extensions(). The parser is only run if it is applicable for the
f97d4c37
MC
711 * given |context| and the parser has not already been run. If this is for a
712 * Certificate message, then we also provide the parser with the relevant
8521ced6 713 * Certificate |x| and its position in the |chainidx| with 0 being the first
29bfd5b7
MC
714 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
715 * present this counted as success.
68db4dda 716 */
38b051a1 717int tls_parse_extension(SSL_CONNECTION *s, TLSEXT_INDEX idx, int context,
f63a17d6 718 RAW_EXTENSION *exts, X509 *x, size_t chainidx)
6b473aca 719{
70af3d8e 720 RAW_EXTENSION *currext = &exts[idx];
38b051a1 721 int (*parser)(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, X509 *x,
f63a17d6 722 size_t chainidx) = NULL;
6b473aca 723
70af3d8e
MC
724 /* Skip if the extension is not present */
725 if (!currext->present)
726 return 1;
6b473aca 727
70af3d8e
MC
728 /* Skip if we've already parsed this extension */
729 if (currext->parsed)
730 return 1;
6b473aca 731
70af3d8e
MC
732 currext->parsed = 1;
733
734 if (idx < OSSL_NELEM(ext_defs)) {
735 /* We are handling a built-in extension */
736 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
737
738 /* Check if extension is defined for our protocol. If not, skip */
739 if (!extension_is_relevant(s, extdef->context, context))
740 return 1;
741
1266eefd 742 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
224135e9 743
1266eefd 744 if (parser != NULL)
f63a17d6 745 return parser(s, &currext->data, context, x, chainidx);
6b473aca 746
70af3d8e
MC
747 /*
748 * If the parser is NULL we fall through to the custom extension
749 * processing
750 */
6b473aca
MC
751 }
752
43ae5eed 753 /* Parse custom extensions */
f63a17d6
MC
754 return custom_ext_parse(s, context, currext->type,
755 PACKET_data(&currext->data),
756 PACKET_remaining(&currext->data),
757 x, chainidx);
805a2e9e
MC
758}
759
760/*
761 * Parse all remaining extensions that have not yet been parsed. Also calls the
735d5b59
TT
762 * finalisation for all extensions at the end if |fin| is nonzero, whether we
763 * collected them or not. Returns 1 for success or 0 for failure. If we are
764 * working on a Certificate message then we also pass the Certificate |x| and
29bfd5b7 765 * its position in the |chainidx|, with 0 being the first certificate.
805a2e9e 766 */
38b051a1
TM
767int tls_parse_all_extensions(SSL_CONNECTION *s, int context,
768 RAW_EXTENSION *exts, X509 *x,
f63a17d6 769 size_t chainidx, int fin)
805a2e9e 770{
1266eefd 771 size_t i, numexts = OSSL_NELEM(ext_defs);
d270de32 772 const EXTENSION_DEFINITION *thisexd;
805a2e9e 773
70af3d8e 774 /* Calculate the number of extensions in the extensions list */
43ae5eed 775 numexts += s->cert->custext.meths_count;
70af3d8e
MC
776
777 /* Parse each extension in turn */
1266eefd 778 for (i = 0; i < numexts; i++) {
f63a17d6
MC
779 if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
780 /* SSLfatal() already called */
70af3d8e 781 return 0;
f63a17d6 782 }
70af3d8e 783 }
805a2e9e 784
735d5b59
TT
785 if (fin) {
786 /*
787 * Finalise all known extensions relevant to this context,
788 * whether we have found them or not
789 */
790 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
791 i++, thisexd++) {
bf5c84f5 792 if (thisexd->final != NULL && (thisexd->context & context) != 0
f63a17d6
MC
793 && !thisexd->final(s, context, exts[i].present)) {
794 /* SSLfatal() already called */
735d5b59 795 return 0;
f63a17d6 796 }
735d5b59 797 }
68db4dda
MC
798 }
799
6b473aca
MC
800 return 1;
801}
802
38b051a1
TM
803int should_add_extension(SSL_CONNECTION *s, unsigned int extctx,
804 unsigned int thisctx, int max_version)
43ae5eed
MC
805{
806 /* Skip if not relevant for our context */
807 if ((extctx & thisctx) == 0)
808 return 0;
809
810 /* Check if this extension is defined for our protocol. If not, skip */
ee36b963 811 if (!extension_is_relevant(s, extctx, thisctx)
43ae5eed
MC
812 || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
813 && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
38b051a1 814 && (SSL_CONNECTION_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
43ae5eed
MC
815 return 0;
816
817 return 1;
818}
819
6b473aca 820/*
70af3d8e 821 * Construct all the extensions relevant to the current |context| and write
30aeba43 822 * them to |pkt|. If this is an extension for a Certificate in a Certificate
8521ced6
MC
823 * message, then |x| will be set to the Certificate we are handling, and
824 * |chainidx| will indicate the position in the chainidx we are processing (with
f63a17d6 825 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
8521ced6 826 * failure construction stops at the first extension to fail to construct.
6b473aca 827 */
38b051a1
TM
828int tls_construct_extensions(SSL_CONNECTION *s, WPACKET *pkt,
829 unsigned int context,
f63a17d6 830 X509 *x, size_t chainidx)
224135e9 831{
1266eefd 832 size_t i;
f63a17d6 833 int min_version, max_version = 0, reason;
d270de32 834 const EXTENSION_DEFINITION *thisexd;
72620ac7 835 int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
224135e9
MC
836
837 if (!WPACKET_start_sub_packet_u16(pkt)
838 /*
839 * If extensions are of zero length then we don't even add the
1c259bb5
BK
840 * extensions length bytes to a ClientHello/ServerHello
841 * (for non-TLSv1.3).
224135e9 842 */
fe874d27
MC
843 || ((context &
844 (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
fe874d27 845 && !WPACKET_set_flags(pkt,
72620ac7
TS
846 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
847 if (!for_comp)
848 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 849 return 0;
224135e9
MC
850 }
851
fe874d27 852 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
b5b993b2 853 reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
ab83e314 854 if (reason != 0) {
72620ac7
TS
855 if (!for_comp)
856 SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
f63a17d6 857 return 0;
ab83e314
MC
858 }
859 }
860
861 /* Add custom extensions first */
fe874d27 862 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
44e69951 863 /* On the server side with initialise during ClientHello parsing */
43ae5eed 864 custom_ext_init(&s->cert->custext);
ab83e314 865 }
f63a17d6
MC
866 if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
867 /* SSLfatal() already called */
868 return 0;
ab83e314
MC
869 }
870
1266eefd 871 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
38b051a1
TM
872 EXT_RETURN (*construct)(SSL_CONNECTION *s, WPACKET *pkt,
873 unsigned int context,
f63a17d6 874 X509 *x, size_t chainidx);
b186a592 875 EXT_RETURN ret;
4b299b8e 876
224135e9 877 /* Skip if not relevant for our context */
43ae5eed 878 if (!should_add_extension(s, thisexd->context, context, max_version))
224135e9
MC
879 continue;
880
1266eefd
MC
881 construct = s->server ? thisexd->construct_stoc
882 : thisexd->construct_ctos;
224135e9 883
43ae5eed 884 if (construct == NULL)
224135e9
MC
885 continue;
886
f63a17d6
MC
887 ret = construct(s, pkt, context, x, chainidx);
888 if (ret == EXT_RETURN_FAIL) {
889 /* SSLfatal() already called */
890 return 0;
891 }
b186a592
MC
892 if (ret == EXT_RETURN_SENT
893 && (context & (SSL_EXT_CLIENT_HELLO
894 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
895 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
896 s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
224135e9
MC
897 }
898
224135e9 899 if (!WPACKET_close(pkt)) {
72620ac7
TS
900 if (!for_comp)
901 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 902 return 0;
224135e9
MC
903 }
904
905 return 1;
906}
805a2e9e 907
70af3d8e
MC
908/*
909 * Built in extension finalisation and initialisation functions. All initialise
910 * or finalise the associated extension type for the given |context|. For
911 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
29bfd5b7 912 * otherwise. These functions return 1 on success or 0 on failure.
70af3d8e
MC
913 */
914
38b051a1 915static int final_renegotiate(SSL_CONNECTION *s, unsigned int context, int sent)
805a2e9e 916{
332eb390
MC
917 if (!s->server) {
918 /*
919 * Check if we can connect to a server that doesn't support safe
920 * renegotiation
921 */
922 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
923 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
924 && !sent) {
c48ffbcc 925 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
f63a17d6 926 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
332eb390
MC
927 return 0;
928 }
929
805a2e9e 930 return 1;
332eb390 931 }
805a2e9e
MC
932
933 /* Need RI if renegotiating */
934 if (s->renegotiate
935 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
936 && !sent) {
c48ffbcc 937 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
f63a17d6 938 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
805a2e9e
MC
939 return 0;
940 }
941
332eb390 942
805a2e9e
MC
943 return 1;
944}
945
acce0557
P
946static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
947 TSAN_QUALIFIER int *stat)
948{
949 if (ssl_tsan_lock(ctx)) {
950 tsan_decr(stat);
951 ssl_tsan_unlock(ctx);
952 }
953}
954
38b051a1 955static int init_server_name(SSL_CONNECTION *s, unsigned int context)
805a2e9e 956{
f01344cb 957 if (s->server) {
805a2e9e
MC
958 s->servername_done = 0;
959
f01344cb
MC
960 OPENSSL_free(s->ext.hostname);
961 s->ext.hostname = NULL;
962 }
963
805a2e9e
MC
964 return 1;
965}
966
38b051a1 967static int final_server_name(SSL_CONNECTION *s, unsigned int context, int sent)
805a2e9e 968{
9ef9088c 969 int ret = SSL_TLSEXT_ERR_NOACK;
805a2e9e 970 int altmp = SSL_AD_UNRECOGNIZED_NAME;
38b051a1
TM
971 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
972 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
973 int was_ticket = (SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0;
805a2e9e 974
38b051a1 975 if (!ossl_assert(sctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
c48ffbcc 976 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
c4715212
MC
977 return 0;
978 }
979
38b051a1
TM
980 if (sctx->ext.servername_cb != NULL)
981 ret = sctx->ext.servername_cb(ssl, &altmp,
982 sctx->ext.servername_arg);
c4715212 983 else if (s->session_ctx->ext.servername_cb != NULL)
38b051a1 984 ret = s->session_ctx->ext.servername_cb(ssl, &altmp,
222da979 985 s->session_ctx->ext.servername_arg);
805a2e9e 986
1c4aa31d
BK
987 /*
988 * For servers, propagate the SNI hostname from the temporary
989 * storage in the SSL to the persistent SSL_SESSION, now that we
990 * know we accepted it.
991 * Clients make this copy when parsing the server's response to
992 * the extension, which is when they find out that the negotiation
993 * was successful.
994 */
995 if (s->server) {
2a538551 996 if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
1c4aa31d
BK
997 /* Only store the hostname in the session if we accepted it. */
998 OPENSSL_free(s->session->ext.hostname);
999 s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
1000 if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
c48ffbcc 1001 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1c4aa31d
BK
1002 }
1003 }
9fb6cb81
MC
1004 }
1005
3be08e30
BK
1006 /*
1007 * If we switched contexts (whether here or in the client_hello callback),
1008 * move the sess_accept increment from the session_ctx to the new
1009 * context, to avoid the confusing situation of having sess_accept_good
1010 * exceed sess_accept (zero) for the new context.
1011 */
38b051a1 1012 if (SSL_IS_FIRST_HANDSHAKE(s) && sctx != s->session_ctx
2d6f72aa 1013 && s->hello_retry_request == SSL_HRR_NONE) {
38b051a1 1014 ssl_tsan_counter(sctx, &sctx->stats.sess_accept);
acce0557 1015 ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
3be08e30
BK
1016 }
1017
a84e5c9a
TS
1018 /*
1019 * If we're expecting to send a ticket, and tickets were previously enabled,
1020 * and now tickets are disabled, then turn off expected ticket.
1021 * Also, if this is not a resumption, create a new session ID
1022 */
1023 if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
38b051a1 1024 && was_ticket && (SSL_get_options(ssl) & SSL_OP_NO_TICKET) != 0) {
a84e5c9a
TS
1025 s->ext.ticket_expected = 0;
1026 if (!s->hit) {
38b051a1 1027 SSL_SESSION* ss = SSL_get_session(ssl);
a84e5c9a
TS
1028
1029 if (ss != NULL) {
1030 OPENSSL_free(ss->ext.tick);
1031 ss->ext.tick = NULL;
1032 ss->ext.ticklen = 0;
1033 ss->ext.tick_lifetime_hint = 0;
1034 ss->ext.tick_age_add = 0;
a84e5c9a 1035 if (!ssl_generate_session_id(s, ss)) {
c48ffbcc 1036 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 1037 return 0;
a84e5c9a
TS
1038 }
1039 } else {
c48ffbcc 1040 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 1041 return 0;
a84e5c9a
TS
1042 }
1043 }
1044 }
1045
805a2e9e
MC
1046 switch (ret) {
1047 case SSL_TLSEXT_ERR_ALERT_FATAL:
c48ffbcc 1048 SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
805a2e9e
MC
1049 return 0;
1050
1051 case SSL_TLSEXT_ERR_ALERT_WARNING:
fb62e47c 1052 /* TLSv1.3 doesn't have warning alerts so we suppress this */
38b051a1 1053 if (!SSL_CONNECTION_IS_TLS13(s))
fb62e47c 1054 ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
cd624ccd 1055 s->servername_done = 0;
805a2e9e
MC
1056 return 1;
1057
1058 case SSL_TLSEXT_ERR_NOACK:
1059 s->servername_done = 0;
1060 return 1;
1061
1062 default:
1063 return 1;
1064 }
1065}
1066
38b051a1
TM
1067static int final_ec_pt_formats(SSL_CONNECTION *s, unsigned int context,
1068 int sent)
332eb390
MC
1069{
1070 unsigned long alg_k, alg_a;
1071
1072 if (s->server)
1073 return 1;
1074
555cbb32
TS
1075 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1076 alg_a = s->s3.tmp.new_cipher->algorithm_auth;
332eb390
MC
1077
1078 /*
1079 * If we are client and using an elliptic curve cryptography cipher
1080 * suite, then if server returns an EC point formats lists extension it
1081 * must contain uncompressed.
1082 */
aff8c126
RS
1083 if (s->ext.ecpointformats != NULL
1084 && s->ext.ecpointformats_len > 0
cd0fb43c
MC
1085 && s->ext.peer_ecpointformats != NULL
1086 && s->ext.peer_ecpointformats_len > 0
1266eefd 1087 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
332eb390
MC
1088 /* we are using an ECC cipher */
1089 size_t i;
cd0fb43c 1090 unsigned char *list = s->ext.peer_ecpointformats;
1266eefd 1091
cd0fb43c 1092 for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
1266eefd 1093 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
332eb390 1094 break;
332eb390 1095 }
cd0fb43c 1096 if (i == s->ext.peer_ecpointformats_len) {
c48ffbcc 1097 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
f63a17d6 1098 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
332eb390
MC
1099 return 0;
1100 }
1101 }
1102
1103 return 1;
1104}
332eb390 1105
38b051a1 1106static int init_session_ticket(SSL_CONNECTION *s, unsigned int context)
332eb390
MC
1107{
1108 if (!s->server)
aff8c126 1109 s->ext.ticket_expected = 0;
332eb390
MC
1110
1111 return 1;
1112}
1113
8f8c11d8 1114#ifndef OPENSSL_NO_OCSP
38b051a1 1115static int init_status_request(SSL_CONNECTION *s, unsigned int context)
805a2e9e 1116{
f63e4288 1117 if (s->server) {
aff8c126 1118 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
f63e4288
MC
1119 } else {
1120 /*
1121 * Ensure we get sensible values passed to tlsext_status_cb in the event
1122 * that we don't receive a status message
1123 */
8cbfcc70
RS
1124 OPENSSL_free(s->ext.ocsp.resp);
1125 s->ext.ocsp.resp = NULL;
1126 s->ext.ocsp.resp_len = 0;
f63e4288 1127 }
332eb390
MC
1128
1129 return 1;
1130}
8f8c11d8 1131#endif
332eb390 1132
805a2e9e 1133#ifndef OPENSSL_NO_NEXTPROTONEG
38b051a1 1134static int init_npn(SSL_CONNECTION *s, unsigned int context)
805a2e9e 1135{
555cbb32 1136 s->s3.npn_seen = 0;
805a2e9e
MC
1137
1138 return 1;
1139}
1140#endif
1141
38b051a1 1142static int init_alpn(SSL_CONNECTION *s, unsigned int context)
805a2e9e 1143{
555cbb32
TS
1144 OPENSSL_free(s->s3.alpn_selected);
1145 s->s3.alpn_selected = NULL;
1146 s->s3.alpn_selected_len = 0;
805a2e9e 1147 if (s->server) {
555cbb32
TS
1148 OPENSSL_free(s->s3.alpn_proposed);
1149 s->s3.alpn_proposed = NULL;
1150 s->s3.alpn_proposed_len = 0;
805a2e9e 1151 }
805a2e9e
MC
1152 return 1;
1153}
1154
38b051a1 1155static int final_alpn(SSL_CONNECTION *s, unsigned int context, int sent)
630369d9 1156{
4be3a7c7
MC
1157 if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
1158 s->ext.early_data_ok = 0;
1159
38b051a1 1160 if (!s->server || !SSL_CONNECTION_IS_TLS13(s))
630369d9
MC
1161 return 1;
1162
1163 /*
1164 * Call alpn_select callback if needed. Has to be done after SNI and
1165 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
1166 * we also have to do this before we decide whether to accept early_data.
1167 * In TLSv1.3 we've already negotiated our cipher so we do this call now.
1168 * For < TLSv1.3 we defer it until after cipher negotiation.
56d36288 1169 *
f63a17d6 1170 * On failure SSLfatal() already called.
630369d9 1171 */
f63a17d6 1172 return tls_handle_alpn(s);
630369d9
MC
1173}
1174
38b051a1 1175static int init_sig_algs(SSL_CONNECTION *s, unsigned int context)
805a2e9e
MC
1176{
1177 /* Clear any signature algorithms extension received */
555cbb32
TS
1178 OPENSSL_free(s->s3.tmp.peer_sigalgs);
1179 s->s3.tmp.peer_sigalgs = NULL;
02b1636f 1180 s->s3.tmp.peer_sigalgslen = 0;
805a2e9e
MC
1181
1182 return 1;
1183}
1184
38b051a1
TM
1185static int init_sig_algs_cert(SSL_CONNECTION *s,
1186 ossl_unused unsigned int context)
c589c34e
BK
1187{
1188 /* Clear any signature algorithms extension received */
555cbb32
TS
1189 OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
1190 s->s3.tmp.peer_cert_sigalgs = NULL;
39a14059 1191 s->s3.tmp.peer_cert_sigalgslen = 0;
c589c34e
BK
1192
1193 return 1;
1194}
1195
805a2e9e 1196#ifndef OPENSSL_NO_SRP
38b051a1 1197static int init_srp(SSL_CONNECTION *s, unsigned int context)
805a2e9e
MC
1198{
1199 OPENSSL_free(s->srp_ctx.login);
1200 s->srp_ctx.login = NULL;
1201
1202 return 1;
1203}
1204#endif
1205
38b051a1 1206static int init_ec_point_formats(SSL_CONNECTION *s, unsigned int context)
3f987381
DB
1207{
1208 OPENSSL_free(s->ext.peer_ecpointformats);
1209 s->ext.peer_ecpointformats = NULL;
1210 s->ext.peer_ecpointformats_len = 0;
1211
1212 return 1;
1213}
1214
38b051a1 1215static int init_etm(SSL_CONNECTION *s, unsigned int context)
805a2e9e 1216{
28a31a0a 1217 s->ext.use_etm = 0;
332eb390
MC
1218
1219 return 1;
1220}
1221
38b051a1 1222static int init_ems(SSL_CONNECTION *s, unsigned int context)
332eb390 1223{
11d3235e
TM
1224 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1225 s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1226 s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
1227 }
332eb390
MC
1228
1229 return 1;
1230}
1231
38b051a1 1232static int final_ems(SSL_CONNECTION *s, unsigned int context, int sent)
332eb390 1233{
11d3235e
TM
1234 /*
1235 * Check extended master secret extension is not dropped on
1236 * renegotiation.
1237 */
1238 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
1239 && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
c48ffbcc 1240 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
11d3235e
TM
1241 return 0;
1242 }
332eb390
MC
1243 if (!s->server && s->hit) {
1244 /*
1245 * Check extended master secret extension is consistent with
1246 * original session.
1247 */
555cbb32 1248 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
332eb390 1249 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
c48ffbcc 1250 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
332eb390
MC
1251 return 0;
1252 }
1253 }
805a2e9e
MC
1254
1255 return 1;
1256}
1257
38b051a1 1258static int init_certificate_authorities(SSL_CONNECTION *s, unsigned int context)
45615c5f 1259{
555cbb32
TS
1260 sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
1261 s->s3.tmp.peer_ca_names = NULL;
45615c5f
DSH
1262 return 1;
1263}
1264
38b051a1
TM
1265static EXT_RETURN tls_construct_certificate_authorities(SSL_CONNECTION *s,
1266 WPACKET *pkt,
b186a592
MC
1267 unsigned int context,
1268 X509 *x,
f63a17d6 1269 size_t chainidx)
45615c5f 1270{
98732979 1271 const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
45615c5f
DSH
1272
1273 if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
b186a592 1274 return EXT_RETURN_NOT_SENT;
45615c5f
DSH
1275
1276 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
f63a17d6 1277 || !WPACKET_start_sub_packet_u16(pkt)) {
c48ffbcc 1278 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
b186a592 1279 return EXT_RETURN_FAIL;
45615c5f
DSH
1280 }
1281
98732979 1282 if (!construct_ca_names(s, ca_sk, pkt)) {
f63a17d6
MC
1283 /* SSLfatal() already called */
1284 return EXT_RETURN_FAIL;
1285 }
1286
1287 if (!WPACKET_close(pkt)) {
c48ffbcc 1288 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6
MC
1289 return EXT_RETURN_FAIL;
1290 }
1291
b186a592 1292 return EXT_RETURN_SENT;
45615c5f
DSH
1293}
1294
38b051a1 1295static int tls_parse_certificate_authorities(SSL_CONNECTION *s, PACKET *pkt,
45615c5f 1296 unsigned int context, X509 *x,
f63a17d6 1297 size_t chainidx)
45615c5f 1298{
f63a17d6 1299 if (!parse_ca_names(s, pkt))
45615c5f
DSH
1300 return 0;
1301 if (PACKET_remaining(pkt) != 0) {
c48ffbcc 1302 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
45615c5f
DSH
1303 return 0;
1304 }
1305 return 1;
1306}
1307
805a2e9e 1308#ifndef OPENSSL_NO_SRTP
38b051a1 1309static int init_srtp(SSL_CONNECTION *s, unsigned int context)
805a2e9e
MC
1310{
1311 if (s->server)
1312 s->srtp_profile = NULL;
1313
1314 return 1;
1315}
1316#endif
04904312 1317
38b051a1 1318static int final_sig_algs(SSL_CONNECTION *s, unsigned int context, int sent)
04904312 1319{
38b051a1 1320 if (!sent && SSL_CONNECTION_IS_TLS13(s) && !s->hit) {
c48ffbcc 1321 SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
f63a17d6 1322 SSL_R_MISSING_SIGALGS_EXTENSION);
04904312
MC
1323 return 0;
1324 }
1325
1326 return 1;
1327}
b2f7e8c0 1328
38b051a1 1329static int final_key_share(SSL_CONNECTION *s, unsigned int context, int sent)
f4bbb37c 1330{
65dc5c3c 1331#if !defined(OPENSSL_NO_TLS1_3)
38b051a1 1332 if (!SSL_CONNECTION_IS_TLS13(s))
f4bbb37c
MC
1333 return 1;
1334
07d447a6
MC
1335 /* Nothing to do for key_share in an HRR */
1336 if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1337 return 1;
1338
f4bbb37c
MC
1339 /*
1340 * If
aff9929b
MC
1341 * we are a client
1342 * AND
f4bbb37c
MC
1343 * we have no key_share
1344 * AND
1345 * (we are not resuming
1346 * OR the kex_mode doesn't allow non key_share resumes)
1347 * THEN
aff9929b 1348 * fail;
f4bbb37c 1349 */
aff9929b
MC
1350 if (!s->server
1351 && !sent
f4bbb37c
MC
1352 && (!s->hit
1353 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
7d061fce 1354 /* Nothing left we can do - just fail */
c48ffbcc 1355 SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
f4bbb37c
MC
1356 return 0;
1357 }
aff9929b 1358 /*
c36001c3 1359 * IF
aff9929b 1360 * we are a server
aff9929b 1361 * THEN
c36001c3
MC
1362 * IF
1363 * we have a suitable key_share
aff9929b 1364 * THEN
c36001c3
MC
1365 * IF
1366 * we are stateless AND we have no cookie
1367 * THEN
1368 * send a HelloRetryRequest
1369 * ELSE
1370 * IF
1371 * we didn't already send a HelloRetryRequest
1372 * AND
1373 * the client sent a key_share extension
1374 * AND
1375 * (we are not resuming
1376 * OR the kex_mode allows key_share resumes)
1377 * AND
1378 * a shared group exists
1379 * THEN
1380 * send a HelloRetryRequest
1381 * ELSE IF
1382 * we are not resuming
1383 * OR
1384 * the kex_mode doesn't allow non key_share resumes
1385 * THEN
1386 * fail
1387 * ELSE IF
1388 * we are stateless AND we have no cookie
1389 * THEN
1390 * send a HelloRetryRequest
aff9929b 1391 */
c36001c3 1392 if (s->server) {
555cbb32 1393 if (s->s3.peer_tmp != NULL) {
c36001c3 1394 /* We have a suitable key_share */
555cbb32 1395 if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
c36001c3
MC
1396 && !s->ext.cookieok) {
1397 if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1398 /*
1399 * If we are stateless then we wouldn't know about any
1400 * previously sent HRR - so how can this be anything other
1401 * than 0?
1402 */
c48ffbcc 1403 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
c36001c3
MC
1404 return 0;
1405 }
1406 s->hello_retry_request = SSL_HRR_PENDING;
1407 return 1;
1408 }
1409 } else {
1410 /* No suitable key_share */
1411 if (s->hello_retry_request == SSL_HRR_NONE && sent
1412 && (!s->hit
1413 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
1414 != 0)) {
1415 const uint16_t *pgroups, *clntgroups;
1416 size_t num_groups, clnt_num_groups, i;
1417 unsigned int group_id = 0;
1418
1419 /* Check if a shared group exists */
1420
1421 /* Get the clients list of supported groups. */
1422 tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
1423 tls1_get_supported_groups(s, &pgroups, &num_groups);
1424
1425 /*
1426 * Find the first group we allow that is also in client's list
1427 */
1428 for (i = 0; i < num_groups; i++) {
1429 group_id = pgroups[i];
1430
1431 if (check_in_list(s, group_id, clntgroups, clnt_num_groups,
16f0e91c 1432 1))
c36001c3
MC
1433 break;
1434 }
1435
1436 if (i < num_groups) {
1437 /* A shared group exists so send a HelloRetryRequest */
555cbb32 1438 s->s3.group_id = group_id;
c36001c3
MC
1439 s->hello_retry_request = SSL_HRR_PENDING;
1440 return 1;
1441 }
1442 }
1443 if (!s->hit
1444 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1445 /* Nothing left we can do - just fail */
1446 SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE
1447 : SSL_AD_MISSING_EXTENSION,
c48ffbcc 1448 SSL_R_NO_SUITABLE_KEY_SHARE);
c36001c3 1449 return 0;
aff9929b
MC
1450 }
1451
555cbb32 1452 if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
c36001c3
MC
1453 && !s->ext.cookieok) {
1454 if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1455 /*
1456 * If we are stateless then we wouldn't know about any
1457 * previously sent HRR - so how can this be anything other
1458 * than 0?
1459 */
c48ffbcc 1460 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
c36001c3
MC
1461 return 0;
1462 }
fc7129dc 1463 s->hello_retry_request = SSL_HRR_PENDING;
aff9929b
MC
1464 return 1;
1465 }
1466 }
c36001c3
MC
1467
1468 /*
1469 * We have a key_share so don't send any more HelloRetryRequest
1470 * messages
1471 */
1472 if (s->hello_retry_request == SSL_HRR_PENDING)
1473 s->hello_retry_request = SSL_HRR_COMPLETE;
1474 } else {
1475 /*
1476 * For a client side resumption with no key_share we need to generate
1477 * the handshake secret (otherwise this is done during key_share
1478 * processing).
1479 */
1480 if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
c48ffbcc 1481 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
aff9929b
MC
1482 return 0;
1483 }
1484 }
65dc5c3c 1485#endif /* !defined(OPENSSL_NO_TLS1_3) */
f4bbb37c
MC
1486 return 1;
1487}
1488
38b051a1 1489static int init_psk_kex_modes(SSL_CONNECTION *s, unsigned int context)
b2f7e8c0
MC
1490{
1491 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
b2f7e8c0
MC
1492 return 1;
1493}
1053a6e2 1494
38b051a1
TM
1495int tls_psk_do_binder(SSL_CONNECTION *s, const EVP_MD *md,
1496 const unsigned char *msgstart,
1053a6e2 1497 size_t binderoffset, const unsigned char *binderin,
3a7c56b2
MC
1498 unsigned char *binderout, SSL_SESSION *sess, int sign,
1499 int external)
1053a6e2
MC
1500{
1501 EVP_PKEY *mackey = NULL;
1502 EVP_MD_CTX *mctx = NULL;
1503 unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1504 unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
4ff1a526 1505 unsigned char *early_secret;
44e47328
TS
1506 /* ASCII: "res binder", in hex for EBCDIC compatibility */
1507 static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
1508 /* ASCII: "ext binder", in hex for EBCDIC compatibility */
1509 static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
4ff1a526
MC
1510 const unsigned char *label;
1511 size_t bindersize, labelsize, hashsize;
ed576acd 1512 int hashsizei = EVP_MD_get_size(md);
1053a6e2 1513 int ret = -1;
add8d0e9 1514 int usepskfored = 0;
38b051a1 1515 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
add8d0e9 1516
bceae201
MC
1517 /* Ensure cast to size_t is safe */
1518 if (!ossl_assert(hashsizei >= 0)) {
c48ffbcc 1519 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
bceae201
MC
1520 goto err;
1521 }
1522 hashsize = (size_t)hashsizei;
1523
add8d0e9
MC
1524 if (external
1525 && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1526 && s->session->ext.max_early_data == 0
1527 && sess->ext.max_early_data > 0)
1528 usepskfored = 1;
1053a6e2 1529
3a7c56b2
MC
1530 if (external) {
1531 label = external_label;
1532 labelsize = sizeof(external_label) - 1;
1533 } else {
1534 label = resumption_label;
1535 labelsize = sizeof(resumption_label) - 1;
1536 }
1537
9368f865
MC
1538 /*
1539 * Generate the early_secret. On the server side we've selected a PSK to
1540 * resume with (internal or external) so we always do this. On the client
add8d0e9
MC
1541 * side we do this for a non-external (i.e. resumption) PSK or external PSK
1542 * that will be used for early_data so that it is in place for sending early
1543 * data. For client side external PSK not being used for early_data we
9368f865
MC
1544 * generate it but store it away for later use.
1545 */
add8d0e9 1546 if (s->server || !external || usepskfored)
9368f865
MC
1547 early_secret = (unsigned char *)s->early_secret;
1548 else
1549 early_secret = (unsigned char *)sess->early_secret;
4ff1a526
MC
1550
1551 if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1552 sess->master_key_length, early_secret)) {
635c8f77 1553 /* SSLfatal() already called */
1053a6e2
MC
1554 goto err;
1555 }
1556
1557 /*
1558 * Create the handshake hash for the binder key...the messages so far are
1559 * empty!
1560 */
1561 mctx = EVP_MD_CTX_new();
1562 if (mctx == NULL
1563 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1564 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
c48ffbcc 1565 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1053a6e2
MC
1566 goto err;
1567 }
1568
1569 /* Generate the binder key */
4ff1a526 1570 if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
0fb2815b 1571 hashsize, binderkey, hashsize, 1)) {
635c8f77 1572 /* SSLfatal() already called */
1053a6e2
MC
1573 goto err;
1574 }
1575
1576 /* Generate the finished key */
1577 if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
635c8f77 1578 /* SSLfatal() already called */
1053a6e2
MC
1579 goto err;
1580 }
1581
aff9929b 1582 if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
c48ffbcc 1583 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
aff9929b
MC
1584 goto err;
1585 }
1586
1053a6e2 1587 /*
aff9929b
MC
1588 * Get a hash of the ClientHello up to the start of the binders. If we are
1589 * following a HelloRetryRequest then this includes the hash of the first
1590 * ClientHello and the HelloRetryRequest itself.
1053a6e2 1591 */
fc7129dc 1592 if (s->hello_retry_request == SSL_HRR_PENDING) {
aff9929b 1593 size_t hdatalen;
60690b5b 1594 long hdatalen_l;
aff9929b
MC
1595 void *hdata;
1596
60690b5b 1597 hdatalen = hdatalen_l =
555cbb32 1598 BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
60690b5b 1599 if (hdatalen_l <= 0) {
c48ffbcc 1600 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
aff9929b
MC
1601 goto err;
1602 }
1603
1604 /*
1605 * For servers the handshake buffer data will include the second
1606 * ClientHello - which we don't want - so we need to take that bit off.
1607 */
1608 if (s->server) {
77815a02
MC
1609 PACKET hashprefix, msg;
1610
1611 /* Find how many bytes are left after the first two messages */
1612 if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1613 || !PACKET_forward(&hashprefix, 1)
1614 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1615 || !PACKET_forward(&hashprefix, 1)
1616 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
c48ffbcc 1617 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
aff9929b
MC
1618 goto err;
1619 }
77815a02 1620 hdatalen -= PACKET_remaining(&hashprefix);
aff9929b
MC
1621 }
1622
1623 if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
c48ffbcc 1624 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
aff9929b
MC
1625 goto err;
1626 }
1627 }
1628
1629 if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1053a6e2 1630 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
c48ffbcc 1631 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1053a6e2
MC
1632 goto err;
1633 }
1634
38b051a1
TM
1635 mackey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC",
1636 sctx->propq, finishedkey,
d8652be0 1637 hashsize);
1053a6e2 1638 if (mackey == NULL) {
c48ffbcc 1639 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1053a6e2
MC
1640 goto err;
1641 }
1642
1643 if (!sign)
1644 binderout = tmpbinder;
1645
1646 bindersize = hashsize;
38b051a1
TM
1647 if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), sctx->libctx,
1648 sctx->propq, mackey, NULL) <= 0
1053a6e2
MC
1649 || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1650 || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1651 || bindersize != hashsize) {
c48ffbcc 1652 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1053a6e2
MC
1653 goto err;
1654 }
1655
1656 if (sign) {
1657 ret = 1;
1658 } else {
1659 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1660 ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
635c8f77 1661 if (!ret)
c48ffbcc 1662 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BINDER_DOES_NOT_VERIFY);
1053a6e2
MC
1663 }
1664
1665 err:
1666 OPENSSL_cleanse(binderkey, sizeof(binderkey));
1667 OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1668 EVP_PKEY_free(mackey);
1669 EVP_MD_CTX_free(mctx);
1670
1671 return ret;
1672}
38df5a45 1673
38b051a1 1674static int final_early_data(SSL_CONNECTION *s, unsigned int context, int sent)
38df5a45 1675{
4be3a7c7
MC
1676 if (!sent)
1677 return 1;
1678
1679 if (!s->server) {
1680 if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1681 && sent
1682 && !s->ext.early_data_ok) {
1683 /*
1684 * If we get here then the server accepted our early_data but we
1685 * later realised that it shouldn't have done (e.g. inconsistent
1686 * ALPN)
1687 */
c48ffbcc 1688 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
4be3a7c7
MC
1689 return 0;
1690 }
1691
38df5a45 1692 return 1;
4be3a7c7 1693 }
38df5a45
MC
1694
1695 if (s->max_early_data == 0
1696 || !s->hit
38df5a45
MC
1697 || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1698 || !s->ext.early_data_ok
c9598459 1699 || s->hello_retry_request != SSL_HRR_NONE
59b2cb26 1700 || (s->allow_early_data_cb != NULL
38b051a1 1701 && !s->allow_early_data_cb(SSL_CONNECTION_GET_SSL(s),
59b2cb26 1702 s->allow_early_data_cb_data))) {
38df5a45
MC
1703 s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1704 } else {
1705 s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1706
1707 if (!tls13_change_cipher_state(s,
1708 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
f63a17d6 1709 /* SSLfatal() already called */
38df5a45
MC
1710 return 0;
1711 }
1712 }
1713
1714 return 1;
1715}
cf72c757 1716
38b051a1
TM
1717static int final_maxfragmentlen(SSL_CONNECTION *s, unsigned int context,
1718 int sent)
cf72c757
F
1719{
1720 /*
1721 * Session resumption on server-side with MFL extension active
1722 * BUT MFL extension packet was not resent (i.e. sent == 0)
1723 */
f63a17d6 1724 if (s->server && s->hit && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
cf72c757 1725 && !sent ) {
c48ffbcc 1726 SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_BAD_EXTENSION);
cf72c757
F
1727 return 0;
1728 }
1729
435d88d7
MC
1730 if (s->session && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) {
1731 s->rlayer.rrlmethod->set_max_frag_len(s->rlayer.rrl,
1732 GET_MAX_FRAGMENT_LENGTH(s->session));
1733 s->rlayer.wrlmethod->set_max_frag_len(s->rlayer.wrl,
1734 ssl_get_max_send_fragment(s));
435d88d7 1735 }
cf72c757
F
1736
1737 return 1;
1738}
9d75dce3 1739
38b051a1
TM
1740static int init_post_handshake_auth(SSL_CONNECTION *s,
1741 ossl_unused unsigned int context)
9d75dce3
TS
1742{
1743 s->post_handshake_auth = SSL_PHA_NONE;
1744
1745 return 1;
1746}
efe0f315
BK
1747
1748/*
1749 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
1750 * extension, servers MUST abort the handshake.
1751 */
38b051a1 1752static int final_psk(SSL_CONNECTION *s, unsigned int context, int sent)
efe0f315
BK
1753{
1754 if (s->server && sent && s->clienthello != NULL
1755 && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
1756 SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1757 SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
1758 return 0;
1759 }
1760
1761 return 1;
1762}
b67cb09f
TS
1763
1764static int tls_init_compress_certificate(SSL_CONNECTION *sc, unsigned int context)
1765{
1766 memset(sc->ext.compress_certificate_from_peer, 0,
1767 sizeof(sc->ext.compress_certificate_from_peer));
1768 return 1;
1769}
1770
1771/* The order these are put into the packet imply a preference order: [brotli, zlib, zstd] */
1772static EXT_RETURN tls_construct_compress_certificate(SSL_CONNECTION *sc, WPACKET *pkt,
1773 unsigned int context,
1774 X509 *x, size_t chainidx)
1775{
1776#ifndef OPENSSL_NO_COMP_ALG
1777 int i;
1778
1779 if (!ossl_comp_has_alg(0))
1780 return EXT_RETURN_NOT_SENT;
1781
1782 /* Do not indicate we support receiving compressed certificates */
1783 if ((sc->options & SSL_OP_NO_RX_CERTIFICATE_COMPRESSION) != 0)
1784 return EXT_RETURN_NOT_SENT;
1785
1786 if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
1787 return EXT_RETURN_NOT_SENT;
1788
1789 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_compress_certificate)
1790 || !WPACKET_start_sub_packet_u16(pkt)
1791 || !WPACKET_start_sub_packet_u8(pkt))
1792 goto err;
1793
1794 for (i = 0; sc->cert_comp_prefs[i] != TLSEXT_comp_cert_none; i++) {
1795 if (!WPACKET_put_bytes_u16(pkt, sc->cert_comp_prefs[i]))
1796 goto err;
1797 }
1798 if (!WPACKET_close(pkt) || !WPACKET_close(pkt))
1799 goto err;
1800
1801 sc->ext.compress_certificate_sent = 1;
1802 return EXT_RETURN_SENT;
1803 err:
1804 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1805 return EXT_RETURN_FAIL;
1806#else
1807 return EXT_RETURN_NOT_SENT;
1808#endif
1809}
1810
1811#ifndef OPENSSL_NO_COMP_ALG
1812static int tls_comp_in_pref(SSL_CONNECTION *sc, int alg)
1813{
1814 int i;
1815
1816 /* ossl_comp_has_alg() considers 0 as "any" */
1817 if (alg == 0)
1818 return 0;
1819 /* Make sure algorithm is enabled */
1820 if (!ossl_comp_has_alg(alg))
1821 return 0;
1822 /* If no preferences are set, it's ok */
1823 if (sc->cert_comp_prefs[0] == TLSEXT_comp_cert_none)
1824 return 1;
1825 /* Find the algorithm */
1826 for (i = 0; i < TLSEXT_comp_cert_limit; i++)
1827 if (sc->cert_comp_prefs[i] == alg)
1828 return 1;
1829 return 0;
1830}
1831#endif
1832
1833int tls_parse_compress_certificate(SSL_CONNECTION *sc, PACKET *pkt, unsigned int context,
1834 X509 *x, size_t chainidx)
1835{
1836#ifndef OPENSSL_NO_COMP_ALG
1837 PACKET supported_comp_algs;
1838 unsigned int comp;
1839 int already_set[TLSEXT_comp_cert_limit];
1840 int j = 0;
1841
1842 /* If no algorithms are available, ignore the extension */
1843 if (!ossl_comp_has_alg(0))
1844 return 1;
1845
1846 /* Ignore the extension and don't send compressed certificates */
1847 if ((sc->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0)
1848 return 1;
1849
1850 if (!PACKET_as_length_prefixed_1(pkt, &supported_comp_algs)
1851 || PACKET_remaining(&supported_comp_algs) == 0) {
1852 SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1853 return 0;
1854 }
1855
1856 memset(already_set, 0, sizeof(already_set));
1857 /*
1858 * The preference array has real values, so take a look at each
1859 * value coming in, and make sure it's in our preference list
1860 * The array is 0 (i.e. "none") terminated
1861 * The preference list only contains supported algorithms
1862 */
ce74e3fb 1863 while (PACKET_get_net_2(&supported_comp_algs, &comp)) {
b67cb09f
TS
1864 if (tls_comp_in_pref(sc, comp) && !already_set[comp]) {
1865 sc->ext.compress_certificate_from_peer[j++] = comp;
1866 already_set[comp] = 1;
1867 }
1868 }
1869#endif
1870 return 1;
1871}