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