1 /* ====================================================================
2 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
55 /* Custom extension utility functions */
57 #include <openssl/ct.h>
61 /* Find a custom extension from the list. */
62 static custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
63 unsigned int ext_type)
66 custom_ext_method *meth = exts->meths;
67 for (i = 0; i < exts->meths_count; i++, meth++) {
68 if (ext_type == meth->ext_type)
75 * Initialise custom extensions flags to indicate neither sent nor received.
77 void custom_ext_init(custom_ext_methods *exts)
80 custom_ext_method *meth = exts->meths;
81 for (i = 0; i < exts->meths_count; i++, meth++)
85 /* Pass received custom extension data to the application for parsing. */
86 int custom_ext_parse(SSL *s, int server,
87 unsigned int ext_type,
88 const unsigned char *ext_data, size_t ext_size, int *al)
90 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
91 custom_ext_method *meth;
92 meth = custom_ext_find(exts, ext_type);
93 /* If not found return success */
98 * If it's ServerHello we can't have any extensions not sent in
101 if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
102 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
106 /* If already present it's a duplicate */
107 if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
108 *al = TLS1_AD_DECODE_ERROR;
111 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
112 /* If no parse function set return success */
116 return meth->parse_cb(s, ext_type, ext_data, ext_size, al,
121 * Request custom extension data from the application and add to the return
124 int custom_ext_add(SSL *s, int server,
125 unsigned char **pret, unsigned char *limit, int *al)
127 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
128 custom_ext_method *meth;
129 unsigned char *ret = *pret;
132 for (i = 0; i < exts->meths_count; i++) {
133 const unsigned char *out = NULL;
135 meth = exts->meths + i;
139 * For ServerHello only send extensions present in ClientHello.
141 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
143 /* If callback absent for server skip it */
149 cb_retval = meth->add_cb(s, meth->ext_type,
150 &out, &outlen, al, meth->add_arg);
152 return 0; /* error */
154 continue; /* skip this extension */
156 if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
158 s2n(meth->ext_type, ret);
161 memcpy(ret, out, outlen);
165 * We can't send duplicates: code logic should prevent this.
167 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
169 * Indicate extension has been sent: this is both a sanity check to
170 * ensure we don't send duplicate extensions and indicates that it is
171 * not an error if the extension is present in ServerHello.
173 meth->ext_flags |= SSL_EXT_FLAG_SENT;
175 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
181 /* Copy table of custom extensions */
182 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
184 if (src->meths_count) {
186 OPENSSL_memdup(src->meths,
187 sizeof(custom_ext_method) * src->meths_count);
188 if (dst->meths == NULL)
190 dst->meths_count = src->meths_count;
195 void custom_exts_free(custom_ext_methods *exts)
197 OPENSSL_free(exts->meths);
200 /* Set callbacks for a custom extension. */
201 static int custom_ext_meth_add(custom_ext_methods *exts,
202 unsigned int ext_type,
203 custom_ext_add_cb add_cb,
204 custom_ext_free_cb free_cb,
206 custom_ext_parse_cb parse_cb, void *parse_arg)
208 custom_ext_method *meth;
210 * Check application error: if add_cb is not set free_cb will never be
213 if (!add_cb && free_cb)
216 * Don't add if extension supported internally, but make exception
217 * for extension types that previously were not supported, but now are.
219 if (SSL_extension_supported(ext_type) &&
220 ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
222 /* Extension type must fit in 16 bits */
223 if (ext_type > 0xffff)
225 /* Search for duplicate */
226 if (custom_ext_find(exts, ext_type))
228 exts->meths = OPENSSL_realloc(exts->meths,
230 1) * sizeof(custom_ext_method));
233 exts->meths_count = 0;
237 meth = exts->meths + exts->meths_count;
238 memset(meth, 0, sizeof(*meth));
239 meth->parse_cb = parse_cb;
240 meth->add_cb = add_cb;
241 meth->free_cb = free_cb;
242 meth->ext_type = ext_type;
243 meth->add_arg = add_arg;
244 meth->parse_arg = parse_arg;
249 /* Return true if a client custom extension exists, false otherwise */
250 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
252 return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
255 /* Application level functions to add custom extension callbacks */
256 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
257 custom_ext_add_cb add_cb,
258 custom_ext_free_cb free_cb,
260 custom_ext_parse_cb parse_cb,
263 int ret = custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
264 free_cb, add_arg, parse_cb, parse_arg);
269 #ifndef OPENSSL_NO_CT
271 * We don't want applications registering callbacks for SCT extensions
272 * whilst simultaneously using the built-in SCT validation features, as
273 * these two things may not play well together.
275 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
276 SSL_CTX_get_ct_validation_callback(ctx) != NULL) {
284 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
285 custom_ext_add_cb add_cb,
286 custom_ext_free_cb free_cb,
288 custom_ext_parse_cb parse_cb,
291 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
292 add_cb, free_cb, add_arg, parse_cb, parse_arg);
295 int SSL_extension_supported(unsigned int ext_type)
298 /* Internally supported extensions. */
299 case TLSEXT_TYPE_application_layer_protocol_negotiation:
300 case TLSEXT_TYPE_ec_point_formats:
301 case TLSEXT_TYPE_elliptic_curves:
302 case TLSEXT_TYPE_heartbeat:
303 case TLSEXT_TYPE_next_proto_neg:
304 case TLSEXT_TYPE_padding:
305 case TLSEXT_TYPE_renegotiate:
306 case TLSEXT_TYPE_server_name:
307 case TLSEXT_TYPE_session_ticket:
308 case TLSEXT_TYPE_signature_algorithms:
309 case TLSEXT_TYPE_srp:
310 case TLSEXT_TYPE_status_request:
311 case TLSEXT_TYPE_signed_certificate_timestamp:
312 case TLSEXT_TYPE_use_srtp:
313 #ifdef TLSEXT_TYPE_encrypt_then_mac
314 case TLSEXT_TYPE_encrypt_then_mac: