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