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