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>
15 /* Find a custom extension from the list. */
16 static custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
17 unsigned int ext_type)
20 custom_ext_method *meth = exts->meths;
21 for (i = 0; i < exts->meths_count; i++, meth++) {
22 if (ext_type == meth->ext_type)
29 * Initialise custom extensions flags to indicate neither sent nor received.
31 void custom_ext_init(custom_ext_methods *exts)
34 custom_ext_method *meth = exts->meths;
35 for (i = 0; i < exts->meths_count; i++, meth++)
39 /* Pass received custom extension data to the application for parsing. */
40 int custom_ext_parse(SSL *s, int server,
41 unsigned int ext_type,
42 const unsigned char *ext_data, size_t ext_size, int *al)
44 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
45 custom_ext_method *meth;
46 meth = custom_ext_find(exts, ext_type);
47 /* If not found return success */
52 * If it's ServerHello we can't have any extensions not sent in
55 if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
56 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
60 /* If already present it's a duplicate */
61 if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
62 *al = TLS1_AD_DECODE_ERROR;
65 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
66 /* If no parse function set return success */
70 return meth->parse_cb(s, ext_type, ext_data, ext_size, al, meth->parse_arg);
74 * Request custom extension data from the application and add to the return
75 * buffer. This is the old style function signature prior to WPACKET. This is
76 * here temporarily until the conversion to WPACKET is completed, i.e. it is
77 * used by code that hasn't been converted yet.
78 * TODO - REMOVE THIS FUNCTION
80 int custom_ext_add_old(SSL *s, int server,
81 unsigned char **pret, unsigned char *limit, int *al)
83 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
84 custom_ext_method *meth;
85 unsigned char *ret = *pret;
88 for (i = 0; i < exts->meths_count; i++) {
89 const unsigned char *out = NULL;
91 meth = exts->meths + i;
95 * For ServerHello only send extensions present in ClientHello.
97 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
99 /* If callback absent for server skip it */
105 cb_retval = meth->add_cb(s, meth->ext_type,
106 &out, &outlen, al, meth->add_arg);
108 return 0; /* error */
110 continue; /* skip this extension */
112 if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
114 s2n(meth->ext_type, ret);
117 memcpy(ret, out, outlen);
121 * We can't send duplicates: code logic should prevent this.
123 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
125 * Indicate extension has been sent: this is both a sanity check to
126 * ensure we don't send duplicate extensions and indicates that it is
127 * not an error if the extension is present in ServerHello.
129 meth->ext_flags |= SSL_EXT_FLAG_SENT;
131 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
139 * Request custom extension data from the application and add to the return
142 int custom_ext_add(SSL *s, int server, WPACKET *pkt, int *al)
144 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
145 custom_ext_method *meth;
148 for (i = 0; i < exts->meths_count; i++) {
149 const unsigned char *out = NULL;
152 meth = exts->meths + i;
156 * For ServerHello only send extensions present in ClientHello.
158 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
160 /* If callback absent for server skip it */
166 cb_retval = meth->add_cb(s, meth->ext_type,
167 &out, &outlen, al, meth->add_arg);
169 return 0; /* error */
171 continue; /* skip this extension */
174 if (!WPACKET_put_bytes_u16(pkt, meth->ext_type)
175 || !WPACKET_start_sub_packet_u16(pkt)
176 || (outlen > 0 && !WPACKET_memcpy(pkt, out, outlen))
177 || !WPACKET_close(pkt)) {
178 *al = SSL_AD_INTERNAL_ERROR;
182 * We can't send duplicates: code logic should prevent this.
184 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
186 * Indicate extension has been sent: this is both a sanity check to
187 * ensure we don't send duplicate extensions and indicates that it is
188 * not an error if the extension is present in ServerHello.
190 meth->ext_flags |= SSL_EXT_FLAG_SENT;
192 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
197 /* Copy table of custom extensions */
198 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
200 if (src->meths_count) {
202 OPENSSL_memdup(src->meths,
203 sizeof(custom_ext_method) * src->meths_count);
204 if (dst->meths == NULL)
206 dst->meths_count = src->meths_count;
211 void custom_exts_free(custom_ext_methods *exts)
213 OPENSSL_free(exts->meths);
216 /* Set callbacks for a custom extension. */
217 static int custom_ext_meth_add(custom_ext_methods *exts,
218 unsigned int ext_type,
219 custom_ext_add_cb add_cb,
220 custom_ext_free_cb free_cb,
222 custom_ext_parse_cb parse_cb, void *parse_arg)
224 custom_ext_method *meth, *tmp;
226 * Check application error: if add_cb is not set free_cb will never be
229 if (!add_cb && free_cb)
232 * Don't add if extension supported internally, but make exception
233 * for extension types that previously were not supported, but now are.
235 if (SSL_extension_supported(ext_type) &&
236 ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
238 /* Extension type must fit in 16 bits */
239 if (ext_type > 0xffff)
241 /* Search for duplicate */
242 if (custom_ext_find(exts, ext_type))
244 tmp = OPENSSL_realloc(exts->meths,
245 (exts->meths_count + 1) * sizeof(custom_ext_method));
248 OPENSSL_free(exts->meths);
250 exts->meths_count = 0;
255 meth = exts->meths + exts->meths_count;
256 memset(meth, 0, sizeof(*meth));
257 meth->parse_cb = parse_cb;
258 meth->add_cb = add_cb;
259 meth->free_cb = free_cb;
260 meth->ext_type = ext_type;
261 meth->add_arg = add_arg;
262 meth->parse_arg = parse_arg;
267 /* Return true if a client custom extension exists, false otherwise */
268 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
270 return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
273 /* Application level functions to add custom extension callbacks */
274 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
275 custom_ext_add_cb add_cb,
276 custom_ext_free_cb free_cb,
278 custom_ext_parse_cb parse_cb, void *parse_arg)
280 #ifndef OPENSSL_NO_CT
282 * We don't want applications registering callbacks for SCT extensions
283 * whilst simultaneously using the built-in SCT validation features, as
284 * these two things may not play well together.
286 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
287 SSL_CTX_ct_is_enabled(ctx))
290 return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
291 free_cb, add_arg, parse_cb, parse_arg);
294 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
295 custom_ext_add_cb add_cb,
296 custom_ext_free_cb free_cb,
298 custom_ext_parse_cb parse_cb, void *parse_arg)
300 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
301 add_cb, free_cb, add_arg, parse_cb, parse_arg);
304 int SSL_extension_supported(unsigned int ext_type)
307 /* Internally supported extensions. */
308 case TLSEXT_TYPE_application_layer_protocol_negotiation:
309 case TLSEXT_TYPE_ec_point_formats:
310 case TLSEXT_TYPE_elliptic_curves:
311 case TLSEXT_TYPE_heartbeat:
312 #ifndef OPENSSL_NO_NEXTPROTONEG
313 case TLSEXT_TYPE_next_proto_neg:
315 case TLSEXT_TYPE_padding:
316 case TLSEXT_TYPE_renegotiate:
317 case TLSEXT_TYPE_server_name:
318 case TLSEXT_TYPE_session_ticket:
319 case TLSEXT_TYPE_signature_algorithms:
320 case TLSEXT_TYPE_srp:
321 case TLSEXT_TYPE_status_request:
322 case TLSEXT_TYPE_signed_certificate_timestamp:
323 case TLSEXT_TYPE_use_srtp:
324 #ifdef TLSEXT_TYPE_encrypt_then_mac
325 case TLSEXT_TYPE_encrypt_then_mac: