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 */
13 #include <openssl/ct.h>
14 #include "../ssl_locl.h"
15 #include "statem_locl.h"
19 custom_ext_add_cb add_cb;
20 custom_ext_free_cb free_cb;
21 } custom_ext_add_cb_wrap;
25 custom_ext_parse_cb parse_cb;
26 } custom_ext_parse_cb_wrap;
29 * Provide thin wrapper callbacks which convert new style arguments to old style
31 static int custom_ext_add_old_cb_wrap(SSL *s, unsigned int ext_type,
33 const unsigned char **out,
34 size_t *outlen, X509 *x, size_t chainidx,
35 int *al, void *add_arg)
37 custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
39 if (add_cb_wrap->add_cb == NULL)
42 return add_cb_wrap->add_cb(s, ext_type, out, outlen, al,
43 add_cb_wrap->add_arg);
46 static void custom_ext_free_old_cb_wrap(SSL *s, unsigned int ext_type,
48 const unsigned char *out, void *add_arg)
50 custom_ext_add_cb_wrap *add_cb_wrap = (custom_ext_add_cb_wrap *)add_arg;
52 if (add_cb_wrap->free_cb == NULL)
55 add_cb_wrap->free_cb(s, ext_type, out, add_cb_wrap->add_arg);
58 static int custom_ext_parse_old_cb_wrap(SSL *s, unsigned int ext_type,
60 const unsigned char *in,
61 size_t inlen, X509 *x, size_t chainidx,
62 int *al, void *parse_arg)
64 custom_ext_parse_cb_wrap *parse_cb_wrap =
65 (custom_ext_parse_cb_wrap *)parse_arg;
67 if (parse_cb_wrap->parse_cb == NULL)
70 return parse_cb_wrap->parse_cb(s, ext_type, in, inlen, al,
71 parse_cb_wrap->parse_arg);
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
81 custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
82 ENDPOINT role, unsigned int ext_type,
86 custom_ext_method *meth = exts->meths;
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)) {
101 * Initialise custom extensions flags to indicate neither sent nor received.
103 void custom_ext_init(custom_ext_methods *exts)
106 custom_ext_method *meth = exts->meths;
108 for (i = 0; i < exts->meths_count; i++, meth++)
112 /* Pass received custom extension data to the application for parsing. */
113 int custom_ext_parse(SSL *s, unsigned int context, unsigned int ext_type,
114 const unsigned char *ext_data, size_t ext_size, X509 *x,
115 size_t chainidx, int *al)
117 custom_ext_methods *exts = &s->cert->custext;
118 custom_ext_method *meth;
119 ENDPOINT role = ENDPOINT_BOTH;
121 if ((context & (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0)
122 role = s->server ? ENDPOINT_SERVER : ENDPOINT_CLIENT;
124 meth = custom_ext_find(exts, role, ext_type, NULL);
125 /* If not found return success */
129 /* Check if extension is defined for our protocol. If not, skip */
130 if (!extension_is_relevant(s, meth->context, context))
133 if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
134 | SSL_EXT_TLS1_3_SERVER_HELLO
135 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
137 * If it's ServerHello or EncryptedExtensions we can't have any
138 * extensions not sent in ClientHello.
140 if ((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0) {
141 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
147 * Extensions received in the ClientHello are marked with the
148 * SSL_EXT_FLAG_RECEIVED. This is so we know to add the equivalent
149 * extensions in the ServerHello/EncryptedExtensions message
151 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
152 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
154 /* If no parse function set return success */
158 return meth->parse_cb(s, ext_type, context, ext_data, ext_size, x, chainidx,
159 al, meth->parse_arg);
163 * Request custom extension data from the application and add to the return
166 int custom_ext_add(SSL *s, int context, WPACKET *pkt, X509 *x, size_t chainidx,
167 int maxversion, int *al)
169 custom_ext_methods *exts = &s->cert->custext;
170 custom_ext_method *meth;
173 for (i = 0; i < exts->meths_count; i++) {
174 const unsigned char *out = NULL;
177 meth = exts->meths + i;
179 if (!should_add_extension(s, meth->context, context, maxversion))
182 if ((context & (SSL_EXT_TLS1_2_SERVER_HELLO
183 | SSL_EXT_TLS1_3_SERVER_HELLO
184 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS)) != 0) {
186 * For ServerHello/EncryptedExtensions only send extensions present
189 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
193 * We skip it if the callback is absent - except for a ClientHello where
194 * we add an empty extension.
196 if ((context & SSL_EXT_CLIENT_HELLO) == 0 && meth->add_cb == NULL)
199 if (meth->add_cb != NULL) {
200 int cb_retval = meth->add_cb(s, meth->ext_type, context, &out,
201 &outlen, x, chainidx, al,
205 return 0; /* error */
207 continue; /* skip this extension */
210 if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
211 || !WPACKET_start_sub_packet_u16(pkt)
212 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
213 || !WPACKET_close(pkt)) {
214 *al = SSL_AD_INTERNAL_ERROR;
217 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
219 * We can't send duplicates: code logic should prevent this.
221 assert((meth->ext_flags & SSL_EXT_FLAG_SENT) == 0);
223 * Indicate extension has been sent: this is both a sanity check to
224 * ensure we don't send duplicate extensions and indicates that it
225 * is not an error if the extension is present in ServerHello.
227 meth->ext_flags |= SSL_EXT_FLAG_SENT;
229 if (meth->free_cb != NULL)
230 meth->free_cb(s, meth->ext_type, context, out, meth->add_arg);
235 /* Copy table of custom extensions */
236 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
241 if (src->meths_count > 0) {
243 OPENSSL_memdup(src->meths,
244 sizeof(*src->meths) * src->meths_count);
245 if (dst->meths == NULL)
247 dst->meths_count = src->meths_count;
249 for (i = 0; i < src->meths_count; i++) {
250 custom_ext_method *methsrc = src->meths + i;
251 custom_ext_method *methdst = dst->meths + i;
253 if (methsrc->add_cb != custom_ext_add_old_cb_wrap)
257 * We have found an old style API wrapper. We need to copy the
262 methdst->add_arg = NULL;
263 methdst->parse_arg = NULL;
267 methdst->add_arg = OPENSSL_memdup(methsrc->add_arg,
268 sizeof(custom_ext_add_cb_wrap));
269 methdst->parse_arg = OPENSSL_memdup(methsrc->parse_arg,
270 sizeof(custom_ext_parse_cb_wrap));
272 if (methdst->add_arg == NULL || methdst->parse_arg == NULL)
278 custom_exts_free(dst);
285 void custom_exts_free(custom_ext_methods *exts)
288 custom_ext_method *meth;
290 for (i = 0, meth = exts->meths; i < exts->meths_count; i++, meth++) {
291 if (meth->add_cb != custom_ext_add_old_cb_wrap)
294 /* Old style API wrapper. Need to free the arguments too */
295 OPENSSL_free(meth->add_arg);
296 OPENSSL_free(meth->parse_arg);
298 OPENSSL_free(exts->meths);
301 /* Return true if a client custom extension exists, false otherwise */
302 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
304 return custom_ext_find(&ctx->cert->custext, ENDPOINT_CLIENT, ext_type,
308 static int add_custom_ext_intern(SSL_CTX *ctx, ENDPOINT role,
309 unsigned int ext_type,
310 unsigned int context,
311 SSL_custom_ext_add_cb_ex add_cb,
312 SSL_custom_ext_free_cb_ex free_cb,
314 SSL_custom_ext_parse_cb_ex parse_cb,
317 custom_ext_methods *exts = &ctx->cert->custext;
318 custom_ext_method *meth, *tmp;
321 * Check application error: if add_cb is not set free_cb will never be
324 if (add_cb == NULL && free_cb != NULL)
327 #ifndef OPENSSL_NO_CT
329 * We don't want applications registering callbacks for SCT extensions
330 * whilst simultaneously using the built-in SCT validation features, as
331 * these two things may not play well together.
333 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp
334 && (context & SSL_EXT_CLIENT_HELLO) != 0
335 && SSL_CTX_ct_is_enabled(ctx))
340 * Don't add if extension supported internally, but make exception
341 * for extension types that previously were not supported, but now are.
343 if (SSL_extension_supported(ext_type)
344 && ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
347 /* Extension type must fit in 16 bits */
348 if (ext_type > 0xffff)
350 /* Search for duplicate */
351 if (custom_ext_find(exts, role, ext_type, NULL))
353 tmp = OPENSSL_realloc(exts->meths,
354 (exts->meths_count + 1) * sizeof(custom_ext_method));
359 meth = exts->meths + exts->meths_count;
360 memset(meth, 0, sizeof(*meth));
362 meth->context = context;
363 meth->parse_cb = parse_cb;
364 meth->add_cb = add_cb;
365 meth->free_cb = free_cb;
366 meth->ext_type = ext_type;
367 meth->add_arg = add_arg;
368 meth->parse_arg = parse_arg;
373 static int add_old_custom_ext(SSL_CTX *ctx, ENDPOINT role,
374 unsigned int ext_type,
375 unsigned int context,
376 custom_ext_add_cb add_cb,
377 custom_ext_free_cb free_cb,
379 custom_ext_parse_cb parse_cb, void *parse_arg)
381 custom_ext_add_cb_wrap *add_cb_wrap
382 = OPENSSL_malloc(sizeof(*add_cb_wrap));
383 custom_ext_parse_cb_wrap *parse_cb_wrap
384 = OPENSSL_malloc(sizeof(*parse_cb_wrap));
387 if (add_cb_wrap == NULL || parse_cb_wrap == NULL) {
388 OPENSSL_free(add_cb_wrap);
389 OPENSSL_free(parse_cb_wrap);
393 add_cb_wrap->add_arg = add_arg;
394 add_cb_wrap->add_cb = add_cb;
395 add_cb_wrap->free_cb = free_cb;
396 parse_cb_wrap->parse_arg = parse_arg;
397 parse_cb_wrap->parse_cb = parse_cb;
399 ret = add_custom_ext_intern(ctx, role, ext_type,
401 custom_ext_add_old_cb_wrap,
402 custom_ext_free_old_cb_wrap,
404 custom_ext_parse_old_cb_wrap,
408 OPENSSL_free(add_cb_wrap);
409 OPENSSL_free(parse_cb_wrap);
415 /* Application level functions to add the old custom extension callbacks */
416 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
417 custom_ext_add_cb add_cb,
418 custom_ext_free_cb free_cb,
420 custom_ext_parse_cb parse_cb, void *parse_arg)
422 return add_old_custom_ext(ctx, ENDPOINT_CLIENT, ext_type,
423 SSL_EXT_TLS1_2_AND_BELOW_ONLY
424 | SSL_EXT_CLIENT_HELLO
425 | SSL_EXT_TLS1_2_SERVER_HELLO
426 | SSL_EXT_IGNORE_ON_RESUMPTION,
427 add_cb, free_cb, add_arg, parse_cb, parse_arg);
430 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
431 custom_ext_add_cb add_cb,
432 custom_ext_free_cb free_cb,
434 custom_ext_parse_cb parse_cb, void *parse_arg)
436 return add_old_custom_ext(ctx, ENDPOINT_SERVER, ext_type,
437 SSL_EXT_TLS1_2_AND_BELOW_ONLY
438 | SSL_EXT_CLIENT_HELLO
439 | SSL_EXT_TLS1_2_SERVER_HELLO
440 | SSL_EXT_IGNORE_ON_RESUMPTION,
441 add_cb, free_cb, add_arg, parse_cb, parse_arg);
444 int SSL_CTX_add_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
445 unsigned int context,
446 SSL_custom_ext_add_cb_ex add_cb,
447 SSL_custom_ext_free_cb_ex free_cb,
449 SSL_custom_ext_parse_cb_ex parse_cb, void *parse_arg)
451 return add_custom_ext_intern(ctx, ENDPOINT_BOTH, ext_type, context, add_cb,
452 free_cb, add_arg, parse_cb, parse_arg);
455 int SSL_extension_supported(unsigned int ext_type)
458 /* Internally supported extensions. */
459 case TLSEXT_TYPE_application_layer_protocol_negotiation:
460 #ifndef OPENSSL_NO_EC
461 case TLSEXT_TYPE_ec_point_formats:
462 case TLSEXT_TYPE_supported_groups:
463 case TLSEXT_TYPE_key_share:
465 #ifndef OPENSSL_NO_NEXTPROTONEG
466 case TLSEXT_TYPE_next_proto_neg:
468 case TLSEXT_TYPE_padding:
469 case TLSEXT_TYPE_renegotiate:
470 case TLSEXT_TYPE_server_name:
471 case TLSEXT_TYPE_session_ticket:
472 case TLSEXT_TYPE_signature_algorithms:
473 #ifndef OPENSSL_NO_SRP
474 case TLSEXT_TYPE_srp:
476 #ifndef OPENSSL_NO_OCSP
477 case TLSEXT_TYPE_status_request:
479 #ifndef OPENSSL_NO_CT
480 case TLSEXT_TYPE_signed_certificate_timestamp:
482 #ifndef OPENSSL_NO_SRTP
483 case TLSEXT_TYPE_use_srtp:
485 case TLSEXT_TYPE_encrypt_then_mac:
486 case TLSEXT_TYPE_supported_versions:
487 case TLSEXT_TYPE_extended_master_secret:
488 case TLSEXT_TYPE_psk_kex_modes:
489 case TLSEXT_TYPE_cookie:
490 case TLSEXT_TYPE_early_data:
491 case TLSEXT_TYPE_certificate_authorities:
492 case TLSEXT_TYPE_psk: