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