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
77 int custom_ext_add(SSL *s, int server,
78 unsigned char **pret, unsigned char *limit, int *al)
80 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
81 custom_ext_method *meth;
82 unsigned char *ret = *pret;
85 for (i = 0; i < exts->meths_count; i++) {
86 const unsigned char *out = NULL;
88 meth = exts->meths + i;
92 * For ServerHello only send extensions present in ClientHello.
94 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
96 /* If callback absent for server skip it */
102 cb_retval = meth->add_cb(s, meth->ext_type,
103 &out, &outlen, al, meth->add_arg);
105 return 0; /* error */
107 continue; /* skip this extension */
109 if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
111 s2n(meth->ext_type, ret);
114 memcpy(ret, out, outlen);
118 * We can't send duplicates: code logic should prevent this.
120 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
122 * Indicate extension has been sent: this is both a sanity check to
123 * ensure we don't send duplicate extensions and indicates that it is
124 * not an error if the extension is present in ServerHello.
126 meth->ext_flags |= SSL_EXT_FLAG_SENT;
128 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
134 /* Copy table of custom extensions */
135 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
137 if (src->meths_count) {
139 OPENSSL_memdup(src->meths,
140 sizeof(custom_ext_method) * src->meths_count);
141 if (dst->meths == NULL)
143 dst->meths_count = src->meths_count;
148 void custom_exts_free(custom_ext_methods *exts)
150 OPENSSL_free(exts->meths);
153 /* Set callbacks for a custom extension. */
154 static int custom_ext_meth_add(custom_ext_methods *exts,
155 unsigned int ext_type,
156 custom_ext_add_cb add_cb,
157 custom_ext_free_cb free_cb,
159 custom_ext_parse_cb parse_cb, void *parse_arg)
161 custom_ext_method *meth, *tmp;
163 * Check application error: if add_cb is not set free_cb will never be
166 if (!add_cb && free_cb)
169 * Don't add if extension supported internally, but make exception
170 * for extension types that previously were not supported, but now are.
172 if (SSL_extension_supported(ext_type) &&
173 ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
175 /* Extension type must fit in 16 bits */
176 if (ext_type > 0xffff)
178 /* Search for duplicate */
179 if (custom_ext_find(exts, ext_type))
181 tmp = OPENSSL_realloc(exts->meths,
182 (exts->meths_count + 1) * sizeof(custom_ext_method));
185 OPENSSL_free(exts->meths);
187 exts->meths_count = 0;
192 meth = exts->meths + exts->meths_count;
193 memset(meth, 0, sizeof(*meth));
194 meth->parse_cb = parse_cb;
195 meth->add_cb = add_cb;
196 meth->free_cb = free_cb;
197 meth->ext_type = ext_type;
198 meth->add_arg = add_arg;
199 meth->parse_arg = parse_arg;
204 /* Return true if a client custom extension exists, false otherwise */
205 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
207 return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
210 /* Application level functions to add custom extension callbacks */
211 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
212 custom_ext_add_cb add_cb,
213 custom_ext_free_cb free_cb,
215 custom_ext_parse_cb parse_cb, void *parse_arg)
217 #ifndef OPENSSL_NO_CT
219 * We don't want applications registering callbacks for SCT extensions
220 * whilst simultaneously using the built-in SCT validation features, as
221 * these two things may not play well together.
223 if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
224 SSL_CTX_ct_is_enabled(ctx))
227 return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
228 free_cb, add_arg, parse_cb, parse_arg);
231 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
232 custom_ext_add_cb add_cb,
233 custom_ext_free_cb free_cb,
235 custom_ext_parse_cb parse_cb, void *parse_arg)
237 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
238 add_cb, free_cb, add_arg, parse_cb, parse_arg);
241 int SSL_extension_supported(unsigned int ext_type)
244 /* Internally supported extensions. */
245 case TLSEXT_TYPE_application_layer_protocol_negotiation:
246 case TLSEXT_TYPE_ec_point_formats:
247 case TLSEXT_TYPE_elliptic_curves:
248 case TLSEXT_TYPE_heartbeat:
249 #ifndef OPENSSL_NO_NEXTPROTONEG
250 case TLSEXT_TYPE_next_proto_neg:
252 case TLSEXT_TYPE_padding:
253 case TLSEXT_TYPE_renegotiate:
254 case TLSEXT_TYPE_server_name:
255 case TLSEXT_TYPE_session_ticket:
256 case TLSEXT_TYPE_signature_algorithms:
257 case TLSEXT_TYPE_srp:
258 case TLSEXT_TYPE_status_request:
259 case TLSEXT_TYPE_signed_certificate_timestamp:
260 case TLSEXT_TYPE_use_srtp:
261 #ifdef TLSEXT_TYPE_encrypt_then_mac
262 case TLSEXT_TYPE_encrypt_then_mac: