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 static int init_status_request(SSL *s, unsigned int context);
24 static int final_status_request(SSL *s, unsigned int context, int sent,
26 #ifndef OPENSSL_NO_NEXTPROTONEG
27 static int init_npn(SSL *s, unsigned int context);
29 static int init_alpn(SSL *s, unsigned int context);
30 static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
31 static int init_sig_algs(SSL *s, unsigned int context);
32 #ifndef OPENSSL_NO_SRP
33 static int init_srp(SSL *s, unsigned int context);
35 static int init_etm(SSL *s, unsigned int context);
36 static int init_ems(SSL *s, unsigned int context);
37 static int final_ems(SSL *s, unsigned int context, int sent, int *al);
38 #ifndef OPENSSL_NO_SRTP
39 static int init_srtp(SSL *s, unsigned int context);
42 /* Structure to define a built-in extension */
43 typedef struct extensions_definition_st {
44 /* The defined type for the extension */
47 * The context that this extension applies to, e.g. what messages and
52 * Initialise extension before parsing. Always called for relevant contexts
53 * even if extension not present
55 int (*init)(SSL *s, unsigned int context);
56 /* Parse extension sent from client to server */
57 int (*parse_ctos)(SSL *s, PACKET *pkt, int *al);
58 /* Parse extension send from server to client */
59 int (*parse_stoc)(SSL *s, PACKET *pkt, int *al);
60 /* Construct extension sent from server to client */
61 int (*construct_stoc)(SSL *s, WPACKET *pkt, int *al);
62 /* Construct extension sent from client to server */
63 int (*construct_ctos)(SSL *s, WPACKET *pkt, int *al);
65 * Finalise extension after parsing. Always called where an extensions was
66 * initialised even if the extension was not present. |sent| is set to 1 if
67 * the extension was seen, or 0 otherwise.
69 int (*final)(SSL *s, unsigned int context, int sent, int *al);
70 } EXTENSION_DEFINITION;
73 * Definitions of all built-in extensions. NOTE: Changes in the number or order
74 * of these extensions should be mirrored with equivalent changes to the indexes
75 * defined in statem_locl.h.
76 * Each extension has an initialiser, a client and
77 * server side parser and a finaliser. The initialiser is called (if the
78 * extension is relevant to the given context) even if we did not see the
79 * extension in the message that we received. The parser functions are only
80 * called if we see the extension in the message. The finalisers are always
81 * called if the initialiser was called.
82 * There are also server and client side constructor functions which are always
83 * called during message construction if the extension is relevant for the
85 * The initialisation, parsing, finalisation and construction functions are
86 * always called in the order defined in this list. Some extensions may depend
87 * on others having been processed first, so the order of this list is
89 * The extension context is defined by a series of flags which specify which
90 * messages the extension is relevant to. These flags also specify whether the
91 * extension is relevant to a paricular protocol or protocol version.
93 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
95 #define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
96 static const EXTENSION_DEFINITION ext_defs[] = {
98 TLSEXT_TYPE_renegotiate,
99 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
100 | EXT_TLS1_2_AND_BELOW_ONLY,
101 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
102 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
106 TLSEXT_TYPE_server_name,
107 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
108 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
110 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
111 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
114 #ifndef OPENSSL_NO_SRP
117 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
118 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
123 #ifndef OPENSSL_NO_EC
125 TLSEXT_TYPE_ec_point_formats,
126 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
127 NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
128 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
132 TLSEXT_TYPE_supported_groups,
133 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
134 NULL, tls_parse_ctos_supported_groups, NULL,
135 NULL /* TODO(TLS1.3): Need to add this */,
136 tls_construct_ctos_supported_groups, NULL
143 TLSEXT_TYPE_session_ticket,
144 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
145 init_session_ticket, tls_parse_ctos_session_ticket,
146 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
147 tls_construct_ctos_session_ticket, NULL
150 TLSEXT_TYPE_signature_algorithms,
152 init_sig_algs, tls_parse_ctos_sig_algs, NULL, NULL,
153 tls_construct_ctos_sig_algs, NULL
155 #ifndef OPENSSL_NO_OCSP
157 TLSEXT_TYPE_status_request,
158 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
159 | EXT_TLS1_3_CERTIFICATE,
160 init_status_request, tls_parse_ctos_status_request,
161 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
162 tls_construct_ctos_status_request, final_status_request
167 #ifndef OPENSSL_NO_NEXTPROTONEG
169 TLSEXT_TYPE_next_proto_neg,
170 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
171 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
172 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
179 * Must appear in this list after server_name so that finalisation
180 * happens after server_name callbacks
182 TLSEXT_TYPE_application_layer_protocol_negotiation,
183 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
184 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
185 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
186 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
188 #ifndef OPENSSL_NO_SRTP
190 TLSEXT_TYPE_use_srtp,
191 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
192 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY,
193 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
194 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
200 TLSEXT_TYPE_encrypt_then_mac,
201 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
202 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
203 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
205 #ifndef OPENSSL_NO_CT
207 TLSEXT_TYPE_signed_certificate_timestamp,
208 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
209 | EXT_TLS1_3_CERTIFICATE,
212 * No server side support for this, but can be provided by a custom
213 * extension. This is an exception to the rule that custom extensions
214 * cannot override built in ones.
216 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
222 TLSEXT_TYPE_extended_master_secret,
223 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
224 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
225 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
228 TLSEXT_TYPE_supported_versions,
229 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY,
231 /* Processed inline as part of version selection */
232 NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL
236 * Must be in this list after supported_groups. We need that to have
237 * been parsed before we do this one.
239 TLSEXT_TYPE_key_share,
240 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
241 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
243 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
244 tls_construct_stoc_key_share, tls_construct_ctos_key_share, NULL
248 * Special unsolicited ServerHello extension only used when
249 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
251 TLSEXT_TYPE_cryptopro_bug,
252 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY,
253 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
256 /* Last in the list because it must be added as the last extension */
260 /* We send this, but don't read it */
261 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
266 * Verify whether we are allowed to use the extension |type| in the current
267 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
268 * indicate the extension is not allowed. If returning 1 then |*found| is set to
269 * 1 if we found a definition for the extension, and |*idx| is set to its index
271 static int verify_extension(SSL *s, unsigned int context, unsigned int type,
272 custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
273 RAW_EXTENSION **found)
276 size_t builtin_num = OSSL_NELEM(ext_defs);
277 const EXTENSION_DEFINITION *thisext;
279 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
280 if (type == thisext->type) {
281 /* Check we're allowed to use this extension in this context */
282 if ((context & thisext->context) == 0)
285 if (SSL_IS_DTLS(s)) {
286 if ((thisext->context & EXT_TLS_ONLY) != 0)
288 } else if ((thisext->context & EXT_DTLS_ONLY) != 0) {
292 *found = &rawexlist[i];
297 if ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) == 0) {
299 * Custom extensions only apply to <=TLS1.2. This extension is unknown
300 * in this context - we allow it
306 /* Check the custom extensions */
308 for (i = builtin_num; i < builtin_num + meths->meths_count; i++) {
309 if (meths->meths[i - builtin_num].ext_type == type) {
310 *found = &rawexlist[i];
316 /* Unknown extension. We allow it */
322 * Check whether the context defined for an extension |extctx| means whether
323 * the extension is relevant for the current context |thisctx| or not. Returns
324 * 1 if the extension is relevant for this context, and 0 otherwise
326 static int extension_is_relevant(SSL *s, unsigned int extctx,
327 unsigned int thisctx)
330 && (extctx & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
331 || (s->version == SSL3_VERSION
332 && (extctx & EXT_SSL3_ALLOWED) == 0)
334 && (extctx & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
335 || (!SSL_IS_TLS13(s) && (extctx & EXT_TLS1_3_ONLY) != 0))
342 * Gather a list of all the extensions from the data in |packet]. |context|
343 * tells us which message this extension is for. The raw extension data is
344 * stored in |*res| on success. In the event of an error the alert type to use
345 * is stored in |*al|. We don't actually process the content of the extensions
346 * yet, except to check their types. This function also runs the initialiser
347 * functions for all known extensions (whether we have collected them or not).
348 * If successful the caller is responsible for freeing the contents of |*res|.
350 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
351 * more than one extension of the same type in a ClientHello or ServerHello.
352 * This function returns 1 if all extensions are unique and we have parsed their
353 * types, and 0 if the extensions contain duplicates, could not be successfully
354 * found, or an internal error occurred. We only check duplicates for
355 * extensions that we know about. We ignore others.
357 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
358 RAW_EXTENSION **res, int *al)
360 PACKET extensions = *packet;
362 custom_ext_methods *exts = NULL;
363 RAW_EXTENSION *raw_extensions = NULL;
364 const EXTENSION_DEFINITION *thisexd;
369 * Initialise server side custom extensions. Client side is done during
370 * construction of extensions for the ClientHello.
372 if ((context & EXT_CLIENT_HELLO) != 0) {
373 exts = &s->cert->srv_ext;
374 custom_ext_init(&s->cert->srv_ext);
375 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
376 exts = &s->cert->cli_ext;
379 raw_extensions = OPENSSL_zalloc((OSSL_NELEM(ext_defs)
380 + (exts != NULL ? exts->meths_count : 0))
381 * sizeof(*raw_extensions));
382 if (raw_extensions == NULL) {
383 *al = SSL_AD_INTERNAL_ERROR;
384 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
388 while (PACKET_remaining(&extensions) > 0) {
391 RAW_EXTENSION *thisex;
393 if (!PACKET_get_net_2(&extensions, &type) ||
394 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
395 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
396 *al = SSL_AD_DECODE_ERROR;
400 * Verify this extension is allowed. We only check duplicates for
401 * extensions that we recognise.
403 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
404 || (thisex != NULL && thisex->present == 1)) {
405 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
406 *al = SSL_AD_ILLEGAL_PARAMETER;
409 if (thisex != NULL) {
410 thisex->data = extension;
417 * Initialise all known extensions relevant to this context, whether we have
420 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
421 if(thisexd->init != NULL && (thisexd->context & context) != 0
422 && extension_is_relevant(s, thisexd->context, context)
423 && !thisexd->init(s, context)) {
424 *al = SSL_AD_INTERNAL_ERROR;
429 *res = raw_extensions;
433 OPENSSL_free(raw_extensions);
438 * Runs the parser for a given extension with index |idx|. |exts| contains the
439 * list of all parsed extensions previously collected by
440 * tls_collect_extensions(). The parser is only run if it is applicable for the
441 * given |context| and the parser has not already been run. Returns 1 on success
442 * or 0 on failure. In the event of a failure |*al| is populated with a suitable
443 * alert code. If an extension is not present this counted as success.
445 int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
446 RAW_EXTENSION *exts, int *al)
448 RAW_EXTENSION *currext = &exts[idx];
449 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
451 /* Skip if the extension is not present */
452 if (!currext->present)
455 if (s->tlsext_debug_cb)
456 s->tlsext_debug_cb(s, !s->server, currext->type,
457 PACKET_data(&currext->data),
458 PACKET_remaining(&currext->data),
459 s->tlsext_debug_arg);
461 /* Skip if we've already parsed this extension */
467 if (idx < OSSL_NELEM(ext_defs)) {
468 /* We are handling a built-in extension */
469 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
471 /* Check if extension is defined for our protocol. If not, skip */
472 if (!extension_is_relevant(s, extdef->context, context))
475 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
478 return parser(s, &currext->data, al);
481 * If the parser is NULL we fall through to the custom extension
487 * This is a custom extension. We only allow this if it is a non
488 * resumed session on the server side.
490 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
491 * We're going to need a new mechanism for TLS1.3 to specify which
492 * messages to add the custom extensions to.
494 if ((!s->hit || !s->server)
496 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
497 && custom_ext_parse(s, s->server, currext->type,
498 PACKET_data(&currext->data),
499 PACKET_remaining(&currext->data),
507 * Parse all remaining extensions that have not yet been parsed. Also calls the
508 * finalisation for all extensions at the end, whether we collected them or not.
509 * Returns 1 for success or 0 for failure. On failure, |*al| is populated with a
510 * suitable alert code.
512 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, int *al)
514 size_t i, numexts = OSSL_NELEM(ext_defs);
515 const EXTENSION_DEFINITION *thisexd;
517 /* Calculate the number of extensions in the extensions list */
518 if ((context & EXT_CLIENT_HELLO) != 0) {
519 numexts += s->cert->srv_ext.meths_count;
520 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
521 numexts += s->cert->cli_ext.meths_count;
524 /* Parse each extension in turn */
525 for (i = 0; i < numexts; i++) {
526 if (!tls_parse_extension(s, i, context, exts, al))
531 * Finalise all known extensions relevant to this context, whether we have
534 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
535 if(thisexd->final != NULL
536 && (thisexd->context & context) != 0
537 && !thisexd->final(s, context, exts[i].present, al))
545 * Construct all the extensions relevant to the current |context| and write
546 * them to |pkt|. Returns 1 on success or 0 on failure. If a failure occurs then
547 * |al| is populated with a suitable alert code. On a failure construction stops
548 * at the first extension to fail to construct.
550 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
554 int addcustom = 0, min_version, max_version = 0, reason, tmpal;
555 const EXTENSION_DEFINITION *thisexd;
558 * Normally if something goes wrong during construction it's an internal
559 * error. We can always override this later.
561 tmpal = SSL_AD_INTERNAL_ERROR;
563 if (!WPACKET_start_sub_packet_u16(pkt)
565 * If extensions are of zero length then we don't even add the
566 * extensions length bytes to a ClientHello/ServerHello in SSLv3
568 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
569 && s->version == SSL3_VERSION
570 && !WPACKET_set_flags(pkt,
571 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
572 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
576 if ((context & EXT_CLIENT_HELLO) != 0) {
577 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
579 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
584 /* Add custom extensions first */
585 if ((context & EXT_CLIENT_HELLO) != 0) {
586 custom_ext_init(&s->cert->cli_ext);
588 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
590 * We already initialised the custom extensions during ClientHello
593 * TODO(TLS1.3): We're going to need a new custom extension mechanism
594 * for TLS1.3, so that custom extensions can specify which of the
595 * multiple message they wish to add themselves to.
600 if (addcustom && !custom_ext_add(s, s->server, pkt, &tmpal)) {
601 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
605 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
606 int (*construct)(SSL *s, WPACKET *pkt, int *al);
608 /* Skip if not relevant for our context */
609 if ((thisexd->context & context) == 0)
612 construct = s->server ? thisexd->construct_stoc
613 : thisexd->construct_ctos;
615 /* Check if this extension is defined for our protocol. If not, skip */
617 && (thisexd->context & EXT_TLS_IMPLEMENTATION_ONLY)
619 || (s->version == SSL3_VERSION
620 && (thisexd->context & EXT_SSL3_ALLOWED) == 0)
622 && (thisexd->context & EXT_TLS1_2_AND_BELOW_ONLY)
625 && (thisexd->context & EXT_TLS1_3_ONLY) != 0
626 && (context & EXT_CLIENT_HELLO) == 0)
627 || ((thisexd->context & EXT_TLS1_3_ONLY) != 0
628 && (context & EXT_CLIENT_HELLO) != 0
629 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
630 || construct == NULL)
633 if (!construct(s, pkt, &tmpal))
637 if (!WPACKET_close(pkt)) {
638 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
650 * Built in extension finalisation and initialisation functions. All initialise
651 * or finalise the associated extension type for the given |context|. For
652 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
653 * otherwise. These functions return 1 on success or 0 on failure. In the event
654 * of a failure then |*al| is populated with a suitable error code.
657 static int final_renegotiate(SSL *s, unsigned int context, int sent,
662 * Check if we can connect to a server that doesn't support safe
665 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
666 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
668 *al = SSL_AD_HANDSHAKE_FAILURE;
669 SSLerr(SSL_F_FINAL_RENEGOTIATE,
670 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
677 /* Need RI if renegotiating */
679 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
681 *al = SSL_AD_HANDSHAKE_FAILURE;
682 SSLerr(SSL_F_FINAL_RENEGOTIATE,
683 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
691 static int init_server_name(SSL *s, unsigned int context)
694 s->servername_done = 0;
699 static int final_server_name(SSL *s, unsigned int context, int sent,
702 int ret = SSL_TLSEXT_ERR_NOACK;
703 int altmp = SSL_AD_UNRECOGNIZED_NAME;
705 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
706 ret = s->ctx->tlsext_servername_callback(s, &altmp,
707 s->ctx->tlsext_servername_arg);
708 else if (s->initial_ctx != NULL
709 && s->initial_ctx->tlsext_servername_callback != 0)
710 ret = s->initial_ctx->tlsext_servername_callback(s, &altmp,
711 s->initial_ctx->tlsext_servername_arg);
714 case SSL_TLSEXT_ERR_ALERT_FATAL:
718 case SSL_TLSEXT_ERR_ALERT_WARNING:
722 case SSL_TLSEXT_ERR_NOACK:
723 s->servername_done = 0;
731 #ifndef OPENSSL_NO_EC
732 static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
735 unsigned long alg_k, alg_a;
740 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
741 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
744 * If we are client and using an elliptic curve cryptography cipher
745 * suite, then if server returns an EC point formats lists extension it
746 * must contain uncompressed.
748 if (s->tlsext_ecpointformatlist != NULL
749 && s->tlsext_ecpointformatlist_length > 0
750 && s->session->tlsext_ecpointformatlist != NULL
751 && s->session->tlsext_ecpointformatlist_length > 0
752 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
753 /* we are using an ECC cipher */
755 unsigned char *list = s->session->tlsext_ecpointformatlist;
757 for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
758 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
761 if (i == s->session->tlsext_ecpointformatlist_length) {
762 SSLerr(SSL_F_FINAL_EC_PT_FORMATS,
763 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
772 static int init_session_ticket(SSL *s, unsigned int context)
775 s->tlsext_ticket_expected = 0;
780 static int init_status_request(SSL *s, unsigned int context)
783 s->tlsext_status_type = TLSEXT_STATUSTYPE_nothing;
788 static int final_status_request(SSL *s, unsigned int context, int sent,
795 * Ensure we get sensible values passed to tlsext_status_cb in the event
796 * that we don't receive a status message
798 OPENSSL_free(s->tlsext_ocsp_resp);
799 s->tlsext_ocsp_resp = NULL;
800 s->tlsext_ocsp_resplen = 0;
805 #ifndef OPENSSL_NO_NEXTPROTONEG
806 static int init_npn(SSL *s, unsigned int context)
808 s->s3->next_proto_neg_seen = 0;
814 static int init_alpn(SSL *s, unsigned int context)
816 OPENSSL_free(s->s3->alpn_selected);
817 s->s3->alpn_selected = NULL;
819 s->s3->alpn_selected_len = 0;
820 OPENSSL_free(s->s3->alpn_proposed);
821 s->s3->alpn_proposed = NULL;
822 s->s3->alpn_proposed_len = 0;
827 static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
829 const unsigned char *selected = NULL;
830 unsigned char selected_len = 0;
835 if (s->ctx->alpn_select_cb != NULL && s->s3->alpn_proposed != NULL) {
836 int r = s->ctx->alpn_select_cb(s, &selected, &selected_len,
837 s->s3->alpn_proposed,
838 (unsigned int)s->s3->alpn_proposed_len,
839 s->ctx->alpn_select_cb_arg);
841 if (r == SSL_TLSEXT_ERR_OK) {
842 OPENSSL_free(s->s3->alpn_selected);
843 s->s3->alpn_selected = OPENSSL_memdup(selected, selected_len);
844 if (s->s3->alpn_selected == NULL) {
845 *al = SSL_AD_INTERNAL_ERROR;
848 s->s3->alpn_selected_len = selected_len;
849 #ifndef OPENSSL_NO_NEXTPROTONEG
850 /* ALPN takes precedence over NPN. */
851 s->s3->next_proto_neg_seen = 0;
854 *al = SSL_AD_NO_APPLICATION_PROTOCOL;
862 static int init_sig_algs(SSL *s, unsigned int context)
864 /* Clear any signature algorithms extension received */
865 OPENSSL_free(s->s3->tmp.peer_sigalgs);
866 s->s3->tmp.peer_sigalgs = NULL;
871 #ifndef OPENSSL_NO_SRP
872 static int init_srp(SSL *s, unsigned int context)
874 OPENSSL_free(s->srp_ctx.login);
875 s->srp_ctx.login = NULL;
881 static int init_etm(SSL *s, unsigned int context)
883 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
888 static int init_ems(SSL *s, unsigned int context)
891 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
896 static int final_ems(SSL *s, unsigned int context, int sent, int *al)
898 if (!s->server && s->hit) {
900 * Check extended master secret extension is consistent with
903 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
904 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
905 *al = SSL_AD_HANDSHAKE_FAILURE;
906 SSLerr(SSL_F_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
914 #ifndef OPENSSL_NO_SRTP
915 static int init_srtp(SSL *s, unsigned int context)
918 s->srtp_profile = NULL;