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"
15 /* The ID for the extension */
17 /* Parse extension received by server from client */
18 int (*parse_client_ext)(SSL *s, PACKET *pkt, int *al);
19 /* Parse extension received by client from server */
20 int (*parse_server_ext)(SSL *s, PACKET *pkt, int *al);
21 /* Construct extension sent by server */
22 int (*construct_server_ext)(SSL *s, WPACKET *pkt, int *al);
23 /* Construct extension sent by client */
24 int (*construct_client_ext)(SSL *s, WPACKET *pkt, int *al);
26 } EXTENSION_DEFINITION;
29 * TODO(TLS1.3): Temporarily modified the definitions below to put all TLS1.3
30 * extensions in the ServerHello for now. That needs to be put back to correct
31 * setting once encrypted extensions is working properly.
33 static const EXTENSION_DEFINITION ext_defs[] = {
35 TLSEXT_TYPE_renegotiate,
36 tls_parse_client_renegotiate,
37 tls_parse_server_renegotiate,
38 tls_construct_server_renegotiate,
39 tls_construct_client_renegotiate,
40 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
41 | EXT_TLS1_2_AND_BELOW_ONLY
44 TLSEXT_TYPE_server_name,
45 tls_parse_client_server_name,
46 tls_parse_server_server_name,
47 tls_construct_server_server_name,
48 tls_construct_client_server_name,
49 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
50 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
52 #ifndef OPENSSL_NO_SRP
58 tls_construct_client_srp,
59 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
64 TLSEXT_TYPE_ec_point_formats,
65 tls_parse_client_ec_pt_formats,
66 tls_parse_server_ec_pt_formats,
67 tls_construct_server_ec_pt_formats,
68 tls_construct_client_ec_pt_formats,
69 EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
72 TLSEXT_TYPE_supported_groups,
73 tls_parse_client_supported_groups,
75 NULL /* TODO(TLS1.3): Need to add this */,
76 tls_construct_client_supported_groups,
78 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
82 TLSEXT_TYPE_session_ticket,
83 tls_parse_client_session_ticket,
84 tls_parse_server_session_ticket,
85 tls_construct_server_session_ticket,
86 tls_construct_client_session_ticket,
87 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
90 TLSEXT_TYPE_signature_algorithms,
91 tls_parse_client_sig_algs,
94 tls_construct_client_sig_algs,
97 #ifndef OPENSSL_NO_OCSP
99 TLSEXT_TYPE_status_request,
100 tls_parse_client_status_request,
101 tls_parse_server_status_request,
102 tls_construct_server_status_request,
103 tls_construct_client_status_request,
104 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
105 | EXT_TLS1_3_CERTIFICATE
108 #ifndef OPENSSL_NO_NEXTPROTONEG
110 TLSEXT_TYPE_next_proto_neg,
111 tls_parse_client_npn,
112 tls_parse_server_npn,
113 tls_construct_server_next_proto_neg,
114 tls_construct_client_npn,
115 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
119 TLSEXT_TYPE_application_layer_protocol_negotiation,
120 tls_parse_client_alpn,
121 tls_parse_server_alpn,
122 tls_construct_server_alpn,
123 tls_construct_client_alpn,
124 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
125 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
127 #ifndef OPENSSL_NO_SRTP
129 TLSEXT_TYPE_use_srtp,
130 tls_parse_client_use_srtp,
131 tls_parse_server_use_srtp,
132 tls_construct_server_use_srtp,
133 tls_construct_client_use_srtp,
134 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
135 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
139 TLSEXT_TYPE_encrypt_then_mac,
140 tls_parse_client_etm,
141 tls_parse_server_etm,
142 tls_construct_server_etm,
143 tls_construct_client_etm,
144 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
146 #ifndef OPENSSL_NO_CT
148 TLSEXT_TYPE_signed_certificate_timestamp,
150 * No server side support for this, but can be provided by a custom
151 * extension. This is an exception to the rule that custom extensions
152 * cannot override built in ones.
155 tls_parse_server_sct,
157 tls_construct_client_sct,
158 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
159 | EXT_TLS1_3_CERTIFICATE
163 TLSEXT_TYPE_extended_master_secret,
164 tls_parse_client_ems,
165 tls_parse_server_ems,
166 tls_construct_server_ems,
167 tls_construct_client_ems,
168 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
171 TLSEXT_TYPE_supported_versions,
172 /* Processed inline as part of version selection */
176 tls_construct_client_supported_versions,
177 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY
180 TLSEXT_TYPE_key_share,
181 tls_parse_client_key_share,
182 tls_parse_server_key_share,
183 tls_construct_server_key_share,
184 tls_construct_client_key_share,
185 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
186 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
191 * Special unsolicited ServerHello extension only used when
192 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
194 TLSEXT_TYPE_cryptopro_bug,
197 tls_construct_server_cryptopro_bug,
199 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
202 /* Last in the list because it must be added as the last extension */
204 /* We send this, but don't read it */
208 tls_construct_client_padding,
214 * Comparison function used in a call to qsort (see tls_collect_extensions()
216 * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
219 * 1 if the type for p1 is greater than p2
220 * 0 if the type for p1 and p2 are the same
221 * -1 if the type for p1 is less than p2
223 static int compare_extensions(const void *p1, const void *p2)
225 const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
226 const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
228 if (e1->type < e2->type)
230 else if (e1->type > e2->type)
237 * Verify whether we are allowed to use the extension |type| in the current
238 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
239 * indicate the extension is not allowed.
241 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
245 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
246 if (type == ext_defs[i].type) {
247 /* Check we're allowed to use this extension in this context */
248 if ((context & ext_defs[i].context) == 0)
251 if (SSL_IS_DTLS(s)) {
252 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
254 } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
262 /* Unknown extension. We allow it */
267 * Finds an extension definition for the give extension |type|.
268 * Returns 1 if found and stores the definition in |*def|, or returns 0
271 static int find_extension_definition(SSL *s, unsigned int type,
272 const EXTENSION_DEFINITION **def)
276 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
277 if (type == ext_defs[i].type) {
283 /* Unknown extension */
288 * Gather a list of all the extensions from the data in |packet]. |context|
289 * tells us which message this extension is for. Ttls_parse_server_ec_pt_formatshe raw extension data is
290 * stored in |*res| with the number of found extensions in |*numfound|. In the
291 * event of an error the alert type to use is stored in |*ad|. We don't actually
292 * process the content of the extensions yet, except to check their types.
294 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
295 * more than one extension of the same type in a ClientHello or ServerHello.
296 * This function returns 1 if all extensions are unique and we have parsed their
297 * types, and 0 if the extensions contain duplicates, could not be successfully
298 * parsed, or an internal error occurred.
301 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
302 RAW_EXTENSION **res, size_t *numfound, int *ad)
304 PACKET extensions = *packet;
305 size_t num_extensions = 0, i = 0;
306 RAW_EXTENSION *raw_extensions = NULL;
308 /* First pass: count the extensions. */
309 while (PACKET_remaining(&extensions) > 0) {
313 if (!PACKET_get_net_2(&extensions, &type) ||
314 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
315 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
316 *ad = SSL_AD_DECODE_ERROR;
319 /* Verify this extension is allowed */
320 if (!verify_extension(s, context, type)) {
321 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
322 *ad = SSL_AD_ILLEGAL_PARAMETER;
328 if (num_extensions > 0) {
329 raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
331 if (raw_extensions == NULL) {
332 *ad = SSL_AD_INTERNAL_ERROR;
333 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
337 /* Second pass: collect the extensions. */
338 for (i = 0; i < num_extensions; i++) {
339 if (!PACKET_get_net_2(packet, &raw_extensions[i].type) ||
340 !PACKET_get_length_prefixed_2(packet,
341 &raw_extensions[i].data)) {
342 /* This should not happen. */
343 *ad = SSL_AD_INTERNAL_ERROR;
344 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
349 if (PACKET_remaining(packet) != 0) {
350 *ad = SSL_AD_DECODE_ERROR;
351 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
354 /* Sort the extensions and make sure there are no duplicates. */
355 qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
357 for (i = 1; i < num_extensions; i++) {
358 if (raw_extensions[i - 1].type == raw_extensions[i].type) {
359 *ad = SSL_AD_DECODE_ERROR;
365 *res = raw_extensions;
366 *numfound = num_extensions;
370 OPENSSL_free(raw_extensions);
374 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
375 size_t numexts, int *al)
379 for (loop = 0; loop < numexts; loop++) {
380 RAW_EXTENSION *currext = &exts[loop];
381 const EXTENSION_DEFINITION *extdef = NULL;
382 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
384 if (s->tlsext_debug_cb)
385 s->tlsext_debug_cb(s, !s->server, currext->type,
386 PACKET_data(&currext->data),
387 PACKET_remaining(&currext->data),
388 s->tlsext_debug_arg);
390 /* Skip if we've already parsed this extension */
397 if (find_extension_definition(s, currext->type, &extdef)) {
398 parser = s->server ? extdef->parse_client_ext
399 : extdef->parse_server_ext;
401 /* Check if extension is defined for our protocol. If not, skip */
403 && (extdef->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
404 || (s->version == SSL3_VERSION
405 && (extdef->context & EXT_SSL3_ALLOWED) == 0)
407 && (extdef->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
409 && (extdef->context & EXT_TLS1_3_ONLY) != 0))
413 if (parser == NULL) {
415 * Could be a custom extension. We only allow this if it is a non
416 * resumed session on the server side.
418 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
419 * We're going to need a new mechanism for TLS1.3 to specify which
420 * messages to add the custom extensions to.
422 if ((!s->hit || !s->server)
424 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
425 && custom_ext_parse(s, s->server, currext->type,
426 PACKET_data(&currext->data),
427 PACKET_remaining(&currext->data),
434 if (!parser(s, &currext->data, al))
442 * Find a specific extension by |type| in the list |exts| containing |numexts|
443 * extensions, and the parse it immediately. Returns 1 on success, or 0 on
444 * failure. If a failure has occurred then |*al| will also be set to the alert
447 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
448 size_t numexts, int *al)
450 RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
455 return tls_parse_all_extensions(s, context, ext, 1, al);
458 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
463 int min_version, max_version = 0, reason;
466 * Normally if something goes wrong during construction its an internal
467 * error. We can always override this later.
469 *al = SSL_AD_INTERNAL_ERROR;
471 if (!WPACKET_start_sub_packet_u16(pkt)
473 * If extensions are of zero length then we don't even add the
474 * extensions length bytes to a ClientHello/ServerHello in SSLv3
476 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
477 && s->version == SSL3_VERSION
478 && !WPACKET_set_flags(pkt,
479 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
480 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
484 if ((context & EXT_CLIENT_HELLO) != 0) {
485 reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
487 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
492 /* Add custom extensions first */
493 if ((context & EXT_CLIENT_HELLO) != 0) {
494 custom_ext_init(&s->cert->cli_ext);
496 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
498 * We already initialised the custom extensions during ClientHello
501 * TODO(TLS1.3): We're going to need a new custom extension mechanism
502 * for TLS1.3, so that custom extensions can specify which of the
503 * multiple message they wish to add themselves to.
508 if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
509 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
513 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
514 int (*construct)(SSL *s, WPACKET *pkt, int *al);
516 /* Skip if not relevant for our context */
517 if ((ext_defs[loop].context & context) == 0)
520 construct = s->server ? ext_defs[loop].construct_server_ext
521 : ext_defs[loop].construct_client_ext;
523 /* Check if this extension is defined for our protocol. If not, skip */
525 && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
527 || (s->version == SSL3_VERSION
528 && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
530 && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
533 && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
534 && (context & EXT_CLIENT_HELLO) == 0)
535 || ((ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
536 && (context & EXT_CLIENT_HELLO) != 0
537 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION))
538 || construct == NULL)
541 if (!construct(s, pkt, al))
545 if (!WPACKET_close(pkt)) {
546 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);