check reviewer --reviewer=emilia
[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 #ifndef OPENSSL_NO_CT
58 # include <openssl/ct.h>
59 #endif
60 #include "ssl_locl.h"
61
62
63 /* Find a custom extension from the list. */
64 static custom_ext_method *custom_ext_find(const custom_ext_methods *exts,
65                                           unsigned int ext_type)
66 {
67     size_t i;
68     custom_ext_method *meth = exts->meths;
69     for (i = 0; i < exts->meths_count; i++, meth++) {
70         if (ext_type == meth->ext_type)
71             return meth;
72     }
73     return NULL;
74 }
75
76 /*
77  * Initialise custom extensions flags to indicate neither sent nor received.
78  */
79 void custom_ext_init(custom_ext_methods *exts)
80 {
81     size_t i;
82     custom_ext_method *meth = exts->meths;
83     for (i = 0; i < exts->meths_count; i++, meth++)
84         meth->ext_flags = 0;
85 }
86
87 /* Pass received custom extension data to the application for parsing. */
88 int custom_ext_parse(SSL *s, int server,
89                      unsigned int ext_type,
90                      const unsigned char *ext_data, size_t ext_size, int *al)
91 {
92     custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
93     custom_ext_method *meth;
94     meth = custom_ext_find(exts, ext_type);
95     /* If not found return success */
96     if (!meth)
97         return 1;
98     if (!server) {
99         /*
100          * If it's ServerHello we can't have any extensions not sent in
101          * ClientHello.
102          */
103         if (!(meth->ext_flags & SSL_EXT_FLAG_SENT)) {
104             *al = TLS1_AD_UNSUPPORTED_EXTENSION;
105             return 0;
106         }
107     }
108     /* If already present it's a duplicate */
109     if (meth->ext_flags & SSL_EXT_FLAG_RECEIVED) {
110         *al = TLS1_AD_DECODE_ERROR;
111         return 0;
112     }
113     meth->ext_flags |= SSL_EXT_FLAG_RECEIVED;
114     /* If no parse function set return success */
115     if (!meth->parse_cb)
116         return 1;
117
118     return meth->parse_cb(s, ext_type, ext_data, ext_size, al,
119                           meth->parse_arg);
120 }
121
122 /*
123  * Request custom extension data from the application and add to the return
124  * buffer.
125  */
126 int custom_ext_add(SSL *s, int server,
127                    unsigned char **pret, unsigned char *limit, int *al)
128 {
129     custom_ext_methods *exts = server ? &s->cert->srv_ext : &s->cert->cli_ext;
130     custom_ext_method *meth;
131     unsigned char *ret = *pret;
132     size_t i;
133
134     for (i = 0; i < exts->meths_count; i++) {
135         const unsigned char *out = NULL;
136         size_t outlen = 0;
137         meth = exts->meths + i;
138
139         if (server) {
140             /*
141              * For ServerHello only send extensions present in ClientHello.
142              */
143             if (!(meth->ext_flags & SSL_EXT_FLAG_RECEIVED))
144                 continue;
145             /* If callback absent for server skip it */
146             if (!meth->add_cb)
147                 continue;
148         }
149         if (meth->add_cb) {
150             int cb_retval = 0;
151             cb_retval = meth->add_cb(s, meth->ext_type,
152                                      &out, &outlen, al, meth->add_arg);
153             if (cb_retval < 0)
154                 return 0;       /* error */
155             if (cb_retval == 0)
156                 continue;       /* skip this extension */
157         }
158         if (4 > limit - ret || outlen > (size_t)(limit - ret - 4))
159             return 0;
160         s2n(meth->ext_type, ret);
161         s2n(outlen, ret);
162         if (outlen) {
163             memcpy(ret, out, outlen);
164             ret += outlen;
165         }
166         /*
167          * We can't send duplicates: code logic should prevent this.
168          */
169         OPENSSL_assert(!(meth->ext_flags & SSL_EXT_FLAG_SENT));
170         /*
171          * Indicate extension has been sent: this is both a sanity check to
172          * ensure we don't send duplicate extensions and indicates that it is
173          * not an error if the extension is present in ServerHello.
174          */
175         meth->ext_flags |= SSL_EXT_FLAG_SENT;
176         if (meth->free_cb)
177             meth->free_cb(s, meth->ext_type, out, meth->add_arg);
178     }
179     *pret = ret;
180     return 1;
181 }
182
183 /* Copy table of custom extensions */
184 int custom_exts_copy(custom_ext_methods *dst, const custom_ext_methods *src)
185 {
186     if (src->meths_count) {
187         dst->meths =
188             OPENSSL_memdup(src->meths,
189                        sizeof(custom_ext_method) * src->meths_count);
190         if (dst->meths == NULL)
191             return 0;
192         dst->meths_count = src->meths_count;
193     }
194     return 1;
195 }
196
197 void custom_exts_free(custom_ext_methods *exts)
198 {
199     OPENSSL_free(exts->meths);
200 }
201
202 /* Set callbacks for a custom extension. */
203 static int custom_ext_meth_add(custom_ext_methods *exts,
204                                unsigned int ext_type,
205                                custom_ext_add_cb add_cb,
206                                custom_ext_free_cb free_cb,
207                                void *add_arg,
208                                custom_ext_parse_cb parse_cb, void *parse_arg)
209 {
210     custom_ext_method *meth;
211     /*
212      * Check application error: if add_cb is not set free_cb will never be
213      * called.
214      */
215     if (!add_cb && free_cb)
216         return 0;
217     /*
218      * Don't add if extension supported internally, but make exception
219      * for extension types that previously were not supported, but now are.
220      */
221     if (SSL_extension_supported(ext_type) &&
222         ext_type != TLSEXT_TYPE_signed_certificate_timestamp)
223         return 0;
224     /* Extension type must fit in 16 bits */
225     if (ext_type > 0xffff)
226         return 0;
227     /* Search for duplicate */
228     if (custom_ext_find(exts, ext_type))
229         return 0;
230     exts->meths = OPENSSL_realloc(exts->meths,
231                                   (exts->meths_count +
232                                    1) * sizeof(custom_ext_method));
233
234     if (!exts->meths) {
235         exts->meths_count = 0;
236         return 0;
237     }
238
239     meth = exts->meths + exts->meths_count;
240     memset(meth, 0, sizeof(*meth));
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;
247     exts->meths_count++;
248     return 1;
249 }
250
251 /* Return true if a client custom extension exists, false otherwise */
252 int SSL_CTX_has_client_custom_ext(const SSL_CTX *ctx, unsigned int ext_type)
253 {
254     return custom_ext_find(&ctx->cert->cli_ext, ext_type) != NULL;
255 }
256
257 /* Application level functions to add custom extension callbacks */
258 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
259                                   custom_ext_add_cb add_cb,
260                                   custom_ext_free_cb free_cb,
261                                   void *add_arg,
262                                   custom_ext_parse_cb parse_cb,
263                                   void *parse_arg)
264 {
265     int ret = custom_ext_meth_add(&ctx->cert->cli_ext, ext_type, add_cb,
266                                   free_cb, add_arg, parse_cb, parse_arg);
267
268     if (ret != 1)
269         goto end;
270
271 #ifndef OPENSSL_NO_CT
272     /*
273      * We don't want applications registering callbacks for SCT extensions
274      * whilst simultaneously using the built-in SCT validation features, as
275      * these two things may not play well together.
276      */
277     if (ext_type == TLSEXT_TYPE_signed_certificate_timestamp &&
278         SSL_CTX_get_ct_validation_callback(ctx) != NULL) {
279         ret = 0;
280     }
281 #endif
282 end:
283     return ret;
284 }
285
286 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned int ext_type,
287                                   custom_ext_add_cb add_cb,
288                                   custom_ext_free_cb free_cb,
289                                   void *add_arg,
290                                   custom_ext_parse_cb parse_cb,
291                                   void *parse_arg)
292 {
293     return custom_ext_meth_add(&ctx->cert->srv_ext, ext_type,
294                                add_cb, free_cb, add_arg, parse_cb, parse_arg);
295 }
296
297 int SSL_extension_supported(unsigned int ext_type)
298 {
299     switch (ext_type) {
300         /* Internally supported extensions. */
301     case TLSEXT_TYPE_application_layer_protocol_negotiation:
302     case TLSEXT_TYPE_ec_point_formats:
303     case TLSEXT_TYPE_elliptic_curves:
304     case TLSEXT_TYPE_heartbeat:
305     case TLSEXT_TYPE_next_proto_neg:
306     case TLSEXT_TYPE_padding:
307     case TLSEXT_TYPE_renegotiate:
308     case TLSEXT_TYPE_server_name:
309     case TLSEXT_TYPE_session_ticket:
310     case TLSEXT_TYPE_signature_algorithms:
311     case TLSEXT_TYPE_srp:
312     case TLSEXT_TYPE_status_request:
313     case TLSEXT_TYPE_signed_certificate_timestamp:
314     case TLSEXT_TYPE_use_srtp:
315 #ifdef TLSEXT_TYPE_encrypt_then_mac
316     case TLSEXT_TYPE_encrypt_then_mac:
317 #endif
318         return 1;
319     default:
320         return 0;
321     }
322 }