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 static int init_certificate_authorities(SSL
*s
, unsigned int context
);
34 static int tls_construct_certificate_authorities(SSL
*s
, WPACKET
*pkt
,
35 unsigned int context
, X509
*x
,
36 size_t chainidx
, int *al
);
37 static int tls_parse_certificate_authorities(SSL
*s
, PACKET
*pkt
,
38 unsigned int context
, X509
*x
,
39 size_t chainidx
, int *al
);
40 #ifndef OPENSSL_NO_SRP
41 static int init_srp(SSL
*s
, unsigned int context
);
43 static int init_etm(SSL
*s
, unsigned int context
);
44 static int init_ems(SSL
*s
, unsigned int context
);
45 static int final_ems(SSL
*s
, unsigned int context
, int sent
, int *al
);
46 static int init_psk_kex_modes(SSL
*s
, unsigned int context
);
48 static int final_key_share(SSL
*s
, unsigned int context
, int sent
, int *al
);
50 #ifndef OPENSSL_NO_SRTP
51 static int init_srtp(SSL
*s
, unsigned int context
);
53 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
, int *al
);
54 static int final_early_data(SSL
*s
, unsigned int context
, int sent
, int *al
);
56 /* Structure to define a built-in extension */
57 typedef struct extensions_definition_st
{
58 /* The defined type for the extension */
61 * The context that this extension applies to, e.g. what messages and
66 * Initialise extension before parsing. Always called for relevant contexts
67 * even if extension not present
69 int (*init
)(SSL
*s
, unsigned int context
);
70 /* Parse extension sent from client to server */
71 int (*parse_ctos
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
72 size_t chainidx
, int *al
);
73 /* Parse extension send from server to client */
74 int (*parse_stoc
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
75 size_t chainidx
, int *al
);
76 /* Construct extension sent from server to client */
77 int (*construct_stoc
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
78 size_t chainidx
, int *al
);
79 /* Construct extension sent from client to server */
80 int (*construct_ctos
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
81 size_t chainidx
, int *al
);
83 * Finalise extension after parsing. Always called where an extensions was
84 * initialised even if the extension was not present. |sent| is set to 1 if
85 * the extension was seen, or 0 otherwise.
87 int (*final
)(SSL
*s
, unsigned int context
, int sent
, int *al
);
88 } EXTENSION_DEFINITION
;
91 * Definitions of all built-in extensions. NOTE: Changes in the number or order
92 * of these extensions should be mirrored with equivalent changes to the
93 * indexes ( TLSEXT_IDX_* ) defined in ssl_locl.h.
94 * Each extension has an initialiser, a client and
95 * server side parser and a finaliser. The initialiser is called (if the
96 * extension is relevant to the given context) even if we did not see the
97 * extension in the message that we received. The parser functions are only
98 * called if we see the extension in the message. The finalisers are always
99 * called if the initialiser was called.
100 * There are also server and client side constructor functions which are always
101 * called during message construction if the extension is relevant for the
103 * The initialisation, parsing, finalisation and construction functions are
104 * always called in the order defined in this list. Some extensions may depend
105 * on others having been processed first, so the order of this list is
107 * The extension context is defined by a series of flags which specify which
108 * messages the extension is relevant to. These flags also specify whether the
109 * extension is relevant to a particular protocol or protocol version.
111 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
113 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
114 static const EXTENSION_DEFINITION ext_defs
[] = {
116 TLSEXT_TYPE_renegotiate
,
117 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_SSL3_ALLOWED
118 | EXT_TLS1_2_AND_BELOW_ONLY
,
119 NULL
, tls_parse_ctos_renegotiate
, tls_parse_stoc_renegotiate
,
120 tls_construct_stoc_renegotiate
, tls_construct_ctos_renegotiate
,
124 TLSEXT_TYPE_server_name
,
125 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
126 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
128 tls_parse_ctos_server_name
, tls_parse_stoc_server_name
,
129 tls_construct_stoc_server_name
, tls_construct_ctos_server_name
,
132 #ifndef OPENSSL_NO_SRP
135 EXT_CLIENT_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
136 init_srp
, tls_parse_ctos_srp
, NULL
, NULL
, tls_construct_ctos_srp
, NULL
141 #ifndef OPENSSL_NO_EC
143 TLSEXT_TYPE_ec_point_formats
,
144 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
145 NULL
, tls_parse_ctos_ec_pt_formats
, tls_parse_stoc_ec_pt_formats
,
146 tls_construct_stoc_ec_pt_formats
, tls_construct_ctos_ec_pt_formats
,
150 TLSEXT_TYPE_supported_groups
,
151 EXT_CLIENT_HELLO
| EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
152 NULL
, tls_parse_ctos_supported_groups
, NULL
,
153 NULL
/* TODO(TLS1.3): Need to add this */,
154 tls_construct_ctos_supported_groups
, NULL
161 TLSEXT_TYPE_session_ticket
,
162 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
163 init_session_ticket
, tls_parse_ctos_session_ticket
,
164 tls_parse_stoc_session_ticket
, tls_construct_stoc_session_ticket
,
165 tls_construct_ctos_session_ticket
, NULL
168 TLSEXT_TYPE_signature_algorithms
,
169 EXT_CLIENT_HELLO
| EXT_TLS1_3_CERTIFICATE_REQUEST
,
170 init_sig_algs
, tls_parse_ctos_sig_algs
,
171 tls_parse_ctos_sig_algs
, tls_construct_ctos_sig_algs
,
172 tls_construct_ctos_sig_algs
, final_sig_algs
174 #ifndef OPENSSL_NO_OCSP
176 TLSEXT_TYPE_status_request
,
177 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
178 | EXT_TLS1_3_CERTIFICATE
,
179 init_status_request
, tls_parse_ctos_status_request
,
180 tls_parse_stoc_status_request
, tls_construct_stoc_status_request
,
181 tls_construct_ctos_status_request
, NULL
186 #ifndef OPENSSL_NO_NEXTPROTONEG
188 TLSEXT_TYPE_next_proto_neg
,
189 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
190 init_npn
, tls_parse_ctos_npn
, tls_parse_stoc_npn
,
191 tls_construct_stoc_next_proto_neg
, tls_construct_ctos_npn
, NULL
198 * Must appear in this list after server_name so that finalisation
199 * happens after server_name callbacks
201 TLSEXT_TYPE_application_layer_protocol_negotiation
,
202 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
203 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
,
204 init_alpn
, tls_parse_ctos_alpn
, tls_parse_stoc_alpn
,
205 tls_construct_stoc_alpn
, tls_construct_ctos_alpn
, final_alpn
207 #ifndef OPENSSL_NO_SRTP
209 TLSEXT_TYPE_use_srtp
,
210 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
211 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
| EXT_DTLS_ONLY
,
212 init_srtp
, tls_parse_ctos_use_srtp
, tls_parse_stoc_use_srtp
,
213 tls_construct_stoc_use_srtp
, tls_construct_ctos_use_srtp
, NULL
219 TLSEXT_TYPE_encrypt_then_mac
,
220 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
| EXT_SSL3_ALLOWED
,
221 init_etm
, tls_parse_ctos_etm
, tls_parse_stoc_etm
,
222 tls_construct_stoc_etm
, tls_construct_ctos_etm
, NULL
224 #ifndef OPENSSL_NO_CT
226 TLSEXT_TYPE_signed_certificate_timestamp
,
227 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
228 | EXT_TLS1_3_CERTIFICATE
,
231 * No server side support for this, but can be provided by a custom
232 * extension. This is an exception to the rule that custom extensions
233 * cannot override built in ones.
235 NULL
, tls_parse_stoc_sct
, NULL
, tls_construct_ctos_sct
, NULL
241 TLSEXT_TYPE_extended_master_secret
,
242 EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
243 init_ems
, tls_parse_ctos_ems
, tls_parse_stoc_ems
,
244 tls_construct_stoc_ems
, tls_construct_ctos_ems
, final_ems
247 TLSEXT_TYPE_supported_versions
,
248 EXT_CLIENT_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
| EXT_TLS1_3_ONLY
,
250 /* Processed inline as part of version selection */
251 NULL
, NULL
, NULL
, tls_construct_ctos_supported_versions
, NULL
254 TLSEXT_TYPE_psk_kex_modes
,
255 EXT_CLIENT_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
| EXT_TLS1_3_ONLY
,
256 init_psk_kex_modes
, tls_parse_ctos_psk_kex_modes
, NULL
, NULL
,
257 tls_construct_ctos_psk_kex_modes
, NULL
259 #ifndef OPENSSL_NO_EC
262 * Must be in this list after supported_groups. We need that to have
263 * been parsed before we do this one.
265 TLSEXT_TYPE_key_share
,
266 EXT_CLIENT_HELLO
| EXT_TLS1_3_SERVER_HELLO
267 | EXT_TLS1_3_HELLO_RETRY_REQUEST
| EXT_TLS_IMPLEMENTATION_ONLY
269 NULL
, tls_parse_ctos_key_share
, tls_parse_stoc_key_share
,
270 tls_construct_stoc_key_share
, tls_construct_ctos_key_share
,
276 EXT_CLIENT_HELLO
| EXT_TLS1_3_HELLO_RETRY_REQUEST
277 | EXT_TLS_IMPLEMENTATION_ONLY
| EXT_TLS1_3_ONLY
,
278 NULL
, NULL
, tls_parse_stoc_cookie
, NULL
, tls_construct_ctos_cookie
,
283 * Special unsolicited ServerHello extension only used when
284 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
286 TLSEXT_TYPE_cryptopro_bug
,
287 EXT_TLS1_2_SERVER_HELLO
| EXT_TLS1_2_AND_BELOW_ONLY
,
288 NULL
, NULL
, NULL
, tls_construct_stoc_cryptopro_bug
, NULL
, NULL
291 TLSEXT_TYPE_early_data
,
292 EXT_CLIENT_HELLO
| EXT_TLS1_3_ENCRYPTED_EXTENSIONS
293 | EXT_TLS1_3_NEW_SESSION_TICKET
,
294 NULL
, tls_parse_ctos_early_data
, tls_parse_stoc_early_data
,
295 tls_construct_stoc_early_data
, tls_construct_ctos_early_data
,
299 TLSEXT_TYPE_certificate_authorities
,
300 EXT_CLIENT_HELLO
| EXT_TLS1_3_CERTIFICATE_REQUEST
| EXT_TLS1_3_ONLY
,
301 init_certificate_authorities
,
302 tls_parse_certificate_authorities
, tls_parse_certificate_authorities
,
303 tls_construct_certificate_authorities
,
304 tls_construct_certificate_authorities
, NULL
,
307 /* Must be immediately before pre_shared_key */
311 /* We send this, but don't read it */
312 NULL
, NULL
, NULL
, tls_construct_ctos_padding
, NULL
315 /* Required by the TLSv1.3 spec to always be the last extension */
317 EXT_CLIENT_HELLO
| EXT_TLS1_3_SERVER_HELLO
| EXT_TLS_IMPLEMENTATION_ONLY
319 NULL
, tls_parse_ctos_psk
, tls_parse_stoc_psk
, tls_construct_stoc_psk
,
320 tls_construct_ctos_psk
, NULL
325 * Verify whether we are allowed to use the extension |type| in the current
326 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
327 * indicate the extension is not allowed. If returning 1 then |*found| is set to
328 * 1 if we found a definition for the extension, and |*idx| is set to its index
330 static int verify_extension(SSL
*s
, unsigned int context
, unsigned int type
,
331 custom_ext_methods
*meths
, RAW_EXTENSION
*rawexlist
,
332 RAW_EXTENSION
**found
)
335 size_t builtin_num
= OSSL_NELEM(ext_defs
);
336 const EXTENSION_DEFINITION
*thisext
;
338 for (i
= 0, thisext
= ext_defs
; i
< builtin_num
; i
++, thisext
++) {
339 if (type
== thisext
->type
) {
340 /* Check we're allowed to use this extension in this context */
341 if ((context
& thisext
->context
) == 0)
344 if (SSL_IS_DTLS(s
)) {
345 if ((thisext
->context
& EXT_TLS_ONLY
) != 0)
347 } else if ((thisext
->context
& EXT_DTLS_ONLY
) != 0) {
351 *found
= &rawexlist
[i
];
356 if ((context
& (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) == 0) {
358 * Custom extensions only apply to <=TLS1.2. This extension is unknown
359 * in this context - we allow it
365 /* Check the custom extensions */
367 for (i
= builtin_num
; i
< builtin_num
+ meths
->meths_count
; i
++) {
368 if (meths
->meths
[i
- builtin_num
].ext_type
== type
) {
369 *found
= &rawexlist
[i
];
375 /* Unknown extension. We allow it */
381 * Check whether the context defined for an extension |extctx| means whether
382 * the extension is relevant for the current context |thisctx| or not. Returns
383 * 1 if the extension is relevant for this context, and 0 otherwise
385 static int extension_is_relevant(SSL
*s
, unsigned int extctx
,
386 unsigned int thisctx
)
389 && (extctx
& EXT_TLS_IMPLEMENTATION_ONLY
) != 0)
390 || (s
->version
== SSL3_VERSION
391 && (extctx
& EXT_SSL3_ALLOWED
) == 0)
393 && (extctx
& EXT_TLS1_2_AND_BELOW_ONLY
) != 0)
394 || (!SSL_IS_TLS13(s
) && (extctx
& EXT_TLS1_3_ONLY
) != 0))
401 * Gather a list of all the extensions from the data in |packet]. |context|
402 * tells us which message this extension is for. The raw extension data is
403 * stored in |*res| on success. In the event of an error the alert type to use
404 * is stored in |*al|. We don't actually process the content of the extensions
405 * yet, except to check their types. This function also runs the initialiser
406 * functions for all known extensions (whether we have collected them or not).
407 * If successful the caller is responsible for freeing the contents of |*res|.
409 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
410 * more than one extension of the same type in a ClientHello or ServerHello.
411 * This function returns 1 if all extensions are unique and we have parsed their
412 * types, and 0 if the extensions contain duplicates, could not be successfully
413 * found, or an internal error occurred. We only check duplicates for
414 * extensions that we know about. We ignore others.
416 int tls_collect_extensions(SSL
*s
, PACKET
*packet
, unsigned int context
,
417 RAW_EXTENSION
**res
, int *al
, size_t *len
)
419 PACKET extensions
= *packet
;
422 custom_ext_methods
*exts
= NULL
;
423 RAW_EXTENSION
*raw_extensions
= NULL
;
424 const EXTENSION_DEFINITION
*thisexd
;
429 * Initialise server side custom extensions. Client side is done during
430 * construction of extensions for the ClientHello.
432 if ((context
& EXT_CLIENT_HELLO
) != 0) {
433 exts
= &s
->cert
->srv_ext
;
434 custom_ext_init(&s
->cert
->srv_ext
);
435 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
436 exts
= &s
->cert
->cli_ext
;
439 num_exts
= OSSL_NELEM(ext_defs
) + (exts
!= NULL
? exts
->meths_count
: 0);
440 raw_extensions
= OPENSSL_zalloc(num_exts
* sizeof(*raw_extensions
));
441 if (raw_extensions
== NULL
) {
442 *al
= SSL_AD_INTERNAL_ERROR
;
443 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, ERR_R_MALLOC_FAILURE
);
447 while (PACKET_remaining(&extensions
) > 0) {
450 RAW_EXTENSION
*thisex
;
452 if (!PACKET_get_net_2(&extensions
, &type
) ||
453 !PACKET_get_length_prefixed_2(&extensions
, &extension
)) {
454 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, SSL_R_BAD_EXTENSION
);
455 *al
= SSL_AD_DECODE_ERROR
;
459 * Verify this extension is allowed. We only check duplicates for
460 * extensions that we recognise. We also have a special case for the
461 * PSK extension, which must be the last one in the ClientHello.
463 if (!verify_extension(s
, context
, type
, exts
, raw_extensions
, &thisex
)
464 || (thisex
!= NULL
&& thisex
->present
== 1)
465 || (type
== TLSEXT_TYPE_psk
466 && (context
& EXT_CLIENT_HELLO
) != 0
467 && PACKET_remaining(&extensions
) != 0)) {
468 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS
, SSL_R_BAD_EXTENSION
);
469 *al
= SSL_AD_ILLEGAL_PARAMETER
;
472 if (thisex
!= NULL
) {
473 thisex
->data
= extension
;
480 * Initialise all known extensions relevant to this context, whether we have
483 for (thisexd
= ext_defs
, i
= 0; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
484 if(thisexd
->init
!= NULL
&& (thisexd
->context
& context
) != 0
485 && extension_is_relevant(s
, thisexd
->context
, context
)
486 && !thisexd
->init(s
, context
)) {
487 *al
= SSL_AD_INTERNAL_ERROR
;
492 *res
= raw_extensions
;
498 OPENSSL_free(raw_extensions
);
503 * Runs the parser for a given extension with index |idx|. |exts| contains the
504 * list of all parsed extensions previously collected by
505 * tls_collect_extensions(). The parser is only run if it is applicable for the
506 * given |context| and the parser has not already been run. If this is for a
507 * Certificate message, then we also provide the parser with the relevant
508 * Certificate |x| and its position in the |chainidx| with 0 being the first
509 * Certificate. Returns 1 on success or 0 on failure. In the event of a failure
510 * |*al| is populated with a suitable alert code. If an extension is not present
511 * this counted as success.
513 int tls_parse_extension(SSL
*s
, TLSEXT_INDEX idx
, int context
,
514 RAW_EXTENSION
*exts
, X509
*x
, size_t chainidx
, int *al
)
516 RAW_EXTENSION
*currext
= &exts
[idx
];
517 int (*parser
)(SSL
*s
, PACKET
*pkt
, unsigned int context
, X509
*x
,
518 size_t chainidx
, int *al
) = NULL
;
520 /* Skip if the extension is not present */
521 if (!currext
->present
)
525 s
->ext
.debug_cb(s
, !s
->server
, currext
->type
,
526 PACKET_data(&currext
->data
),
527 PACKET_remaining(&currext
->data
),
530 /* Skip if we've already parsed this extension */
536 if (idx
< OSSL_NELEM(ext_defs
)) {
537 /* We are handling a built-in extension */
538 const EXTENSION_DEFINITION
*extdef
= &ext_defs
[idx
];
540 /* Check if extension is defined for our protocol. If not, skip */
541 if (!extension_is_relevant(s
, extdef
->context
, context
))
544 parser
= s
->server
? extdef
->parse_ctos
: extdef
->parse_stoc
;
547 return parser(s
, &currext
->data
, context
, x
, chainidx
, al
);
550 * If the parser is NULL we fall through to the custom extension
556 * This is a custom extension. We only allow this if it is a non
557 * resumed session on the server side.
559 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
560 * We're going to need a new mechanism for TLS1.3 to specify which
561 * messages to add the custom extensions to.
563 if ((!s
->hit
|| !s
->server
)
565 & (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) != 0
566 && custom_ext_parse(s
, s
->server
, currext
->type
,
567 PACKET_data(&currext
->data
),
568 PACKET_remaining(&currext
->data
),
576 * Parse all remaining extensions that have not yet been parsed. Also calls the
577 * finalisation for all extensions at the end, whether we collected them or not.
578 * Returns 1 for success or 0 for failure. If we are working on a Certificate
579 * message then we also pass the Certificate |x| and its position in the
580 * |chainidx|, with 0 being the first certificate. On failure, |*al| is
581 * populated with a suitable alert code.
583 int tls_parse_all_extensions(SSL
*s
, int context
, RAW_EXTENSION
*exts
, X509
*x
,
584 size_t chainidx
, int *al
)
586 size_t i
, numexts
= OSSL_NELEM(ext_defs
);
587 const EXTENSION_DEFINITION
*thisexd
;
589 /* Calculate the number of extensions in the extensions list */
590 if ((context
& EXT_CLIENT_HELLO
) != 0) {
591 numexts
+= s
->cert
->srv_ext
.meths_count
;
592 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
593 numexts
+= s
->cert
->cli_ext
.meths_count
;
596 /* Parse each extension in turn */
597 for (i
= 0; i
< numexts
; i
++) {
598 if (!tls_parse_extension(s
, i
, context
, exts
, x
, chainidx
, al
))
603 * Finalise all known extensions relevant to this context, whether we have
606 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
607 if(thisexd
->final
!= NULL
608 && (thisexd
->context
& context
) != 0
609 && !thisexd
->final(s
, context
, exts
[i
].present
, al
))
617 * Construct all the extensions relevant to the current |context| and write
618 * them to |pkt|. If this is an extension for a Certificate in a Certificate
619 * message, then |x| will be set to the Certificate we are handling, and
620 * |chainidx| will indicate the position in the chainidx we are processing (with
621 * 0 being the first in the chain). Returns 1 on success or 0 on failure. If a
622 * failure occurs then |al| is populated with a suitable alert code. On a
623 * failure construction stops at the first extension to fail to construct.
625 int tls_construct_extensions(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
626 X509
*x
, size_t chainidx
, int *al
)
629 int addcustom
= 0, min_version
, max_version
= 0, reason
, tmpal
;
630 const EXTENSION_DEFINITION
*thisexd
;
633 * Normally if something goes wrong during construction it's an internal
634 * error. We can always override this later.
636 tmpal
= SSL_AD_INTERNAL_ERROR
;
638 if (!WPACKET_start_sub_packet_u16(pkt
)
640 * If extensions are of zero length then we don't even add the
641 * extensions length bytes to a ClientHello/ServerHello in SSLv3
643 || ((context
& (EXT_CLIENT_HELLO
| EXT_TLS1_2_SERVER_HELLO
)) != 0
644 && s
->version
== SSL3_VERSION
645 && !WPACKET_set_flags(pkt
,
646 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))) {
647 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
651 if ((context
& EXT_CLIENT_HELLO
) != 0) {
652 reason
= ssl_get_client_min_max_version(s
, &min_version
, &max_version
);
654 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, reason
);
659 /* Add custom extensions first */
660 if ((context
& EXT_CLIENT_HELLO
) != 0) {
661 custom_ext_init(&s
->cert
->cli_ext
);
663 } else if ((context
& EXT_TLS1_2_SERVER_HELLO
) != 0) {
665 * We already initialised the custom extensions during ClientHello
668 * TODO(TLS1.3): We're going to need a new custom extension mechanism
669 * for TLS1.3, so that custom extensions can specify which of the
670 * multiple message they wish to add themselves to.
675 if (addcustom
&& !custom_ext_add(s
, s
->server
, pkt
, &tmpal
)) {
676 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
680 for (i
= 0, thisexd
= ext_defs
; i
< OSSL_NELEM(ext_defs
); i
++, thisexd
++) {
681 int (*construct
)(SSL
*s
, WPACKET
*pkt
, unsigned int context
, X509
*x
,
682 size_t chainidx
, int *al
);
684 /* Skip if not relevant for our context */
685 if ((thisexd
->context
& context
) == 0)
688 construct
= s
->server
? thisexd
->construct_stoc
689 : thisexd
->construct_ctos
;
691 /* Check if this extension is defined for our protocol. If not, skip */
693 && (thisexd
->context
& EXT_TLS_IMPLEMENTATION_ONLY
)
695 || (s
->version
== SSL3_VERSION
696 && (thisexd
->context
& EXT_SSL3_ALLOWED
) == 0)
698 && (thisexd
->context
& EXT_TLS1_2_AND_BELOW_ONLY
)
701 && (thisexd
->context
& EXT_TLS1_3_ONLY
) != 0
702 && (context
& EXT_CLIENT_HELLO
) == 0)
703 || ((thisexd
->context
& EXT_TLS1_3_ONLY
) != 0
704 && (context
& EXT_CLIENT_HELLO
) != 0
705 && (SSL_IS_DTLS(s
) || max_version
< TLS1_3_VERSION
))
706 || construct
== NULL
)
709 if (!construct(s
, pkt
, context
, x
, chainidx
, &tmpal
))
713 if (!WPACKET_close(pkt
)) {
714 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS
, ERR_R_INTERNAL_ERROR
);
726 * Built in extension finalisation and initialisation functions. All initialise
727 * or finalise the associated extension type for the given |context|. For
728 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
729 * otherwise. These functions return 1 on success or 0 on failure. In the event
730 * of a failure then |*al| is populated with a suitable error code.
733 static int final_renegotiate(SSL
*s
, unsigned int context
, int sent
,
738 * Check if we can connect to a server that doesn't support safe
741 if (!(s
->options
& SSL_OP_LEGACY_SERVER_CONNECT
)
742 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
744 *al
= SSL_AD_HANDSHAKE_FAILURE
;
745 SSLerr(SSL_F_FINAL_RENEGOTIATE
,
746 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
753 /* Need RI if renegotiating */
755 && !(s
->options
& SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)
757 *al
= SSL_AD_HANDSHAKE_FAILURE
;
758 SSLerr(SSL_F_FINAL_RENEGOTIATE
,
759 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
767 static int init_server_name(SSL
*s
, unsigned int context
)
770 s
->servername_done
= 0;
775 static int final_server_name(SSL
*s
, unsigned int context
, int sent
,
778 int ret
= SSL_TLSEXT_ERR_NOACK
;
779 int altmp
= SSL_AD_UNRECOGNIZED_NAME
;
781 if (s
->ctx
!= NULL
&& s
->ctx
->ext
.servername_cb
!= 0)
782 ret
= s
->ctx
->ext
.servername_cb(s
, &altmp
,
783 s
->ctx
->ext
.servername_arg
);
784 else if (s
->session_ctx
!= NULL
785 && s
->session_ctx
->ext
.servername_cb
!= 0)
786 ret
= s
->session_ctx
->ext
.servername_cb(s
, &altmp
,
787 s
->session_ctx
->ext
.servername_arg
);
790 case SSL_TLSEXT_ERR_ALERT_FATAL
:
794 case SSL_TLSEXT_ERR_ALERT_WARNING
:
798 case SSL_TLSEXT_ERR_NOACK
:
799 s
->servername_done
= 0;
807 #ifndef OPENSSL_NO_EC
808 static int final_ec_pt_formats(SSL
*s
, unsigned int context
, int sent
,
811 unsigned long alg_k
, alg_a
;
816 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
817 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
820 * If we are client and using an elliptic curve cryptography cipher
821 * suite, then if server returns an EC point formats lists extension it
822 * must contain uncompressed.
824 if (s
->ext
.ecpointformats
!= NULL
825 && s
->ext
.ecpointformats_len
> 0
826 && s
->session
->ext
.ecpointformats
!= NULL
827 && s
->session
->ext
.ecpointformats_len
> 0
828 && ((alg_k
& SSL_kECDHE
) || (alg_a
& SSL_aECDSA
))) {
829 /* we are using an ECC cipher */
831 unsigned char *list
= s
->session
->ext
.ecpointformats
;
833 for (i
= 0; i
< s
->session
->ext
.ecpointformats_len
; i
++) {
834 if (*list
++ == TLSEXT_ECPOINTFORMAT_uncompressed
)
837 if (i
== s
->session
->ext
.ecpointformats_len
) {
838 SSLerr(SSL_F_FINAL_EC_PT_FORMATS
,
839 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST
);
848 static int init_session_ticket(SSL
*s
, unsigned int context
)
851 s
->ext
.ticket_expected
= 0;
856 #ifndef OPENSSL_NO_OCSP
857 static int init_status_request(SSL
*s
, unsigned int context
)
860 s
->ext
.status_type
= TLSEXT_STATUSTYPE_nothing
;
863 * Ensure we get sensible values passed to tlsext_status_cb in the event
864 * that we don't receive a status message
866 OPENSSL_free(s
->ext
.ocsp
.resp
);
867 s
->ext
.ocsp
.resp
= NULL
;
868 s
->ext
.ocsp
.resp_len
= 0;
875 #ifndef OPENSSL_NO_NEXTPROTONEG
876 static int init_npn(SSL
*s
, unsigned int context
)
884 static int init_alpn(SSL
*s
, unsigned int context
)
886 OPENSSL_free(s
->s3
->alpn_selected
);
887 s
->s3
->alpn_selected
= NULL
;
888 s
->s3
->alpn_selected_len
= 0;
890 OPENSSL_free(s
->s3
->alpn_proposed
);
891 s
->s3
->alpn_proposed
= NULL
;
892 s
->s3
->alpn_proposed_len
= 0;
897 static int final_alpn(SSL
*s
, unsigned int context
, int sent
, int *al
)
899 const unsigned char *selected
= NULL
;
900 unsigned char selected_len
= 0;
905 if (s
->ctx
->ext
.alpn_select_cb
!= NULL
&& s
->s3
->alpn_proposed
!= NULL
) {
906 int r
= s
->ctx
->ext
.alpn_select_cb(s
, &selected
, &selected_len
,
907 s
->s3
->alpn_proposed
,
908 (unsigned int)s
->s3
->alpn_proposed_len
,
909 s
->ctx
->ext
.alpn_select_cb_arg
);
911 if (r
== SSL_TLSEXT_ERR_OK
) {
912 OPENSSL_free(s
->s3
->alpn_selected
);
913 s
->s3
->alpn_selected
= OPENSSL_memdup(selected
, selected_len
);
914 if (s
->s3
->alpn_selected
== NULL
) {
915 *al
= SSL_AD_INTERNAL_ERROR
;
918 s
->s3
->alpn_selected_len
= selected_len
;
919 #ifndef OPENSSL_NO_NEXTPROTONEG
920 /* ALPN takes precedence over NPN. */
924 *al
= SSL_AD_NO_APPLICATION_PROTOCOL
;
932 static int init_sig_algs(SSL
*s
, unsigned int context
)
934 /* Clear any signature algorithms extension received */
935 OPENSSL_free(s
->s3
->tmp
.peer_sigalgs
);
936 s
->s3
->tmp
.peer_sigalgs
= NULL
;
941 #ifndef OPENSSL_NO_SRP
942 static int init_srp(SSL
*s
, unsigned int context
)
944 OPENSSL_free(s
->srp_ctx
.login
);
945 s
->srp_ctx
.login
= NULL
;
951 static int init_etm(SSL
*s
, unsigned int context
)
958 static int init_ems(SSL
*s
, unsigned int context
)
961 s
->s3
->flags
&= ~TLS1_FLAGS_RECEIVED_EXTMS
;
966 static int final_ems(SSL
*s
, unsigned int context
, int sent
, int *al
)
968 if (!s
->server
&& s
->hit
) {
970 * Check extended master secret extension is consistent with
973 if (!(s
->s3
->flags
& TLS1_FLAGS_RECEIVED_EXTMS
) !=
974 !(s
->session
->flags
& SSL_SESS_FLAG_EXTMS
)) {
975 *al
= SSL_AD_HANDSHAKE_FAILURE
;
976 SSLerr(SSL_F_FINAL_EMS
, SSL_R_INCONSISTENT_EXTMS
);
984 static int init_certificate_authorities(SSL
*s
, unsigned int context
)
986 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
987 s
->s3
->tmp
.ca_names
= NULL
;
991 static int tls_construct_certificate_authorities(SSL
*s
, WPACKET
*pkt
,
992 unsigned int context
, X509
*x
,
993 size_t chainidx
, int *al
)
995 STACK_OF(X509_NAME
) *ca_sk
= SSL_get_client_CA_list(s
);
997 if (ca_sk
== NULL
|| sk_X509_NAME_num(ca_sk
) == 0)
1000 if (!WPACKET_put_bytes_u16(pkt
, TLSEXT_TYPE_certificate_authorities
)
1001 || !WPACKET_start_sub_packet_u16(pkt
)
1002 || !construct_ca_names(s
, pkt
)
1003 || !WPACKET_close(pkt
)) {
1004 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES
,
1005 ERR_R_INTERNAL_ERROR
);
1012 static int tls_parse_certificate_authorities(SSL
*s
, PACKET
*pkt
,
1013 unsigned int context
, X509
*x
,
1014 size_t chainidx
, int *al
)
1016 if (!parse_ca_names(s
, pkt
, al
))
1018 if (PACKET_remaining(pkt
) != 0) {
1019 *al
= SSL_AD_DECODE_ERROR
;
1025 #ifndef OPENSSL_NO_SRTP
1026 static int init_srtp(SSL
*s
, unsigned int context
)
1029 s
->srtp_profile
= NULL
;
1035 static int final_sig_algs(SSL
*s
, unsigned int context
, int sent
, int *al
)
1037 if (!sent
&& SSL_IS_TLS13(s
)) {
1038 *al
= TLS13_AD_MISSING_EXTENSION
;
1039 SSLerr(SSL_F_FINAL_SIG_ALGS
, SSL_R_MISSING_SIGALGS_EXTENSION
);
1046 #ifndef OPENSSL_NO_EC
1047 static int final_key_share(SSL
*s
, unsigned int context
, int sent
, int *al
)
1049 if (!SSL_IS_TLS13(s
))
1056 * we have no key_share
1058 * (we are not resuming
1059 * OR the kex_mode doesn't allow non key_share resumes)
1066 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE
) == 0)) {
1067 /* Nothing left we can do - just fail */
1068 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1069 SSLerr(SSL_F_FINAL_KEY_SHARE
, SSL_R_NO_SUITABLE_KEY_SHARE
);
1076 * we have no key_share
1079 * we didn't already send a HelloRetryRequest
1081 * the client sent a key_share extension
1083 * (we are not resuming
1084 * OR the kex_mode allows key_share resumes)
1086 * a shared group exists
1088 * send a HelloRetryRequest
1090 * we are not resuming
1092 * the kex_mode doesn't allow non key_share resumes
1096 if (s
->server
&& s
->s3
->peer_tmp
== NULL
) {
1097 /* No suitable share */
1098 if (s
->hello_retry_request
== 0 && sent
1100 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE_DHE
)
1102 const unsigned char *pcurves
, *pcurvestmp
, *clntcurves
;
1103 size_t num_curves
, clnt_num_curves
, i
;
1104 unsigned int group_id
= 0;
1106 /* Check if a shared group exists */
1108 /* Get the clients list of supported groups. */
1109 if (!tls1_get_curvelist(s
, 1, &clntcurves
, &clnt_num_curves
)) {
1110 *al
= SSL_AD_INTERNAL_ERROR
;
1111 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1115 /* Get our list of available groups */
1116 if (!tls1_get_curvelist(s
, 0, &pcurves
, &num_curves
)) {
1117 *al
= SSL_AD_INTERNAL_ERROR
;
1118 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1122 /* Find the first group we allow that is also in client's list */
1123 for (i
= 0, pcurvestmp
= pcurves
; i
< num_curves
;
1124 i
++, pcurvestmp
+= 2) {
1125 group_id
= bytestogroup(pcurvestmp
);
1127 if (check_in_list(s
, group_id
, clntcurves
, clnt_num_curves
, 1))
1131 if (i
< num_curves
) {
1132 /* A shared group exists so send a HelloRetryRequest */
1133 s
->s3
->group_id
= group_id
;
1134 s
->hello_retry_request
= 1;
1139 || (s
->ext
.psk_kex_mode
& TLSEXT_KEX_MODE_FLAG_KE
) == 0) {
1140 /* Nothing left we can do - just fail */
1141 *al
= SSL_AD_HANDSHAKE_FAILURE
;
1142 SSLerr(SSL_F_FINAL_KEY_SHARE
, SSL_R_NO_SUITABLE_KEY_SHARE
);
1147 /* We have a key_share so don't send any more HelloRetryRequest messages */
1149 s
->hello_retry_request
= 0;
1152 * For a client side resumption with no key_share we need to generate
1153 * the handshake secret (otherwise this is done during key_share
1156 if (!sent
&& !s
->server
&& !tls13_generate_handshake_secret(s
, NULL
, 0)) {
1157 *al
= SSL_AD_INTERNAL_ERROR
;
1158 SSLerr(SSL_F_FINAL_KEY_SHARE
, ERR_R_INTERNAL_ERROR
);
1166 static int init_psk_kex_modes(SSL
*s
, unsigned int context
)
1168 s
->ext
.psk_kex_mode
= TLSEXT_KEX_MODE_FLAG_NONE
;
1172 int tls_psk_do_binder(SSL
*s
, const EVP_MD
*md
, const unsigned char *msgstart
,
1173 size_t binderoffset
, const unsigned char *binderin
,
1174 unsigned char *binderout
,
1175 SSL_SESSION
*sess
, int sign
)
1177 EVP_PKEY
*mackey
= NULL
;
1178 EVP_MD_CTX
*mctx
= NULL
;
1179 unsigned char hash
[EVP_MAX_MD_SIZE
], binderkey
[EVP_MAX_MD_SIZE
];
1180 unsigned char finishedkey
[EVP_MAX_MD_SIZE
], tmpbinder
[EVP_MAX_MD_SIZE
];
1181 const char resumption_label
[] = "resumption psk binder key";
1182 size_t bindersize
, hashsize
= EVP_MD_size(md
);
1185 /* Generate the early_secret */
1186 if (!tls13_generate_secret(s
, md
, NULL
, sess
->master_key
,
1187 sess
->master_key_length
,
1188 (unsigned char *)&s
->early_secret
)) {
1189 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1194 * Create the handshake hash for the binder key...the messages so far are
1197 mctx
= EVP_MD_CTX_new();
1199 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
1200 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1201 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1205 /* Generate the binder key */
1206 if (!tls13_hkdf_expand(s
, md
, s
->early_secret
,
1207 (unsigned char *)resumption_label
,
1208 sizeof(resumption_label
) - 1, hash
, binderkey
,
1210 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1214 /* Generate the finished key */
1215 if (!tls13_derive_finishedkey(s
, md
, binderkey
, finishedkey
, hashsize
)) {
1216 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1220 if (EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0) {
1221 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1226 * Get a hash of the ClientHello up to the start of the binders. If we are
1227 * following a HelloRetryRequest then this includes the hash of the first
1228 * ClientHello and the HelloRetryRequest itself.
1230 if (s
->hello_retry_request
) {
1234 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
1235 if (hdatalen
<= 0) {
1236 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, SSL_R_BAD_HANDSHAKE_LENGTH
);
1241 * For servers the handshake buffer data will include the second
1242 * ClientHello - which we don't want - so we need to take that bit off.
1245 if (hdatalen
< s
->init_num
+ SSL3_HM_HEADER_LENGTH
) {
1246 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1249 hdatalen
-= s
->init_num
+ SSL3_HM_HEADER_LENGTH
;
1252 if (EVP_DigestUpdate(mctx
, hdata
, hdatalen
) <= 0) {
1253 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1258 if (EVP_DigestUpdate(mctx
, msgstart
, binderoffset
) <= 0
1259 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
1260 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1264 mackey
= EVP_PKEY_new_mac_key(EVP_PKEY_HMAC
, NULL
, finishedkey
, hashsize
);
1265 if (mackey
== NULL
) {
1266 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1271 binderout
= tmpbinder
;
1273 bindersize
= hashsize
;
1274 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, mackey
) <= 0
1275 || EVP_DigestSignUpdate(mctx
, hash
, hashsize
) <= 0
1276 || EVP_DigestSignFinal(mctx
, binderout
, &bindersize
) <= 0
1277 || bindersize
!= hashsize
) {
1278 SSLerr(SSL_F_TLS_PSK_DO_BINDER
, ERR_R_INTERNAL_ERROR
);
1285 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1286 ret
= (CRYPTO_memcmp(binderin
, binderout
, hashsize
) == 0);
1290 OPENSSL_cleanse(binderkey
, sizeof(binderkey
));
1291 OPENSSL_cleanse(finishedkey
, sizeof(finishedkey
));
1292 EVP_PKEY_free(mackey
);
1293 EVP_MD_CTX_free(mctx
);
1298 static int final_early_data(SSL
*s
, unsigned int context
, int sent
, int *al
)
1300 if (!s
->server
|| !sent
)
1303 if (s
->max_early_data
== 0
1305 || s
->session
->ext
.tick_identity
!= 0
1306 || s
->early_data_state
!= SSL_EARLY_DATA_ACCEPTING
1307 || !s
->ext
.early_data_ok
1308 || s
->hello_retry_request
1309 || s
->s3
->alpn_selected_len
!= s
->session
->ext
.alpn_selected_len
1310 || (s
->s3
->alpn_selected_len
> 0
1311 && memcmp(s
->s3
->alpn_selected
, s
->session
->ext
.alpn_selected
,
1312 s
->s3
->alpn_selected_len
) != 0)) {
1313 s
->ext
.early_data
= SSL_EARLY_DATA_REJECTED
;
1315 s
->ext
.early_data
= SSL_EARLY_DATA_ACCEPTED
;
1317 if (!tls13_change_cipher_state(s
,
1318 SSL3_CC_EARLY
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
1319 *al
= SSL_AD_INTERNAL_ERROR
;