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