Move client parsing of ServerHello 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         NULL,
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         NULL,
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         NULL,
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         NULL,
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         NULL,
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         NULL,
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         NULL,
95         EXT_CLIENT_HELLO
96     },
97     {
98         TLSEXT_TYPE_status_request,
99         tls_parse_client_status_request,
100         tls_parse_server_status_request,
101         tls_construct_server_status_request,
102         NULL,
103         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
104         | EXT_TLS1_3_CERTIFICATE
105     },
106 #ifndef OPENSSL_NO_NEXTPROTONEG
107     {
108         TLSEXT_TYPE_next_proto_neg,
109         tls_parse_client_npn,
110         tls_parse_server_npn,
111         tls_construct_server_next_proto_neg,
112         NULL,
113         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
114     },
115 #endif
116     {
117         TLSEXT_TYPE_application_layer_protocol_negotiation,
118         tls_parse_client_alpn,
119         tls_parse_server_alpn,
120         tls_construct_server_alpn,
121         NULL,
122         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
123         | EXT_TLS1_3_ENCRYPTED_EXTENSIONS
124     },
125 #ifndef OPENSSL_NO_SRTP
126     {
127         TLSEXT_TYPE_use_srtp,
128         tls_parse_client_use_srtp,
129         tls_parse_server_use_srtp,
130         tls_construct_server_use_srtp,
131         NULL,
132         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
133         | EXT_TLS1_3_ENCRYPTED_EXTENSIONS | EXT_DTLS_ONLY
134     },
135 #endif
136     {
137         TLSEXT_TYPE_encrypt_then_mac,
138         tls_parse_client_etm,
139         tls_parse_server_etm,
140         tls_construct_server_etm,
141         NULL,
142         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
143     },
144 #ifndef OPENSSL_NO_CT
145     {
146         TLSEXT_TYPE_signed_certificate_timestamp,
147         /*
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.
151          */
152         NULL,
153         tls_parse_server_sct,
154         NULL,
155         NULL,
156         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO
157         | EXT_TLS1_3_CERTIFICATE
158     },
159 #endif
160     {
161         TLSEXT_TYPE_extended_master_secret,
162         tls_parse_client_ems,
163         tls_parse_server_ems,
164         tls_construct_server_ems,
165         NULL,
166         EXT_CLIENT_HELLO | EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
167     },
168     {
169         TLSEXT_TYPE_supported_versions,
170         /* Processed inline as part of version selection */
171         NULL,
172         NULL,
173         NULL,
174         NULL,
175         EXT_CLIENT_HELLO | EXT_TLS_IMPLEMENTATION_ONLY
176     },
177     {
178         TLSEXT_TYPE_padding,
179         /* We send this, but don't read it */
180         NULL,
181         NULL,
182         NULL,
183         NULL,
184         EXT_CLIENT_HELLO
185     },
186     {
187         TLSEXT_TYPE_key_share,
188         tls_parse_client_key_share,
189         tls_parse_server_key_share,
190         tls_construct_server_key_share,
191         NULL,
192         EXT_CLIENT_HELLO | EXT_TLS1_3_SERVER_HELLO
193         | EXT_TLS1_3_HELLO_RETRY_REQUEST | EXT_TLS_IMPLEMENTATION_ONLY
194         | EXT_TLS1_3_ONLY
195     },
196     {
197         /*
198          * Special unsolicited ServerHello extension only used when
199          * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
200          */
201         TLSEXT_TYPE_cryptopro_bug,
202         NULL,
203         NULL,
204         tls_construct_server_cryptopro_bug,
205         NULL,
206         EXT_TLS1_2_SERVER_HELLO | EXT_TLS1_2_AND_BELOW_ONLY
207     }
208 };
209
210 /*
211  * Comparison function used in a call to qsort (see tls_collect_extensions()
212  * below.)
213  * The two arguments |p1| and |p2| are expected to be pointers to RAW_EXTENSIONs
214  *
215  * Returns:
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
219  */
220 static int compare_extensions(const void *p1, const void *p2)
221 {
222     const RAW_EXTENSION *e1 = (const RAW_EXTENSION *)p1;
223     const RAW_EXTENSION *e2 = (const RAW_EXTENSION *)p2;
224
225     if (e1->type < e2->type)
226         return -1;
227     else if (e1->type > e2->type)
228         return 1;
229
230     return 0;
231 }
232
233 /*
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.
237  */
238 static int verify_extension(SSL *s, unsigned int context, unsigned int type)
239 {
240     size_t i;
241
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)
246                 return 0;
247
248             if (SSL_IS_DTLS(s)) {
249                 if ((ext_defs[i].context & EXT_TLS_ONLY) != 0)
250                     return 0;
251             } else if ((ext_defs[i].context & EXT_DTLS_ONLY) != 0) {
252                     return 0;
253             }
254
255             return 1;
256         }
257     }
258
259     /* Unknown extension. We allow it */
260     return 1;
261 }
262
263 /*
264  * Finds an extension definition for the give extension |type|.
265  * Returns 1 if found and stores the definition in |*def|, or returns 0
266  * otherwise.
267  */
268 static int find_extension_definition(SSL *s, unsigned int type,
269                                      const EXTENSION_DEFINITION **def)
270 {
271     size_t i;
272
273     for (i = 0; i < OSSL_NELEM(ext_defs); i++) {
274         if (type == ext_defs[i].type) {
275             *def = &ext_defs[i];
276             return 1;
277         }
278     }
279
280     /* Unknown extension */
281     return 0;
282 }
283
284 /*
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.
290  *
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.
296  */
297
298 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
299                            RAW_EXTENSION **res, size_t *numfound, int *ad)
300 {
301     PACKET extensions = *packet;
302     size_t num_extensions = 0, i = 0;
303     RAW_EXTENSION *raw_extensions = NULL;
304
305     /* First pass: count the extensions. */
306     while (PACKET_remaining(&extensions) > 0) {
307         unsigned int type;
308         PACKET extension;
309
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;
314             goto err;
315         }
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;
320             goto err;
321         }
322         num_extensions++;
323     }
324
325     if (num_extensions > 0) {
326         raw_extensions = OPENSSL_zalloc(sizeof(*raw_extensions)
327                                         * num_extensions);
328         if (raw_extensions == NULL) {
329             *ad = SSL_AD_INTERNAL_ERROR;
330             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
331             goto err;
332         }
333
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);
342                 goto err;
343             }
344         }
345
346         if (PACKET_remaining(packet) != 0) {
347             *ad = SSL_AD_DECODE_ERROR;
348             SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_LENGTH_MISMATCH);
349             goto err;
350         }
351         /* Sort the extensions and make sure there are no duplicates. */
352         qsort(raw_extensions, num_extensions, sizeof(*raw_extensions),
353               compare_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;
357                 goto err;
358             }
359         }
360     }
361
362     *res = raw_extensions;
363     *numfound = num_extensions;
364     return 1;
365
366  err:
367     OPENSSL_free(raw_extensions);
368     return 0;
369 }
370
371 int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
372                              size_t numexts, int *al)
373 {
374     size_t loop;
375
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;
380
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);
386
387         /* Skip if we've already parsed this extension */
388         if (currext->parsed)
389             continue;
390
391         currext->parsed = 1;
392
393         parser = NULL;
394         if (find_extension_definition(s, currext->type, &extdef)) {
395             parser = s->server ? extdef->parse_client_ext
396                                : extdef->parse_server_ext;
397
398             /* Check if extension is defined for our protocol. If not, skip */
399             if ((SSL_IS_DTLS(s)
400                         && (extdef->context & EXT_TLS_IMPLEMENTATION_ONLY) != 0)
401                     || (s->version == SSL3_VERSION
402                             && (extdef->context & EXT_SSL3_ALLOWED) == 0)
403                     || (SSL_IS_TLS13(s)
404                         && (extdef->context & EXT_TLS1_2_AND_BELOW_ONLY) != 0)
405                     || (!SSL_IS_TLS13(s)
406                         && (extdef->context & EXT_TLS1_3_ONLY) != 0))
407                 continue;
408         }
409
410         if (parser == NULL) {
411             /*
412              * Could be a custom extension. We only allow this if it is a non
413              * resumed session on the server side.
414              * 
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.
418              */
419             if ((!s->hit || !s->server)
420                     && (context
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),
425                                         al) <= 0)
426                 return 0;
427
428             continue;
429         }
430
431         if (!parser(s, &currext->data, al))
432             return 0;
433     }
434
435     return 1;
436 }
437
438 /*
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
442  * to be sent.
443  */
444 int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
445                         size_t numexts, int *al)
446 {
447     RAW_EXTENSION *ext = tls_get_extension_by_type(exts, numexts, type);
448
449     if (ext == NULL)
450         return 1;
451
452     return tls_parse_all_extensions(s, context, ext, 1, al);
453 }
454
455 int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
456                              int *al)
457 {
458     size_t loop;
459     int addcustom = 0;
460
461     /*
462      * Normally if something goes wrong during construction its an internal
463      * error. We can always override this later.
464      */
465     *al = SSL_AD_INTERNAL_ERROR;
466
467     if (!WPACKET_start_sub_packet_u16(pkt)
468                /*
469                 * If extensions are of zero length then we don't even add the
470                 * extensions length bytes to a ClientHello/ServerHello in SSLv3
471                 */
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);
477         return 0;
478     }
479
480     for (loop = 0; loop < OSSL_NELEM(ext_defs); loop++) {
481         int (*construct)(SSL *s, WPACKET *pkt, int *al);
482
483         /* Skip if not relevant for our context */
484         if ((ext_defs[loop].context & context) == 0)
485             continue;
486
487         construct = s->server ? ext_defs[loop].construct_server_ext
488                               : ext_defs[loop].construct_client_ext;
489
490         /* Check if this extension is defined for our protocol. If not, skip */
491         if ((SSL_IS_DTLS(s)
492                     && (ext_defs[loop].context & EXT_TLS_IMPLEMENTATION_ONLY)
493                        != 0)
494                 || (s->version == SSL3_VERSION
495                         && (ext_defs[loop].context & EXT_SSL3_ALLOWED) == 0)
496                 || (SSL_IS_TLS13(s)
497                     && (ext_defs[loop].context & EXT_TLS1_2_AND_BELOW_ONLY)
498                        != 0)
499                 || (!SSL_IS_TLS13(s)
500                     && (ext_defs[loop].context & EXT_TLS1_3_ONLY) != 0
501                     && (context & EXT_CLIENT_HELLO) == 0)
502                 || construct == NULL)
503             continue;
504
505         if (!construct(s, pkt, al))
506             return 0;
507     }
508
509     /* Add custom extensions */
510     if ((context & EXT_CLIENT_HELLO) != 0) {
511         custom_ext_init(&s->cert->cli_ext);
512         addcustom = 1;
513     } else if ((context & EXT_TLS1_2_SERVER_HELLO) != 0) {
514         /*
515          * We already initialised the custom extensions during ClientHello
516          * parsing.
517          * 
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.
521          */
522         addcustom = 1;
523     }
524
525     if (addcustom && !custom_ext_add(s, s->server, pkt, al)) {
526         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
527         return 0;
528     }
529
530     if (!WPACKET_close(pkt)) {
531         SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
532         return 0;
533     }
534
535     return 1;
536 }