2 /* ====================================================================
3 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
56 /* Custom extension utility functions */
60 #ifndef OPENSSL_NO_TLSEXT
62 /* Find a custom extension from the list */
64 static custom_ext_method *custom_ext_find(custom_ext_methods *exts,
65 unsigned short ext_type)
68 custom_ext_method *meth = exts->meths;
69 for (i = 0; i < exts->meths_count; i++, meth++)
71 if (ext_type == meth->ext_type)
76 /* Initialise custom extensions flags to indicate neither sent nor
79 void custom_ext_init(custom_ext_methods *exts)
82 custom_ext_method *meth = exts->meths;
83 for (i = 0; i < exts->meths_count; i++, meth++)
87 /* pass received custom extension data to the application for parsing */
89 int custom_ext_parse(SSL *s, int server,
90 unsigned int ext_type,
91 const unsigned char *ext_data,
95 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
96 custom_ext_method *meth;
97 meth = custom_ext_find(exts, ext_type);
98 /* If not found or no parse function set, return success */
99 /* If not found return success */
104 /* If it's ServerHello we can't have any extensions not
105 * sent in ClientHello.
107 if (!(meth->ext_flags & SSL_EXT_FLAG_SENT))
109 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
113 /* If already present it's a duplicate */
114 if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED)
116 *al = TLS1_AD_DECODE_ERROR;
119 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
123 return meth->parse_cb(s, ext_type, ext_data, ext_size, al, meth->parse_arg);
126 /* request custom extension data from the application and add to the
130 int custom_ext_add(SSL *s, int server,
131 unsigned char **pret,
132 unsigned char *limit,
135 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
136 custom_ext_method *meth;
137 unsigned char *ret = *pret;
140 for (i = 0; i < exts->meths_count; i++)
142 const unsigned char *out = NULL;
144 meth = exts->meths + i;
148 /* For ServerHello only send extensions present
151 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
153 /* If callback absent for server skip it */
160 cb_retval = meth->add_cb(s, meth->ext_type,
164 return 0; /* error */
166 continue; /* skip this extension */
168 if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
170 s2n(meth->ext_type, ret);
174 memcpy(ret, out, outlen);
177 /* We can't send duplicates: code logic should prevent this */
178 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
179 /* Indicate extension has been sent: this is both a sanity
180 * check to ensure we don't send duplicate extensions
181 * and indicates to servers that an extension can be
182 * sent in ServerHello.
184 meth->ext_flags |= SSL_EXT_FLAG_SENT;
186 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
192 /* Copy table of custom extensions */
194 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
196 if (src->meths_count)
198 dst->meths = BUF_memdup(src->meths, sizeof(custom_ext_method) * src->meths_count);
199 if (dst->meths == NULL)
201 dst->meths_count = src->meths_count;
206 void custom_exts_free(custom_ext_methods *exts)
209 OPENSSL_free(exts->meths);
212 /* Set callbacks for a custom extension */
213 static int custom_ext_set(custom_ext_methods *exts,
214 unsigned int ext_type,
215 custom_ext_add_cb add_cb,
216 custom_ext_free_cb free_cb,
218 custom_ext_parse_cb parse_cb, void *parse_arg)
220 custom_ext_method *meth;
221 /* Don't add if extension supported internall */
222 if (SSL_extension_supported(ext_type))
224 /* Extension type must fit in 16 bits */
225 if (ext_type > 0xffff)
227 /* Search for duplicate */
228 if (custom_ext_find(exts, ext_type))
230 exts->meths = OPENSSL_realloc(exts->meths,
231 (exts->meths_count + 1) * sizeof(custom_ext_method));
235 exts->meths_count = 0;
239 meth = exts->meths + exts->meths_count;
240 memset(meth, 0, sizeof(custom_ext_method));
241 meth->parse_cb = parse_cb;
242 meth->add_cb = add_cb;
243 meth->free_cb = free_cb;
244 meth->ext_type = ext_type;
245 meth->add_arg = add_arg;
246 meth->parse_arg = parse_arg;
251 /* Application level functions to add custom extension callbacks */
253 int SSL_CTX_set_custom_cli_ext(SSL_CTX *ctx, unsigned int ext_type,
254 custom_ext_add_cb add_cb,
255 custom_ext_free_cb free_cb,
257 custom_ext_parse_cb parse_cb, void *parse_arg)
260 return custom_ext_set(&ctx->cert->cli_ext, ext_type,
261 add_cb, free_cb, add_arg,
262 parse_cb, parse_arg);
265 int SSL_CTX_set_custom_srv_ext(SSL_CTX *ctx, unsigned int ext_type,
266 custom_ext_add_cb add_cb,
267 custom_ext_free_cb free_cb,
269 custom_ext_parse_cb parse_cb, void *parse_arg)
271 return custom_ext_set(&ctx->cert->srv_ext, ext_type,
272 add_cb, free_cb, add_arg,
273 parse_cb, parse_arg);
276 int SSL_extension_supported(unsigned int ext_type)
278 /* See if it is a supported internally */
281 case TLSEXT_TYPE_application_layer_protocol_negotiation:
282 case TLSEXT_TYPE_ec_point_formats:
283 case TLSEXT_TYPE_elliptic_curves:
284 case TLSEXT_TYPE_heartbeat:
285 case TLSEXT_TYPE_next_proto_neg:
286 case TLSEXT_TYPE_padding:
287 case TLSEXT_TYPE_renegotiate:
288 case TLSEXT_TYPE_server_name:
289 case TLSEXT_TYPE_session_ticket:
290 case TLSEXT_TYPE_signature_algorithms:
291 case TLSEXT_TYPE_srp:
292 case TLSEXT_TYPE_status_request:
293 case TLSEXT_TYPE_use_srtp:
294 #ifdef TLSEXT_TYPE_opaque_prf_input
295 case TLSEXT_TYPE_opaque_prf_input:
297 #ifdef TLSEXT_TYPE_encrypt_then_mac
298 case TLSEXT_TYPE_encrypt_then_mac: