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 tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent,
16 static int tls_ext_init_server_name(SSL *s, unsigned int context);
17 static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent,
19 static int tls_ext_init_status_request(SSL *s, unsigned int context);
20 #ifndef OPENSSL_NO_NEXTPROTONEG
21 static int tls_ext_init_npn(SSL *s, unsigned int context);
23 static int tls_ext_init_alpn(SSL *s, unsigned int context);
24 static int tls_ext_init_sig_algs(SSL *s, unsigned int context);
25 #ifndef OPENSSL_NO_SRP
26 static int tls_ext_init_srp(SSL *s, unsigned int context);
28 static int tls_ext_init_etm(SSL *s, unsigned int context);
29 #ifndef OPENSSL_NO_SRTP
30 static int tls_ext_init_srtp(SSL *s, unsigned int context);
34 /* The ID for the extension */
37 * Initialise extension before parsing. Always called for relevant contexts
38 * even if extension not present
40 int (*init_ext)(SSL *s, unsigned int context);
41 /* Parse extension received by server from client */
42 int (*parse_client_ext)(SSL *s, PACKET *pkt, int *al);
43 /* Parse extension received by client from server */
44 int (*parse_server_ext)(SSL *s, PACKET *pkt, int *al);
45 /* Construct extension sent by server */
46 int (*construct_server_ext)(SSL *s, WPACKET *pkt, int *al);
47 /* Construct extension sent by client */
48 int (*construct_client_ext)(SSL *s, WPACKET *pkt, int *al);
50 * Finalise extension after parsing. Always called where an extensions was
51 * initialised even if the extension was not present. |sent| is set to 1 if
52 * the extension was seen, or 0 otherwise.
54 int (*finalise_ext)(SSL *s, unsigned int context, int sent, int *al);
56 } EXTENSION_DEFINITION;
59 * TODO(TLS1.3): Temporarily modified the definitions below to put all TLS1.3
60 * extensions in the ServerHello for now. That needs to be put back to correct
61 * setting once encrypted extensions is working properly.
63 static const EXTENSION_DEFINITION ext_defs[] = {
65 TLSEXT_TYPE_renegotiate,
67 tls_parse_client_renegotiate,
68 tls_parse_server_renegotiate,
69 tls_construct_server_renegotiate,
70 tls_construct_client_renegotiate,
71 tls_ext_final_renegotiate,
72 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
73 | EXT_TLS1_2_AND_BELOW_ONLY
76 TLSEXT_TYPE_server_name,
77 tls_ext_init_server_name,
78 tls_parse_client_server_name,
79 tls_parse_server_server_name,
80 tls_construct_server_server_name,
81 tls_construct_client_server_name,
82 tls_ext_final_server_name,
83 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
84 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
86 #ifndef OPENSSL_NO_SRP
93 tls_construct_client_srp,
95 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
100 TLSEXT_TYPE_ec_point_formats,
102 tls_parse_client_ec_pt_formats,
103 tls_parse_server_ec_pt_formats,
104 tls_construct_server_ec_pt_formats,
105 tls_construct_client_ec_pt_formats,
107 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
110 TLSEXT_TYPE_supported_groups,
112 tls_parse_client_supported_groups,
114 NULL /* TODO(TLS1.3): Need to add this */,
115 tls_construct_client_supported_groups,
117 EXT_CLIENT_HELLO | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
121 TLSEXT_TYPE_session_ticket,
123 tls_parse_client_session_ticket,
124 tls_parse_server_session_ticket,
125 tls_construct_server_session_ticket,
126 tls_construct_client_session_ticket,
128 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
131 TLSEXT_TYPE_signature_algorithms,
132 tls_ext_init_sig_algs,
133 tls_parse_client_sig_algs,
136 tls_construct_client_sig_algs,
140 #ifndef OPENSSL_NO_OCSP
142 TLSEXT_TYPE_status_request,
143 tls_ext_init_status_request,
144 tls_parse_client_status_request,
145 tls_parse_server_status_request,
146 tls_construct_server_status_request,
147 tls_construct_client_status_request,
149 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
150 | EXT_TLS1_3_CERTIFICATE
153 #ifndef OPENSSL_NO_NEXTPROTONEG
155 TLSEXT_TYPE_next_proto_neg,
157 tls_parse_client_npn,
158 tls_parse_server_npn,
159 tls_construct_server_next_proto_neg,
160 tls_construct_client_npn,
162 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
166 TLSEXT_TYPE_application_layer_protocol_negotiation,
168 tls_parse_client_alpn,
169 tls_parse_server_alpn,
170 tls_construct_server_alpn,
171 tls_construct_client_alpn,
173 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
174 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
176 #ifndef OPENSSL_NO_SRTP
178 TLSEXT_TYPE_use_srtp,
180 tls_parse_client_use_srtp,
181 tls_parse_server_use_srtp,
182 tls_construct_server_use_srtp,
183 tls_construct_client_use_srtp,
185 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
186 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
190 TLSEXT_TYPE_encrypt_then_mac,
192 tls_parse_client_etm,
193 tls_parse_server_etm,
194 tls_construct_server_etm,
195 tls_construct_client_etm,
197 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
199 #ifndef OPENSSL_NO_CT
201 TLSEXT_TYPE_signed_certificate_timestamp,
204 * No server side support for this, but can be provided by a custom
205 * extension. This is an exception to the rule that custom extensions
206 * cannot override built in ones.
209 tls_parse_server_sct,
211 tls_construct_client_sct,
213 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
214 | EXT_TLS1_3_CERTIFICATE
218 TLSEXT_TYPE_extended_master_secret,
220 tls_parse_client_ems,
221 tls_parse_server_ems,
222 tls_construct_server_ems,
223 tls_construct_client_ems,
225 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
228 TLSEXT_TYPE_supported_versions,
230 /* Processed inline as part of version selection */
234 tls_construct_client_supported_versions,
236 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY
239 TLSEXT_TYPE_key_share,
241 tls_parse_client_key_share,
242 tls_parse_server_key_share,
243 tls_construct_server_key_share,
244 tls_construct_client_key_share,
246 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
247 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
252 * Special unsolicited ServerHello extension only used when
253 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
255 TLSEXT_TYPE_cryptopro_bug,
259 tls_construct_server_cryptopro_bug,
262 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
265 /* Last in the list because it must be added as the last extension */
268 /* We send this, but don't read it */
272 tls_construct_client_padding,
279 * Comparison function used in a call to qsort (see tls_collect_extensions()
281 * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
284 * 1 if the type for p1 is greater than p2
285 * 0 if the type for p1 and p2 are the same
286 * -1 if the type for p1 is less than p2
288 static int compare_extensions(const void *p1, const void *p2)
290 const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
291 const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
293 if (e1->type < e2->type)
295 else if (e1->type > e2->type)
302 * Verify whether we are allowed to use the extension |type| in the current
303 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
304 * indicate the extension is not allowed.
306 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
310 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
311 if (type == ext_defs[i].type) {
312 /* Check we're allowed to use this extension in this context */
313 if ((context & ext_defs[i].context) == 0)
316 if (SSL_IS_DTLS(s)) {
317 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
319 } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
327 /* Unknown extension. We allow it */
332 * Finds an extension definition for the give extension |type|.
333 * Returns 1 if found and stores the definition in |*def|, or returns 0
336 static int find_extension_definition(SSL *s, unsigned int type,
337 const EXTENSION_DEFINITION **def)
341 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
342 if (type == ext_defs[i].type) {
348 /* Unknown extension */
352 static int extension_is_relevant(SSL *s, unsigned int extctx,
353 unsigned int thisctx)
356 && (extctx & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
357 || (s->version == SSL3_VERSION
358 && (extctx & EXT_SSL3_ALLOWED) == 0)
360 && (extctx & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
361 || (!SSL_IS_TLS13(s) && (extctx & EXT_TLS1_3_ONLY) != 0))
368 * Gather a list of all the extensions from the data in |packet]. |context|
369 * tells us which message this extension is for. Ttls_parse_server_ec_pt_formatshe raw extension data is
370 * stored in |*res| with the number of found extensions in |*numfound|. In the
371 * event of an error the alert type to use is stored in |*ad|. We don't actually
372 * process the content of the extensions yet, except to check their types.
374 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
375 * more than one extension of the same type in a ClientHello or ServerHello.
376 * This function returns 1 if all extensions are unique and we have parsed their
377 * types, and 0 if the extensions contain duplicates, could not be successfully
378 * parsed, or an internal error occurred.
381 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
382 RAW_EXTENSION **res, size_t *numfound, int *ad)
384 PACKET extensions = *packet;
385 size_t num_extensions = 0, i = 0;
386 RAW_EXTENSION *raw_extensions = NULL;
388 /* First pass: count the extensions. */
389 while (PACKET_remaining(&extensions) > 0) {
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 *ad = SSL_AD_DECODE_ERROR;
399 /* Verify this extension is allowed */
400 if (!verify_extension(s, context, type)) {
401 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
402 *ad = SSL_AD_ILLEGAL_PARAMETER;
408 if (num_extensions > 0) {
409 raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
411 if (raw_extensions == NULL) {
412 *ad = SSL_AD_INTERNAL_ERROR;
413 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
417 /* Second pass: collect the extensions. */
418 for (i = 0; i < num_extensions; i++) {
419 if (!PACKET_get_net_2(packet, &raw_extensions[i].type) ||
420 !PACKET_get_length_prefixed_2(packet,
421 &raw_extensions[i].data)) {
422 /* This should not happen. */
423 *ad = SSL_AD_INTERNAL_ERROR;
424 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
429 if (PACKET_remaining(packet) != 0) {
430 *ad = SSL_AD_DECODE_ERROR;
431 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
434 /* Sort the extensions and make sure there are no duplicates. */
435 qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
437 for (i = 1; i < num_extensions; i++) {
438 if (raw_extensions[i - 1].type == raw_extensions[i].type) {
439 *ad = SSL_AD_DECODE_ERROR;
446 * Initialise all known extensions relevant to this context, whether we have
449 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
450 if(ext_defs[i].init_ext != NULL && (ext_defs[i].context & context) != 0
451 && extension_is_relevant(s, ext_defs[i].context, context)
452 && !ext_defs[i].init_ext(s, context)) {
453 *ad = SSL_AD_INTERNAL_ERROR;
459 * Initialise server side custom extensions. Client side is done during
460 * construction of extensions for the ClientHello.
462 if ((context & (EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_3_SERVER_HELLO)) != 0)
463 custom_ext_init(&s->cert->srv_ext);
465 *res = raw_extensions;
466 *numfound = num_extensions;
470 OPENSSL_free(raw_extensions);
475 * Runs the parsers for all of the extensions in the given list |exts|, which
476 * should have |numexts| extensions in it. The parsers are only run if they are
477 * applicable for the given |context| and the parser has not already been run
478 * for that extension. Returns 1 on success or 0 on failure. In the event of a
479 * failure |*al| is populated with a suitable alert code.
481 static int tls_parse_extension_list(SSL *s, int context, RAW_EXTENSION *exts,
482 size_t numexts, int *al)
486 for (loop = 0; loop < numexts; loop++) {
487 RAW_EXTENSION *currext = &exts[loop];
488 const EXTENSION_DEFINITION *extdef = NULL;
489 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
491 if (s->tlsext_debug_cb)
492 s->tlsext_debug_cb(s, !s->server, currext->type,
493 PACKET_data(&currext->data),
494 PACKET_remaining(&currext->data),
495 s->tlsext_debug_arg);
497 /* Skip if we've already parsed this extension */
504 if (find_extension_definition(s, currext->type, &extdef)) {
505 parser = s->server ? extdef->parse_client_ext
506 : extdef->parse_server_ext;
508 /* Check if extension is defined for our protocol. If not, skip */
509 if (!extension_is_relevant(s, extdef->context, context))
513 if (parser == NULL) {
515 * Could be a custom extension. We only allow this if it is a non
516 * resumed session on the server side.
518 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
519 * We're going to need a new mechanism for TLS1.3 to specify which
520 * messages to add the custom extensions to.
522 if ((!s->hit || !s->server)
524 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
525 && custom_ext_parse(s, s->server, currext->type,
526 PACKET_data(&currext->data),
527 PACKET_remaining(&currext->data),
534 if (!parser(s, &currext->data, al))
542 * Parse all remaining extensions that have not yet been parsed. Also calls the
543 * finalisation for all extensions at the end. The given extensions must be in
544 * order of type (which happens by default during collection). Returns 1 for
545 * success or 0 for failure. On failure, |*al| is populated with a suitable
548 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
549 size_t numexts, int *al)
553 if (!tls_parse_extension_list(s, context, exts, numexts, al))
557 * Finalise all known extensions relevant to this context, whether we have
560 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
561 if(ext_defs[loop].finalise_ext != NULL
562 && (ext_defs[loop].context & context) != 0) {
566 * Work out whether this extension was sent or not. The sent
567 * extensions in |exts| are sorted by order of type
569 for (curr = 0; curr < numexts
570 && exts[curr].type < ext_defs[loop].type; curr++)
573 if (!ext_defs[loop].finalise_ext(s, context,
574 (curr < numexts && exts[curr].type == ext_defs[loop].type),
584 * Find a specific extension by |type| in the list |exts| containing |numexts|
585 * extensions, and the parse it immediately. Returns 1 on success, or 0 on
586 * failure. If a failure has occurred then |*al| will also be set to the alert
589 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
590 size_t numexts, int *al)
592 RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
597 return tls_parse_extension_list(s, context, ext, 1, al);
600 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
605 int min_version, max_version = 0, reason;
608 * Normally if something goes wrong during construction its an internal
609 * error. We can always override this later.
611 *al = SSL_AD_INTERNAL_ERROR;
613 if (!WPACKET_start_sub_packet_u16(pkt)
615 * If extensions are of zero length then we don't even add the
616 * extensions length bytes to a ClientHello/ServerHello in SSLv3
618 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
619 && s->version == SSL3_VERSION
620 && !WPACKET_set_flags(pkt,
621 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
622 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
626 if ((context & EXT_CLIENT_HELLO) != 0) {
627 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
629 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
634 /* Add custom extensions first */
635 if ((context & EXT_CLIENT_HELLO) != 0) {
636 custom_ext_init(&s->cert->cli_ext);
638 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
640 * We already initialised the custom extensions during ClientHello
643 * TODO(TLS1.3): We're going to need a new custom extension mechanism
644 * for TLS1.3, so that custom extensions can specify which of the
645 * multiple message they wish to add themselves to.
650 if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
651 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
655 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
656 int (*construct)(SSL *s, WPACKET *pkt, int *al);
658 /* Skip if not relevant for our context */
659 if ((ext_defs[loop].context & context) == 0)
662 construct = s->server ? ext_defs[loop].construct_server_ext
663 : ext_defs[loop].construct_client_ext;
665 /* Check if this extension is defined for our protocol. If not, skip */
667 && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
669 || (s->version == SSL3_VERSION
670 && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
672 && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
675 && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
676 && (context & EXT_CLIENT_HELLO) == 0)
677 || ((ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
678 && (context & EXT_CLIENT_HELLO) != 0
679 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
680 || construct == NULL)
683 if (!construct(s, pkt, al))
687 if (!WPACKET_close(pkt)) {
688 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
695 static int tls_ext_final_renegotiate(SSL *s, unsigned int context, int sent,
701 /* Need RI if renegotiating */
703 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
705 *al = SSL_AD_HANDSHAKE_FAILURE;
706 SSLerr(SSL_F_TLS_EXT_FINAL_RENEGOTIATE,
707 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
714 static int tls_ext_init_server_name(SSL *s, unsigned int context)
717 s->servername_done = 0;
722 /* Call the servername callback. Returns 1 for success or 0 for failure. */
723 static int tls_ext_final_server_name(SSL *s, unsigned int context, int sent,
726 int ret = SSL_TLSEXT_ERR_NOACK;
727 int altmp = SSL_AD_UNRECOGNIZED_NAME;
732 if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
733 ret = s->ctx->tlsext_servername_callback(s, &altmp,
734 s->ctx->tlsext_servername_arg);
735 else if (s->initial_ctx != NULL
736 && s->initial_ctx->tlsext_servername_callback != 0)
737 ret = s->initial_ctx->tlsext_servername_callback(s, &altmp,
738 s->initial_ctx->tlsext_servername_arg);
741 case SSL_TLSEXT_ERR_ALERT_FATAL:
745 case SSL_TLSEXT_ERR_ALERT_WARNING:
749 case SSL_TLSEXT_ERR_NOACK:
750 s->servername_done = 0;
758 static int tls_ext_init_status_request(SSL *s, unsigned int context)
761 s->tlsext_status_type = -1;
766 #ifndef OPENSSL_NO_NEXTPROTONEG
767 static int tls_ext_init_npn(SSL *s, unsigned int context)
770 s->s3->next_proto_neg_seen = 0;
776 static int tls_ext_init_alpn(SSL *s, unsigned int context)
779 OPENSSL_free(s->s3->alpn_selected);
780 s->s3->alpn_selected = NULL;
781 s->s3->alpn_selected_len = 0;
782 OPENSSL_free(s->s3->alpn_proposed);
783 s->s3->alpn_proposed = NULL;
784 s->s3->alpn_proposed_len = 0;
790 static int tls_ext_init_sig_algs(SSL *s, unsigned int context)
792 /* Clear any signature algorithms extension received */
793 OPENSSL_free(s->s3->tmp.peer_sigalgs);
794 s->s3->tmp.peer_sigalgs = NULL;
799 #ifndef OPENSSL_NO_SRP
800 static int tls_ext_init_srp(SSL *s, unsigned int context)
802 OPENSSL_free(s->srp_ctx.login);
803 s->srp_ctx.login = NULL;
809 static int tls_ext_init_etm(SSL *s, unsigned int context)
812 s->s3->flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC;
817 #ifndef OPENSSL_NO_SRTP
818 static int tls_ext_init_srtp(SSL *s, unsigned int context)
821 s->srtp_profile = NULL;