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,
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,
49 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
50 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
52 #ifndef OPENSSL_NO_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,
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 */,
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,
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,
98 TLSEXT_TYPE_status_request,
99 tls_parse_client_status_request,
100 tls_parse_server_status_request,
101 tls_construct_server_status_request,
103 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
104 | EXT_TLS1_3_CERTIFICATE
106 #ifndef OPENSSL_NO_NEXTPROTONEG
108 TLSEXT_TYPE_next_proto_neg,
109 tls_parse_client_npn,
110 tls_parse_server_npn,
111 tls_construct_server_next_proto_neg,
113 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
117 TLSEXT_TYPE_application_layer_protocol_negotiation,
118 tls_parse_client_alpn,
119 tls_parse_server_alpn,
120 tls_construct_server_alpn,
122 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
123 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
125 #ifndef OPENSSL_NO_SRTP
127 TLSEXT_TYPE_use_srtp,
128 tls_parse_client_use_srtp,
129 tls_parse_server_use_srtp,
130 tls_construct_server_use_srtp,
132 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
133 | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
137 TLSEXT_TYPE_encrypt_then_mac,
138 tls_parse_client_etm,
139 tls_parse_server_etm,
140 tls_construct_server_etm,
142 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
144 #ifndef OPENSSL_NO_CT
146 TLSEXT_TYPE_signed_certificate_timestamp,
148 * No server side support for this, but can be provided by a custom
149 * extension. This is an exception to the rule that custom extensions
150 * cannot override built in ones.
153 tls_parse_server_sct,
156 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
157 | EXT_TLS1_3_CERTIFICATE
161 TLSEXT_TYPE_extended_master_secret,
162 tls_parse_client_ems,
163 tls_parse_server_ems,
164 tls_construct_server_ems,
166 EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
169 TLSEXT_TYPE_supported_versions,
170 /* Processed inline as part of version selection */
175 EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY
179 /* We send this, but don't read it */
187 TLSEXT_TYPE_key_share,
188 tls_parse_client_key_share,
189 tls_parse_server_key_share,
190 tls_construct_server_key_share,
192 EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
193 | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
198 * Special unsolicited ServerHello extension only used when
199 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
201 TLSEXT_TYPE_cryptopro_bug,
204 tls_construct_server_cryptopro_bug,
206 EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
211 * Comparison function used in a call to qsort (see tls_collect_extensions()
213 * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
216 * 1 if the type for p1 is greater than p2
217 * 0 if the type for p1 and p2 are the same
218 * -1 if the type for p1 is less than p2
220 static int compare_extensions(const void *p1, const void *p2)
222 const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
223 const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
225 if (e1->type < e2->type)
227 else if (e1->type > e2->type)
234 * Verify whether we are allowed to use the extension |type| in the current
235 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
236 * indicate the extension is not allowed.
238 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
242 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
243 if (type == ext_defs[i].type) {
244 /* Check we're allowed to use this extension in this context */
245 if ((context & ext_defs[i].context) == 0)
248 if (SSL_IS_DTLS(s)) {
249 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
251 } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
259 /* Unknown extension. We allow it */
264 * Finds an extension definition for the give extension |type|.
265 * Returns 1 if found and stores the definition in |*def|, or returns 0
268 static int find_extension_definition(SSL *s, unsigned int type,
269 const EXTENSION_DEFINITION **def)
273 for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
274 if (type == ext_defs[i].type) {
280 /* Unknown extension */
285 * Gather a list of all the extensions from the data in |packet]. |context|
286 * tells us which message this extension is for. Ttls_parse_server_ec_pt_formatshe raw extension data is
287 * stored in |*res| with the number of found extensions in |*numfound|. In the
288 * event of an error the alert type to use is stored in |*ad|. We don't actually
289 * process the content of the extensions yet, except to check their types.
291 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
292 * more than one extension of the same type in a ClientHello or ServerHello.
293 * This function returns 1 if all extensions are unique and we have parsed their
294 * types, and 0 if the extensions contain duplicates, could not be successfully
295 * parsed, or an internal error occurred.
298 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
299 RAW_EXTENSION **res, size_t *numfound, int *ad)
301 PACKET extensions = *packet;
302 size_t num_extensions = 0, i = 0;
303 RAW_EXTENSION *raw_extensions = NULL;
305 /* First pass: count the extensions. */
306 while (PACKET_remaining(&extensions) > 0) {
310 if (!PACKET_get_net_2(&extensions, &type) ||
311 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
312 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
313 *ad = SSL_AD_DECODE_ERROR;
316 /* Verify this extension is allowed */
317 if (!verify_extension(s, context, type)) {
318 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
319 *ad = SSL_AD_ILLEGAL_PARAMETER;
325 if (num_extensions > 0) {
326 raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
328 if (raw_extensions == NULL) {
329 *ad = SSL_AD_INTERNAL_ERROR;
330 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
334 /* Second pass: collect the extensions. */
335 for (i = 0; i < num_extensions; i++) {
336 if (!PACKET_get_net_2(packet, &raw_extensions[i].type) ||
337 !PACKET_get_length_prefixed_2(packet,
338 &raw_extensions[i].data)) {
339 /* This should not happen. */
340 *ad = SSL_AD_INTERNAL_ERROR;
341 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
346 if (PACKET_remaining(packet) != 0) {
347 *ad = SSL_AD_DECODE_ERROR;
348 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
351 /* Sort the extensions and make sure there are no duplicates. */
352 qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
354 for (i = 1; i < num_extensions; i++) {
355 if (raw_extensions[i - 1].type == raw_extensions[i].type) {
356 *ad = SSL_AD_DECODE_ERROR;
362 *res = raw_extensions;
363 *numfound = num_extensions;
367 OPENSSL_free(raw_extensions);
371 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
372 size_t numexts, int *al)
376 for (loop = 0; loop < numexts; loop++) {
377 RAW_EXTENSION *currext = &exts[loop];
378 const EXTENSION_DEFINITION *extdef = NULL;
379 int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
381 if (s->tlsext_debug_cb)
382 s->tlsext_debug_cb(s, !s->server, currext->type,
383 PACKET_data(&currext->data),
384 PACKET_remaining(&currext->data),
385 s->tlsext_debug_arg);
387 /* Skip if we've already parsed this extension */
394 if (find_extension_definition(s, currext->type, &extdef)) {
395 parser = s->server ? extdef->parse_client_ext
396 : extdef->parse_server_ext;
398 /* Check if extension is defined for our protocol. If not, skip */
400 && (extdef->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
401 || (s->version == SSL3_VERSION
402 && (extdef->context & EXT_SSL3_ALLOWED) == 0)
404 && (extdef->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
406 && (extdef->context & EXT_TLS1_3_ONLY) != 0))
410 if (parser == NULL) {
412 * Could be a custom extension. We only allow this if it is a non
413 * resumed session on the server side.
415 * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
416 * We're going to need a new mechanism for TLS1.3 to specify which
417 * messages to add the custom extensions to.
419 if ((!s->hit || !s->server)
421 & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
422 && custom_ext_parse(s, s->server, currext->type,
423 PACKET_data(&currext->data),
424 PACKET_remaining(&currext->data),
431 if (!parser(s, &currext->data, al))
439 * Find a specific extension by |type| in the list |exts| containing |numexts|
440 * extensions, and the parse it immediately. Returns 1 on success, or 0 on
441 * failure. If a failure has occurred then |*al| will also be set to the alert
444 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
445 size_t numexts, int *al)
447 RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
452 return tls_parse_all_extensions(s, context, ext, 1, al);
455 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
462 * Normally if something goes wrong during construction its an internal
463 * error. We can always override this later.
465 *al = SSL_AD_INTERNAL_ERROR;
467 if (!WPACKET_start_sub_packet_u16(pkt)
469 * If extensions are of zero length then we don't even add the
470 * extensions length bytes to a ClientHello/ServerHello in SSLv3
472 || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
473 && s->version == SSL3_VERSION
474 && !WPACKET_set_flags(pkt,
475 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
476 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
480 for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
481 int (*construct)(SSL *s, WPACKET *pkt, int *al);
483 /* Skip if not relevant for our context */
484 if ((ext_defs[loop].context & context) == 0)
487 construct = s->server ? ext_defs[loop].construct_server_ext
488 : ext_defs[loop].construct_client_ext;
490 /* Check if this extension is defined for our protocol. If not, skip */
492 && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
494 || (s->version == SSL3_VERSION
495 && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
497 && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
500 && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
501 && (context & EXT_CLIENT_HELLO) == 0)
502 || construct == NULL)
505 if (!construct(s, pkt, al))
509 /* Add custom extensions */
510 if ((context & EXT_CLIENT_HELLO) != 0) {
511 custom_ext_init(&s->cert->cli_ext);
513 } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
515 * We already initialised the custom extensions during ClientHello
518 * TODO(TLS1.3): We're going to need a new custom extension mechanism
519 * for TLS1.3, so that custom extensions can specify which of the
520 * multiple message they wish to add themselves to.
525 if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
526 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
530 if (!WPACKET_close(pkt)) {
531 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);