898b9b97d1ba414580128408eab675061c7e8110
[openssl.git] / ssl / statem / extensions.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdlib.h>
11 #include "../ssl_locl.h"
12 #include "statem_locl.h"
13
14 typedef struct {
15     /* The ID for the extension */
16     unsigned int type;
17     int (*server_parse)(SSL *s, PACKET *pkt, int *al);
18     int (*client_parse)(SSL *s, PACKET *pkt, int *al);
19     int (*server_construct)(SSL *s, WPACKET *pkt, int *al);
20     int (*client_construct)(SSL *s, WPACKET *pkt, int *al);
21     unsigned int context;
22 } EXTENSION_DEFINITION;
23
24 /*
25  * TODO(TLS1.3): Temporarily modified the definitions below to put all TLS1.3
26  * extensions in the ServerHello for now. That needs to be put back to correct
27  * setting once encrypted extensions is working properly.
28  */
29 static const EXTENSION_DEFINITION ext_defs[] = {
30     {
31         TLSEXT_TYPE_renegotiate,
32         tls_parse_client_renegotiate,
33         NULL,
34         tls_construct_server_renegotiate,
35         NULL,
36         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_SSL3_ALLOWED
37         | EXT_TLS1_2_AND_BELOW_ONLY
38     },
39     {
40         TLSEXT_TYPE_server_name,
41         tls_parse_client_server_name,
42         NULL,
43         tls_construct_server_server_name,
44         NULL,
45         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
46         | /*EXT_TLS1_3_ENCRYPTED_EXTENSIONS*/EXT_TLS1_3_SERVER_HELLO
47     },
48 #ifndef OPENSSL_NO_SRP
49     {
50         TLSEXT_TYPE_srp,
51         tls_parse_client_srp,
52         NULL,
53         NULL,
54         NULL,
55         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
56     },
57 #endif
58 #ifndef OPENSSL_NO_EC
59     {
60         TLSEXT_TYPE_ec_point_formats,
61         tls_parse_client_ec_pt_formats,
62         NULL,
63         tls_construct_server_ec_pt_formats,
64         NULL,
65         EXT_CLIENT_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
66     },
67     {
68         TLSEXT_TYPE_supported_groups,
69         tls_parse_client_supported_groups,
70         NULL,
71         NULL /* TODO(TLS1.3): Need to add this */,
72         NULL,
73         EXT_CLIENT_HELLO
74         | /*EXT_TLS1_3_ENCRYPTED_EXTENSIONS*/EXT_TLS1_3_SERVER_HELLO
75     },
76 #endif
77     {
78         TLSEXT_TYPE_session_ticket,
79         tls_parse_client_session_ticket,
80         NULL,
81         tls_construct_server_session_ticket,
82         NULL,
83         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
84     },
85     {
86         TLSEXT_TYPE_signature_algorithms,
87         tls_parse_client_sig_algs,
88         NULL,
89         NULL,
90         NULL,
91         EXT_CLIENT_HELLO
92     },
93     {
94         TLSEXT_TYPE_status_request,
95         tls_parse_client_status_request,
96         NULL,
97         tls_construct_server_status_request,
98         NULL,
99         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
100         | /*EXT_TLS1_3_CERTIFICATE*/EXT_TLS1_3_SERVER_HELLO
101     },
102 #ifndef OPENSSL_NO_NEXTPROTONEG
103     {
104         TLSEXT_TYPE_next_proto_neg,
105         tls_parse_client_npn,
106         NULL,
107         tls_construct_server_next_proto_neg,
108         NULL,
109         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
110     },
111 #endif
112     {
113         TLSEXT_TYPE_application_layer_protocol_negotiation,
114         tls_parse_client_alpn,
115         NULL,
116         tls_construct_server_alpn,
117         NULL,
118         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
119         | /*EXT_TLS1_3_ENCRYPTED_EXTENSIONS*/EXT_TLS1_3_SERVER_HELLO
120     },
121 #ifndef OPENSSL_NO_SRTP
122     {
123         TLSEXT_TYPE_use_srtp,
124         tls_parse_client_use_srtp,
125         NULL,
126         tls_construct_server_use_srtp,
127         NULL,
128         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
129         | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
130     },
131 #endif
132     {
133         TLSEXT_TYPE_encrypt_then_mac,
134         tls_parse_client_etm,
135         NULL,
136         tls_construct_server_etm,
137         NULL,
138         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
139     },
140     {
141         TLSEXT_TYPE_signed_certificate_timestamp,
142         /*
143          * No server side support for this, but can be provided by a custom
144          * extension. This is an exception to the rule that custom extensions
145          * cannot override built in ones.
146          */
147         NULL,
148         NULL,
149         NULL,
150         NULL,
151         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
152         | /*EXT_TLS1_3_CERTIFICATE*/EXT_TLS1_3_SERVER_HELLO
153     },
154     {
155         TLSEXT_TYPE_extended_master_secret,
156         tls_parse_client_ems,
157         NULL,
158         tls_construct_server_ems,
159         NULL,
160         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
161     },
162     {
163         TLSEXT_TYPE_supported_versions,
164         /* Processed inline as part of version selection */
165         NULL,
166         NULL,
167         NULL,
168         NULL,
169         EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY
170     },
171     {
172         TLSEXT_TYPE_padding,
173         /* We send this, but don't read it */
174         NULL,
175         NULL,
176         NULL,
177         NULL,
178         EXT_CLIENT_HELLO
179     },
180     {
181         TLSEXT_TYPE_key_share,
182         tls_parse_client_key_share,
183         NULL,
184         tls_construct_server_key_share,
185         NULL,
186         EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
187         | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
188         | EXT_TLS1_3_ONLY
189     },
190     {
191         /*
192          * Special unsolicited ServerHello extension only used when
193          * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
194          */
195         TLSEXT_TYPE_cryptopro_bug,
196         NULL,
197         NULL,
198         tls_construct_server_cryptopro_bug,
199         NULL,
200         EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
201     }
202 };
203
204 /*
205  * Comparison function used in a call to qsort (see tls_collect_extensions()
206  * below.)
207  * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
208  *
209  * Returns:
210  *  1 if the type for p1 is greater than p2
211  *  0 if the type for p1 and p2 are the same
212  * -1 if the type for p1 is less than p2
213  */
214 static int compare_extensions(const void *p1, const void *p2)
215 {
216     const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
217     const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
218
219     if (e1->type < e2->type)
220         return -1;
221     else if (e1->type > e2->type)
222         return 1;
223
224     return 0;
225 }
226
227 /*
228  * Verify whether we are allowed to use the extension |type| in the current
229  * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
230  * indicate the extension is not allowed.
231  */
232 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
233 {
234     size_t i;
235
236     for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
237         if (type == ext_defs[i].type) {
238             /* Check we're allowed to use this extension in this context */
239             if ((context & ext_defs[i].context) == 0)
240                 return 0;
241
242             if (SSL_IS_DTLS(s)) {
243                 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
244                     return 0;
245             } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
246                     return 0;
247             }
248
249             return 1;
250         }
251     }
252
253     /* Unknown extension. We allow it */
254     return 1;
255 }
256
257 /*
258  * Finds an extension definition for the give extension |type|.
259  * Returns 1 if found and stores the definition in |*def|, or returns 0
260  * otherwise.
261  */
262 static int find_extension_definition(SSL *s, unsigned int type,
263                                      const EXTENSION_DEFINITION **def)
264 {
265     size_t i;
266
267     for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
268         if (type == ext_defs[i].type) {
269             *def = &ext_defs[i];
270             return 1;
271         }
272     }
273
274     /* Unknown extension */
275     return 0;
276 }
277
278 /*
279  * Gather a list of all the extensions from the data in |packet]. |context|
280  * tells us which message this extension is for. The raw extension data is
281  * stored in |*res| with the number of found extensions in |*numfound|. In the
282  * event of an error the alert type to use is stored in |*ad|. We don't actually
283  * process the content of the extensions yet, except to check their types.
284  *
285  * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
286  * more than one extension of the same type in a ClientHello or ServerHello.
287  * This function returns 1 if all extensions are unique and we have parsed their
288  * types, and 0 if the extensions contain duplicates, could not be successfully
289  * parsed, or an internal error occurred.
290  */
291 /*
292  * TODO(TLS1.3): Refactor ServerHello extension parsing to use this and then
293  * remove tls1_check_duplicate_extensions()
294  */
295 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
296                            RAW_EXTENSION **res, size_t *numfound, int *ad)
297 {
298     PACKET extensions = *packet;
299     size_t num_extensions = 0, i = 0;
300     RAW_EXTENSION *raw_extensions = NULL;
301
302     /* First pass: count the extensions. */
303     while (PACKET_remaining(&extensions) > 0) {
304         unsigned int type;
305         PACKET extension;
306
307         if (!PACKET_get_net_2(&extensions, &type) ||
308             !PACKET_get_length_prefixed_2(&extensions, &extension)) {
309             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
310             *ad = SSL_AD_DECODE_ERROR;
311             goto err;
312         }
313         /* Verify this extension is allowed */
314         if (!verify_extension(s, context, type)) {
315             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
316             *ad = SSL_AD_ILLEGAL_PARAMETER;
317             goto err;
318         }
319         num_extensions++;
320     }
321
322     if (num_extensions > 0) {
323         raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
324                                         * num_extensions);
325         if (raw_extensions == NULL) {
326             *ad = SSL_AD_INTERNAL_ERROR;
327             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
328             goto err;
329         }
330
331         /* Second pass: collect the extensions. */
332         for (i = 0; i < num_extensions; i++) {
333             if (!PACKET_get_net_2(packet, &raw_extensions[i].type) ||
334                 !PACKET_get_length_prefixed_2(packet,
335                                               &raw_extensions[i].data)) {
336                 /* This should not happen. */
337                 *ad = SSL_AD_INTERNAL_ERROR;
338                 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
339                 goto err;
340             }
341         }
342
343         if (PACKET_remaining(packet) != 0) {
344             *ad = SSL_AD_DECODE_ERROR;
345             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
346             goto err;
347         }
348         /* Sort the extensions and make sure there are no duplicates. */
349         qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
350               compare_extensions);
351         for (i = 1; i < num_extensions; i++) {
352             if (raw_extensions[i - 1].type == raw_extensions[i].type) {
353                 *ad = SSL_AD_DECODE_ERROR;
354                 goto err;
355             }
356         }
357     }
358
359     *res = raw_extensions;
360     *numfound = num_extensions;
361     return 1;
362
363  err:
364     OPENSSL_free(raw_extensions);
365     return 0;
366 }
367
368 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
369                              size_t numexts, int *al)
370 {
371     size_t loop;
372
373     for (loop = 0; loop < numexts; loop++) {
374         RAW_EXTENSION *currext = &exts[loop];
375         const EXTENSION_DEFINITION *extdef = NULL;
376         int (*parser)(SSL *s, PACKET *pkt, int *al) = NULL;
377
378         if (s->tlsext_debug_cb)
379             s->tlsext_debug_cb(s, 0, currext->type,
380                                PACKET_data(&currext->data),
381                                PACKET_remaining(&currext->data),
382                                s->tlsext_debug_arg);
383
384         /* Skip if we've already parsed this extension */
385         if (currext->parsed)
386             continue;
387
388         currext->parsed = 1;
389
390         parser = NULL;
391         if (find_extension_definition(s, currext->type, &extdef)) {
392             parser = s->server ? extdef->server_parse : extdef->client_parse;
393
394             /* Check if extension is defined for our protocol. If not, skip */
395             if ((SSL_IS_DTLS(s)
396                         && (extdef->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
397                     || (s->version == SSL3_VERSION
398                             && (extdef->context & EXT_SSL3_ALLOWED) == 0)
399                     || (SSL_IS_TLS13(s)
400                         && (extdef->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
401                     || (!SSL_IS_TLS13(s)
402                         && (extdef->context & EXT_TLS1_3_ONLY) != 0))
403                 continue;
404         }
405
406         if (parser == NULL) {
407             /*
408              * Could be a custom extension. We only allow this if it is a non
409              * resumed session on the server side.
410              * 
411              * TODO(TLS1.3): We only allow old style <=TLS1.2 custom extensions.
412              * We're going to need a new mechanism for TLS1.3 to specify which
413              * messages to add the custom extensions to.
414              */
415             if ((!s->hit || !s->server)
416                     && (context
417                         & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
418                     && custom_ext_parse(s, s->server, currext->type,
419                                         PACKET_data(&currext->data),
420                                         PACKET_remaining(&currext->data),
421                                         al) <= 0)
422                 return 0;
423
424             continue;
425         }
426
427         if (!parser(s, &currext->data, al))
428             return 0;
429     }
430
431     return 1;
432 }
433
434 /*
435  * Find a specific extension by |type| in the list |exts| containing |numexts|
436  * extensions, and the parse it immediately. Returns 1 on success, or 0 on
437  * failure. If a failure has occurred then |*al| will also be set to the alert
438  * to be sent.
439  */
440 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
441                         size_t numexts, int *al)
442 {
443     RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
444
445     if (ext == NULL)
446         return 1;
447
448     return tls_parse_all_extensions(s, context, ext, 1, al);
449 }
450
451 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
452                              int *al)
453 {
454     size_t loop;
455     int addcustom = 0;
456
457     /*
458      * Normally if something goes wrong during construction its an internal
459      * error. We can always override this later.
460      */
461     *al = SSL_AD_INTERNAL_ERROR;
462
463     if (!WPACKET_start_sub_packet_u16(pkt)
464                /*
465                 * If extensions are of zero length then we don't even add the
466                 * extensions length bytes to a ClientHello/ServerHello in SSLv3
467                 */
468             || ((context & (EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO)) != 0
469                && s->version == SSL3_VERSION
470                && !WPACKET_set_flags(pkt,
471                                      WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
472         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
473         return 0;
474     }
475
476     for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
477         int (*construct)(SSL *s, WPACKET *pkt, int *al);
478
479         /* Skip if not relevant for our context */
480         if ((ext_defs[loop].context & context) == 0)
481             continue;
482
483         construct = s->server ? ext_defs[loop].server_construct
484                               : ext_defs[loop].client_construct;
485
486         /* Check if this extension is defined for our protocol. If not, skip */
487         if ((SSL_IS_DTLS(s)
488                     && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
489                        != 0)
490                 || (s->version == SSL3_VERSION
491                         && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
492                 || (SSL_IS_TLS13(s)
493                     && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
494                        != 0)
495                 || (!SSL_IS_TLS13(s)
496                     && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
497                     && (context & EXT_CLIENT_HELLO) == 0)
498                 || construct == NULL)
499             continue;
500
501         if (!construct(s, pkt, al))
502             return 0;
503     }
504
505     /* Add custom extensions */
506     if ((context & EXT_CLIENT_HELLO) != 0) {
507         custom_ext_init(&s->cert->cli_ext);
508         addcustom = 1;
509     } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
510         /*
511          * We already initialised the custom extensions during ClientHello
512          * parsing.
513          * 
514          * TODO(TLS1.3): We're going to need a new custom extension mechanism
515          * for TLS1.3, so that custom extensions can specify which of the
516          * multiple message they wish to add themselves to.
517          */
518         addcustom = 1;
519     }
520
521     if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
522         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
523         return 0;
524     }
525
526     if (!WPACKET_close(pkt)) {
527         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
528         return 0;
529     }
530
531     return 1;
532 }