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