Move client construction of ClientHello extensions into new framework
[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     /* 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);
25     unsigned int context;
26 } EXTENSION_DEFINITION;
27
28 /*
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.
32  */
33 static const EXTENSION_DEFINITION ext_defs[] = {
34     {
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
42     },
43     {
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
51     },
52 #ifndef OPENSSL_NO_SRP
53     {
54         TLSEXT_TYPE_srp,
55         tls_parse_client_srp,
56         NULL,
57         NULL,
58         tls_construct_client_srp,
59         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
60     },
61 #endif
62 #ifndef OPENSSL_NO_EC
63     {
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
70     },
71     {
72         TLSEXT_TYPE_supported_groups,
73         tls_parse_client_supported_groups,
74         NULL,
75         NULL /* TODO(TLS1.3): Need to add this */,
76         tls_construct_client_supported_groups,
77         EXT_CLIENT_HELLO
78         | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
79     },
80 #endif
81     {
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
88     },
89     {
90         TLSEXT_TYPE_signature_algorithms,
91         tls_parse_client_sig_algs,
92         NULL,
93         NULL,
94         tls_construct_client_sig_algs,
95         EXT_CLIENT_HELLO
96     },
97 #ifndef OPENSSL_NO_OCSP
98     {
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
106     },
107 #endif
108 #ifndef OPENSSL_NO_NEXTPROTONEG
109     {
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
116     },
117 #endif
118     {
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
126     },
127 #ifndef OPENSSL_NO_SRTP
128     {
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
136     },
137 #endif
138     {
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
145     },
146 #ifndef OPENSSL_NO_CT
147     {
148         TLSEXT_TYPE_signed_certificate_timestamp,
149         /*
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.
153          */
154         NULL,
155         tls_parse_server_sct,
156         NULL,
157         tls_construct_client_sct,
158         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
159         | EXT_TLS1_3_CERTIFICATE
160     },
161 #endif
162     {
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
169     },
170     {
171         TLSEXT_TYPE_supported_versions,
172         /* Processed inline as part of version selection */
173         NULL,
174         NULL,
175         NULL,
176         tls_construct_client_supported_versions,
177         EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY | EXT_TLS1_3_ONLY
178     },
179     {
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
187         | EXT_TLS1_3_ONLY
188     },
189     {
190         /*
191          * Special unsolicited ServerHello extension only used when
192          * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
193          */
194         TLSEXT_TYPE_cryptopro_bug,
195         NULL,
196         NULL,
197         tls_construct_server_cryptopro_bug,
198         NULL,
199         EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
200     },
201     {
202         /* Last in the list because it must be added as the last extension */
203         TLSEXT_TYPE_padding,
204         /* We send this, but don't read it */
205         NULL,
206         NULL,
207         NULL,
208         tls_construct_client_padding,
209         EXT_CLIENT_HELLO
210     }
211 };
212
213 /*
214  * Comparison function used in a call to qsort (see tls_collect_extensions()
215  * below.)
216  * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
217  *
218  * Returns:
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
222  */
223 static int compare_extensions(const void *p1, const void *p2)
224 {
225     const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
226     const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
227
228     if (e1->type < e2->type)
229         return -1;
230     else if (e1->type > e2->type)
231         return 1;
232
233     return 0;
234 }
235
236 /*
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.
240  */
241 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
242 {
243     size_t i;
244
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)
249                 return 0;
250
251             if (SSL_IS_DTLS(s)) {
252                 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
253                     return 0;
254             } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
255                     return 0;
256             }
257
258             return 1;
259         }
260     }
261
262     /* Unknown extension. We allow it */
263     return 1;
264 }
265
266 /*
267  * Finds an extension definition for the give extension |type|.
268  * Returns 1 if found and stores the definition in |*def|, or returns 0
269  * otherwise.
270  */
271 static int find_extension_definition(SSL *s, unsigned int type,
272                                      const EXTENSION_DEFINITION **def)
273 {
274     size_t i;
275
276     for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
277         if (type == ext_defs[i].type) {
278             *def = &ext_defs[i];
279             return 1;
280         }
281     }
282
283     /* Unknown extension */
284     return 0;
285 }
286
287 /*
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.
293  *
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.
299  */
300
301 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
302                            RAW_EXTENSION **res, size_t *numfound, int *ad)
303 {
304     PACKET extensions = *packet;
305     size_t num_extensions = 0, i = 0;
306     RAW_EXTENSION *raw_extensions = NULL;
307
308     /* First pass: count the extensions. */
309     while (PACKET_remaining(&extensions) > 0) {
310         unsigned int type;
311         PACKET extension;
312
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;
317             goto err;
318         }
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;
323             goto err;
324         }
325         num_extensions++;
326     }
327
328     if (num_extensions > 0) {
329         raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
330                                         * num_extensions);
331         if (raw_extensions == NULL) {
332             *ad = SSL_AD_INTERNAL_ERROR;
333             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
334             goto err;
335         }
336
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);
345                 goto err;
346             }
347         }
348
349         if (PACKET_remaining(packet) != 0) {
350             *ad = SSL_AD_DECODE_ERROR;
351             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
352             goto err;
353         }
354         /* Sort the extensions and make sure there are no duplicates. */
355         qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
356               compare_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;
360                 goto err;
361             }
362         }
363     }
364
365     *res = raw_extensions;
366     *numfound = num_extensions;
367     return 1;
368
369  err:
370     OPENSSL_free(raw_extensions);
371     return 0;
372 }
373
374 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
375                              size_t numexts, int *al)
376 {
377     size_t loop;
378
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;
383
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);
389
390         /* Skip if we've already parsed this extension */
391         if (currext->parsed)
392             continue;
393
394         currext->parsed = 1;
395
396         parser = NULL;
397         if (find_extension_definition(s, currext->type, &extdef)) {
398             parser = s->server ? extdef->parse_client_ext
399                                : extdef->parse_server_ext;
400
401             /* Check if extension is defined for our protocol. If not, skip */
402             if ((SSL_IS_DTLS(s)
403                         && (extdef->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
404                     || (s->version == SSL3_VERSION
405                             && (extdef->context & EXT_SSL3_ALLOWED) == 0)
406                     || (SSL_IS_TLS13(s)
407                         && (extdef->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
408                     || (!SSL_IS_TLS13(s)
409                         && (extdef->context & EXT_TLS1_3_ONLY) != 0))
410                 continue;
411         }
412
413         if (parser == NULL) {
414             /*
415              * Could be a custom extension. We only allow this if it is a non
416              * resumed session on the server side.
417              * 
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.
421              */
422             if ((!s->hit || !s->server)
423                     && (context
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),
428                                         al) <= 0)
429                 return 0;
430
431             continue;
432         }
433
434         if (!parser(s, &currext->data, al))
435             return 0;
436     }
437
438     return 1;
439 }
440
441 /*
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
445  * to be sent.
446  */
447 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
448                         size_t numexts, int *al)
449 {
450     RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
451
452     if (ext == NULL)
453         return 1;
454
455     return tls_parse_all_extensions(s, context, ext, 1, al);
456 }
457
458 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
459                              int *al)
460 {
461     size_t loop;
462     int addcustom = 0;
463     int min_version, max_version = 0, reason;
464
465     /*
466      * Normally if something goes wrong during construction its an internal
467      * error. We can always override this later.
468      */
469     *al = SSL_AD_INTERNAL_ERROR;
470
471     if (!WPACKET_start_sub_packet_u16(pkt)
472                /*
473                 * If extensions are of zero length then we don't even add the
474                 * extensions length bytes to a ClientHello/ServerHello in SSLv3
475                 */
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);
481         return 0;
482     }
483
484     if ((context & EXT_CLIENT_HELLO) != 0) {
485         reason = ssl_get_client_min_max_version(s, &min_version, &max_version);
486         if (reason != 0) {
487             SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
488             return 0;
489         }
490     }
491
492     /* Add custom extensions first */
493     if ((context & EXT_CLIENT_HELLO) != 0) {
494         custom_ext_init(&s->cert->cli_ext);
495         addcustom = 1;
496     } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
497         /*
498          * We already initialised the custom extensions during ClientHello
499          * parsing.
500          * 
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.
504          */
505         addcustom = 1;
506     }
507
508     if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
509         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
510         return 0;
511     }
512
513     for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
514         int (*construct)(SSL *s, WPACKET *pkt, int *al);
515
516         /* Skip if not relevant for our context */
517         if ((ext_defs[loop].context & context) == 0)
518             continue;
519
520         construct = s->server ? ext_defs[loop].construct_server_ext
521                               : ext_defs[loop].construct_client_ext;
522
523         /* Check if this extension is defined for our protocol. If not, skip */
524         if ((SSL_IS_DTLS(s)
525                     && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
526                        != 0)
527                 || (s->version == SSL3_VERSION
528                         && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
529                 || (SSL_IS_TLS13(s)
530                     && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
531                        != 0)
532                 || (!SSL_IS_TLS13(s)
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)
539             continue;
540
541         if (!construct(s, pkt, al))
542             return 0;
543     }
544
545     if (!WPACKET_close(pkt)) {
546         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
547         return 0;
548     }
549
550     return 1;
551 }