2 * Copyright 2014-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* Custom extension utility functions */
12 #include <openssl/ct.h>
13 #include "../ssl_locl.h"
14 #include "statem_locl.h"
18 custom_ext_add_cb add_cb;
19 custom_ext_free_cb free_cb;
20 } custom_ext_add_cb_wrap;
24 custom_ext_parse_cb parse_cb;
25 } custom_ext_parse_cb_wrap;
28 * Provide thin wrapper callbacks which convert new style arguments to old style
30 static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,
32 const unsigned char **out,
33 size_t *outlen, X509 *x, size_t chainidx,
34 int *al, void *add_arg)
36 custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
38 if (add_cb_wrap->add_cb == NULL)
41 return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,
42 add_cb_wrap->add_arg);
45 static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,
47 const unsigned char *out, void *add_arg)
49 custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
51 if (add_cb_wrap->free_cb == NULL)
54 add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);
57 static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,
59 const unsigned char *in,
60 size_t inlen, X509 *x, size_t chainidx,
61 int *al, void *parse_arg)
63 custom_ext_parse_cb_wrap *parse_cb_wrap =
64 (custom_ext_parse_cb_wrap *)parse_arg;
66 if (parse_cb_wrap->parse_cb == NULL)
69 return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,
70 parse_cb_wrap->parse_arg);
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
80 custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
81 ENDPOINT role, unsigned int ext_type,
85 custom_ext_method *meth = exts->meths;
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)) {
100 * Initialise custom extensions flags to indicate neither sent nor received.
102 void custom_ext_init(custom_ext_methods *exts)
105 custom_ext_method *meth = exts->meths;
107 for (i = 0; i < exts->meths_count; i++, meth++)
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)
116 custom_ext_methods *exts = &s->cert->custext;
117 custom_ext_method *meth;
118 ENDPOINT role = ENDPOINT_BOTH;
120 if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
121 role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
123 meth = custom_ext_find(exts, role, ext_type, NULL);
124 /* If not found return success */
128 /* Check if extension is defined for our protocol. If not, skip */
129 if (!extension_is_relevant(s, meth->context, context))
132 if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
133 | SSL_EXT_TLS1_3_SERVER_HELLO
134 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
136 * If it's ServerHello or EncryptedExtensions we can't have any
137 * extensions not sent in ClientHello.
139 if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
140 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
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
150 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
151 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
153 /* If no parse function set return success */
157 return meth->parse_cb(s, ext_type, context, ext_data, ext_size, x, chainidx,
158 al, meth->parse_arg);
162 * Request custom extension data from the application and add to the return
165 int custom_ext_add(SSL *s, int context, WPACKET *pkt, X509 *x, size_t chainidx,
166 int maxversion, int *al)
168 custom_ext_methods *exts = &s->cert->custext;
169 custom_ext_method *meth;
172 for (i = 0; i < exts->meths_count; i++) {
173 const unsigned char *out = NULL;
176 meth = exts->meths + i;
178 if (!should_add_extension(s, meth->context, context, maxversion))
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))
191 * We skip it if the callback is absent - except for a ClientHello where
192 * we add an empty extension.
194 if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
197 if (meth->add_cb != NULL) {
198 int cb_retval = meth->add_cb(s, meth->ext_type, context, &out,
199 &outlen, x, chainidx, al,
203 return 0; /* error */
205 continue; /* skip this extension */
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;
215 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
217 * We can't send duplicates: code logic should prevent this.
219 if (!ossl_assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0)) {
220 *al = SSL_AD_INTERNAL_ERROR;
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.
228 meth->ext_flags |= SSL_EXT_FLAG_SENT;
230 if (meth->free_cb != NULL)
231 meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
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)
241 custom_ext_method *methsrc = src->meths;
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);
250 methdst->ext_flags = methsrc->ext_flags;
256 /* Copy table of custom extensions */
257 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
262 if (src->meths_count > 0) {
264 OPENSSL_memdup(src->meths,
265 sizeof(*src->meths) * src->meths_count);
266 if (dst->meths == NULL)
268 dst->meths_count = src->meths_count;
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;
274 if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
278 * We have found an old style API wrapper. We need to copy the
283 methdst->add_arg = NULL;
284 methdst->parse_arg = NULL;
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));
293 if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
299 custom_exts_free(dst);
306 void custom_exts_free(custom_ext_methods *exts)
309 custom_ext_method *meth;
311 for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
312 if (meth->add_cb != custom_ext_add_old_cb_wrap)
315 /* Old style API wrapper. Need to free the arguments too */
316 OPENSSL_free(meth->add_arg);
317 OPENSSL_free(meth->parse_arg);
319 OPENSSL_free(exts->meths);
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)
325 return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
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,
335 SSL_custom_ext_parse_cb_ex parse_cb,
338 custom_ext_methods *exts = &ctx->cert->custext;
339 custom_ext_method *meth, *tmp;
342 * Check application error: if add_cb is not set free_cb will never be
345 if (add_cb == NULL && free_cb != NULL)
348 #ifndef OPENSSL_NO_CT
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.
354 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
355 && (context & SSL_EXT_CLIENT_HELLO) != 0
356 && SSL_CTX_ct_is_enabled(ctx))
361 * Don't add if extension supported internally, but make exception
362 * for extension types that previously were not supported, but now are.
364 if (SSL_extension_supported(ext_type)
365 && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
368 /* Extension type must fit in 16 bits */
369 if (ext_type > 0xffff)
371 /* Search for duplicate */
372 if (custom_ext_find(exts, role, ext_type, NULL))
374 tmp = OPENSSL_realloc(exts->meths,
375 (exts->meths_count + 1) * sizeof(custom_ext_method));
380 meth = exts->meths + exts->meths_count;
381 memset(meth, 0, sizeof(*meth));
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;
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,
400 custom_ext_parse_cb parse_cb, void *parse_arg)
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));
408 if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
409 OPENSSL_free(add_cb_wrap);
410 OPENSSL_free(parse_cb_wrap);
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;
420 ret = add_custom_ext_intern(ctx, role, ext_type,
422 custom_ext_add_old_cb_wrap,
423 custom_ext_free_old_cb_wrap,
425 custom_ext_parse_old_cb_wrap,
429 OPENSSL_free(add_cb_wrap);
430 OPENSSL_free(parse_cb_wrap);
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,
441 custom_ext_parse_cb parse_cb, void *parse_arg)
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);
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,
455 custom_ext_parse_cb parse_cb, void *parse_arg)
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);
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,
470 SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
472 return add_custom_ext_intern(ctx, ENDPOINT_BOTH, ext_type, context, add_cb,
473 free_cb, add_arg, parse_cb, parse_arg);
476 int SSL_extension_supported(unsigned int 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:
486 #ifndef OPENSSL_NO_NEXTPROTONEG
487 case TLSEXT_TYPE_next_proto_neg:
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:
497 #ifndef OPENSSL_NO_OCSP
498 case TLSEXT_TYPE_status_request:
500 #ifndef OPENSSL_NO_CT
501 case TLSEXT_TYPE_signed_certificate_timestamp:
503 #ifndef OPENSSL_NO_SRTP
504 case TLSEXT_TYPE_use_srtp:
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: