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