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