2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
10 #if defined(__TANDEM) && defined(_SPT_MODEL_)
11 # include <spthread.h>
12 # include <spt_extensions.h> /* timeval */
16 #include "internal/nelem.h"
17 #include "internal/cryptlib.h"
18 #include "../ssl_local.h"
19 #include "statem_local.h"
21 static int final_renegotiate(SSL
*s
, unsigned int context
, int sent
);
22 static int init_server_name(SSL
*s
, unsigned int context
);
23 static int final_server_name(SSL
*s
, unsigned int context
, int sent
);
24 static int final_ec_pt_formats(SSL
*s
, unsigned int context
, int sent
);
25 static int init_session_ticket(SSL
*s
, unsigned int context
);
26 #ifndef OPENSSL_NO_OCSP
27 static int init_status_request(SSL
*s
, unsigned int context
);
29 #ifndef OPENSSL_NO_NEXTPROTONEG
30 static int init_npn(SSL
*s
, unsigned int context
);
32 static int init_alpn(SSL
*s
, unsigned int context
);
33 static int final_alpn(SSL
*s
, unsigned int context
, int sent
);
34 static int init_sig_algs_cert(SSL
*s
, unsigned int context
);
35 static int init_sig_algs(SSL
*s
, unsigned int context
);
36 static int init_certificate_authorities(SSL
*s
, unsigned int context
);
37 static EXT_RETURN
tls_construct_certificate_authorities(SSL
*s
, WPACKET
*pkt
,
41 static int tls_parse_certificate_authorities(SSL
*s
, PACKET
*pkt
,
42 unsigned int context
, X509
*x
,
44 #ifndef OPENSSL_NO_SRP
45 static int init_srp(SSL
*s
, unsigned int context
);
47 static int init_ec_point_formats(SSL
*s
, unsigned int context
);
48 static int init_etm(SSL
*s
, unsigned int context
);
49 static int init_ems(SSL
*s
, unsigned int context
);
50 static int final_ems(SSL
*s
, unsigned int context
, int sent
);
51 static int init_psk_kex_modes(SSL
*s
, unsigned int context
);
52 static int final_key_share(SSL
*s
, unsigned int context
, int sent
);
53 #ifndef OPENSSL_NO_SRTP
54 static int init_srtp(SSL
*s
, unsigned int context
);
56 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
);
57 static int final_early_data(SSL
*s
, unsigned int context
, int sent
);
58 static int final_maxfragmentlen(SSL
*s
, unsigned int context
, int sent
);
59 static int init_post_handshake_auth(SSL
*s
, unsigned int context
);
60 static int final_psk(SSL
*s
, unsigned int context
, int sent
);
62 /* Structure to define a built-in extension */
63 typedef struct extensions_definition_st
{
64 /* The defined type for the extension */
67 * The context that this extension applies to, e.g. what messages and
72 * Initialise extension before parsing. Always called for relevant contexts
73 * even if extension not present
75 int (*init
)(SSL
*s
, unsigned int context
);
76 /* Parse extension sent from client to server */
77 int (*parse_ctos
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
79 /* Parse extension send from server to client */
80 int (*parse_stoc
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
82 /* Construct extension sent from server to client */
83 EXT_RETURN (*construct_stoc
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
84 X509
*x
, size_t chainidx
);
85 /* Construct extension sent from client to server */
86 EXT_RETURN (*construct_ctos
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
87 X509
*x
, size_t chainidx
);
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.
93 int (*final
)(SSL
*s
, unsigned int context
, int sent
);
94 } EXTENSION_DEFINITION
;
97 * Definitions of all built-in extensions. NOTE: Changes in the number or order
98 * of these extensions should be mirrored with equivalent changes to the
99 * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
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
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
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
115 * extension is relevant to a particular protocol or protocol version.
117 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
118 * the end, keep these extensions before signature_algorithm.
120 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
121 static const EXTENSION_DEFINITION ext_defs
[] = {
123 TLSEXT_TYPE_renegotiate
,
124 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
125 | SSL_EXT_SSL3_ALLOWED
| SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
126 NULL
, tls_parse_ctos_renegotiate
, tls_parse_stoc_renegotiate
,
127 tls_construct_stoc_renegotiate
, tls_construct_ctos_renegotiate
,
131 TLSEXT_TYPE_server_name
,
132 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
133 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
135 tls_parse_ctos_server_name
, tls_parse_stoc_server_name
,
136 tls_construct_stoc_server_name
, tls_construct_ctos_server_name
,
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
,
147 #ifndef OPENSSL_NO_SRP
150 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
151 init_srp
, tls_parse_ctos_srp
, NULL
, NULL
, tls_construct_ctos_srp
, NULL
157 TLSEXT_TYPE_ec_point_formats
,
158 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
159 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
160 init_ec_point_formats
, tls_parse_ctos_ec_pt_formats
, tls_parse_stoc_ec_pt_formats
,
161 tls_construct_stoc_ec_pt_formats
, tls_construct_ctos_ec_pt_formats
,
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.
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.
190 TLSEXT_TYPE_supported_groups
,
191 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
192 | SSL_EXT_TLS1_2_SERVER_HELLO
,
193 NULL
, tls_parse_ctos_supported_groups
, NULL
,
194 tls_construct_stoc_supported_groups
,
195 tls_construct_ctos_supported_groups
, NULL
198 TLSEXT_TYPE_session_ticket
,
199 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
200 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
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
205 #ifndef OPENSSL_NO_OCSP
207 TLSEXT_TYPE_status_request
,
208 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
209 | SSL_EXT_TLS1_3_CERTIFICATE
| SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
,
210 init_status_request
, tls_parse_ctos_status_request
,
211 tls_parse_stoc_status_request
, tls_construct_stoc_status_request
,
212 tls_construct_ctos_status_request
, NULL
217 #ifndef OPENSSL_NO_NEXTPROTONEG
219 TLSEXT_TYPE_next_proto_neg
,
220 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
221 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
222 init_npn
, tls_parse_ctos_npn
, tls_parse_stoc_npn
,
223 tls_construct_stoc_next_proto_neg
, tls_construct_ctos_npn
, NULL
230 * Must appear in this list after server_name so that finalisation
231 * happens after server_name callbacks
233 TLSEXT_TYPE_application_layer_protocol_negotiation
,
234 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
235 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
236 init_alpn
, tls_parse_ctos_alpn
, tls_parse_stoc_alpn
,
237 tls_construct_stoc_alpn
, tls_construct_ctos_alpn
, final_alpn
239 #ifndef OPENSSL_NO_SRTP
241 TLSEXT_TYPE_use_srtp
,
242 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
243 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
| SSL_EXT_DTLS_ONLY
,
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
251 TLSEXT_TYPE_encrypt_then_mac
,
252 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
253 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
254 init_etm
, tls_parse_ctos_etm
, tls_parse_stoc_etm
,
255 tls_construct_stoc_etm
, tls_construct_ctos_etm
, NULL
257 #ifndef OPENSSL_NO_CT
259 TLSEXT_TYPE_signed_certificate_timestamp
,
260 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
261 | SSL_EXT_TLS1_3_CERTIFICATE
| SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
,
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.
268 NULL
, tls_parse_stoc_sct
, NULL
, tls_construct_ctos_sct
, NULL
274 TLSEXT_TYPE_extended_master_secret
,
275 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
276 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
277 init_ems
, tls_parse_ctos_ems
, tls_parse_stoc_ems
,
278 tls_construct_stoc_ems
, tls_construct_ctos_ems
, final_ems
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. */
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
,
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
304 TLSEXT_TYPE_supported_versions
,
305 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_SERVER_HELLO
306 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
| SSL_EXT_TLS_IMPLEMENTATION_ONLY
,
308 /* Processed inline as part of version selection */
309 NULL
, tls_parse_stoc_supported_versions
,
310 tls_construct_stoc_supported_versions
,
311 tls_construct_ctos_supported_versions
, NULL
314 TLSEXT_TYPE_psk_kex_modes
,
315 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS_IMPLEMENTATION_ONLY
316 | SSL_EXT_TLS1_3_ONLY
,
317 init_psk_kex_modes
, tls_parse_ctos_psk_kex_modes
, NULL
, NULL
,
318 tls_construct_ctos_psk_kex_modes
, NULL
322 * Must be in this list after supported_groups. We need that to have
323 * been parsed before we do this one.
325 TLSEXT_TYPE_key_share
,
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
,
329 NULL
, tls_parse_ctos_key_share
, tls_parse_stoc_key_share
,
330 tls_construct_stoc_key_share
, tls_construct_ctos_key_share
,
334 /* Must be after key_share */
336 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
337 | SSL_EXT_TLS_IMPLEMENTATION_ONLY
| SSL_EXT_TLS1_3_ONLY
,
338 NULL
, tls_parse_ctos_cookie
, tls_parse_stoc_cookie
,
339 tls_construct_stoc_cookie
, tls_construct_ctos_cookie
, NULL
343 * Special unsolicited ServerHello extension only used when
344 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
347 TLSEXT_TYPE_cryptopro_bug
,
348 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
349 | SSL_EXT_TLS1_2_AND_BELOW_ONLY
,
350 NULL
, NULL
, NULL
, tls_construct_stoc_cryptopro_bug
, NULL
, NULL
353 TLSEXT_TYPE_early_data
,
354 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
355 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET
| SSL_EXT_TLS1_3_ONLY
,
356 NULL
, tls_parse_ctos_early_data
, tls_parse_stoc_early_data
,
357 tls_construct_stoc_early_data
, tls_construct_ctos_early_data
,
361 TLSEXT_TYPE_certificate_authorities
,
362 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
363 | SSL_EXT_TLS1_3_ONLY
,
364 init_certificate_authorities
,
365 tls_parse_certificate_authorities
, tls_parse_certificate_authorities
,
366 tls_construct_certificate_authorities
,
367 tls_construct_certificate_authorities
, NULL
,
370 /* Must be immediately before pre_shared_key */
372 SSL_EXT_CLIENT_HELLO
,
374 /* We send this, but don't read it */
375 NULL
, NULL
, NULL
, tls_construct_ctos_padding
, NULL
378 /* Required by the TLSv1.3 spec to always be the last extension */
380 SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_3_SERVER_HELLO
381 | SSL_EXT_TLS_IMPLEMENTATION_ONLY
| SSL_EXT_TLS1_3_ONLY
,
382 NULL
, tls_parse_ctos_psk
, tls_parse_stoc_psk
, tls_construct_stoc_psk
,
383 tls_construct_ctos_psk
, final_psk
387 /* Check whether an extension's context matches the current context */
388 static int validate_context(SSL
*s
, unsigned int extctx
, unsigned int thisctx
)
390 /* Check we're allowed to use this extension in this context */
391 if ((thisctx
& extctx
) == 0)
394 if (SSL_IS_DTLS(s
)) {
395 if ((extctx
& SSL_EXT_TLS_ONLY
) != 0)
397 } else if ((extctx
& SSL_EXT_DTLS_ONLY
) != 0) {
404 int tls_validate_all_contexts(SSL
*s
, unsigned int thisctx
, RAW_EXTENSION
*exts
)
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
;
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
;
416 /* Calculate the number of extensions in the extensions list */
417 num_exts
= builtin_num
+ s
->cert
->custext
.meths_count
;
419 for (thisext
= exts
, i
= 0; i
< num_exts
; i
++, thisext
++) {
420 if (!thisext
->present
)
423 if (i
< builtin_num
) {
424 context
= ext_defs
[i
].context
;
426 custom_ext_method
*meth
= NULL
;
428 meth
= custom_ext_find(&s
->cert
->custext
, role
, thisext
->type
,
430 if (!ossl_assert(meth
!= NULL
))
432 context
= meth
->context
;
435 if (!validate_context(s
, context
, thisctx
))
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
445 * indicate the extension is not allowed. If returning 1 then |*found| is set to
446 * the definition for the extension we found.
448 static int verify_extension(SSL
*s
, unsigned int context
, unsigned int type
,
449 custom_ext_methods
*meths
, RAW_EXTENSION
*rawexlist
,
450 RAW_EXTENSION
**found
)
453 size_t builtin_num
= OSSL_NELEM(ext_defs
);
454 const EXTENSION_DEFINITION
*thisext
;
456 for (i
= 0, thisext
= ext_defs
; i
< builtin_num
; i
++, thisext
++) {
457 if (type
== thisext
->type
) {
458 if (!validate_context(s
, thisext
->context
, context
))
461 *found
= &rawexlist
[i
];
466 /* Check the custom extensions */
469 ENDPOINT role
= ENDPOINT_BOTH
;
470 custom_ext_method
*meth
= NULL
;
472 if ((context
& SSL_EXT_CLIENT_HELLO
) != 0)
473 role
= ENDPOINT_SERVER
;
474 else if ((context
& SSL_EXT_TLS1_2_SERVER_HELLO
) != 0)
475 role
= ENDPOINT_CLIENT
;
477 meth
= custom_ext_find(meths
, role
, type
, &offset
);
479 if (!validate_context(s
, meth
->context
, context
))
481 *found
= &rawexlist
[offset
+ builtin_num
];
486 /* Unknown extension. We allow it */
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
496 int extension_is_relevant(SSL
*s
, unsigned int extctx
, unsigned int thisctx
)
501 * For HRR we haven't selected the version yet but we know it will be
504 if ((thisctx
& SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
) != 0)
507 is_tls13
= SSL_IS_TLS13(s
);
510 && (extctx
& SSL_EXT_TLS_IMPLEMENTATION_ONLY
) != 0)
511 || (s
->version
== SSL3_VERSION
512 && (extctx
& SSL_EXT_SSL3_ALLOWED
) == 0)
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
521 || (is_tls13
&& (extctx
& SSL_EXT_TLS1_2_AND_BELOW_ONLY
) != 0)
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)
525 || (s
->hit
&& (extctx
& SSL_EXT_IGNORE_ON_RESUMPTION
) != 0))
531 * Gather a list of all the extensions from the data in |packet]. |context|
532 * tells us which message this extension is for. The raw extension data is
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|.
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
543 * found, or an internal error occurred. We only check duplicates for
544 * extensions that we know about. We ignore others.
546 int tls_collect_extensions(SSL
*s
, PACKET
*packet
, unsigned int context
,
547 RAW_EXTENSION
**res
, size_t *len
, int init
)
549 PACKET extensions
= *packet
;
552 custom_ext_methods
*exts
= &s
->cert
->custext
;
553 RAW_EXTENSION
*raw_extensions
= NULL
;
554 const EXTENSION_DEFINITION
*thisexd
;
559 * Initialise server side custom extensions. Client side is done during
560 * construction of extensions for the ClientHello.
562 if ((context
& SSL_EXT_CLIENT_HELLO
) != 0)
563 custom_ext_init(&s
->cert
->custext
);
565 num_exts
= OSSL_NELEM(ext_defs
) + (exts
!= NULL
? exts
->meths_count
: 0);
566 raw_extensions
= OPENSSL_zalloc(num_exts
* sizeof(*raw_extensions
));
567 if (raw_extensions
== NULL
) {
568 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
573 while (PACKET_remaining(&extensions
) > 0) {
574 unsigned int type
, idx
;
576 RAW_EXTENSION
*thisex
;
578 if (!PACKET_get_net_2(&extensions
, &type
) ||
579 !PACKET_get_length_prefixed_2(&extensions
, &extension
)) {
580 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
584 * Verify this extension is allowed. We only check duplicates for
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.
588 if (!verify_extension(s
, context
, type
, exts
, raw_extensions
, &thisex
)
589 || (thisex
!= NULL
&& thisex
->present
== 1)
590 || (type
== TLSEXT_TYPE_psk
591 && (context
& SSL_EXT_CLIENT_HELLO
) != 0
592 && PACKET_remaining(&extensions
) != 0)) {
593 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_EXTENSION
);
596 idx
= thisex
- raw_extensions
;
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.
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.
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
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
)
624 SSLfatal(s
, SSL_AD_UNSUPPORTED_EXTENSION
,
625 SSL_R_UNSOLICITED_EXTENSION
);
628 if (thisex
!= NULL
) {
629 thisex
->data
= extension
;
632 thisex
->received_order
= i
++;
634 s
->ext
.debug_cb(s
, !s
->server
, thisex
->type
,
635 PACKET_data(&thisex
->data
),
636 PACKET_remaining(&thisex
->data
),
643 * Initialise all known extensions relevant to this context,
644 * whether we have found them or not
646 for (thisexd
= ext_defs
, i
= 0; i
< OSSL_NELEM(ext_defs
);
648 if (thisexd
->init
!= NULL
&& (thisexd
->context
& context
) != 0
649 && extension_is_relevant(s
, thisexd
->context
, context
)
650 && !thisexd
->init(s
, context
)) {
651 /* SSLfatal() already called */
657 *res
= raw_extensions
;
663 OPENSSL_free(raw_extensions
);
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
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
673 * Certificate |x| and its position in the |chainidx| with 0 being the first
674 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
675 * present this counted as success.
677 int tls_parse_extension(SSL
*s
, TLSEXT_INDEX idx
, int context
,
678 RAW_EXTENSION
*exts
, X509
*x
, size_t chainidx
)
680 RAW_EXTENSION
*currext
= &exts
[idx
];
681 int (*parser
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
682 size_t chainidx
) = NULL
;
684 /* Skip if the extension is not present */
685 if (!currext
->present
)
688 /* Skip if we've already parsed this extension */
694 if (idx
< OSSL_NELEM(ext_defs
)) {
695 /* We are handling a built-in extension */
696 const EXTENSION_DEFINITION
*extdef
= &ext_defs
[idx
];
698 /* Check if extension is defined for our protocol. If not, skip */
699 if (!extension_is_relevant(s
, extdef
->context
, context
))
702 parser
= s
->server
? extdef
->parse_ctos
: extdef
->parse_stoc
;
705 return parser(s
, &currext
->data
, context
, x
, chainidx
);
708 * If the parser is NULL we fall through to the custom extension
713 /* Parse custom extensions */
714 return custom_ext_parse(s
, context
, currext
->type
,
715 PACKET_data(&currext
->data
),
716 PACKET_remaining(&currext
->data
),
721 * Parse all remaining extensions that have not yet been parsed. Also calls the
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
725 * its position in the |chainidx|, with 0 being the first certificate.
727 int tls_parse_all_extensions(SSL
*s
, int context
, RAW_EXTENSION
*exts
, X509
*x
,
728 size_t chainidx
, int fin
)
730 size_t i
, numexts
= OSSL_NELEM(ext_defs
);
731 const EXTENSION_DEFINITION
*thisexd
;
733 /* Calculate the number of extensions in the extensions list */
734 numexts
+= s
->cert
->custext
.meths_count
;
736 /* Parse each extension in turn */
737 for (i
= 0; i
< numexts
; i
++) {
738 if (!tls_parse_extension(s
, i
, context
, exts
, x
, chainidx
)) {
739 /* SSLfatal() already called */
746 * Finalise all known extensions relevant to this context,
747 * whether we have found them or not
749 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
);
751 if (thisexd
->final
!= NULL
&& (thisexd
->context
& context
) != 0
752 && !thisexd
->final(s
, context
, exts
[i
].present
)) {
753 /* SSLfatal() already called */
762 int should_add_extension(SSL
*s
, unsigned int extctx
, unsigned int thisctx
,
765 /* Skip if not relevant for our context */
766 if ((extctx
& thisctx
) == 0)
769 /* Check if this extension is defined for our protocol. If not, skip */
770 if (!extension_is_relevant(s
, extctx
, thisctx
)
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
)))
780 * Construct all the extensions relevant to the current |context| and write
781 * them to |pkt|. If this is an extension for a Certificate in a Certificate
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
784 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
785 * failure construction stops at the first extension to fail to construct.
787 int tls_construct_extensions(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
788 X509
*x
, size_t chainidx
)
791 int min_version
, max_version
= 0, reason
;
792 const EXTENSION_DEFINITION
*thisexd
;
794 if (!WPACKET_start_sub_packet_u16(pkt
)
796 * If extensions are of zero length then we don't even add the
797 * extensions length bytes to a ClientHello/ServerHello
801 (SSL_EXT_CLIENT_HELLO
| SSL_EXT_TLS1_2_SERVER_HELLO
)) != 0
802 && !WPACKET_set_flags(pkt
,
803 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))) {
804 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
808 if ((context
& SSL_EXT_CLIENT_HELLO
) != 0) {
809 reason
= ssl_get_min_max_version(s
, &min_version
, &max_version
, NULL
);
811 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, reason
);
816 /* Add custom extensions first */
817 if ((context
& SSL_EXT_CLIENT_HELLO
) != 0) {
818 /* On the server side with initialise during ClientHello parsing */
819 custom_ext_init(&s
->cert
->custext
);
821 if (!custom_ext_add(s
, context
, pkt
, x
, chainidx
, max_version
)) {
822 /* SSLfatal() already called */
826 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
827 EXT_RETURN (*construct
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
828 X509
*x
, size_t chainidx
);
831 /* Skip if not relevant for our context */
832 if (!should_add_extension(s
, thisexd
->context
, context
, max_version
))
835 construct
= s
->server
? thisexd
->construct_stoc
836 : thisexd
->construct_ctos
;
838 if (construct
== NULL
)
841 ret
= construct(s
, pkt
, context
, x
, chainidx
);
842 if (ret
== EXT_RETURN_FAIL
) {
843 /* SSLfatal() already called */
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
;
853 if (!WPACKET_close(pkt
)) {
854 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
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
865 * otherwise. These functions return 1 on success or 0 on failure.
868 static int final_renegotiate(SSL
*s
, unsigned int context
, int sent
)
872 * Check if we can connect to a server that doesn't support safe
875 if (!(s
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)
876 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
878 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
879 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
886 /* Need RI if renegotiating */
888 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
890 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
,
891 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
899 static ossl_inline
void ssl_tsan_decr(const SSL_CTX
*ctx
,
900 TSAN_QUALIFIER
int *stat
)
902 if (ssl_tsan_lock(ctx
)) {
904 ssl_tsan_unlock(ctx
);
908 static int init_server_name(SSL
*s
, unsigned int context
)
911 s
->servername_done
= 0;
913 OPENSSL_free(s
->ext
.hostname
);
914 s
->ext
.hostname
= NULL
;
920 static int final_server_name(SSL
*s
, unsigned int context
, int sent
)
922 int ret
= SSL_TLSEXT_ERR_NOACK
;
923 int altmp
= SSL_AD_UNRECOGNIZED_NAME
;
924 int was_ticket
= (SSL_get_options(s
) & SSL_OP_NO_TICKET
) == 0;
926 if (!ossl_assert(s
->ctx
!= NULL
) || !ossl_assert(s
->session_ctx
!= NULL
)) {
927 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
931 if (s
->ctx
->ext
.servername_cb
!= NULL
)
932 ret
= s
->ctx
->ext
.servername_cb(s
, &altmp
,
933 s
->ctx
->ext
.servername_arg
);
934 else if (s
->session_ctx
->ext
.servername_cb
!= NULL
)
935 ret
= s
->session_ctx
->ext
.servername_cb(s
, &altmp
,
936 s
->session_ctx
->ext
.servername_arg
);
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
947 if (sent
&& ret
== SSL_TLSEXT_ERR_OK
&& !s
->hit
) {
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
) {
952 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
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.
963 if (SSL_IS_FIRST_HANDSHAKE(s
) && s
->ctx
!= s
->session_ctx
964 && s
->hello_retry_request
== SSL_HRR_NONE
) {
965 ssl_tsan_counter(s
->ctx
, &s
->ctx
->stats
.sess_accept
);
966 ssl_tsan_decr(s
->session_ctx
, &s
->session_ctx
->stats
.sess_accept
);
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
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;
978 SSL_SESSION
* ss
= SSL_get_session(s
);
981 OPENSSL_free(ss
->ext
.tick
);
984 ss
->ext
.tick_lifetime_hint
= 0;
985 ss
->ext
.tick_age_add
= 0;
986 if (!ssl_generate_session_id(s
, ss
)) {
987 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
991 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
998 case SSL_TLSEXT_ERR_ALERT_FATAL
:
999 SSLfatal(s
, altmp
, SSL_R_CALLBACK_FAILED
);
1002 case SSL_TLSEXT_ERR_ALERT_WARNING
:
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
);
1006 s
->servername_done
= 0;
1009 case SSL_TLSEXT_ERR_NOACK
:
1010 s
->servername_done
= 0;
1018 static int final_ec_pt_formats(SSL
*s
, unsigned int context
, int sent
)
1020 unsigned long alg_k
, alg_a
;
1025 alg_k
= s
->s3
.tmp
.new_cipher
->algorithm_mkey
;
1026 alg_a
= s
->s3
.tmp
.new_cipher
->algorithm_auth
;
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.
1033 if (s
->ext
.ecpointformats
!= NULL
1034 && s
->ext
.ecpointformats_len
> 0
1035 && s
->ext
.peer_ecpointformats
!= NULL
1036 && s
->ext
.peer_ecpointformats_len
> 0
1037 && ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
))) {
1038 /* we are using an ECC cipher */
1040 unsigned char *list
= s
->ext
.peer_ecpointformats
;
1042 for (i
= 0; i
< s
->ext
.peer_ecpointformats_len
; i
++) {
1043 if (*list
++ == TLSEXT_ECPOINTFORMAT_uncompressed
)
1046 if (i
== s
->ext
.peer_ecpointformats_len
) {
1047 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1048 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST
);
1056 static int init_session_ticket(SSL
*s
, unsigned int context
)
1059 s
->ext
.ticket_expected
= 0;
1064 #ifndef OPENSSL_NO_OCSP
1065 static int init_status_request(SSL
*s
, unsigned int context
)
1068 s
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
1071 * Ensure we get sensible values passed to tlsext_status_cb in the event
1072 * that we don't receive a status message
1074 OPENSSL_free(s
->ext
.ocsp
.resp
);
1075 s
->ext
.ocsp
.resp
= NULL
;
1076 s
->ext
.ocsp
.resp_len
= 0;
1083 #ifndef OPENSSL_NO_NEXTPROTONEG
1084 static int init_npn(SSL
*s
, unsigned int context
)
1092 static int init_alpn(SSL
*s
, unsigned int context
)
1094 OPENSSL_free(s
->s3
.alpn_selected
);
1095 s
->s3
.alpn_selected
= NULL
;
1096 s
->s3
.alpn_selected_len
= 0;
1098 OPENSSL_free(s
->s3
.alpn_proposed
);
1099 s
->s3
.alpn_proposed
= NULL
;
1100 s
->s3
.alpn_proposed_len
= 0;
1105 static int final_alpn(SSL
*s
, unsigned int context
, int sent
)
1107 if (!s
->server
&& !sent
&& s
->session
->ext
.alpn_selected
!= NULL
)
1108 s
->ext
.early_data_ok
= 0;
1110 if (!s
->server
|| !SSL_IS_TLS13(s
))
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.
1120 * On failure SSLfatal() already called.
1122 return tls_handle_alpn(s
);
1125 static int init_sig_algs(SSL
*s
, unsigned int context
)
1127 /* Clear any signature algorithms extension received */
1128 OPENSSL_free(s
->s3
.tmp
.peer_sigalgs
);
1129 s
->s3
.tmp
.peer_sigalgs
= NULL
;
1130 s
->s3
.tmp
.peer_sigalgslen
= 0;
1135 static int init_sig_algs_cert(SSL
*s
, ossl_unused
unsigned int context
)
1137 /* Clear any signature algorithms extension received */
1138 OPENSSL_free(s
->s3
.tmp
.peer_cert_sigalgs
);
1139 s
->s3
.tmp
.peer_cert_sigalgs
= NULL
;
1140 s
->s3
.tmp
.peer_cert_sigalgslen
= 0;
1145 #ifndef OPENSSL_NO_SRP
1146 static int init_srp(SSL
*s
, unsigned int context
)
1148 OPENSSL_free(s
->srp_ctx
.login
);
1149 s
->srp_ctx
.login
= NULL
;
1155 static int init_ec_point_formats(SSL
*s
, unsigned int context
)
1157 OPENSSL_free(s
->ext
.peer_ecpointformats
);
1158 s
->ext
.peer_ecpointformats
= NULL
;
1159 s
->ext
.peer_ecpointformats_len
= 0;
1164 static int init_etm(SSL
*s
, unsigned int context
)
1171 static int init_ems(SSL
*s
, unsigned int context
)
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
;
1181 static int final_ems(SSL
*s
, unsigned int context
, int sent
)
1184 * Check extended master secret extension is not dropped on
1187 if (!(s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
)
1188 && (s
->s3
.flags
& TLS1_FLAGS_REQUIRED_EXTMS
)) {
1189 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_EXTMS
);
1192 if (!s
->server
&& s
->hit
) {
1194 * Check extended master secret extension is consistent with
1197 if (!(s
->s3
.flags
& TLS1_FLAGS_RECEIVED_EXTMS
) !=
1198 !(s
->session
->flags
& SSL_SESS_FLAG_EXTMS
)) {
1199 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_INCONSISTENT_EXTMS
);
1207 static int init_certificate_authorities(SSL
*s
, unsigned int context
)
1209 sk_X509_NAME_pop_free(s
->s3
.tmp
.peer_ca_names
, X509_NAME_free
);
1210 s
->s3
.tmp
.peer_ca_names
= NULL
;
1214 static EXT_RETURN
tls_construct_certificate_authorities(SSL
*s
, WPACKET
*pkt
,
1215 unsigned int context
,
1219 const STACK_OF(X509_NAME
) *ca_sk
= get_ca_names(s
);
1221 if (ca_sk
== NULL
|| sk_X509_NAME_num(ca_sk
) == 0)
1222 return EXT_RETURN_NOT_SENT
;
1224 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_certificate_authorities
)
1225 || !WPACKET_start_sub_packet_u16(pkt
)) {
1226 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1227 return EXT_RETURN_FAIL
;
1230 if (!construct_ca_names(s
, ca_sk
, pkt
)) {
1231 /* SSLfatal() already called */
1232 return EXT_RETURN_FAIL
;
1235 if (!WPACKET_close(pkt
)) {
1236 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1237 return EXT_RETURN_FAIL
;
1240 return EXT_RETURN_SENT
;
1243 static int tls_parse_certificate_authorities(SSL
*s
, PACKET
*pkt
,
1244 unsigned int context
, X509
*x
,
1247 if (!parse_ca_names(s
, pkt
))
1249 if (PACKET_remaining(pkt
) != 0) {
1250 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_EXTENSION
);
1256 #ifndef OPENSSL_NO_SRTP
1257 static int init_srtp(SSL
*s
, unsigned int context
)
1260 s
->srtp_profile
= NULL
;
1266 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
)
1268 if (!sent
&& SSL_IS_TLS13(s
) && !s
->hit
) {
1269 SSLfatal(s
, TLS13_AD_MISSING_EXTENSION
,
1270 SSL_R_MISSING_SIGALGS_EXTENSION
);
1277 static int final_key_share(SSL
*s
, unsigned int context
, int sent
)
1279 #if !defined(OPENSSL_NO_TLS1_3)
1280 if (!SSL_IS_TLS13(s
))
1283 /* Nothing to do for key_share in an HRR */
1284 if ((context
& SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
) != 0)
1291 * we have no key_share
1293 * (we are not resuming
1294 * OR the kex_mode doesn't allow non key_share resumes)
1301 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE
) == 0)) {
1302 /* Nothing left we can do - just fail */
1303 SSLfatal(s
, SSL_AD_MISSING_EXTENSION
, SSL_R_NO_SUITABLE_KEY_SHARE
);
1311 * we have a suitable key_share
1314 * we are stateless AND we have no cookie
1316 * send a HelloRetryRequest
1319 * we didn't already send a HelloRetryRequest
1321 * the client sent a key_share extension
1323 * (we are not resuming
1324 * OR the kex_mode allows key_share resumes)
1326 * a shared group exists
1328 * send a HelloRetryRequest
1330 * we are not resuming
1332 * the kex_mode doesn't allow non key_share resumes
1336 * we are stateless AND we have no cookie
1338 * send a HelloRetryRequest
1341 if (s
->s3
.peer_tmp
!= NULL
) {
1342 /* We have a suitable key_share */
1343 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) != 0
1344 && !s
->ext
.cookieok
) {
1345 if (!ossl_assert(s
->hello_retry_request
== SSL_HRR_NONE
)) {
1347 * If we are stateless then we wouldn't know about any
1348 * previously sent HRR - so how can this be anything other
1351 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1354 s
->hello_retry_request
= SSL_HRR_PENDING
;
1358 /* No suitable key_share */
1359 if (s
->hello_retry_request
== SSL_HRR_NONE
&& sent
1361 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE_DHE
)
1363 const uint16_t *pgroups
, *clntgroups
;
1364 size_t num_groups
, clnt_num_groups
, i
;
1365 unsigned int group_id
= 0;
1367 /* Check if a shared group exists */
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
);
1374 * Find the first group we allow that is also in client's list
1376 for (i
= 0; i
< num_groups
; i
++) {
1377 group_id
= pgroups
[i
];
1379 if (check_in_list(s
, group_id
, clntgroups
, clnt_num_groups
,
1384 if (i
< num_groups
) {
1385 /* A shared group exists so send a HelloRetryRequest */
1386 s
->s3
.group_id
= group_id
;
1387 s
->hello_retry_request
= SSL_HRR_PENDING
;
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
,
1396 SSL_R_NO_SUITABLE_KEY_SHARE
);
1400 if ((s
->s3
.flags
& TLS1_FLAGS_STATELESS
) != 0
1401 && !s
->ext
.cookieok
) {
1402 if (!ossl_assert(s
->hello_retry_request
== SSL_HRR_NONE
)) {
1404 * If we are stateless then we wouldn't know about any
1405 * previously sent HRR - so how can this be anything other
1408 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1411 s
->hello_retry_request
= SSL_HRR_PENDING
;
1417 * We have a key_share so don't send any more HelloRetryRequest
1420 if (s
->hello_retry_request
== SSL_HRR_PENDING
)
1421 s
->hello_retry_request
= SSL_HRR_COMPLETE
;
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
1428 if (!sent
&& !tls13_generate_handshake_secret(s
, NULL
, 0)) {
1429 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1433 #endif /* !defined(OPENSSL_NO_TLS1_3) */
1437 static int init_psk_kex_modes(SSL
*s
, unsigned int context
)
1439 s
->ext
.psk_kex_mode
= TLSEXT_KEX_MODE_FLAG_NONE
;
1443 int tls_psk_do_binder(SSL
*s
, const EVP_MD
*md
, const unsigned char *msgstart
,
1444 size_t binderoffset
, const unsigned char *binderin
,
1445 unsigned char *binderout
, SSL_SESSION
*sess
, int sign
,
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
];
1452 unsigned char *early_secret
;
1453 #ifdef CHARSET_EBCDIC
1454 static const unsigned char resumption_label
[] = { 0x72, 0x65, 0x73, 0x20, 0x62, 0x69, 0x6E, 0x64, 0x65, 0x72, 0x00 };
1455 static const unsigned char external_label
[] = { 0x65, 0x78, 0x74, 0x20, 0x62, 0x69, 0x6E, 0x64, 0x65, 0x72, 0x00 };
1457 static const unsigned char resumption_label
[] = "res binder";
1458 static const unsigned char external_label
[] = "ext binder";
1460 const unsigned char *label
;
1461 size_t bindersize
, labelsize
, hashsize
;
1462 int hashsizei
= EVP_MD_get_size(md
);
1464 int usepskfored
= 0;
1466 /* Ensure cast to size_t is safe */
1467 if (!ossl_assert(hashsizei
>= 0)) {
1468 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1471 hashsize
= (size_t)hashsizei
;
1474 && s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
1475 && s
->session
->ext
.max_early_data
== 0
1476 && sess
->ext
.max_early_data
> 0)
1480 label
= external_label
;
1481 labelsize
= sizeof(external_label
) - 1;
1483 label
= resumption_label
;
1484 labelsize
= sizeof(resumption_label
) - 1;
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
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
1493 * generate it but store it away for later use.
1495 if (s
->server
|| !external
|| usepskfored
)
1496 early_secret
= (unsigned char *)s
->early_secret
;
1498 early_secret
= (unsigned char *)sess
->early_secret
;
1500 if (!tls13_generate_secret(s
, md
, NULL
, sess
->master_key
,
1501 sess
->master_key_length
, early_secret
)) {
1502 /* SSLfatal() already called */
1507 * Create the handshake hash for the binder key...the messages so far are
1510 mctx
= EVP_MD_CTX_new();
1512 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
1513 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1514 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1518 /* Generate the binder key */
1519 if (!tls13_hkdf_expand(s
, md
, early_secret
, label
, labelsize
, hash
,
1520 hashsize
, binderkey
, hashsize
, 1)) {
1521 /* SSLfatal() already called */
1525 /* Generate the finished key */
1526 if (!tls13_derive_finishedkey(s
, md
, binderkey
, finishedkey
, hashsize
)) {
1527 /* SSLfatal() already called */
1531 if (EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0) {
1532 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
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.
1541 if (s
->hello_retry_request
== SSL_HRR_PENDING
) {
1546 hdatalen
= hdatalen_l
=
1547 BIO_get_mem_data(s
->s3
.handshake_buffer
, &hdata
);
1548 if (hdatalen_l
<= 0) {
1549 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_LENGTH
);
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.
1558 PACKET hashprefix
, msg
;
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
)) {
1566 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1569 hdatalen
-= PACKET_remaining(&hashprefix
);
1572 if (EVP_DigestUpdate(mctx
, hdata
, hdatalen
) <= 0) {
1573 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1578 if (EVP_DigestUpdate(mctx
, msgstart
, binderoffset
) <= 0
1579 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1580 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1584 mackey
= EVP_PKEY_new_raw_private_key_ex(s
->ctx
->libctx
, "HMAC",
1585 s
->ctx
->propq
, finishedkey
,
1587 if (mackey
== NULL
) {
1588 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1593 binderout
= tmpbinder
;
1595 bindersize
= hashsize
;
1596 if (EVP_DigestSignInit_ex(mctx
, NULL
, EVP_MD_get0_name(md
), s
->ctx
->libctx
,
1597 s
->ctx
->propq
, mackey
, NULL
) <= 0
1598 || EVP_DigestSignUpdate(mctx
, hash
, hashsize
) <= 0
1599 || EVP_DigestSignFinal(mctx
, binderout
, &bindersize
) <= 0
1600 || bindersize
!= hashsize
) {
1601 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1608 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1609 ret
= (CRYPTO_memcmp(binderin
, binderout
, hashsize
) == 0);
1611 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BINDER_DOES_NOT_VERIFY
);
1615 OPENSSL_cleanse(binderkey
, sizeof(binderkey
));
1616 OPENSSL_cleanse(finishedkey
, sizeof(finishedkey
));
1617 EVP_PKEY_free(mackey
);
1618 EVP_MD_CTX_free(mctx
);
1623 static int final_early_data(SSL
*s
, unsigned int context
, int sent
)
1629 if (context
== SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1631 && !s
->ext
.early_data_ok
) {
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
1637 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_EARLY_DATA
);
1644 if (s
->max_early_data
== 0
1646 || s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
1647 || !s
->ext
.early_data_ok
1648 || s
->hello_retry_request
!= SSL_HRR_NONE
1649 || (s
->allow_early_data_cb
!= NULL
1650 && !s
->allow_early_data_cb(s
,
1651 s
->allow_early_data_cb_data
))) {
1652 s
->ext
.early_data
= SSL_EARLY_DATA_REJECTED
;
1654 s
->ext
.early_data
= SSL_EARLY_DATA_ACCEPTED
;
1656 if (!tls13_change_cipher_state(s
,
1657 SSL3_CC_EARLY
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
1658 /* SSLfatal() already called */
1666 static int final_maxfragmentlen(SSL
*s
, unsigned int context
, int sent
)
1669 * Session resumption on server-side with MFL extension active
1670 * BUT MFL extension packet was not resent (i.e. sent == 0)
1672 if (s
->server
&& s
->hit
&& USE_MAX_FRAGMENT_LENGTH_EXT(s
->session
)
1674 SSLfatal(s
, SSL_AD_MISSING_EXTENSION
, SSL_R_BAD_EXTENSION
);
1678 /* Current SSL buffer is lower than requested MFL */
1679 if (s
->session
&& USE_MAX_FRAGMENT_LENGTH_EXT(s
->session
)
1680 && s
->max_send_fragment
< GET_MAX_FRAGMENT_LENGTH(s
->session
))
1681 /* trigger a larger buffer reallocation */
1682 if (!ssl3_setup_buffers(s
)) {
1683 /* SSLfatal() already called */
1690 static int init_post_handshake_auth(SSL
*s
, ossl_unused
unsigned int context
)
1692 s
->post_handshake_auth
= SSL_PHA_NONE
;
1698 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
1699 * extension, servers MUST abort the handshake.
1701 static int final_psk(SSL
*s
, unsigned int context
, int sent
)
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
);