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
10 #include "../ssl_locl.h"
11 #include "statem_locl.h"
13 static int final_renegotiate(SSL *s, unsigned int context, int sent,
15 static int init_server_name(SSL *s, unsigned int context);
16 static int final_server_name(SSL *s, unsigned int context, int sent,
19 static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
22 static int init_session_ticket(SSL *s, unsigned int context);
23 #ifndef OPENSSL_NO_OCSP
24 static int init_status_request(SSL *s, unsigned int context);
25 static int final_status_request(SSL *s, unsigned int context, int sent,
28 #ifndef OPENSSL_NO_NEXTPROTONEG
29 static int init_npn(SSL *s, unsigned int context);
31 static int init_alpn(SSL *s, unsigned int context);
32 static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
33 static int init_sig_algs(SSL *s, unsigned int context);
34 #ifndef OPENSSL_NO_SRP
35 static int init_srp(SSL *s, unsigned int context);
37 static int init_etm(SSL *s, unsigned int context);
38 static int init_ems(SSL *s, unsigned int context);
39 static int final_ems(SSL *s, unsigned int context, int sent, int *al);
40 #ifndef OPENSSL_NO_SRTP
41 static int init_srtp(SSL *s, unsigned int context);
44 /* Structure to define a built-in extension */
45 typedef struct extensions_definition_st {
46 /* The defined type for the extension */
49 * The context that this extension applies to, e.g. what messages and
54 * Initialise extension before parsing. Always called for relevant contexts
55 * even if extension not present
57 int (*init)(SSL *s, unsigned int context);
58 /* Parse extension sent from client to server */
59 int (*parse_ctos)(SSL *s, PACKET *pkt, int *al);
60 /* Parse extension send from server to client */
61 int (*parse_stoc)(SSL *s, PACKET *pkt, int *al);
62 /* Construct extension sent from server to client */
63 int (*construct_stoc)(SSL *s, WPACKET *pkt, X509 *x, size_t chain,
65 /* Construct extension sent from client to server */
66 int (*construct_ctos)(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al);
68 * Finalise extension after parsing. Always called where an extensions was
69 * initialised even if the extension was not present. |sent| is set to 1 if
70 * the extension was seen, or 0 otherwise.
72 int (*final)(SSL *s, unsigned int context, int sent, int *al);
73 } EXTENSION_DEFINITION;
76 * Definitions of all built-in extensions. NOTE: Changes in the number or order
77 * of these extensions should be mirrored with equivalent changes to the indexes
78 * defined in statem_locl.h.
79 * Each extension has an initialiser, a client and
80 * server side parser and a finaliser. The initialiser is called (if the
81 * extension is relevant to the given context) even if we did not see the
82 * extension in the message that we received. The parser functions are only
83 * called if we see the extension in the message. The finalisers are always
84 * called if the initialiser was called.
85 * There are also server and client side constructor functions which are always
86 * called during message construction if the extension is relevant for the
88 * The initialisation, parsing, finalisation and construction functions are
89 * always called in the order defined in this list. Some extensions may depend
90 * on others having been processed first, so the order of this list is
92 * The extension context is defined by a series of flags which specify which
93 * messages the extension is relevant to. These flags also specify whether the
94 * extension is relevant to a paricular protocol or protocol version.
96 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
98 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
99 static const EXTENSION_DEFINITION ext_defs[] = {
101 TLSEXT_TYPE_renegotiate,
102 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
103 | EXT_TLS1_2_AND_BELOW_ONLY,
104 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
105 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
109 TLSEXT_TYPE_server_name,
110 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
111 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
113 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
114 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
117 #ifndef OPENSSL_NO_SRP
120 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
121 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
126 #ifndef OPENSSL_NO_EC
128 TLSEXT_TYPE_ec_point_formats,
129 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
130 NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
131 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
135 TLSEXT_TYPE_supported_groups,
136 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
137 NULL, tls_parse_ctos_supported_groups, NULL,
138 NULL /* TODO(TLS1.3): Need to add this */,
139 tls_construct_ctos_supported_groups, NULL
146 TLSEXT_TYPE_session_ticket,
147 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
148 init_session_ticket, tls_parse_ctos_session_ticket,
149 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
150 tls_construct_ctos_session_ticket, NULL
153 TLSEXT_TYPE_signature_algorithms,
155 init_sig_algs, tls_parse_ctos_sig_algs, NULL, NULL,
156 tls_construct_ctos_sig_algs, NULL
158 #ifndef OPENSSL_NO_OCSP
160 TLSEXT_TYPE_status_request,
161 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
162 | EXT_TLS1_3_CERTIFICATE,
163 init_status_request, tls_parse_ctos_status_request,
164 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
165 tls_construct_ctos_status_request, final_status_request
170 #ifndef OPENSSL_NO_NEXTPROTONEG
172 TLSEXT_TYPE_next_proto_neg,
173 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
174 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
175 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
182 * Must appear in this list after server_name so that finalisation
183 * happens after server_name callbacks
185 TLSEXT_TYPE_application_layer_protocol_negotiation,
186 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
187 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
188 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
189 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
191 #ifndef OPENSSL_NO_SRTP
193 TLSEXT_TYPE_use_srtp,
194 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
195 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY,
196 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
197 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
203 TLSEXT_TYPE_encrypt_then_mac,
204 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
205 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
206 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
208 #ifndef OPENSSL_NO_CT
210 TLSEXT_TYPE_signed_certificate_timestamp,
211 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
212 | EXT_TLS1_3_CERTIFICATE,
215 * No server side support for this, but can be provided by a custom
216 * extension. This is an exception to the rule that custom extensions
217 * cannot override built in ones.
219 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
225 TLSEXT_TYPE_extended_master_secret,
226 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
227 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
228 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
231 TLSEXT_TYPE_supported_versions,
232 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY,
234 /* Processed inline as part of version selection */
235 NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL
239 * Must be in this list after supported_groups. We need that to have
240 * been parsed before we do this one.
242 TLSEXT_TYPE_key_share,
243 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
244 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
246 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
247 tls_construct_stoc_key_share, tls_construct_ctos_key_share, NULL
251 * Special unsolicited ServerHello extension only used when
252 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
254 TLSEXT_TYPE_cryptopro_bug,
255 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
256 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
259 /* Last in the list because it must be added as the last extension */
263 /* We send this, but don't read it */
264 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
269 * Verify whether we are allowed to use the extension |type| in the current
270 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
271 * indicate the extension is not allowed. If returning 1 then |*found| is set to
272 * 1 if we found a definition for the extension, and |*idx| is set to its index
274 static int verify_extension(SSL *s, unsigned int context, unsigned int type,
275 custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
276 RAW_EXTENSION **found)
279 size_t builtin_num = OSSL_NELEM(ext_defs);
280 const EXTENSION_DEFINITION *thisext;
282 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
283 if (type == thisext->type) {
284 /* Check we're allowed to use this extension in this context */
285 if ((context & thisext->context) == 0)
288 if (SSL_IS_DTLS(s)) {
289 if ((thisext->context & EXT_TLS_ONLY) != 0)
291 } else if ((thisext->context & EXT_DTLS_ONLY) != 0) {
295 *found = &rawexlist[i];
300 if ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) == 0) {
302 * Custom extensions only apply to <=TLS1.2. This extension is unknown
303 * in this context - we allow it
309 /* Check the custom extensions */
311 for (i = builtin_num; i < builtin_num + meths->meths_count; i++) {
312 if (meths->meths[i - builtin_num].ext_type == type) {
313 *found = &rawexlist[i];
319 /* Unknown extension. We allow it */
325 * Check whether the context defined for an extension |extctx| means whether
326 * the extension is relevant for the current context |thisctx| or not. Returns
327 * 1 if the extension is relevant for this context, and 0 otherwise
329 static int extension_is_relevant(SSL *s, unsigned int extctx,
330 unsigned int thisctx)
333 && (extctx & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
334 || (s->version == SSL3_VERSION
335 && (extctx & EXT_SSL3_ALLOWED) == 0)
337 && (extctx & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
338 || (!SSL_IS_TLS13(s) && (extctx & EXT_TLS1_3_ONLY) != 0))
345 * Gather a list of all the extensions from the data in |packet]. |context|
346 * tells us which message this extension is for. The raw extension data is
347 * stored in |*res| on success. In the event of an error the alert type to use
348 * is stored in |*al|. We don't actually process the content of the extensions
349 * yet, except to check their types. This function also runs the initialiser
350 * functions for all known extensions (whether we have collected them or not).
351 * If successful the caller is responsible for freeing the contents of |*res|.
353 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
354 * more than one extension of the same type in a ClientHello or ServerHello.
355 * This function returns 1 if all extensions are unique and we have parsed their
356 * types, and 0 if the extensions contain duplicates, could not be successfully
357 * found, or an internal error occurred. We only check duplicates for
358 * extensions that we know about. We ignore others.
360 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
361 RAW_EXTENSION **res, int *al)
363 PACKET extensions = *packet;
365 custom_ext_methods *exts = NULL;
366 RAW_EXTENSION *raw_extensions = NULL;
367 const EXTENSION_DEFINITION *thisexd;
372 * Initialise server side custom extensions. Client side is done during
373 * construction of extensions for the ClientHello.
375 if ((context & EXT_CLIENT_HELLO) != 0) {
376 exts = &s->cert->srv_ext;
377 custom_ext_init(&s->cert->srv_ext);
378 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
379 exts = &s->cert->cli_ext;
382 raw_extensions = OPENSSL_zalloc((OSSL_NELEM(ext_defs)
383 + (exts != NULL ? exts->meths_count : 0))
384 * sizeof(*raw_extensions));
385 if (raw_extensions == NULL) {
386 *al = SSL_AD_INTERNAL_ERROR;
387 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
391 while (PACKET_remaining(&extensions) > 0) {
394 RAW_EXTENSION *thisex;
396 if (!PACKET_get_net_2(&extensions, &type) ||
397 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
398 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
399 *al = SSL_AD_DECODE_ERROR;
403 * Verify this extension is allowed. We only check duplicates for
404 * extensions that we recognise.
406 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
407 || (thisex != NULL && thisex->present == 1)) {
408 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
409 *al = SSL_AD_ILLEGAL_PARAMETER;
412 if (thisex != NULL) {
413 thisex->data = extension;
420 * Initialise all known extensions relevant to this context, whether we have
423 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
424 if(thisexd->init != NULL && (thisexd->context & context) != 0
425 && extension_is_relevant(s, thisexd->context, context)
426 && !thisexd->init(s, context)) {
427 *al = SSL_AD_INTERNAL_ERROR;
432 *res = raw_extensions;
436 OPENSSL_free(raw_extensions);
441 * Runs the parser for a given extension with index |idx|. |exts| contains the
442 * list of all parsed extensions previously collected by
443 * tls_collect_extensions(). The parser is only run if it is applicable for the
444 * given |context| and the parser has not already been run. Returns 1 on success
445 * or 0 on failure. In the event of a failure |*al| is populated with a suitable
446 * alert code. If an extension is not present this counted as success.
448 int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
449 RAW_EXTENSION *exts, int *al)
451 RAW_EXTENSION *currext = &exts[idx];
452 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
454 /* Skip if the extension is not present */
455 if (!currext->present)
458 if (s->tlsext_debug_cb)
459 s->tlsext_debug_cb(s, !s->server, currext->type,
460 PACKET_data(&currext->data),
461 PACKET_remaining(&currext->data),
462 s->tlsext_debug_arg);
464 /* Skip if we've already parsed this extension */
470 if (idx < OSSL_NELEM(ext_defs)) {
471 /* We are handling a built-in extension */
472 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
474 /* Check if extension is defined for our protocol. If not, skip */
475 if (!extension_is_relevant(s, extdef->context, context))
478 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
481 return parser(s, &currext->data, al);
484 * If the parser is NULL we fall through to the custom extension
490 * This is a custom extension. We only allow this if it is a non
491 * resumed session on the server side.
493 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
494 * We're going to need a new mechanism for TLS1.3 to specify which
495 * messages to add the custom extensions to.
497 if ((!s->hit || !s->server)
499 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
500 && custom_ext_parse(s, s->server, currext->type,
501 PACKET_data(&currext->data),
502 PACKET_remaining(&currext->data),
510 * Parse all remaining extensions that have not yet been parsed. Also calls the
511 * finalisation for all extensions at the end, whether we collected them or not.
512 * Returns 1 for success or 0 for failure. On failure, |*al| is populated with a
513 * suitable alert code.
515 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al)
517 size_t i, numexts = OSSL_NELEM(ext_defs);
518 const EXTENSION_DEFINITION *thisexd;
520 /* Calculate the number of extensions in the extensions list */
521 if ((context & EXT_CLIENT_HELLO) != 0) {
522 numexts += s->cert->srv_ext.meths_count;
523 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
524 numexts += s->cert->cli_ext.meths_count;
527 /* Parse each extension in turn */
528 for (i = 0; i < numexts; i++) {
529 if (!tls_parse_extension(s, i, context, exts, al))
534 * Finalise all known extensions relevant to this context, whether we have
537 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
538 if(thisexd->final != NULL
539 && (thisexd->context & context) != 0
540 && !thisexd->final(s, context, exts[i].present, al))
548 * Construct all the extensions relevant to the current |context| and write
549 * them to |pkt|. If this is an extension for a Certificate in a Certificate
550 * message, then |x| will be set to the Certificate we are handling, and |chain|
551 * will indicate the position in the chain we are processing (with 0 being the
552 * first in the chain). Returns 1 on success or 0 on failure. If a failure
553 * occurs then |al| is populated with a suitable alert code. On a failure
554 * construction stops at the first extension to fail to construct.
556 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
557 X509 *x, size_t chain, int *al)
560 int addcustom = 0, min_version, max_version = 0, reason, tmpal;
561 const EXTENSION_DEFINITION *thisexd;
564 * Normally if something goes wrong during construction it's an internal
565 * error. We can always override this later.
567 tmpal = SSL_AD_INTERNAL_ERROR;
569 if (!WPACKET_start_sub_packet_u16(pkt)
571 * If extensions are of zero length then we don't even add the
572 * extensions length bytes to a ClientHello/ServerHello in SSLv3
574 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
575 && s->version == SSL3_VERSION
576 && !WPACKET_set_flags(pkt,
577 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
578 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
582 if ((context & EXT_CLIENT_HELLO) != 0) {
583 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
585 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
590 /* Add custom extensions first */
591 if ((context & EXT_CLIENT_HELLO) != 0) {
592 custom_ext_init(&s->cert->cli_ext);
594 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
596 * We already initialised the custom extensions during ClientHello
599 * TODO(TLS1.3): We're going to need a new custom extension mechanism
600 * for TLS1.3, so that custom extensions can specify which of the
601 * multiple message they wish to add themselves to.
606 if (addcustom && !custom_ext_add(s, s->server, pkt, &tmpal)) {
607 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
611 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
612 int (*construct)(SSL *s, WPACKET *pkt, X509 *x, size_t chain, int *al);
614 /* Skip if not relevant for our context */
615 if ((thisexd->context & context) == 0)
618 construct = s->server ? thisexd->construct_stoc
619 : thisexd->construct_ctos;
621 /* Check if this extension is defined for our protocol. If not, skip */
623 && (thisexd->context & EXT_TLS_IMPLEMENTATION_ONLY)
625 || (s->version == SSL3_VERSION
626 && (thisexd->context & EXT_SSL3_ALLOWED) == 0)
628 && (thisexd->context & EXT_TLS1_2_AND_BELOW_ONLY)
631 && (thisexd->context & EXT_TLS1_3_ONLY) != 0
632 && (context & EXT_CLIENT_HELLO) == 0)
633 || ((thisexd->context & EXT_TLS1_3_ONLY) != 0
634 && (context & EXT_CLIENT_HELLO) != 0
635 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
636 || construct == NULL)
639 if (!construct(s, pkt, x, chain, &tmpal))
643 if (!WPACKET_close(pkt)) {
644 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
656 * Built in extension finalisation and initialisation functions. All initialise
657 * or finalise the associated extension type for the given |context|. For
658 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
659 * otherwise. These functions return 1 on success or 0 on failure. In the event
660 * of a failure then |*al| is populated with a suitable error code.
663 static int final_renegotiate(SSL *s, unsigned int context, int sent,
668 * Check if we can connect to a server that doesn't support safe
671 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
672 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
674 *al = SSL_AD_HANDSHAKE_FAILURE;
675 SSLerr(SSL_F_FINAL_RENEGOTIATE,
676 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
683 /* Need RI if renegotiating */
685 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
687 *al = SSL_AD_HANDSHAKE_FAILURE;
688 SSLerr(SSL_F_FINAL_RENEGOTIATE,
689 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
697 static int init_server_name(SSL *s, unsigned int context)
700 s->servername_done = 0;
705 static int final_server_name(SSL *s, unsigned int context, int sent,
708 int ret = SSL_TLSEXT_ERR_NOACK;
709 int altmp = SSL_AD_UNRECOGNIZED_NAME;
711 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
712 ret = s->ctx->tlsext_servername_callback(s, &altmp,
713 s->ctx->tlsext_servername_arg);
714 else if (s->initial_ctx != NULL
715 && s->initial_ctx->tlsext_servername_callback != 0)
716 ret = s->initial_ctx->tlsext_servername_callback(s, &altmp,
717 s->initial_ctx->tlsext_servername_arg);
720 case SSL_TLSEXT_ERR_ALERT_FATAL:
724 case SSL_TLSEXT_ERR_ALERT_WARNING:
728 case SSL_TLSEXT_ERR_NOACK:
729 s->servername_done = 0;
737 #ifndef OPENSSL_NO_EC
738 static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
741 unsigned long alg_k, alg_a;
746 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
747 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
750 * If we are client and using an elliptic curve cryptography cipher
751 * suite, then if server returns an EC point formats lists extension it
752 * must contain uncompressed.
754 if (s->tlsext_ecpointformatlist != NULL
755 && s->tlsext_ecpointformatlist_length > 0
756 && s->session->tlsext_ecpointformatlist != NULL
757 && s->session->tlsext_ecpointformatlist_length > 0
758 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
759 /* we are using an ECC cipher */
761 unsigned char *list = s->session->tlsext_ecpointformatlist;
763 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
764 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
767 if (i == s->session->tlsext_ecpointformatlist_length) {
768 SSLerr(SSL_F_FINAL_EC_PT_FORMATS,
769 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
778 static int init_session_ticket(SSL *s, unsigned int context)
781 s->tlsext_ticket_expected = 0;
786 #ifndef OPENSSL_NO_OCSP
787 static int init_status_request(SSL *s, unsigned int context)
790 s->tlsext_status_type = TLSEXT_STATUSTYPE_nothing;
795 static int final_status_request(SSL *s, unsigned int context, int sent,
802 * Ensure we get sensible values passed to tlsext_status_cb in the event
803 * that we don't receive a status message
805 OPENSSL_free(s->tlsext_ocsp_resp);
806 s->tlsext_ocsp_resp = NULL;
807 s->tlsext_ocsp_resplen = 0;
813 #ifndef OPENSSL_NO_NEXTPROTONEG
814 static int init_npn(SSL *s, unsigned int context)
816 s->s3->next_proto_neg_seen = 0;
822 static int init_alpn(SSL *s, unsigned int context)
824 OPENSSL_free(s->s3->alpn_selected);
825 s->s3->alpn_selected = NULL;
827 s->s3->alpn_selected_len = 0;
828 OPENSSL_free(s->s3->alpn_proposed);
829 s->s3->alpn_proposed = NULL;
830 s->s3->alpn_proposed_len = 0;
835 static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
837 const unsigned char *selected = NULL;
838 unsigned char selected_len = 0;
843 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
844 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
845 s->s3->alpn_proposed,
846 (unsigned int)s->s3->alpn_proposed_len,
847 s->ctx->alpn_select_cb_arg);
849 if (r == SSL_TLSEXT_ERR_OK) {
850 OPENSSL_free(s->s3->alpn_selected);
851 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
852 if (s->s3->alpn_selected == NULL) {
853 *al = SSL_AD_INTERNAL_ERROR;
856 s->s3->alpn_selected_len = selected_len;
857 #ifndef OPENSSL_NO_NEXTPROTONEG
858 /* ALPN takes precedence over NPN. */
859 s->s3->next_proto_neg_seen = 0;
862 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
870 static int init_sig_algs(SSL *s, unsigned int context)
872 /* Clear any signature algorithms extension received */
873 OPENSSL_free(s->s3->tmp.peer_sigalgs);
874 s->s3->tmp.peer_sigalgs = NULL;
879 #ifndef OPENSSL_NO_SRP
880 static int init_srp(SSL *s, unsigned int context)
882 OPENSSL_free(s->srp_ctx.login);
883 s->srp_ctx.login = NULL;
889 static int init_etm(SSL *s, unsigned int context)
891 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
896 static int init_ems(SSL *s, unsigned int context)
899 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
904 static int final_ems(SSL *s, unsigned int context, int sent, int *al)
906 if (!s->server && s->hit) {
908 * Check extended master secret extension is consistent with
911 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
912 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
913 *al = SSL_AD_HANDSHAKE_FAILURE;
914 SSLerr(SSL_F_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
922 #ifndef OPENSSL_NO_SRTP
923 static int init_srtp(SSL *s, unsigned int context)
926 s->srtp_profile = NULL;