2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 #include "../ssl_locl.h"
12 #include "statem_locl.h"
14 static int final_renegotiate(SSL
*s
, unsigned int context
, int sent
,
16 static int init_server_name(SSL
*s
, unsigned int context
);
17 static int final_server_name(SSL
*s
, unsigned int context
, int sent
,
20 static int final_ec_pt_formats(SSL
*s
, unsigned int context
, int sent
,
23 static int init_session_ticket(SSL
*s
, unsigned int context
);
24 #ifndef OPENSSL_NO_OCSP
25 static int init_status_request(SSL
*s
, unsigned int context
);
27 #ifndef OPENSSL_NO_NEXTPROTONEG
28 static int init_npn(SSL
*s
, unsigned int context
);
30 static int init_alpn(SSL
*s
, unsigned int context
);
31 static int final_alpn(SSL
*s
, unsigned int context
, int sent
, int *al
);
32 static int init_sig_algs(SSL
*s
, unsigned int context
);
33 #ifndef OPENSSL_NO_SRP
34 static int init_srp(SSL
*s
, unsigned int context
);
36 static int init_etm(SSL
*s
, unsigned int context
);
37 static int init_ems(SSL
*s
, unsigned int context
);
38 static int final_ems(SSL
*s
, unsigned int context
, int sent
, int *al
);
39 static int init_psk_kex_modes(SSL
*s
, unsigned int context
);
41 static int final_key_share(SSL
*s
, unsigned int context
, int sent
, int *al
);
43 #ifndef OPENSSL_NO_SRTP
44 static int init_srtp(SSL
*s
, unsigned int context
);
46 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
, int *al
);
47 static int final_early_data(SSL
*s
, unsigned int context
, int sent
, int *al
);
49 /* Structure to define a built-in extension */
50 typedef struct extensions_definition_st
{
51 /* The defined type for the extension */
54 * The context that this extension applies to, e.g. what messages and
59 * Initialise extension before parsing. Always called for relevant contexts
60 * even if extension not present
62 int (*init
)(SSL
*s
, unsigned int context
);
63 /* Parse extension sent from client to server */
64 int (*parse_ctos
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
65 size_t chainidx
, int *al
);
66 /* Parse extension send from server to client */
67 int (*parse_stoc
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
68 size_t chainidx
, int *al
);
69 /* Construct extension sent from server to client */
70 int (*construct_stoc
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
71 size_t chainidx
, int *al
);
72 /* Construct extension sent from client to server */
73 int (*construct_ctos
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
74 size_t chainidx
, int *al
);
76 * Finalise extension after parsing. Always called where an extensions was
77 * initialised even if the extension was not present. |sent| is set to 1 if
78 * the extension was seen, or 0 otherwise.
80 int (*final
)(SSL
*s
, unsigned int context
, int sent
, int *al
);
81 } EXTENSION_DEFINITION
;
84 * Definitions of all built-in extensions. NOTE: Changes in the number or order
85 * of these extensions should be mirrored with equivalent changes to the
86 * indexes ( TLSEXT_IDX_* ) defined in ssl_locl.h.
87 * Each extension has an initialiser, a client and
88 * server side parser and a finaliser. The initialiser is called (if the
89 * extension is relevant to the given context) even if we did not see the
90 * extension in the message that we received. The parser functions are only
91 * called if we see the extension in the message. The finalisers are always
92 * called if the initialiser was called.
93 * There are also server and client side constructor functions which are always
94 * called during message construction if the extension is relevant for the
96 * The initialisation, parsing, finalisation and construction functions are
97 * always called in the order defined in this list. Some extensions may depend
98 * on others having been processed first, so the order of this list is
100 * The extension context is defined by a series of flags which specify which
101 * messages the extension is relevant to. These flags also specify whether the
102 * extension is relevant to a particular protocol or protocol version.
104 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
106 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
107 static const EXTENSION_DEFINITION ext_defs
[] = {
109 TLSEXT_TYPE_renegotiate
,
110 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_SSL3_ALLOWED
111 | EXT_TLS1_2_AND_BELOW_ONLY
,
112 NULL
, tls_parse_ctos_renegotiate
, tls_parse_stoc_renegotiate
,
113 tls_construct_stoc_renegotiate
, tls_construct_ctos_renegotiate
,
117 TLSEXT_TYPE_server_name
,
118 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
119 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
121 tls_parse_ctos_server_name
, tls_parse_stoc_server_name
,
122 tls_construct_stoc_server_name
, tls_construct_ctos_server_name
,
125 #ifndef OPENSSL_NO_SRP
128 EXT_CLIENT_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
129 init_srp
, tls_parse_ctos_srp
, NULL
, NULL
, tls_construct_ctos_srp
, NULL
135 TLSEXT_TYPE_early_data_info
,
136 EXT_TLS1_3_NEW_SESSION_TICKET
,
137 NULL
, NULL
, tls_parse_stoc_early_data_info
,
138 tls_construct_stoc_early_data_info
, NULL
, NULL
140 #ifndef OPENSSL_NO_EC
142 TLSEXT_TYPE_ec_point_formats
,
143 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
144 NULL
, tls_parse_ctos_ec_pt_formats
, tls_parse_stoc_ec_pt_formats
,
145 tls_construct_stoc_ec_pt_formats
, tls_construct_ctos_ec_pt_formats
,
149 TLSEXT_TYPE_supported_groups
,
150 EXT_CLIENT_HELLO
| EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
151 NULL
, tls_parse_ctos_supported_groups
, NULL
,
152 NULL
/* TODO(TLS1.3): Need to add this */,
153 tls_construct_ctos_supported_groups
, NULL
160 TLSEXT_TYPE_session_ticket
,
161 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
162 init_session_ticket
, tls_parse_ctos_session_ticket
,
163 tls_parse_stoc_session_ticket
, tls_construct_stoc_session_ticket
,
164 tls_construct_ctos_session_ticket
, NULL
167 TLSEXT_TYPE_signature_algorithms
,
169 init_sig_algs
, tls_parse_ctos_sig_algs
, NULL
, NULL
,
170 tls_construct_ctos_sig_algs
, final_sig_algs
172 #ifndef OPENSSL_NO_OCSP
174 TLSEXT_TYPE_status_request
,
175 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
176 | EXT_TLS1_3_CERTIFICATE
,
177 init_status_request
, tls_parse_ctos_status_request
,
178 tls_parse_stoc_status_request
, tls_construct_stoc_status_request
,
179 tls_construct_ctos_status_request
, NULL
184 #ifndef OPENSSL_NO_NEXTPROTONEG
186 TLSEXT_TYPE_next_proto_neg
,
187 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
188 init_npn
, tls_parse_ctos_npn
, tls_parse_stoc_npn
,
189 tls_construct_stoc_next_proto_neg
, tls_construct_ctos_npn
, NULL
196 * Must appear in this list after server_name so that finalisation
197 * happens after server_name callbacks
199 TLSEXT_TYPE_application_layer_protocol_negotiation
,
200 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
201 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
202 init_alpn
, tls_parse_ctos_alpn
, tls_parse_stoc_alpn
,
203 tls_construct_stoc_alpn
, tls_construct_ctos_alpn
, final_alpn
205 #ifndef OPENSSL_NO_SRTP
207 TLSEXT_TYPE_use_srtp
,
208 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
209 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
| EXT_DTLS_ONLY
,
210 init_srtp
, tls_parse_ctos_use_srtp
, tls_parse_stoc_use_srtp
,
211 tls_construct_stoc_use_srtp
, tls_construct_ctos_use_srtp
, NULL
217 TLSEXT_TYPE_encrypt_then_mac
,
218 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
| EXT_SSL3_ALLOWED
,
219 init_etm
, tls_parse_ctos_etm
, tls_parse_stoc_etm
,
220 tls_construct_stoc_etm
, tls_construct_ctos_etm
, NULL
222 #ifndef OPENSSL_NO_CT
224 TLSEXT_TYPE_signed_certificate_timestamp
,
225 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
226 | EXT_TLS1_3_CERTIFICATE
,
229 * No server side support for this, but can be provided by a custom
230 * extension. This is an exception to the rule that custom extensions
231 * cannot override built in ones.
233 NULL
, tls_parse_stoc_sct
, NULL
, tls_construct_ctos_sct
, NULL
239 TLSEXT_TYPE_extended_master_secret
,
240 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
241 init_ems
, tls_parse_ctos_ems
, tls_parse_stoc_ems
,
242 tls_construct_stoc_ems
, tls_construct_ctos_ems
, final_ems
245 TLSEXT_TYPE_supported_versions
,
246 EXT_CLIENT_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
| EXT_TLS1_3_ONLY
,
248 /* Processed inline as part of version selection */
249 NULL
, NULL
, NULL
, tls_construct_ctos_supported_versions
, NULL
252 TLSEXT_TYPE_psk_kex_modes
,
253 EXT_CLIENT_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
| EXT_TLS1_3_ONLY
,
254 init_psk_kex_modes
, tls_parse_ctos_psk_kex_modes
, NULL
, NULL
,
255 tls_construct_ctos_psk_kex_modes
, NULL
257 #ifndef OPENSSL_NO_EC
260 * Must be in this list after supported_groups. We need that to have
261 * been parsed before we do this one.
263 TLSEXT_TYPE_key_share
,
264 EXT_CLIENT_HELLO
| EXT_TLS1_3_SERVER_HELLO
265 | EXT_TLS1_3_HELLO_RETRY_REQUEST
| EXT_TLS_IMPLEMENTATION_ONLY
267 NULL
, tls_parse_ctos_key_share
, tls_parse_stoc_key_share
,
268 tls_construct_stoc_key_share
, tls_construct_ctos_key_share
,
274 * Special unsolicited ServerHello extension only used when
275 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
277 TLSEXT_TYPE_cryptopro_bug
,
278 EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
279 NULL
, NULL
, NULL
, tls_construct_stoc_cryptopro_bug
, NULL
, NULL
282 TLSEXT_TYPE_early_data
,
283 EXT_CLIENT_HELLO
| EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
284 NULL
, tls_parse_ctos_early_data
, tls_parse_stoc_early_data
,
285 tls_construct_stoc_early_data
, tls_construct_ctos_early_data
,
289 /* Must be immediately before pre_shared_key */
290 /* TODO(TLS1.3): Fix me */
294 /* We send this, but don't read it */
295 NULL
, NULL
, NULL
, tls_construct_ctos_padding
, NULL
298 /* Required by the TLSv1.3 spec to always be the last extension */
300 EXT_CLIENT_HELLO
| EXT_TLS1_3_SERVER_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
302 NULL
, tls_parse_ctos_psk
, tls_parse_stoc_psk
, tls_construct_stoc_psk
,
303 tls_construct_ctos_psk
, NULL
308 * Verify whether we are allowed to use the extension |type| in the current
309 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
310 * indicate the extension is not allowed. If returning 1 then |*found| is set to
311 * 1 if we found a definition for the extension, and |*idx| is set to its index
313 static int verify_extension(SSL
*s
, unsigned int context
, unsigned int type
,
314 custom_ext_methods
*meths
, RAW_EXTENSION
*rawexlist
,
315 RAW_EXTENSION
**found
)
318 size_t builtin_num
= OSSL_NELEM(ext_defs
);
319 const EXTENSION_DEFINITION
*thisext
;
321 for (i
= 0, thisext
= ext_defs
; i
< builtin_num
; i
++, thisext
++) {
322 if (type
== thisext
->type
) {
323 /* Check we're allowed to use this extension in this context */
324 if ((context
& thisext
->context
) == 0)
327 if (SSL_IS_DTLS(s
)) {
328 if ((thisext
->context
& EXT_TLS_ONLY
) != 0)
330 } else if ((thisext
->context
& EXT_DTLS_ONLY
) != 0) {
334 *found
= &rawexlist
[i
];
339 if ((context
& (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) == 0) {
341 * Custom extensions only apply to <=TLS1.2. This extension is unknown
342 * in this context - we allow it
348 /* Check the custom extensions */
350 for (i
= builtin_num
; i
< builtin_num
+ meths
->meths_count
; i
++) {
351 if (meths
->meths
[i
- builtin_num
].ext_type
== type
) {
352 *found
= &rawexlist
[i
];
358 /* Unknown extension. We allow it */
364 * Check whether the context defined for an extension |extctx| means whether
365 * the extension is relevant for the current context |thisctx| or not. Returns
366 * 1 if the extension is relevant for this context, and 0 otherwise
368 static int extension_is_relevant(SSL
*s
, unsigned int extctx
,
369 unsigned int thisctx
)
372 && (extctx
& EXT_TLS_IMPLEMENTATION_ONLY
) != 0)
373 || (s
->version
== SSL3_VERSION
374 && (extctx
& EXT_SSL3_ALLOWED
) == 0)
376 && (extctx
& EXT_TLS1_2_AND_BELOW_ONLY
) != 0)
377 || (!SSL_IS_TLS13(s
) && (extctx
& EXT_TLS1_3_ONLY
) != 0))
384 * Gather a list of all the extensions from the data in |packet]. |context|
385 * tells us which message this extension is for. The raw extension data is
386 * stored in |*res| on success. In the event of an error the alert type to use
387 * is stored in |*al|. We don't actually process the content of the extensions
388 * yet, except to check their types. This function also runs the initialiser
389 * functions for all known extensions (whether we have collected them or not).
390 * If successful the caller is responsible for freeing the contents of |*res|.
392 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
393 * more than one extension of the same type in a ClientHello or ServerHello.
394 * This function returns 1 if all extensions are unique and we have parsed their
395 * types, and 0 if the extensions contain duplicates, could not be successfully
396 * found, or an internal error occurred. We only check duplicates for
397 * extensions that we know about. We ignore others.
399 int tls_collect_extensions(SSL
*s
, PACKET
*packet
, unsigned int context
,
400 RAW_EXTENSION
**res
, int *al
, size_t *len
)
402 PACKET extensions
= *packet
;
405 custom_ext_methods
*exts
= NULL
;
406 RAW_EXTENSION
*raw_extensions
= NULL
;
407 const EXTENSION_DEFINITION
*thisexd
;
412 * Initialise server side custom extensions. Client side is done during
413 * construction of extensions for the ClientHello.
415 if ((context
& EXT_CLIENT_HELLO
) != 0) {
416 exts
= &s
->cert
->srv_ext
;
417 custom_ext_init(&s
->cert
->srv_ext
);
418 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
419 exts
= &s
->cert
->cli_ext
;
422 num_exts
= OSSL_NELEM(ext_defs
) + (exts
!= NULL
? exts
->meths_count
: 0);
423 raw_extensions
= OPENSSL_zalloc(num_exts
* sizeof(*raw_extensions
));
424 if (raw_extensions
== NULL
) {
425 *al
= SSL_AD_INTERNAL_ERROR
;
426 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, ERR_R_MALLOC_FAILURE
);
430 while (PACKET_remaining(&extensions
) > 0) {
433 RAW_EXTENSION
*thisex
;
435 if (!PACKET_get_net_2(&extensions
, &type
) ||
436 !PACKET_get_length_prefixed_2(&extensions
, &extension
)) {
437 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, SSL_R_BAD_EXTENSION
);
438 *al
= SSL_AD_DECODE_ERROR
;
442 * Verify this extension is allowed. We only check duplicates for
443 * extensions that we recognise.
445 if (!verify_extension(s
, context
, type
, exts
, raw_extensions
, &thisex
)
446 || (thisex
!= NULL
&& thisex
->present
== 1)) {
447 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, SSL_R_BAD_EXTENSION
);
448 *al
= SSL_AD_ILLEGAL_PARAMETER
;
451 if (thisex
!= NULL
) {
452 thisex
->data
= extension
;
459 * Initialise all known extensions relevant to this context, whether we have
462 for (thisexd
= ext_defs
, i
= 0; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
463 if(thisexd
->init
!= NULL
&& (thisexd
->context
& context
) != 0
464 && extension_is_relevant(s
, thisexd
->context
, context
)
465 && !thisexd
->init(s
, context
)) {
466 *al
= SSL_AD_INTERNAL_ERROR
;
471 *res
= raw_extensions
;
477 OPENSSL_free(raw_extensions
);
482 * Runs the parser for a given extension with index |idx|. |exts| contains the
483 * list of all parsed extensions previously collected by
484 * tls_collect_extensions(). The parser is only run if it is applicable for the
485 * given |context| and the parser has not already been run. If this is for a
486 * Certificate message, then we also provide the parser with the relevant
487 * Certificate |x| and its position in the |chainidx| with 0 being the first
488 * Certificate. Returns 1 on success or 0 on failure. In the event of a failure
489 * |*al| is populated with a suitable alert code. If an extension is not present
490 * this counted as success.
492 int tls_parse_extension(SSL
*s
, TLSEXT_INDEX idx
, int context
,
493 RAW_EXTENSION
*exts
, X509
*x
, size_t chainidx
, int *al
)
495 RAW_EXTENSION
*currext
= &exts
[idx
];
496 int (*parser
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
497 size_t chainidx
, int *al
) = NULL
;
499 /* Skip if the extension is not present */
500 if (!currext
->present
)
504 s
->ext
.debug_cb(s
, !s
->server
, currext
->type
,
505 PACKET_data(&currext
->data
),
506 PACKET_remaining(&currext
->data
),
509 /* Skip if we've already parsed this extension */
515 if (idx
< OSSL_NELEM(ext_defs
)) {
516 /* We are handling a built-in extension */
517 const EXTENSION_DEFINITION
*extdef
= &ext_defs
[idx
];
519 /* Check if extension is defined for our protocol. If not, skip */
520 if (!extension_is_relevant(s
, extdef
->context
, context
))
523 parser
= s
->server
? extdef
->parse_ctos
: extdef
->parse_stoc
;
526 return parser(s
, &currext
->data
, context
, x
, chainidx
, al
);
529 * If the parser is NULL we fall through to the custom extension
535 * This is a custom extension. We only allow this if it is a non
536 * resumed session on the server side.
538 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
539 * We're going to need a new mechanism for TLS1.3 to specify which
540 * messages to add the custom extensions to.
542 if ((!s
->hit
|| !s
->server
)
544 & (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) != 0
545 && custom_ext_parse(s
, s
->server
, currext
->type
,
546 PACKET_data(&currext
->data
),
547 PACKET_remaining(&currext
->data
),
555 * Parse all remaining extensions that have not yet been parsed. Also calls the
556 * finalisation for all extensions at the end, whether we collected them or not.
557 * Returns 1 for success or 0 for failure. If we are working on a Certificate
558 * message then we also pass the Certificate |x| and its position in the
559 * |chainidx|, with 0 being the first certificate. On failure, |*al| is
560 * populated with a suitable alert code.
562 int tls_parse_all_extensions(SSL
*s
, int context
, RAW_EXTENSION
*exts
, X509
*x
,
563 size_t chainidx
, int *al
)
565 size_t i
, numexts
= OSSL_NELEM(ext_defs
);
566 const EXTENSION_DEFINITION
*thisexd
;
568 /* Calculate the number of extensions in the extensions list */
569 if ((context
& EXT_CLIENT_HELLO
) != 0) {
570 numexts
+= s
->cert
->srv_ext
.meths_count
;
571 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
572 numexts
+= s
->cert
->cli_ext
.meths_count
;
575 /* Parse each extension in turn */
576 for (i
= 0; i
< numexts
; i
++) {
577 if (!tls_parse_extension(s
, i
, context
, exts
, x
, chainidx
, al
))
582 * Finalise all known extensions relevant to this context, whether we have
585 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
586 if(thisexd
->final
!= NULL
587 && (thisexd
->context
& context
) != 0
588 && !thisexd
->final(s
, context
, exts
[i
].present
, al
))
596 * Construct all the extensions relevant to the current |context| and write
597 * them to |pkt|. If this is an extension for a Certificate in a Certificate
598 * message, then |x| will be set to the Certificate we are handling, and
599 * |chainidx| will indicate the position in the chainidx we are processing (with
600 * 0 being the first in the chain). Returns 1 on success or 0 on failure. If a
601 * failure occurs then |al| is populated with a suitable alert code. On a
602 * failure construction stops at the first extension to fail to construct.
604 int tls_construct_extensions(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
605 X509
*x
, size_t chainidx
, int *al
)
608 int addcustom
= 0, min_version
, max_version
= 0, reason
, tmpal
;
609 const EXTENSION_DEFINITION
*thisexd
;
612 * Normally if something goes wrong during construction it's an internal
613 * error. We can always override this later.
615 tmpal
= SSL_AD_INTERNAL_ERROR
;
617 if (!WPACKET_start_sub_packet_u16(pkt
)
619 * If extensions are of zero length then we don't even add the
620 * extensions length bytes to a ClientHello/ServerHello in SSLv3
622 || ((context
& (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) != 0
623 && s
->version
== SSL3_VERSION
624 && !WPACKET_set_flags(pkt
,
625 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))) {
626 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
630 if ((context
& EXT_CLIENT_HELLO
) != 0) {
631 reason
= ssl_get_client_min_max_version(s
, &min_version
, &max_version
);
633 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, reason
);
638 /* Add custom extensions first */
639 if ((context
& EXT_CLIENT_HELLO
) != 0) {
640 custom_ext_init(&s
->cert
->cli_ext
);
642 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
644 * We already initialised the custom extensions during ClientHello
647 * TODO(TLS1.3): We're going to need a new custom extension mechanism
648 * for TLS1.3, so that custom extensions can specify which of the
649 * multiple message they wish to add themselves to.
654 if (addcustom
&& !custom_ext_add(s
, s
->server
, pkt
, &tmpal
)) {
655 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
659 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
660 int (*construct
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
661 size_t chainidx
, int *al
);
663 /* Skip if not relevant for our context */
664 if ((thisexd
->context
& context
) == 0)
667 construct
= s
->server
? thisexd
->construct_stoc
668 : thisexd
->construct_ctos
;
670 /* Check if this extension is defined for our protocol. If not, skip */
672 && (thisexd
->context
& EXT_TLS_IMPLEMENTATION_ONLY
)
674 || (s
->version
== SSL3_VERSION
675 && (thisexd
->context
& EXT_SSL3_ALLOWED
) == 0)
677 && (thisexd
->context
& EXT_TLS1_2_AND_BELOW_ONLY
)
680 && (thisexd
->context
& EXT_TLS1_3_ONLY
) != 0
681 && (context
& EXT_CLIENT_HELLO
) == 0)
682 || ((thisexd
->context
& EXT_TLS1_3_ONLY
) != 0
683 && (context
& EXT_CLIENT_HELLO
) != 0
684 && (SSL_IS_DTLS(s
) || max_version
< TLS1_3_VERSION
))
685 || construct
== NULL
)
688 if (!construct(s
, pkt
, context
, x
, chainidx
, &tmpal
))
692 if (!WPACKET_close(pkt
)) {
693 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
705 * Built in extension finalisation and initialisation functions. All initialise
706 * or finalise the associated extension type for the given |context|. For
707 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
708 * otherwise. These functions return 1 on success or 0 on failure. In the event
709 * of a failure then |*al| is populated with a suitable error code.
712 static int final_renegotiate(SSL
*s
, unsigned int context
, int sent
,
717 * Check if we can connect to a server that doesn't support safe
720 if (!(s
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)
721 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
723 *al
= SSL_AD_HANDSHAKE_FAILURE
;
724 SSLerr(SSL_F_FINAL_RENEGOTIATE
,
725 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
732 /* Need RI if renegotiating */
734 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
736 *al
= SSL_AD_HANDSHAKE_FAILURE
;
737 SSLerr(SSL_F_FINAL_RENEGOTIATE
,
738 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
746 static int init_server_name(SSL
*s
, unsigned int context
)
749 s
->servername_done
= 0;
754 static int final_server_name(SSL
*s
, unsigned int context
, int sent
,
757 int ret
= SSL_TLSEXT_ERR_NOACK
;
758 int altmp
= SSL_AD_UNRECOGNIZED_NAME
;
760 if (s
->ctx
!= NULL
&& s
->ctx
->ext
.servername_cb
!= 0)
761 ret
= s
->ctx
->ext
.servername_cb(s
, &altmp
,
762 s
->ctx
->ext
.servername_arg
);
763 else if (s
->session_ctx
!= NULL
764 && s
->session_ctx
->ext
.servername_cb
!= 0)
765 ret
= s
->session_ctx
->ext
.servername_cb(s
, &altmp
,
766 s
->session_ctx
->ext
.servername_arg
);
769 case SSL_TLSEXT_ERR_ALERT_FATAL
:
773 case SSL_TLSEXT_ERR_ALERT_WARNING
:
777 case SSL_TLSEXT_ERR_NOACK
:
778 s
->servername_done
= 0;
786 #ifndef OPENSSL_NO_EC
787 static int final_ec_pt_formats(SSL
*s
, unsigned int context
, int sent
,
790 unsigned long alg_k
, alg_a
;
795 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
796 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
799 * If we are client and using an elliptic curve cryptography cipher
800 * suite, then if server returns an EC point formats lists extension it
801 * must contain uncompressed.
803 if (s
->ext
.ecpointformats
!= NULL
804 && s
->ext
.ecpointformats_len
> 0
805 && s
->session
->ext
.ecpointformats
!= NULL
806 && s
->session
->ext
.ecpointformats_len
> 0
807 && ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
))) {
808 /* we are using an ECC cipher */
810 unsigned char *list
= s
->session
->ext
.ecpointformats
;
812 for (i
= 0; i
< s
->session
->ext
.ecpointformats_len
; i
++) {
813 if (*list
++ == TLSEXT_ECPOINTFORMAT_uncompressed
)
816 if (i
== s
->session
->ext
.ecpointformats_len
) {
817 SSLerr(SSL_F_FINAL_EC_PT_FORMATS
,
818 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST
);
827 static int init_session_ticket(SSL
*s
, unsigned int context
)
830 s
->ext
.ticket_expected
= 0;
835 #ifndef OPENSSL_NO_OCSP
836 static int init_status_request(SSL
*s
, unsigned int context
)
839 s
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
842 * Ensure we get sensible values passed to tlsext_status_cb in the event
843 * that we don't receive a status message
845 OPENSSL_free(s
->ext
.ocsp
.resp
);
846 s
->ext
.ocsp
.resp
= NULL
;
847 s
->ext
.ocsp
.resp_len
= 0;
854 #ifndef OPENSSL_NO_NEXTPROTONEG
855 static int init_npn(SSL
*s
, unsigned int context
)
863 static int init_alpn(SSL
*s
, unsigned int context
)
865 OPENSSL_free(s
->s3
->alpn_selected
);
866 s
->s3
->alpn_selected
= NULL
;
868 s
->s3
->alpn_selected_len
= 0;
869 OPENSSL_free(s
->s3
->alpn_proposed
);
870 s
->s3
->alpn_proposed
= NULL
;
871 s
->s3
->alpn_proposed_len
= 0;
876 static int final_alpn(SSL
*s
, unsigned int context
, int sent
, int *al
)
878 const unsigned char *selected
= NULL
;
879 unsigned char selected_len
= 0;
884 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
->alpn_proposed
!= NULL
) {
885 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
886 s
->s3
->alpn_proposed
,
887 (unsigned int)s
->s3
->alpn_proposed_len
,
888 s
->ctx
->ext
.alpn_select_cb_arg
);
890 if (r
== SSL_TLSEXT_ERR_OK
) {
891 OPENSSL_free(s
->s3
->alpn_selected
);
892 s
->s3
->alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
893 if (s
->s3
->alpn_selected
== NULL
) {
894 *al
= SSL_AD_INTERNAL_ERROR
;
897 s
->s3
->alpn_selected_len
= selected_len
;
898 #ifndef OPENSSL_NO_NEXTPROTONEG
899 /* ALPN takes precedence over NPN. */
903 *al
= SSL_AD_NO_APPLICATION_PROTOCOL
;
911 static int init_sig_algs(SSL
*s
, unsigned int context
)
913 /* Clear any signature algorithms extension received */
914 OPENSSL_free(s
->s3
->tmp
.peer_sigalgs
);
915 s
->s3
->tmp
.peer_sigalgs
= NULL
;
920 #ifndef OPENSSL_NO_SRP
921 static int init_srp(SSL
*s
, unsigned int context
)
923 OPENSSL_free(s
->srp_ctx
.login
);
924 s
->srp_ctx
.login
= NULL
;
930 static int init_etm(SSL
*s
, unsigned int context
)
937 static int init_ems(SSL
*s
, unsigned int context
)
940 s
->s3
->flags
&= ~TLS1_FLAGS_RECEIVED_EXTMS
;
945 static int final_ems(SSL
*s
, unsigned int context
, int sent
, int *al
)
947 if (!s
->server
&& s
->hit
) {
949 * Check extended master secret extension is consistent with
952 if (!(s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
) !=
953 !(s
->session
->flags
& SSL_SESS_FLAG_EXTMS
)) {
954 *al
= SSL_AD_HANDSHAKE_FAILURE
;
955 SSLerr(SSL_F_FINAL_EMS
, SSL_R_INCONSISTENT_EXTMS
);
963 #ifndef OPENSSL_NO_SRTP
964 static int init_srtp(SSL
*s
, unsigned int context
)
967 s
->srtp_profile
= NULL
;
973 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
, int *al
)
975 if (!sent
&& SSL_IS_TLS13(s
)) {
976 *al
= TLS13_AD_MISSING_EXTENSION
;
977 SSLerr(SSL_F_FINAL_SIG_ALGS
, SSL_R_MISSING_SIGALGS_EXTENSION
);
984 #ifndef OPENSSL_NO_EC
985 static int final_key_share(SSL
*s
, unsigned int context
, int sent
, int *al
)
987 if (!SSL_IS_TLS13(s
))
994 * we have no key_share
996 * (we are not resuming
997 * OR the kex_mode doesn't allow non key_share resumes)
1004 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE
) == 0)) {
1005 /* Nothing left we can do - just fail */
1006 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1007 SSLerr(SSL_F_FINAL_KEY_SHARE
, SSL_R_NO_SUITABLE_KEY_SHARE
);
1014 * we have no key_share
1017 * we didn't already send a HelloRetryRequest
1019 * the client sent a key_share extension
1021 * (we are not resuming
1022 * OR the kex_mode allows key_share resumes)
1024 * a shared group exists
1026 * send a HelloRetryRequest
1028 * we are not resuming
1030 * the kex_mode doesn't allow non key_share resumes
1034 if (s
->server
&& s
->s3
->peer_tmp
== NULL
) {
1035 /* No suitable share */
1036 if (s
->hello_retry_request
== 0 && sent
1038 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE_DHE
)
1040 const unsigned char *pcurves
, *pcurvestmp
, *clntcurves
;
1041 size_t num_curves
, clnt_num_curves
, i
;
1042 unsigned int group_id
= 0;
1044 /* Check if a shared group exists */
1046 /* Get the clients list of supported groups. */
1047 if (!tls1_get_curvelist(s
, 1, &clntcurves
, &clnt_num_curves
)) {
1048 *al
= SSL_AD_INTERNAL_ERROR
;
1049 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1053 /* Get our list of available groups */
1054 if (!tls1_get_curvelist(s
, 0, &pcurves
, &num_curves
)) {
1055 *al
= SSL_AD_INTERNAL_ERROR
;
1056 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1060 /* Find the first group we allow that is also in client's list */
1061 for (i
= 0, pcurvestmp
= pcurves
; i
< num_curves
;
1062 i
++, pcurvestmp
+= 2) {
1063 group_id
= bytestogroup(pcurvestmp
);
1065 if (check_in_list(s
, group_id
, clntcurves
, clnt_num_curves
, 1))
1069 if (i
< num_curves
) {
1070 /* A shared group exists so send a HelloRetryRequest */
1071 s
->s3
->group_id
= group_id
;
1072 s
->hello_retry_request
= 1;
1077 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE
) == 0) {
1078 /* Nothing left we can do - just fail */
1079 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1080 SSLerr(SSL_F_FINAL_KEY_SHARE
, SSL_R_NO_SUITABLE_KEY_SHARE
);
1085 /* We have a key_share so don't send any more HelloRetryRequest messages */
1087 s
->hello_retry_request
= 0;
1090 * For a client side resumption with no key_share we need to generate
1091 * the handshake secret (otherwise this is done during key_share
1094 if (!sent
&& !s
->server
&& !tls13_generate_handshake_secret(s
, NULL
, 0)) {
1095 *al
= SSL_AD_INTERNAL_ERROR
;
1096 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1104 static int init_psk_kex_modes(SSL
*s
, unsigned int context
)
1106 s
->ext
.psk_kex_mode
= TLSEXT_KEX_MODE_FLAG_NONE
;
1110 int tls_psk_do_binder(SSL
*s
, const EVP_MD
*md
, const unsigned char *msgstart
,
1111 size_t binderoffset
, const unsigned char *binderin
,
1112 unsigned char *binderout
,
1113 SSL_SESSION
*sess
, int sign
)
1115 EVP_PKEY
*mackey
= NULL
;
1116 EVP_MD_CTX
*mctx
= NULL
;
1117 unsigned char hash
[EVP_MAX_MD_SIZE
], binderkey
[EVP_MAX_MD_SIZE
];
1118 unsigned char finishedkey
[EVP_MAX_MD_SIZE
], tmpbinder
[EVP_MAX_MD_SIZE
];
1119 const char resumption_label
[] = "resumption psk binder key";
1120 size_t bindersize
, hashsize
= EVP_MD_size(md
);
1123 /* Generate the early_secret */
1124 if (!tls13_generate_secret(s
, md
, NULL
, sess
->master_key
,
1125 sess
->master_key_length
,
1126 (unsigned char *)&s
->early_secret
)) {
1127 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1132 * Create the handshake hash for the binder key...the messages so far are
1135 mctx
= EVP_MD_CTX_new();
1137 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
1138 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1139 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1143 /* Generate the binder key */
1144 if (!tls13_hkdf_expand(s
, md
, s
->early_secret
,
1145 (unsigned char *)resumption_label
,
1146 sizeof(resumption_label
) - 1, hash
, binderkey
,
1148 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1152 /* Generate the finished key */
1153 if (!tls13_derive_finishedkey(s
, md
, binderkey
, finishedkey
, hashsize
)) {
1154 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1158 if (EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0) {
1159 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1164 * Get a hash of the ClientHello up to the start of the binders. If we are
1165 * following a HelloRetryRequest then this includes the hash of the first
1166 * ClientHello and the HelloRetryRequest itself.
1168 if (s
->hello_retry_request
) {
1172 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
1173 if (hdatalen
<= 0) {
1174 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, SSL_R_BAD_HANDSHAKE_LENGTH
);
1179 * For servers the handshake buffer data will include the second
1180 * ClientHello - which we don't want - so we need to take that bit off.
1183 if (hdatalen
< s
->init_num
+ SSL3_HM_HEADER_LENGTH
) {
1184 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1187 hdatalen
-= s
->init_num
+ SSL3_HM_HEADER_LENGTH
;
1190 if (EVP_DigestUpdate(mctx
, hdata
, hdatalen
) <= 0) {
1191 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1196 if (EVP_DigestUpdate(mctx
, msgstart
, binderoffset
) <= 0
1197 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1198 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1202 mackey
= EVP_PKEY_new_mac_key(EVP_PKEY_HMAC
, NULL
, finishedkey
, hashsize
);
1203 if (mackey
== NULL
) {
1204 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1209 binderout
= tmpbinder
;
1211 bindersize
= hashsize
;
1212 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, mackey
) <= 0
1213 || EVP_DigestSignUpdate(mctx
, hash
, hashsize
) <= 0
1214 || EVP_DigestSignFinal(mctx
, binderout
, &bindersize
) <= 0
1215 || bindersize
!= hashsize
) {
1216 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1223 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1224 ret
= (CRYPTO_memcmp(binderin
, binderout
, hashsize
) == 0);
1228 OPENSSL_cleanse(binderkey
, sizeof(binderkey
));
1229 OPENSSL_cleanse(finishedkey
, sizeof(finishedkey
));
1230 EVP_PKEY_free(mackey
);
1231 EVP_MD_CTX_free(mctx
);
1236 static int final_early_data(SSL
*s
, unsigned int context
, int sent
, int *al
)
1238 if (!s
->server
|| !sent
)
1241 if (s
->max_early_data
== 0
1243 || s
->session
->ext
.tick_identity
!= 0
1244 || s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
1245 || !s
->ext
.early_data_ok
1246 || s
->hello_retry_request
1247 || s
->s3
->alpn_selected_len
!= s
->session
->ext
.alpn_selected_len
1248 || memcmp(s
->s3
->alpn_selected
, s
->session
->ext
.alpn_selected
,
1249 s
->s3
->alpn_selected_len
) != 0){
1250 s
->ext
.early_data
= SSL_EARLY_DATA_REJECTED
;
1252 s
->ext
.early_data
= SSL_EARLY_DATA_ACCEPTED
;
1254 if (!tls13_change_cipher_state(s
,
1255 SSL3_CC_EARLY
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
1256 *al
= SSL_AD_INTERNAL_ERROR
;