Copy custom extension flags in a call to SSL_set_SSL_CTX()
[openssl.git] / ssl / statem / extensions_cust.c
1 /*
2  * Copyright 2014-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 /* Custom extension utility functions */
11
12 #include <assert.h>
13 #include <openssl/ct.h>
14 #include "../ssl_locl.h"
15 #include "statem_locl.h"
16
17 typedef struct {
18     void *add_arg;
19     custom_ext_add_cb add_cb;
20     custom_ext_free_cb free_cb;
21 } custom_ext_add_cb_wrap;
22
23 typedef struct {
24     void *parse_arg;
25     custom_ext_parse_cb parse_cb;
26 } custom_ext_parse_cb_wrap;
27
28 /*
29  * Provide thin wrapper callbacks which convert new style arguments to old style
30  */
31 static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,
32                                       unsigned int context,
33                                       const unsigned char **out,
34                                       size_t *outlen, X509 *x, size_t chainidx,
35                                       int *al, void *add_arg)
36 {
37     custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
38
39     if (add_cb_wrap->add_cb == NULL)
40         return 1;
41
42     return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,
43                                add_cb_wrap->add_arg);
44 }
45
46 static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,
47                                         unsigned int context,
48                                         const unsigned char *out, void *add_arg)
49 {
50     custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
51
52     if (add_cb_wrap->free_cb == NULL)
53         return;
54
55     add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);
56 }
57
58 static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,
59                                         unsigned int context,
60                                         const unsigned char *in,
61                                         size_t inlen, X509 *x, size_t chainidx,
62                                         int *al, void *parse_arg)
63 {
64     custom_ext_parse_cb_wrap *parse_cb_wrap =
65         (custom_ext_parse_cb_wrap *)parse_arg;
66
67     if (parse_cb_wrap->parse_cb == NULL)
68         return 1;
69
70     return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,
71                                    parse_cb_wrap->parse_arg);
72 }
73
74 /*
75  * Find a custom extension from the list. The |role| param is there to
76  * support the legacy API where custom extensions for client and server could
77  * be set independently on the same SSL_CTX. It is set to ENDPOINT_SERVER if we
78  * are trying to find a method relevant to the server, ENDPOINT_CLIENT for the
79  * client, or ENDPOINT_BOTH for either
80  */
81 custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
82                                    ENDPOINT role, unsigned int ext_type,
83                                    size_t *idx)
84 {
85     size_t i;
86     custom_ext_method *meth = exts->meths;
87
88     for (i = 0; i < exts->meths_count; i++, meth++) {
89         if (ext_type == meth->ext_type
90                 && (role == ENDPOINT_BOTH || role == meth->role
91                     || meth->role == ENDPOINT_BOTH)) {
92             if (idx != NULL)
93                 *idx = i;
94             return meth;
95         }
96     }
97     return NULL;
98 }
99
100 /*
101  * Initialise custom extensions flags to indicate neither sent nor received.
102  */
103 void custom_ext_init(custom_ext_methods *exts)
104 {
105     size_t i;
106     custom_ext_method *meth = exts->meths;
107
108     for (i = 0; i < exts->meths_count; i++, meth++)
109         meth->ext_flags = 0;
110 }
111
112 /* Pass received custom extension data to the application for parsing. */
113 int custom_ext_parse(SSL *s, unsigned int context, unsigned int ext_type,
114                      const unsigned char *ext_data, size_t ext_size, X509 *x,
115                      size_t chainidx, int *al)
116 {
117     custom_ext_methods *exts = &s->cert->custext;
118     custom_ext_method *meth;
119     ENDPOINT role = ENDPOINT_BOTH;
120
121     if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
122         role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
123
124     meth = custom_ext_find(exts, role, ext_type, NULL);
125     /* If not found return success */
126     if (!meth)
127         return 1;
128
129     /* Check if extension is defined for our protocol. If not, skip */
130     if (!extension_is_relevant(s, meth->context, context))
131         return 1;
132
133     if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
134                     | SSL_EXT_TLS1_3_SERVER_HELLO
135                     | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
136         /*
137          * If it's ServerHello or EncryptedExtensions we can't have any
138          * extensions not sent in ClientHello.
139          */
140         if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
141             *al = TLS1_AD_UNSUPPORTED_EXTENSION;
142             return 0;
143         }
144     }
145
146     /*
147      * Extensions received in the ClientHello are marked with the
148      * SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
149      * extensions in the ServerHello/EncryptedExtensions message
150      */
151     if ((context & SSL_EXT_CLIENT_HELLO) != 0)
152         meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
153
154     /* If no parse function set return success */
155     if (!meth->parse_cb)
156         return 1;
157
158     return meth->parse_cb(s, ext_type, context, ext_data, ext_size, x, chainidx,
159                           al, meth->parse_arg);
160 }
161
162 /*
163  * Request custom extension data from the application and add to the return
164  * buffer.
165  */
166 int custom_ext_add(SSL *s, int context, WPACKET *pkt, X509 *x, size_t chainidx,
167                    int maxversion, int *al)
168 {
169     custom_ext_methods *exts = &s->cert->custext;
170     custom_ext_method *meth;
171     size_t i;
172
173     for (i = 0; i < exts->meths_count; i++) {
174         const unsigned char *out = NULL;
175         size_t outlen = 0;
176
177         meth = exts->meths + i;
178
179         if (!should_add_extension(s, meth->context, context, maxversion))
180             continue;
181
182         if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
183                         | SSL_EXT_TLS1_3_SERVER_HELLO
184                         | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
185                         | SSL_EXT_TLS1_3_CERTIFICATE
186                         | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) {
187             /* Only send extensions present in ClientHello. */
188             if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
189                 continue;
190         }
191         /*
192          * We skip it if the callback is absent - except for a ClientHello where
193          * we add an empty extension.
194          */
195         if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
196             continue;
197
198         if (meth->add_cb != NULL) {
199             int cb_retval = meth->add_cb(s, meth->ext_type, context, &out,
200                                          &outlen, x, chainidx, al,
201                                          meth->add_arg);
202
203             if (cb_retval < 0)
204                 return 0;       /* error */
205             if (cb_retval == 0)
206                 continue;       /* skip this extension */
207         }
208
209         if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
210                 || !WPACKET_start_sub_packet_u16(pkt)
211                 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
212                 || !WPACKET_close(pkt)) {
213             *al = SSL_AD_INTERNAL_ERROR;
214             return 0;
215         }
216         if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
217             /*
218              * We can't send duplicates: code logic should prevent this.
219              */
220             assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0);
221             /*
222              * Indicate extension has been sent: this is both a sanity check to
223              * ensure we don't send duplicate extensions and indicates that it
224              * is not an error if the extension is present in ServerHello.
225              */
226             meth->ext_flags |= SSL_EXT_FLAG_SENT;
227         }
228         if (meth->free_cb != NULL)
229             meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
230     }
231     return 1;
232 }
233
234 /* Copy the flags from src to dst for any extensions that exist in both */
235 int custom_exts_copy_flags(custom_ext_methods *dst,
236                            const custom_ext_methods *src)
237 {
238     size_t i;
239     custom_ext_method *methsrc = src->meths;
240
241     for (i = 0; i < src->meths_count; i++, methsrc++) {
242         custom_ext_method *methdst = custom_ext_find(dst, methsrc->role,
243                                                      methsrc->ext_type, NULL);
244
245         if (methdst == NULL)
246             continue;
247
248         methdst->ext_flags = methsrc->ext_flags;
249     }
250
251     return 1;
252 }
253
254 /* Copy table of custom extensions */
255 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
256 {
257     size_t i;
258     int err = 0;
259
260     if (src->meths_count > 0) {
261         dst->meths =
262             OPENSSL_memdup(src->meths,
263                            sizeof(*src->meths) * src->meths_count);
264         if (dst->meths == NULL)
265             return 0;
266         dst->meths_count = src->meths_count;
267
268         for (i = 0; i < src->meths_count; i++) {
269             custom_ext_method *methsrc = src->meths + i;
270             custom_ext_method *methdst = dst->meths + i;
271
272             if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
273                 continue;
274
275             /*
276              * We have found an old style API wrapper. We need to copy the
277              * arguments too.
278              */
279
280             if (err) {
281                 methdst->add_arg = NULL;
282                 methdst->parse_arg = NULL;
283                 continue;
284             }
285
286             methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
287                                               sizeof(custom_ext_add_cb_wrap));
288             methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
289                                             sizeof(custom_ext_parse_cb_wrap));
290
291             if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
292                 err = 1;
293         }
294     }
295
296     if (err) {
297         custom_exts_free(dst);
298         return 0;
299     }
300
301     return 1;
302 }
303
304 void custom_exts_free(custom_ext_methods *exts)
305 {
306     size_t i;
307     custom_ext_method *meth;
308
309     for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
310         if (meth->add_cb != custom_ext_add_old_cb_wrap)
311             continue;
312
313         /* Old style API wrapper. Need to free the arguments too */
314         OPENSSL_free(meth->add_arg);
315         OPENSSL_free(meth->parse_arg);
316     }
317     OPENSSL_free(exts->meths);
318 }
319
320 /* Return true if a client custom extension exists, false otherwise */
321 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
322 {
323     return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
324                            NULL) != NULL;
325 }
326
327 static int add_custom_ext_intern(SSL_CTX *ctx, ENDPOINT role,
328                                  unsigned int ext_type,
329                                  unsigned int context,
330                                  SSL_custom_ext_add_cb_ex add_cb,
331                                  SSL_custom_ext_free_cb_ex free_cb,
332                                  void *add_arg,
333                                  SSL_custom_ext_parse_cb_ex parse_cb,
334                                  void *parse_arg)
335 {
336     custom_ext_methods *exts = &ctx->cert->custext;
337     custom_ext_method *meth, *tmp;
338
339     /*
340      * Check application error: if add_cb is not set free_cb will never be
341      * called.
342      */
343     if (add_cb == NULL && free_cb != NULL)
344         return 0;
345
346 #ifndef OPENSSL_NO_CT
347     /*
348      * We don't want applications registering callbacks for SCT extensions
349      * whilst simultaneously using the built-in SCT validation features, as
350      * these two things may not play well together.
351      */
352     if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
353             && (context & SSL_EXT_CLIENT_HELLO) != 0
354             && SSL_CTX_ct_is_enabled(ctx))
355         return 0;
356 #endif
357
358     /*
359      * Don't add if extension supported internally, but make exception
360      * for extension types that previously were not supported, but now are.
361      */
362     if (SSL_extension_supported(ext_type)
363             && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
364         return 0;
365
366     /* Extension type must fit in 16 bits */
367     if (ext_type > 0xffff)
368         return 0;
369     /* Search for duplicate */
370     if (custom_ext_find(exts, role, ext_type, NULL))
371         return 0;
372     tmp = OPENSSL_realloc(exts->meths,
373                           (exts->meths_count + 1) * sizeof(custom_ext_method));
374     if (tmp == NULL)
375         return 0;
376
377     exts->meths = tmp;
378     meth = exts->meths + exts->meths_count;
379     memset(meth, 0, sizeof(*meth));
380     meth->role = role;
381     meth->context = context;
382     meth->parse_cb = parse_cb;
383     meth->add_cb = add_cb;
384     meth->free_cb = free_cb;
385     meth->ext_type = ext_type;
386     meth->add_arg = add_arg;
387     meth->parse_arg = parse_arg;
388     exts->meths_count++;
389     return 1;
390 }
391
392 static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,
393                               unsigned int ext_type,
394                               unsigned int context,
395                               custom_ext_add_cb add_cb,
396                               custom_ext_free_cb free_cb,
397                               void *add_arg,
398                               custom_ext_parse_cb parse_cb, void *parse_arg)
399 {
400     custom_ext_add_cb_wrap *add_cb_wrap
401         = OPENSSL_malloc(sizeof(*add_cb_wrap));
402     custom_ext_parse_cb_wrap *parse_cb_wrap
403         = OPENSSL_malloc(sizeof(*parse_cb_wrap));
404     int ret;
405
406     if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
407         OPENSSL_free(add_cb_wrap);
408         OPENSSL_free(parse_cb_wrap);
409         return 0;
410     }
411
412     add_cb_wrap->add_arg = add_arg;
413     add_cb_wrap->add_cb = add_cb;
414     add_cb_wrap->free_cb = free_cb;
415     parse_cb_wrap->parse_arg = parse_arg;
416     parse_cb_wrap->parse_cb = parse_cb;
417
418     ret = add_custom_ext_intern(ctx, role, ext_type,
419                                 context,
420                                 custom_ext_add_old_cb_wrap,
421                                 custom_ext_free_old_cb_wrap,
422                                 add_cb_wrap,
423                                 custom_ext_parse_old_cb_wrap,
424                                 parse_cb_wrap);
425
426     if (!ret) {
427         OPENSSL_free(add_cb_wrap);
428         OPENSSL_free(parse_cb_wrap);
429     }
430
431     return ret;
432 }
433
434 /* Application level functions to add the old custom extension callbacks */
435 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
436                                   custom_ext_add_cb add_cb,
437                                   custom_ext_free_cb free_cb,
438                                   void *add_arg,
439                                   custom_ext_parse_cb parse_cb, void *parse_arg)
440 {
441     return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,
442                               SSL_EXT_TLS1_2_AND_BELOW_ONLY
443                               | SSL_EXT_CLIENT_HELLO
444                               | SSL_EXT_TLS1_2_SERVER_HELLO
445                               | SSL_EXT_IGNORE_ON_RESUMPTION,
446                               add_cb, free_cb, add_arg, parse_cb, parse_arg);
447 }
448
449 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
450                                   custom_ext_add_cb add_cb,
451                                   custom_ext_free_cb free_cb,
452                                   void *add_arg,
453                                   custom_ext_parse_cb parse_cb, void *parse_arg)
454 {
455     return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,
456                               SSL_EXT_TLS1_2_AND_BELOW_ONLY
457                               | SSL_EXT_CLIENT_HELLO
458                               | SSL_EXT_TLS1_2_SERVER_HELLO
459                               | SSL_EXT_IGNORE_ON_RESUMPTION,
460                               add_cb, free_cb, add_arg, parse_cb, parse_arg);
461 }
462
463 int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
464                            unsigned int context,
465                            SSL_custom_ext_add_cb_ex add_cb,
466                            SSL_custom_ext_free_cb_ex free_cb,
467                            void *add_arg,
468                            SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
469 {
470     return add_custom_ext_intern(ctx, ENDPOINT_BOTH, ext_type, context, add_cb,
471                                  free_cb, add_arg, parse_cb, parse_arg);
472 }
473
474 int SSL_extension_supported(unsigned int ext_type)
475 {
476     switch (ext_type) {
477         /* Internally supported extensions. */
478     case TLSEXT_TYPE_application_layer_protocol_negotiation:
479 #ifndef OPENSSL_NO_EC
480     case TLSEXT_TYPE_ec_point_formats:
481     case TLSEXT_TYPE_supported_groups:
482     case TLSEXT_TYPE_key_share:
483 #endif
484 #ifndef OPENSSL_NO_NEXTPROTONEG
485     case TLSEXT_TYPE_next_proto_neg:
486 #endif
487     case TLSEXT_TYPE_padding:
488     case TLSEXT_TYPE_renegotiate:
489     case TLSEXT_TYPE_server_name:
490     case TLSEXT_TYPE_session_ticket:
491     case TLSEXT_TYPE_signature_algorithms:
492 #ifndef OPENSSL_NO_SRP
493     case TLSEXT_TYPE_srp:
494 #endif
495 #ifndef OPENSSL_NO_OCSP
496     case TLSEXT_TYPE_status_request:
497 #endif
498 #ifndef OPENSSL_NO_CT
499     case TLSEXT_TYPE_signed_certificate_timestamp:
500 #endif
501 #ifndef OPENSSL_NO_SRTP
502     case TLSEXT_TYPE_use_srtp:
503 #endif
504     case TLSEXT_TYPE_encrypt_then_mac:
505     case TLSEXT_TYPE_supported_versions:
506     case TLSEXT_TYPE_extended_master_secret:
507     case TLSEXT_TYPE_psk_kex_modes:
508     case TLSEXT_TYPE_cookie:
509     case TLSEXT_TYPE_early_data:
510     case TLSEXT_TYPE_certificate_authorities:
511     case TLSEXT_TYPE_psk:
512         return 1;
513     default:
514         return 0;
515     }
516 }