Add the ability to add a custom extension on an SSL object
[openssl.git] / ssl / statem / extensions_cust.c
1 /*
2  * Copyright 2014-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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_local.h"
14 #include "internal/cryptlib.h"
15 #include "statem_local.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_CONNECTION *s, unsigned int context,
114                      unsigned int ext_type,
115                      const unsigned char *ext_data, size_t ext_size, X509 *x,
116                      size_t chainidx)
117 {
118     int al;
119     custom_ext_methods *exts = &s->cert->custext;
120     custom_ext_method *meth;
121     ENDPOINT role = ENDPOINT_BOTH;
122
123     if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
124         role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
125
126     meth = custom_ext_find(exts, role, ext_type, NULL);
127     /* If not found return success */
128     if (!meth)
129         return 1;
130
131     /* Check if extension is defined for our protocol. If not, skip */
132     if (!extension_is_relevant(s, meth->context, context))
133         return 1;
134
135     if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
136                     | SSL_EXT_TLS1_3_SERVER_HELLO
137                     | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
138         /*
139          * If it's ServerHello or EncryptedExtensions we can't have any
140          * extensions not sent in ClientHello.
141          */
142         if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
143             SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION);
144             return 0;
145         }
146     }
147
148     /*
149      * Extensions received in the ClientHello or CertificateRequest are marked
150      * with the SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
151      * extensions in the response messages
152      */
153     if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST))
154             != 0)
155         meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
156
157     /* If no parse function set return success */
158     if (meth->parse_cb == NULL)
159         return 1;
160
161     if (meth->parse_cb(SSL_CONNECTION_GET_SSL(s), ext_type, context, ext_data,
162                        ext_size, x, chainidx, &al, meth->parse_arg) <= 0) {
163         SSLfatal(s, al, SSL_R_BAD_EXTENSION);
164         return 0;
165     }
166
167     return 1;
168 }
169
170 /*
171  * Request custom extension data from the application and add to the return
172  * buffer.
173  */
174 int custom_ext_add(SSL_CONNECTION *s, int context, WPACKET *pkt, X509 *x,
175                    size_t chainidx, int maxversion)
176 {
177     custom_ext_methods *exts = &s->cert->custext;
178     custom_ext_method *meth;
179     size_t i;
180     int al;
181     int for_comp = (context & SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION) != 0;
182
183     for (i = 0; i < exts->meths_count; i++) {
184         const unsigned char *out = NULL;
185         size_t outlen = 0;
186
187         meth = exts->meths + i;
188
189         if (!should_add_extension(s, meth->context, context, maxversion))
190             continue;
191
192         if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
193                         | SSL_EXT_TLS1_3_SERVER_HELLO
194                         | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
195                         | SSL_EXT_TLS1_3_CERTIFICATE
196                         | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST)) != 0) {
197             /* Only send extensions present in ClientHello/CertificateRequest */
198             if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
199                 continue;
200         }
201         /*
202          * We skip it if the callback is absent - except for a ClientHello where
203          * we add an empty extension.
204          */
205         if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
206             continue;
207
208         if (meth->add_cb != NULL) {
209             int cb_retval = meth->add_cb(SSL_CONNECTION_GET_SSL(s),
210                                          meth->ext_type, context, &out,
211                                          &outlen, x, chainidx, &al,
212                                          meth->add_arg);
213
214             if (cb_retval < 0) {
215                 if (!for_comp)
216                     SSLfatal(s, al, SSL_R_CALLBACK_FAILED);
217                 return 0;       /* error */
218             }
219             if (cb_retval == 0)
220                 continue;       /* skip this extension */
221         }
222
223         if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
224                 || !WPACKET_start_sub_packet_u16(pkt)
225                 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
226                 || !WPACKET_close(pkt)) {
227             if (!for_comp)
228                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
229             return 0;
230         }
231         if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
232             /*
233              * We can't send duplicates: code logic should prevent this.
234              */
235             if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {
236                 if (!for_comp)
237                     SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
238                 return 0;
239             }
240             /*
241              * Indicate extension has been sent: this is both a sanity check to
242              * ensure we don't send duplicate extensions and indicates that it
243              * is not an error if the extension is present in ServerHello.
244              */
245             meth->ext_flags |= SSL_EXT_FLAG_SENT;
246         }
247         if (meth->free_cb != NULL)
248             meth->free_cb(SSL_CONNECTION_GET_SSL(s), meth->ext_type, context,
249                           out, meth->add_arg);
250     }
251     return 1;
252 }
253
254 /* Copy the flags from src to dst for any extensions that exist in both */
255 int custom_exts_copy_flags(custom_ext_methods *dst,
256                            const custom_ext_methods *src)
257 {
258     size_t i;
259     custom_ext_method *methsrc = src->meths;
260
261     for (i = 0; i < src->meths_count; i++, methsrc++) {
262         custom_ext_method *methdst = custom_ext_find(dst, methsrc->role,
263                                                      methsrc->ext_type, NULL);
264
265         if (methdst == NULL)
266             continue;
267
268         methdst->ext_flags = methsrc->ext_flags;
269     }
270
271     return 1;
272 }
273
274 /* Copy table of custom extensions */
275 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
276 {
277     size_t i;
278     int err = 0;
279
280     if (src->meths_count > 0) {
281         dst->meths =
282             OPENSSL_memdup(src->meths,
283                            sizeof(*src->meths) * src->meths_count);
284         if (dst->meths == NULL)
285             return 0;
286         dst->meths_count = src->meths_count;
287
288         for (i = 0; i < src->meths_count; i++) {
289             custom_ext_method *methsrc = src->meths + i;
290             custom_ext_method *methdst = dst->meths + i;
291
292             if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
293                 continue;
294
295             /*
296              * We have found an old style API wrapper. We need to copy the
297              * arguments too.
298              */
299
300             if (err) {
301                 methdst->add_arg = NULL;
302                 methdst->parse_arg = NULL;
303                 continue;
304             }
305
306             methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
307                                               sizeof(custom_ext_add_cb_wrap));
308             methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
309                                             sizeof(custom_ext_parse_cb_wrap));
310
311             if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
312                 err = 1;
313         }
314     }
315
316     if (err) {
317         custom_exts_free(dst);
318         return 0;
319     }
320
321     return 1;
322 }
323
324 void custom_exts_free(custom_ext_methods *exts)
325 {
326     size_t i;
327     custom_ext_method *meth;
328
329     for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
330         if (meth->add_cb != custom_ext_add_old_cb_wrap)
331             continue;
332
333         /* Old style API wrapper. Need to free the arguments too */
334         OPENSSL_free(meth->add_arg);
335         OPENSSL_free(meth->parse_arg);
336     }
337     OPENSSL_free(exts->meths);
338 }
339
340 /* Return true if a client custom extension exists, false otherwise */
341 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
342 {
343     return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
344                            NULL) != NULL;
345 }
346
347 int ossl_tls_add_custom_ext_intern(SSL_CTX *ctx, custom_ext_methods *exts,
348                                    ENDPOINT role, unsigned int ext_type,
349                                    unsigned int context,
350                                    SSL_custom_ext_add_cb_ex add_cb,
351                                    SSL_custom_ext_free_cb_ex free_cb,
352                                    void *add_arg,
353                                    SSL_custom_ext_parse_cb_ex parse_cb,
354                                    void *parse_arg)
355 {
356     custom_ext_method *meth, *tmp;
357
358     /*
359      * Check application error: if add_cb is not set free_cb will never be
360      * called.
361      */
362     if (add_cb == NULL && free_cb != NULL)
363         return 0;
364
365     if (exts == NULL)
366         exts = &ctx->cert->custext;
367
368 #ifndef OPENSSL_NO_CT
369     /*
370      * We don't want applications registering callbacks for SCT extensions
371      * whilst simultaneously using the built-in SCT validation features, as
372      * these two things may not play well together.
373      */
374     if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
375             && (context & SSL_EXT_CLIENT_HELLO) != 0
376             && ctx != NULL
377             && SSL_CTX_ct_is_enabled(ctx))
378         return 0;
379 #endif
380
381     /*
382      * Don't add if extension supported internally, but make exception
383      * for extension types that previously were not supported, but now are.
384      */
385     if (SSL_extension_supported(ext_type)
386             && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
387         return 0;
388
389     /* Extension type must fit in 16 bits */
390     if (ext_type > 0xffff)
391         return 0;
392     /* Search for duplicate */
393     if (custom_ext_find(exts, role, ext_type, NULL))
394         return 0;
395     tmp = OPENSSL_realloc(exts->meths,
396                           (exts->meths_count + 1) * sizeof(custom_ext_method));
397     if (tmp == NULL)
398         return 0;
399
400     exts->meths = tmp;
401     meth = exts->meths + exts->meths_count;
402     memset(meth, 0, sizeof(*meth));
403     meth->role = role;
404     meth->context = context;
405     meth->parse_cb = parse_cb;
406     meth->add_cb = add_cb;
407     meth->free_cb = free_cb;
408     meth->ext_type = ext_type;
409     meth->add_arg = add_arg;
410     meth->parse_arg = parse_arg;
411     exts->meths_count++;
412     return 1;
413 }
414
415 static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,
416                               unsigned int ext_type,
417                               unsigned int context,
418                               custom_ext_add_cb add_cb,
419                               custom_ext_free_cb free_cb,
420                               void *add_arg,
421                               custom_ext_parse_cb parse_cb, void *parse_arg)
422 {
423     custom_ext_add_cb_wrap *add_cb_wrap
424         = OPENSSL_malloc(sizeof(*add_cb_wrap));
425     custom_ext_parse_cb_wrap *parse_cb_wrap
426         = OPENSSL_malloc(sizeof(*parse_cb_wrap));
427     int ret;
428
429     if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
430         OPENSSL_free(add_cb_wrap);
431         OPENSSL_free(parse_cb_wrap);
432         return 0;
433     }
434
435     add_cb_wrap->add_arg = add_arg;
436     add_cb_wrap->add_cb = add_cb;
437     add_cb_wrap->free_cb = free_cb;
438     parse_cb_wrap->parse_arg = parse_arg;
439     parse_cb_wrap->parse_cb = parse_cb;
440
441     ret = ossl_tls_add_custom_ext_intern(ctx, NULL, role, ext_type,
442                                          context,
443                                          custom_ext_add_old_cb_wrap,
444                                          custom_ext_free_old_cb_wrap,
445                                          add_cb_wrap,
446                                          custom_ext_parse_old_cb_wrap,
447                                          parse_cb_wrap);
448
449     if (!ret) {
450         OPENSSL_free(add_cb_wrap);
451         OPENSSL_free(parse_cb_wrap);
452     }
453
454     return ret;
455 }
456
457 /* Application level functions to add the old custom extension callbacks */
458 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
459                                   custom_ext_add_cb add_cb,
460                                   custom_ext_free_cb free_cb,
461                                   void *add_arg,
462                                   custom_ext_parse_cb parse_cb, void *parse_arg)
463 {
464     return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,
465                               SSL_EXT_TLS1_2_AND_BELOW_ONLY
466                               | SSL_EXT_CLIENT_HELLO
467                               | SSL_EXT_TLS1_2_SERVER_HELLO
468                               | SSL_EXT_IGNORE_ON_RESUMPTION,
469                               add_cb, free_cb, add_arg, parse_cb, parse_arg);
470 }
471
472 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
473                                   custom_ext_add_cb add_cb,
474                                   custom_ext_free_cb free_cb,
475                                   void *add_arg,
476                                   custom_ext_parse_cb parse_cb, void *parse_arg)
477 {
478     return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,
479                               SSL_EXT_TLS1_2_AND_BELOW_ONLY
480                               | SSL_EXT_CLIENT_HELLO
481                               | SSL_EXT_TLS1_2_SERVER_HELLO
482                               | SSL_EXT_IGNORE_ON_RESUMPTION,
483                               add_cb, free_cb, add_arg, parse_cb, parse_arg);
484 }
485
486 int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
487                            unsigned int context,
488                            SSL_custom_ext_add_cb_ex add_cb,
489                            SSL_custom_ext_free_cb_ex free_cb,
490                            void *add_arg,
491                            SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
492 {
493     return ossl_tls_add_custom_ext_intern(ctx, NULL, ENDPOINT_BOTH, ext_type,
494                                           context, add_cb, free_cb, add_arg,
495                                           parse_cb, parse_arg);
496 }
497
498 int SSL_extension_supported(unsigned int ext_type)
499 {
500     switch (ext_type) {
501         /* Internally supported extensions. */
502     case TLSEXT_TYPE_application_layer_protocol_negotiation:
503     case TLSEXT_TYPE_ec_point_formats:
504     case TLSEXT_TYPE_supported_groups:
505     case TLSEXT_TYPE_key_share:
506 #ifndef OPENSSL_NO_NEXTPROTONEG
507     case TLSEXT_TYPE_next_proto_neg:
508 #endif
509     case TLSEXT_TYPE_padding:
510     case TLSEXT_TYPE_renegotiate:
511     case TLSEXT_TYPE_max_fragment_length:
512     case TLSEXT_TYPE_server_name:
513     case TLSEXT_TYPE_session_ticket:
514     case TLSEXT_TYPE_signature_algorithms:
515 #ifndef OPENSSL_NO_SRP
516     case TLSEXT_TYPE_srp:
517 #endif
518 #ifndef OPENSSL_NO_OCSP
519     case TLSEXT_TYPE_status_request:
520 #endif
521 #ifndef OPENSSL_NO_CT
522     case TLSEXT_TYPE_signed_certificate_timestamp:
523 #endif
524 #ifndef OPENSSL_NO_SRTP
525     case TLSEXT_TYPE_use_srtp:
526 #endif
527     case TLSEXT_TYPE_encrypt_then_mac:
528     case TLSEXT_TYPE_supported_versions:
529     case TLSEXT_TYPE_extended_master_secret:
530     case TLSEXT_TYPE_psk_kex_modes:
531     case TLSEXT_TYPE_cookie:
532     case TLSEXT_TYPE_early_data:
533     case TLSEXT_TYPE_certificate_authorities:
534     case TLSEXT_TYPE_psk:
535     case TLSEXT_TYPE_post_handshake_auth:
536     case TLSEXT_TYPE_compress_certificate:
537         return 1;
538     default:
539         return 0;
540     }
541 }