]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/extensions_cust.c
Add documentation for the new custom extensions API
[thirdparty/openssl.git] / ssl / statem / extensions_cust.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
ecf4d660 3 *
846e33c7
RS
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
ecf4d660
DSH
8 */
9
10/* Custom extension utility functions */
11
43ae5eed 12#include <assert.h>
3c27208f 13#include <openssl/ct.h>
b443c845 14#include "../ssl_locl.h"
43ae5eed 15#include "statem_locl.h"
ecf4d660 16
43ae5eed
MC
17typedef 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
23typedef 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 */
31static 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
46static 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
58static 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 */
78custom_ext_method *custom_ext_find(const custom_ext_methods *exts, int server,
79 unsigned int ext_type, size_t *idx)
0f113f3e
MC
80{
81 size_t i;
43ae5eed 82
0f113f3e
MC
83 custom_ext_method *meth = exts->meths;
84 for (i = 0; i < exts->meths_count; i++, meth++) {
43ae5eed
MC
85 if (ext_type == meth->ext_type
86 && (server == -1 || server == meth->server
87 || meth->server == -1)) {
88 if (idx != NULL)
89 *idx = i;
0f113f3e 90 return meth;
43ae5eed 91 }
0f113f3e
MC
92 }
93 return NULL;
94}
95
96/*
97 * Initialise custom extensions flags to indicate neither sent nor received.
28ea0a0c
DSH
98 */
99void custom_ext_init(custom_ext_methods *exts)
0f113f3e
MC
100{
101 size_t i;
102 custom_ext_method *meth = exts->meths;
103 for (i = 0; i < exts->meths_count; i++, meth++)
104 meth->ext_flags = 0;
105}
ecf4d660 106
0cfefe4b 107/* Pass received custom extension data to the application for parsing. */
43ae5eed
MC
108int custom_ext_parse(SSL *s, unsigned int context, unsigned int ext_type,
109 const unsigned char *ext_data, size_t ext_size, X509 *x,
110 size_t chainidx, int *al)
0f113f3e 111{
43ae5eed 112 custom_ext_methods *exts = &s->cert->custext;
0f113f3e 113 custom_ext_method *meth;
43ae5eed
MC
114 int server = -1;
115
116 if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
117 server = s->server;
118
119 meth = custom_ext_find(exts, server, ext_type, NULL);
0f113f3e
MC
120 /* If not found return success */
121 if (!meth)
122 return 1;
43ae5eed
MC
123
124 /* Check if extension is defined for our protocol. If not, skip */
125 if (!extension_is_relevant(s, meth->context, context))
126 return 1;
127
128 if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
129 | SSL_EXT_TLS1_3_SERVER_HELLO
130 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
0f113f3e 131 /*
43ae5eed
MC
132 * If it's ServerHello or EncryptedExtensions we can't have any
133 * extensions not sent in ClientHello.
0f113f3e 134 */
43ae5eed 135 if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
0f113f3e
MC
136 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
137 return 0;
138 }
139 }
43ae5eed
MC
140
141 /*
142 * Extensions received in the ClientHello are marked with the
143 * SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
144 * extensions in the ServerHello/EncryptedExtensions message
145 */
146 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
147 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
148
0f113f3e
MC
149 /* If no parse function set return success */
150 if (!meth->parse_cb)
151 return 1;
ecf4d660 152
43ae5eed
MC
153 return meth->parse_cb(s, ext_type, context, ext_data, ext_size, x, chainidx,
154 al, meth->parse_arg);
0f113f3e 155}
ecf4d660 156
2c7b4dbc
MC
157/*
158 * Request custom extension data from the application and add to the return
159 * buffer.
160 */
43ae5eed
MC
161int custom_ext_add(SSL *s, int context, WPACKET *pkt, X509 *x, size_t chainidx,
162 int maxversion, int *al)
2c7b4dbc 163{
43ae5eed 164 custom_ext_methods *exts = &s->cert->custext;
2c7b4dbc
MC
165 custom_ext_method *meth;
166 size_t i;
167
168 for (i = 0; i < exts->meths_count; i++) {
169 const unsigned char *out = NULL;
170 size_t outlen = 0;
2c7b4dbc
MC
171
172 meth = exts->meths + i;
173
43ae5eed
MC
174 if (!should_add_extension(s, meth->context, context, maxversion))
175 continue;
176
177 if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
178 | SSL_EXT_TLS1_3_SERVER_HELLO
179 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
2c7b4dbc 180 /*
43ae5eed
MC
181 * For ServerHello/EncryptedExtensions only send extensions present
182 * in ClientHello.
2c7b4dbc
MC
183 */
184 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
185 continue;
2c7b4dbc 186 }
43ae5eed
MC
187 /*
188 * We skip it if the callback is absent - except for a ClientHello where
189 * we add an empty extension.
190 */
191 if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
192 continue;
193
194 if (meth->add_cb != NULL) {
2c7b4dbc 195 int cb_retval = 0;
43ae5eed
MC
196 cb_retval = meth->add_cb(s, meth->ext_type, context, &out, &outlen,
197 x, chainidx, al, meth->add_arg);
2c7b4dbc
MC
198 if (cb_retval < 0)
199 return 0; /* error */
200 if (cb_retval == 0)
201 continue; /* skip this extension */
202 }
203
08029dfa 204 if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
de451856 205 || !WPACKET_start_sub_packet_u16(pkt)
0217dd19
MC
206 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
207 || !WPACKET_close(pkt)) {
2c7b4dbc
MC
208 *al = SSL_AD_INTERNAL_ERROR;
209 return 0;
210 }
43ae5eed
MC
211 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
212 /*
213 * We can't send duplicates: code logic should prevent this.
214 */
215 assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
216 /*
217 * Indicate extension has been sent: this is both a sanity check to
218 * ensure we don't send duplicate extensions and indicates that it
219 * is not an error if the extension is present in ServerHello.
220 */
221 meth->ext_flags |= SSL_EXT_FLAG_SENT;
222 }
2c7b4dbc 223 if (meth->free_cb)
43ae5eed 224 meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
2c7b4dbc
MC
225 }
226 return 1;
227}
228
ecf4d660 229/* Copy table of custom extensions */
ecf4d660 230int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
0f113f3e 231{
43ae5eed
MC
232 size_t i;
233 int err = 0;
234
235 if (src->meths_count > 0) {
0f113f3e 236 dst->meths =
7644a9ae 237 OPENSSL_memdup(src->meths,
a230b26e 238 sizeof(custom_ext_method) * src->meths_count);
0f113f3e
MC
239 if (dst->meths == NULL)
240 return 0;
241 dst->meths_count = src->meths_count;
43ae5eed
MC
242
243 for (i = 0; i < src->meths_count; i++) {
244 custom_ext_method *methsrc = src->meths + i;
245 custom_ext_method *methdst = dst->meths + i;
246
247 if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
248 continue;
249
250 /*
251 * We have found an old style API wrapper. We need to copy the
252 * arguments too.
253 */
254
255 if (err) {
256 methdst->add_arg = NULL;
257 methdst->parse_arg = NULL;
258 continue;
259 }
260
261 methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
262 sizeof(custom_ext_add_cb_wrap));
263 methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
264 sizeof(custom_ext_parse_cb_wrap));
265
266 if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
267 err = 1;
268 }
269 }
270
271 if (err) {
272 custom_exts_free(dst);
273 return 0;
0f113f3e 274 }
43ae5eed 275
0f113f3e
MC
276 return 1;
277}
ecf4d660
DSH
278
279void custom_exts_free(custom_ext_methods *exts)
0f113f3e 280{
43ae5eed
MC
281 size_t i;
282
283 for (i = 0; i < exts->meths_count; i++) {
284 custom_ext_method *meth = exts->meths + i;
285
286 if (meth->add_cb != custom_ext_add_old_cb_wrap)
287 continue;
288
289 /* Old style API wrapper. Need to free the arguments too */
290 OPENSSL_free(meth->add_arg);
291 OPENSSL_free(meth->parse_arg);
292 }
b548a1f1 293 OPENSSL_free(exts->meths);
0f113f3e 294}
ecf4d660 295
43ae5eed
MC
296/* Return true if a client custom extension exists, false otherwise */
297int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
0f113f3e 298{
43ae5eed
MC
299 return custom_ext_find(&ctx->cert->custext, 0, ext_type, NULL) != NULL;
300}
301
302static int add_custom_ext_intern(SSL_CTX *ctx, int server,
303 unsigned int ext_type,
304 unsigned int context,
305 custom_ext_add_cb_ex add_cb,
306 custom_ext_free_cb_ex free_cb,
307 void *add_arg,
308 custom_ext_parse_cb_ex parse_cb,
309 void *parse_arg)
310{
311 custom_ext_methods *exts = &ctx->cert->custext;
7c0ef843 312 custom_ext_method *meth, *tmp;
43ae5eed 313
0f113f3e
MC
314 /*
315 * Check application error: if add_cb is not set free_cb will never be
316 * called.
317 */
318 if (!add_cb && free_cb)
319 return 0;
43ae5eed
MC
320
321#ifndef OPENSSL_NO_CT
322 /*
323 * We don't want applications registering callbacks for SCT extensions
324 * whilst simultaneously using the built-in SCT validation features, as
325 * these two things may not play well together.
326 */
327 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
328 && (context & SSL_EXT_CLIENT_HELLO) != 0
329 && SSL_CTX_ct_is_enabled(ctx))
330 return 0;
331#endif
332
ed29e82a
RP
333 /*
334 * Don't add if extension supported internally, but make exception
335 * for extension types that previously were not supported, but now are.
336 */
43ae5eed
MC
337 if (SSL_extension_supported(ext_type)
338 && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
0f113f3e 339 return 0;
43ae5eed 340
0f113f3e
MC
341 /* Extension type must fit in 16 bits */
342 if (ext_type > 0xffff)
343 return 0;
344 /* Search for duplicate */
43ae5eed 345 if (custom_ext_find(exts, server, ext_type, NULL))
0f113f3e 346 return 0;
7c0ef843
DSH
347 tmp = OPENSSL_realloc(exts->meths,
348 (exts->meths_count + 1) * sizeof(custom_ext_method));
ecf4d660 349
ed874fac 350 if (tmp == NULL)
0f113f3e 351 return 0;
ecf4d660 352
7c0ef843 353 exts->meths = tmp;
0f113f3e 354 meth = exts->meths + exts->meths_count;
16f8d4eb 355 memset(meth, 0, sizeof(*meth));
43ae5eed
MC
356 meth->server = server;
357 meth->context = context;
0f113f3e
MC
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}
ecf4d660 367
43ae5eed
MC
368static 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)
ed29e82a 374{
43ae5eed
MC
375 custom_ext_add_cb_wrap *add_cb_wrap
376 = OPENSSL_malloc(sizeof(custom_ext_add_cb_wrap));
377 custom_ext_parse_cb_wrap *parse_cb_wrap
378 = OPENSSL_malloc(sizeof(custom_ext_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;
ed29e82a
RP
412}
413
43ae5eed 414/* Application level functions to add the old custom extension callbacks */
8cafe9e8 415int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
0f113f3e
MC
416 custom_ext_add_cb add_cb,
417 custom_ext_free_cb free_cb,
0cfefe4b 418 void *add_arg,
a230b26e 419 custom_ext_parse_cb parse_cb, void *parse_arg)
0f113f3e 420{
43ae5eed
MC
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);
0f113f3e 427}
ecf4d660 428
8cafe9e8 429int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
0f113f3e
MC
430 custom_ext_add_cb add_cb,
431 custom_ext_free_cb free_cb,
0cfefe4b 432 void *add_arg,
a230b26e 433 custom_ext_parse_cb parse_cb, void *parse_arg)
0f113f3e 434{
43ae5eed
MC
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
443int 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);
0f113f3e 452}
c846a5f5
DSH
453
454int SSL_extension_supported(unsigned int ext_type)
0f113f3e
MC
455{
456 switch (ext_type) {
457 /* Internally supported extensions. */
458 case TLSEXT_TYPE_application_layer_protocol_negotiation:
36abb6a2 459#ifndef OPENSSL_NO_EC
0f113f3e 460 case TLSEXT_TYPE_ec_point_formats:
de4d764e 461 case TLSEXT_TYPE_supported_groups:
36abb6a2
MC
462 case TLSEXT_TYPE_key_share:
463#endif
1595ca02 464#ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e 465 case TLSEXT_TYPE_next_proto_neg:
1595ca02 466#endif
0f113f3e
MC
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:
36abb6a2 472#ifndef OPENSSL_NO_SRP
0f113f3e 473 case TLSEXT_TYPE_srp:
36abb6a2
MC
474#endif
475#ifndef OPENSSL_NO_OCSP
0f113f3e 476 case TLSEXT_TYPE_status_request:
36abb6a2
MC
477#endif
478#ifndef OPENSSL_NO_CT
ed29e82a 479 case TLSEXT_TYPE_signed_certificate_timestamp:
36abb6a2
MC
480#endif
481#ifndef OPENSSL_NO_SRTP
0f113f3e 482 case TLSEXT_TYPE_use_srtp:
e481f9b9 483#endif
36abb6a2 484 case TLSEXT_TYPE_encrypt_then_mac:
91b60e2a
MC
485 case TLSEXT_TYPE_supported_versions:
486 case TLSEXT_TYPE_extended_master_secret:
36abb6a2
MC
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:
0f113f3e
MC
492 return 1;
493 default:
494 return 0;
495 }
496}