]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/t1_ext.c
09f10440396c2ae492528e039b0c9c767136d90b
[thirdparty/openssl.git] / ssl / t1_ext.c
1 /* ====================================================================
2 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
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
14 * distribution.
15 *
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/)"
20 *
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.
25 *
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.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
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 * ====================================================================
48 *
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).
52 *
53 */
54
55 /* Custom extension utility functions */
56
57 #include "ssl_locl.h"
58
59
60 /* Find a custom extension from the list. */
61 static custom_ext_method *custom_ext_find(custom_ext_methods *exts,
62 unsigned int ext_type)
63 {
64 size_t i;
65 custom_ext_method *meth = exts->meths;
66 for (i = 0; i < exts->meths_count; i++, meth++) {
67 if (ext_type == meth->ext_type)
68 return meth;
69 }
70 return NULL;
71 }
72
73 /*
74 * Initialise custom extensions flags to indicate neither sent nor received.
75 */
76 void custom_ext_init(custom_ext_methods *exts)
77 {
78 size_t i;
79 custom_ext_method *meth = exts->meths;
80 for (i = 0; i < exts->meths_count; i++, meth++)
81 meth->ext_flags = 0;
82 }
83
84 /* Pass received custom extension data to the application for parsing. */
85 int custom_ext_parse(SSL *s, int server,
86 unsigned int ext_type,
87 const unsigned char *ext_data, size_t ext_size, int *al)
88 {
89 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
90 custom_ext_method *meth;
91 meth = custom_ext_find(exts, ext_type);
92 /* If not found return success */
93 if (!meth)
94 return 1;
95 if (!server) {
96 /*
97 * If it's ServerHello we can't have any extensions not sent in
98 * ClientHello.
99 */
100 if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
101 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
102 return 0;
103 }
104 }
105 /* If already present it's a duplicate */
106 if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
107 *al = TLS1_AD_DECODE_ERROR;
108 return 0;
109 }
110 meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
111 /* If no parse function set return success */
112 if (!meth->parse_cb)
113 return 1;
114
115 return meth->parse_cb(s, ext_type, ext_data, ext_size, al,
116 meth->parse_arg);
117 }
118
119 /*
120 * Request custom extension data from the application and add to the return
121 * buffer.
122 */
123 int custom_ext_add(SSL *s, int server,
124 unsigned char **pret, unsigned char *limit, int *al)
125 {
126 custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
127 custom_ext_method *meth;
128 unsigned char *ret = *pret;
129 size_t i;
130
131 for (i = 0; i < exts->meths_count; i++) {
132 const unsigned char *out = NULL;
133 size_t outlen = 0;
134 meth = exts->meths + i;
135
136 if (server) {
137 /*
138 * For ServerHello only send extensions present in ClientHello.
139 */
140 if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
141 continue;
142 /* If callback absent for server skip it */
143 if (!meth->add_cb)
144 continue;
145 }
146 if (meth->add_cb) {
147 int cb_retval = 0;
148 cb_retval = meth->add_cb(s, meth->ext_type,
149 &out, &outlen, al, meth->add_arg);
150 if (cb_retval < 0)
151 return 0; /* error */
152 if (cb_retval == 0)
153 continue; /* skip this extension */
154 }
155 if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
156 return 0;
157 s2n(meth->ext_type, ret);
158 s2n(outlen, ret);
159 if (outlen) {
160 memcpy(ret, out, outlen);
161 ret += outlen;
162 }
163 /*
164 * We can't send duplicates: code logic should prevent this.
165 */
166 OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
167 /*
168 * Indicate extension has been sent: this is both a sanity check to
169 * ensure we don't send duplicate extensions and indicates that it is
170 * not an error if the extension is present in ServerHello.
171 */
172 meth->ext_flags |= SSL_EXT_FLAG_SENT;
173 if (meth->free_cb)
174 meth->free_cb(s, meth->ext_type, out, meth->add_arg);
175 }
176 *pret = ret;
177 return 1;
178 }
179
180 /* Copy table of custom extensions */
181 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
182 {
183 if (src->meths_count) {
184 dst->meths =
185 OPENSSL_memdup(src->meths,
186 sizeof(custom_ext_method) * src->meths_count);
187 if (dst->meths == NULL)
188 return 0;
189 dst->meths_count = src->meths_count;
190 }
191 return 1;
192 }
193
194 void custom_exts_free(custom_ext_methods *exts)
195 {
196 OPENSSL_free(exts->meths);
197 }
198
199 /* Set callbacks for a custom extension. */
200 static int custom_ext_meth_add(custom_ext_methods *exts,
201 unsigned int ext_type,
202 custom_ext_add_cb add_cb,
203 custom_ext_free_cb free_cb,
204 void *add_arg,
205 custom_ext_parse_cb parse_cb, void *parse_arg)
206 {
207 custom_ext_method *meth;
208 /*
209 * Check application error: if add_cb is not set free_cb will never be
210 * called.
211 */
212 if (!add_cb && free_cb)
213 return 0;
214 /* Don't add if extension supported internally. */
215 if (SSL_extension_supported(ext_type))
216 return 0;
217 /* Extension type must fit in 16 bits */
218 if (ext_type > 0xffff)
219 return 0;
220 /* Search for duplicate */
221 if (custom_ext_find(exts, ext_type))
222 return 0;
223 exts->meths = OPENSSL_realloc(exts->meths,
224 (exts->meths_count +
225 1) * sizeof(custom_ext_method));
226
227 if (!exts->meths) {
228 exts->meths_count = 0;
229 return 0;
230 }
231
232 meth = exts->meths + exts->meths_count;
233 memset(meth, 0, sizeof(*meth));
234 meth->parse_cb = parse_cb;
235 meth->add_cb = add_cb;
236 meth->free_cb = free_cb;
237 meth->ext_type = ext_type;
238 meth->add_arg = add_arg;
239 meth->parse_arg = parse_arg;
240 exts->meths_count++;
241 return 1;
242 }
243
244 /* Application level functions to add custom extension callbacks */
245 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
246 custom_ext_add_cb add_cb,
247 custom_ext_free_cb free_cb,
248 void *add_arg,
249 custom_ext_parse_cb parse_cb,
250 void *parse_arg)
251 {
252 return custom_ext_meth_add(&ctx->cert->cli_ext, ext_type,
253 add_cb, free_cb, add_arg, parse_cb, parse_arg);
254 }
255
256 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
257 custom_ext_add_cb add_cb,
258 custom_ext_free_cb free_cb,
259 void *add_arg,
260 custom_ext_parse_cb parse_cb,
261 void *parse_arg)
262 {
263 return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
264 add_cb, free_cb, add_arg, parse_cb, parse_arg);
265 }
266
267 int SSL_extension_supported(unsigned int ext_type)
268 {
269 switch (ext_type) {
270 /* Internally supported extensions. */
271 case TLSEXT_TYPE_application_layer_protocol_negotiation:
272 case TLSEXT_TYPE_ec_point_formats:
273 case TLSEXT_TYPE_elliptic_curves:
274 case TLSEXT_TYPE_heartbeat:
275 case TLSEXT_TYPE_next_proto_neg:
276 case TLSEXT_TYPE_padding:
277 case TLSEXT_TYPE_renegotiate:
278 case TLSEXT_TYPE_server_name:
279 case TLSEXT_TYPE_session_ticket:
280 case TLSEXT_TYPE_signature_algorithms:
281 case TLSEXT_TYPE_srp:
282 case TLSEXT_TYPE_status_request:
283 case TLSEXT_TYPE_use_srtp:
284 #ifdef TLSEXT_TYPE_encrypt_then_mac
285 case TLSEXT_TYPE_encrypt_then_mac:
286 #endif
287 return 1;
288 default:
289 return 0;
290 }
291 }