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 tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent,
15 static int tls_ext_init_server_name(SSL *s, unsigned int context);
16 static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent,
19 static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent,
22 static int tls_ext_init_session_ticket(SSL *s, unsigned int context);
23 static int tls_ext_init_status_request(SSL *s, unsigned int context);
24 static int tls_ext_final_status_request(SSL *s, unsigned int context, int sent,
26 #ifndef OPENSSL_NO_NEXTPROTONEG
27 static int tls_ext_init_npn(SSL *s, unsigned int context);
29 static int tls_ext_init_alpn(SSL *s, unsigned int context);
30 static int tls_ext_final_alpn(SSL *s, unsigned int context, int sent, int *al);
31 static int tls_ext_init_sig_algs(SSL *s, unsigned int context);
32 #ifndef OPENSSL_NO_SRP
33 static int tls_ext_init_srp(SSL *s, unsigned int context);
35 static int tls_ext_init_etm(SSL *s, unsigned int context);
36 static int tls_ext_init_ems(SSL *s, unsigned int context);
37 static int tls_ext_final_ems(SSL *s, unsigned int context, int sent, int *al);
38 #ifndef OPENSSL_NO_SRTP
39 static int tls_ext_init_srtp(SSL *s, unsigned int context);
42 /* Structure to define a built-in extension */
44 /* The ID for the extension */
47 * Initialise extension before parsing. Always called for relevant contexts
48 * even if extension not present
50 int (*init_ext)(SSL *s, unsigned int context);
51 /* Parse extension received by server from client */
52 int (*parse_client_ext)(SSL *s, PACKET *pkt, int *al);
53 /* Parse extension received by client from server */
54 int (*parse_server_ext)(SSL *s, PACKET *pkt, int *al);
55 /* Construct extension sent by server */
56 int (*construct_server_ext)(SSL *s, WPACKET *pkt, int *al);
57 /* Construct extension sent by client */
58 int (*construct_client_ext)(SSL *s, WPACKET *pkt, int *al);
60 * Finalise extension after parsing. Always called where an extensions was
61 * initialised even if the extension was not present. |sent| is set to 1 if
62 * the extension was seen, or 0 otherwise.
64 int (*finalise_ext)(SSL *s, unsigned int context, int sent, int *al);
66 } EXTENSION_DEFINITION;
69 * Definitions of all built-in extensions. NOTE: Changes in the number or order
70 * of these extensions should be mirrored with equivalent changes to the indexes
71 * defined in statem_locl.h.
72 * Each extension has an initialiser, a client and
73 * server side parser and a finaliser. The initialiser is called (if the
74 * extension is relevant to the given context) even if we did not see the
75 * extension in the message that we received. The parser functions are only
76 * called if we see the extension in the message. The finalisers are always
77 * called if the initialiser was called.
78 * There are also server and client side constructor functions which are always
79 * called during message construction if the extension is relevant for the
81 * The initialisation, parsing, finalisation and construction functions are
82 * always called in the order defined in this list. Some extensions may depend
83 * on others having been processed first, so the order of this list is
85 * The extension context is defined by a series of flags which specify which
86 * messages the extension is relevant to. These flags also specify whether the
87 * extension is relevant to a paricular protocol or protocol version.
89 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
91 static const EXTENSION_DEFINITION ext_defs[] = {
93 TLSEXT_TYPE_renegotiate,
95 tls_parse_client_renegotiate,
96 tls_parse_server_renegotiate,
97 tls_construct_server_renegotiate,
98 tls_construct_client_renegotiate,
99 tls_ext_final_renegotiate,
100 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
101 | EXT_TLS1_2_AND_BELOW_ONLY
104 TLSEXT_TYPE_server_name,
105 tls_ext_init_server_name,
106 tls_parse_client_server_name,
107 tls_parse_server_server_name,
108 tls_construct_server_server_name,
109 tls_construct_client_server_name,
110 tls_ext_final_server_name,
111 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
112 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
114 #ifndef OPENSSL_NO_SRP
118 tls_parse_client_srp,
121 tls_construct_client_srp,
123 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
126 #ifndef OPENSSL_NO_EC
128 TLSEXT_TYPE_ec_point_formats,
130 tls_parse_client_ec_pt_formats,
131 tls_parse_server_ec_pt_formats,
132 tls_construct_server_ec_pt_formats,
133 tls_construct_client_ec_pt_formats,
134 tls_ext_final_ec_pt_formats,
135 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
138 TLSEXT_TYPE_supported_groups,
140 tls_parse_client_supported_groups,
142 NULL /* TODO(TLS1.3): Need to add this */,
143 tls_construct_client_supported_groups,
145 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
149 TLSEXT_TYPE_session_ticket,
150 tls_ext_init_session_ticket,
151 tls_parse_client_session_ticket,
152 tls_parse_server_session_ticket,
153 tls_construct_server_session_ticket,
154 tls_construct_client_session_ticket,
156 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
159 TLSEXT_TYPE_signature_algorithms,
160 tls_ext_init_sig_algs,
161 tls_parse_client_sig_algs,
164 tls_construct_client_sig_algs,
168 #ifndef OPENSSL_NO_OCSP
170 TLSEXT_TYPE_status_request,
171 tls_ext_init_status_request,
172 tls_parse_client_status_request,
173 tls_parse_server_status_request,
174 tls_construct_server_status_request,
175 tls_construct_client_status_request,
176 tls_ext_final_status_request,
177 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
178 | EXT_TLS1_3_CERTIFICATE
181 #ifndef OPENSSL_NO_NEXTPROTONEG
183 TLSEXT_TYPE_next_proto_neg,
185 tls_parse_client_npn,
186 tls_parse_server_npn,
187 tls_construct_server_next_proto_neg,
188 tls_construct_client_npn,
190 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
195 * Must appear in this list after server_name so that finalisation
196 * happens after server_name callbacks
198 TLSEXT_TYPE_application_layer_protocol_negotiation,
200 tls_parse_client_alpn,
201 tls_parse_server_alpn,
202 tls_construct_server_alpn,
203 tls_construct_client_alpn,
205 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
206 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
208 #ifndef OPENSSL_NO_SRTP
210 TLSEXT_TYPE_use_srtp,
212 tls_parse_client_use_srtp,
213 tls_parse_server_use_srtp,
214 tls_construct_server_use_srtp,
215 tls_construct_client_use_srtp,
217 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
218 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
222 TLSEXT_TYPE_encrypt_then_mac,
224 tls_parse_client_etm,
225 tls_parse_server_etm,
226 tls_construct_server_etm,
227 tls_construct_client_etm,
229 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
231 #ifndef OPENSSL_NO_CT
233 TLSEXT_TYPE_signed_certificate_timestamp,
236 * No server side support for this, but can be provided by a custom
237 * extension. This is an exception to the rule that custom extensions
238 * cannot override built in ones.
241 tls_parse_server_sct,
243 tls_construct_client_sct,
245 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
246 | EXT_TLS1_3_CERTIFICATE
250 TLSEXT_TYPE_extended_master_secret,
252 tls_parse_client_ems,
253 tls_parse_server_ems,
254 tls_construct_server_ems,
255 tls_construct_client_ems,
257 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
260 TLSEXT_TYPE_supported_versions,
262 /* Processed inline as part of version selection */
266 tls_construct_client_supported_versions,
268 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY
272 * Must be in this list after supported_groups. We need that to have
273 * been parsed before we do this one.
275 TLSEXT_TYPE_key_share,
277 tls_parse_client_key_share,
278 tls_parse_server_key_share,
279 tls_construct_server_key_share,
280 tls_construct_client_key_share,
282 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
283 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
288 * Special unsolicited ServerHello extension only used when
289 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
291 TLSEXT_TYPE_cryptopro_bug,
295 tls_construct_server_cryptopro_bug,
298 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
301 /* Last in the list because it must be added as the last extension */
304 /* We send this, but don't read it */
308 tls_construct_client_padding,
315 * Verify whether we are allowed to use the extension |type| in the current
316 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
317 * indicate the extension is not allowed. If returning 1 then |*found| is set to
318 * 1 if we found a definition for the extension, and |*idx| is set to its index
320 static int verify_extension(SSL *s, unsigned int context, unsigned int type,
321 custom_ext_methods *meths, int *found, size_t *idx)
324 size_t builtin_num = OSSL_NELEM(ext_defs);
326 for (i = 0; i < builtin_num; i++) {
327 if (type == ext_defs[i].type) {
328 /* Check we're allowed to use this extension in this context */
329 if ((context & ext_defs[i].context) == 0)
332 if (SSL_IS_DTLS(s)) {
333 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
335 } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
345 if ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) == 0) {
347 * Custom extensions only apply to <=TLS1.2. This extension is unknown
348 * in this context - we allow it
354 /* Check the custom extensions */
356 for (i = builtin_num; i < builtin_num + meths->meths_count; i++) {
357 if (meths->meths[i - builtin_num].ext_type == type) {
365 /* Unknown extension. We allow it */
371 * Check whether the context defined for an extension |extctx| means whether
372 * the extension is relevant for the current context |thisctx| or not. Returns
373 * 1 if the extension is relevant for this context, and 0 otherwise
375 static int extension_is_relevant(SSL *s, unsigned int extctx,
376 unsigned int thisctx)
379 && (extctx & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
380 || (s->version == SSL3_VERSION
381 && (extctx & EXT_SSL3_ALLOWED) == 0)
383 && (extctx & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
384 || (!SSL_IS_TLS13(s) && (extctx & EXT_TLS1_3_ONLY) != 0))
391 * Gather a list of all the extensions from the data in |packet]. |context|
392 * tells us which message this extension is for. The raw extension data is
393 * stored in |*res|. In the event of an error the alert type to use is stored in
394 * |*al|. We don't actually process the content of the extensions yet, except to
395 * check their types. This function also runs the initialiser functions for all
396 * known extensions (whether we have collected them or not).
398 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
399 * more than one extension of the same type in a ClientHello or ServerHello.
400 * This function returns 1 if all extensions are unique and we have parsed their
401 * types, and 0 if the extensions contain duplicates, could not be successfully
402 * collected, or an internal error occurred. We only check duplicates for
403 * extensions that we know about. We ignore others.
405 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
406 RAW_EXTENSION **res, int *al)
408 PACKET extensions = *packet;
411 custom_ext_methods *exts = NULL;
412 RAW_EXTENSION *raw_extensions = NULL;
415 * Initialise server side custom extensions. Client side is done during
416 * construction of extensions for the ClientHello.
418 if ((context & EXT_CLIENT_HELLO) != 0) {
419 exts = &s->cert->srv_ext;
420 custom_ext_init(&s->cert->srv_ext);
421 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
422 exts = &s->cert->cli_ext;
425 raw_extensions = OPENSSL_zalloc((OSSL_NELEM(ext_defs)
426 + (exts != NULL ? exts->meths_count : 0))
427 * sizeof(RAW_EXTENSION));
428 if (raw_extensions == NULL) {
429 *al = SSL_AD_INTERNAL_ERROR;
430 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
434 while (PACKET_remaining(&extensions) > 0) {
438 if (!PACKET_get_net_2(&extensions, &type) ||
439 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
440 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
441 *al = SSL_AD_DECODE_ERROR;
445 * Verify this extension is allowed. We only check duplicates for
446 * extensions that we recognise.
448 if (!verify_extension(s, context, type, exts, &found, &idx)
450 && raw_extensions[idx].present == 1)) {
451 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
452 *al = SSL_AD_ILLEGAL_PARAMETER;
456 raw_extensions[idx].data = extension;
457 raw_extensions[idx].present = 1;
458 raw_extensions[idx].type = type;
463 * Initialise all known extensions relevant to this context, whether we have
466 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
467 if(ext_defs[i].init_ext != NULL && (ext_defs[i].context & context) != 0
468 && extension_is_relevant(s, ext_defs[i].context, context)
469 && !ext_defs[i].init_ext(s, context)) {
470 *al = SSL_AD_INTERNAL_ERROR;
475 *res = raw_extensions;
479 OPENSSL_free(raw_extensions);
484 * Runs the parser for a given extension with index |idx|. |exts| contains the
485 * list of all parsed extensions previously collected by
486 * tls_collect_extensions(). The parser is only run if it is applicable for the
487 * given |context| and the parser has not already been run. Returns 1 on success
488 * or 0 on failure. In the event of a failure |*al| is populated with a suitable
489 * alert code. If an extension is not present this counted as success.
491 int tls_parse_extension(SSL *s, unsigned int idx, int context,
492 RAW_EXTENSION *exts, int *al)
494 RAW_EXTENSION *currext = &exts[idx];
495 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
497 /* Skip if the extension is not present */
498 if (!currext->present)
501 if (s->tlsext_debug_cb)
502 s->tlsext_debug_cb(s, !s->server, currext->type,
503 PACKET_data(&currext->data),
504 PACKET_remaining(&currext->data),
505 s->tlsext_debug_arg);
507 /* Skip if we've already parsed this extension */
513 if (idx < OSSL_NELEM(ext_defs)) {
514 /* We are handling a built-in extension */
515 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
517 /* Check if extension is defined for our protocol. If not, skip */
518 if (!extension_is_relevant(s, extdef->context, context))
521 parser = s->server ? extdef->parse_client_ext : extdef->parse_server_ext;
523 if (parser != NULL) {
524 if (!parser(s, &currext->data, al))
531 * If the parser is NULL we fall through to the custom extension
537 * This is a custom extension. We only allow this if it is a non
538 * resumed session on the server side.
540 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
541 * We're going to need a new mechanism for TLS1.3 to specify which
542 * messages to add the custom extensions to.
544 if ((!s->hit || !s->server)
546 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
547 && custom_ext_parse(s, s->server, currext->type,
548 PACKET_data(&currext->data),
549 PACKET_remaining(&currext->data),
557 * Parse all remaining extensions that have not yet been parsed. Also calls the
558 * finalisation for all extensions at the end, whether we collected them or not.
559 * Returns 1 for success or 0 for failure. On failure, |*al| is populated with a
560 * suitable alert code.
562 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al)
564 size_t loop, numexts = OSSL_NELEM(ext_defs);
566 /* Calculate the number of extensions in the extensions list */
567 if ((context & EXT_CLIENT_HELLO) != 0) {
568 numexts += s->cert->srv_ext.meths_count;
569 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
570 numexts += s->cert->cli_ext.meths_count;
573 /* Parse each extension in turn */
574 for (loop = 0; loop < numexts; loop++) {
575 if (!tls_parse_extension(s, loop, context, exts, al))
580 * Finalise all known extensions relevant to this context, whether we have
583 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
584 if(ext_defs[loop].finalise_ext != NULL
585 && (ext_defs[loop].context & context) != 0
586 && !ext_defs[loop].finalise_ext(s, context, exts[loop].present,
595 * Construct all the extensions relevant to the current |context| and write
596 * them to |pkt|. Returns 1 on success or 0 on failure. If a failure occurs then
597 * |al| is populated with a suitable alert code.
599 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
604 int min_version, max_version = 0, reason, tmpal;
607 * Normally if something goes wrong during construction it's an internal
608 * error. We can always override this later.
610 tmpal = SSL_AD_INTERNAL_ERROR;
612 if (!WPACKET_start_sub_packet_u16(pkt)
614 * If extensions are of zero length then we don't even add the
615 * extensions length bytes to a ClientHello/ServerHello in SSLv3
617 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
618 && s->version == SSL3_VERSION
619 && !WPACKET_set_flags(pkt,
620 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
621 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
625 if ((context & EXT_CLIENT_HELLO) != 0) {
626 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
628 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
633 /* Add custom extensions first */
634 if ((context & EXT_CLIENT_HELLO) != 0) {
635 custom_ext_init(&s->cert->cli_ext);
637 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
639 * We already initialised the custom extensions during ClientHello
642 * TODO(TLS1.3): We're going to need a new custom extension mechanism
643 * for TLS1.3, so that custom extensions can specify which of the
644 * multiple message they wish to add themselves to.
649 if (addcustom && !custom_ext_add(s, s->server, pkt, &tmpal)) {
650 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
654 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
655 int (*construct)(SSL *s, WPACKET *pkt, int *al);
657 /* Skip if not relevant for our context */
658 if ((ext_defs[loop].context & context) == 0)
661 construct = s->server ? ext_defs[loop].construct_server_ext
662 : ext_defs[loop].construct_client_ext;
664 /* Check if this extension is defined for our protocol. If not, skip */
666 && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
668 || (s->version == SSL3_VERSION
669 && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
671 && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
674 && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
675 && (context & EXT_CLIENT_HELLO) == 0)
676 || ((ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
677 && (context & EXT_CLIENT_HELLO) != 0
678 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
679 || construct == NULL)
682 if (!construct(s, pkt, &tmpal))
686 if (!WPACKET_close(pkt)) {
687 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
699 * Built in extension finalisation and initialisation functions. All initialise
700 * or finalise the associated extension type for the given |context|. For
701 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
702 * otherwise. These functions return 1 on success or 0 on failure. In the event
703 * of a failure then |*al| is populated with a suitable error code.
706 static int tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent,
711 * Check if we can connect to a server that doesn't support safe
714 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
715 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
717 *al = SSL_AD_HANDSHAKE_FAILURE;
718 SSLerr(SSL_F_TLS_EXT_FINAL_RENEGOTIATE,
719 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
726 /* Need RI if renegotiating */
728 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
730 *al = SSL_AD_HANDSHAKE_FAILURE;
731 SSLerr(SSL_F_TLS_EXT_FINAL_RENEGOTIATE,
732 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
740 static int tls_ext_init_server_name(SSL *s, unsigned int context)
743 s->servername_done = 0;
748 static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent,
751 int ret = SSL_TLSEXT_ERR_NOACK;
752 int altmp = SSL_AD_UNRECOGNIZED_NAME;
754 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
755 ret = s->ctx->tlsext_servername_callback(s, &altmp,
756 s->ctx->tlsext_servername_arg);
757 else if (s->initial_ctx != NULL
758 && s->initial_ctx->tlsext_servername_callback != 0)
759 ret = s->initial_ctx->tlsext_servername_callback(s, &altmp,
760 s->initial_ctx->tlsext_servername_arg);
763 case SSL_TLSEXT_ERR_ALERT_FATAL:
767 case SSL_TLSEXT_ERR_ALERT_WARNING:
771 case SSL_TLSEXT_ERR_NOACK:
772 s->servername_done = 0;
780 #ifndef OPENSSL_NO_EC
781 static int tls_ext_final_ec_pt_formats(SSL *s, unsigned int context, int sent,
784 unsigned long alg_k, alg_a;
789 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
790 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
793 * If we are client and using an elliptic curve cryptography cipher
794 * suite, then if server returns an EC point formats lists extension it
795 * must contain uncompressed.
797 if ((s->tlsext_ecpointformatlist != NULL)
798 && (s->tlsext_ecpointformatlist_length > 0)
799 && (s->session->tlsext_ecpointformatlist != NULL)
800 && (s->session->tlsext_ecpointformatlist_length > 0)
801 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
802 /* we are using an ECC cipher */
805 int found_uncompressed = 0;
806 list = s->session->tlsext_ecpointformatlist;
807 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
808 if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
809 found_uncompressed = 1;
813 if (!found_uncompressed) {
814 SSLerr(SSL_F_TLS_EXT_FINAL_EC_PT_FORMATS,
815 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
824 static int tls_ext_init_session_ticket(SSL *s, unsigned int context)
827 s->tlsext_ticket_expected = 0;
832 static int tls_ext_init_status_request(SSL *s, unsigned int context)
835 s->tlsext_status_type = -1;
840 static int tls_ext_final_status_request(SSL *s, unsigned int context, int sent,
847 * Ensure we get sensible values passed to tlsext_status_cb in the event
848 * that we don't receive a status message
850 OPENSSL_free(s->tlsext_ocsp_resp);
851 s->tlsext_ocsp_resp = NULL;
852 s->tlsext_ocsp_resplen = 0;
857 #ifndef OPENSSL_NO_NEXTPROTONEG
858 static int tls_ext_init_npn(SSL *s, unsigned int context)
860 s->s3->next_proto_neg_seen = 0;
866 static int tls_ext_init_alpn(SSL *s, unsigned int context)
868 OPENSSL_free(s->s3->alpn_selected);
869 s->s3->alpn_selected = NULL;
871 s->s3->alpn_selected_len = 0;
872 OPENSSL_free(s->s3->alpn_proposed);
873 s->s3->alpn_proposed = NULL;
874 s->s3->alpn_proposed_len = 0;
879 static int tls_ext_final_alpn(SSL *s, unsigned int context, int sent, int *al)
881 const unsigned char *selected = NULL;
882 unsigned char selected_len = 0;
887 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
888 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
889 s->s3->alpn_proposed,
890 (unsigned int)s->s3->alpn_proposed_len,
891 s->ctx->alpn_select_cb_arg);
893 if (r == SSL_TLSEXT_ERR_OK) {
894 OPENSSL_free(s->s3->alpn_selected);
895 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
896 if (s->s3->alpn_selected == NULL) {
897 *al = SSL_AD_INTERNAL_ERROR;
900 s->s3->alpn_selected_len = selected_len;
901 #ifndef OPENSSL_NO_NEXTPROTONEG
902 /* ALPN takes precedence over NPN. */
903 s->s3->next_proto_neg_seen = 0;
906 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
914 static int tls_ext_init_sig_algs(SSL *s, unsigned int context)
916 /* Clear any signature algorithms extension received */
917 OPENSSL_free(s->s3->tmp.peer_sigalgs);
918 s->s3->tmp.peer_sigalgs = NULL;
923 #ifndef OPENSSL_NO_SRP
924 static int tls_ext_init_srp(SSL *s, unsigned int context)
926 OPENSSL_free(s->srp_ctx.login);
927 s->srp_ctx.login = NULL;
933 static int tls_ext_init_etm(SSL *s, unsigned int context)
935 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
940 static int tls_ext_init_ems(SSL *s, unsigned int context)
943 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
948 static int tls_ext_final_ems(SSL *s, unsigned int context, int sent, int *al)
950 if (!s->server && s->hit) {
952 * Check extended master secret extension is consistent with
955 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
956 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
957 *al = SSL_AD_HANDSHAKE_FAILURE;
958 SSLerr(SSL_F_TLS_EXT_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
966 #ifndef OPENSSL_NO_SRTP
967 static int tls_ext_init_srtp(SSL *s, unsigned int context)
970 s->srtp_profile = NULL;