xref: /minix/crypto/external/bsd/openssl/dist/ssl/t1_lib.c (revision 0a6a1f1d)
1ebfedea0SLionel Sambuc /* ssl/t1_lib.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60ebfedea0SLionel Sambuc  *
61ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc  * are met:
64ebfedea0SLionel Sambuc  *
65ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc  *
68ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc  *    distribution.
72ebfedea0SLionel Sambuc  *
73ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
75ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc  *
78ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
82ebfedea0SLionel Sambuc  *
83ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc  *
87ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc  *    acknowledgment:
89ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc  *
92ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc  * ====================================================================
105ebfedea0SLionel Sambuc  *
106ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
108ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc  *
110ebfedea0SLionel Sambuc  */
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc #include <stdio.h>
113ebfedea0SLionel Sambuc #include <openssl/objects.h>
114ebfedea0SLionel Sambuc #include <openssl/evp.h>
115ebfedea0SLionel Sambuc #include <openssl/hmac.h>
116ebfedea0SLionel Sambuc #include <openssl/ocsp.h>
117ebfedea0SLionel Sambuc #include <openssl/rand.h>
118ebfedea0SLionel Sambuc #include "ssl_locl.h"
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc const char tls1_version_str[] = "TLSv1" OPENSSL_VERSION_PTEXT;
121ebfedea0SLionel Sambuc 
122ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
123ebfedea0SLionel Sambuc static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen,
124ebfedea0SLionel Sambuc                               const unsigned char *sess_id, int sesslen,
125ebfedea0SLionel Sambuc                               SSL_SESSION **psess);
126ebfedea0SLionel Sambuc #endif
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc SSL3_ENC_METHOD TLSv1_enc_data = {
129ebfedea0SLionel Sambuc     tls1_enc,
130ebfedea0SLionel Sambuc     tls1_mac,
131ebfedea0SLionel Sambuc     tls1_setup_key_block,
132ebfedea0SLionel Sambuc     tls1_generate_master_secret,
133ebfedea0SLionel Sambuc     tls1_change_cipher_state,
134ebfedea0SLionel Sambuc     tls1_final_finish_mac,
135ebfedea0SLionel Sambuc     TLS1_FINISH_MAC_LENGTH,
136ebfedea0SLionel Sambuc     tls1_cert_verify_mac,
137ebfedea0SLionel Sambuc     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
138ebfedea0SLionel Sambuc     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
139ebfedea0SLionel Sambuc     tls1_alert_code,
140ebfedea0SLionel Sambuc     tls1_export_keying_material,
141ebfedea0SLionel Sambuc };
142ebfedea0SLionel Sambuc 
tls1_default_timeout(void)143ebfedea0SLionel Sambuc long tls1_default_timeout(void)
144ebfedea0SLionel Sambuc {
145*0a6a1f1dSLionel Sambuc     /*
146*0a6a1f1dSLionel Sambuc      * 2 hours, the 24 hours mentioned in the TLSv1 spec is way too long for
147*0a6a1f1dSLionel Sambuc      * http, the cache would over fill
148*0a6a1f1dSLionel Sambuc      */
149ebfedea0SLionel Sambuc     return (60 * 60 * 2);
150ebfedea0SLionel Sambuc }
151ebfedea0SLionel Sambuc 
tls1_new(SSL * s)152ebfedea0SLionel Sambuc int tls1_new(SSL *s)
153ebfedea0SLionel Sambuc {
154*0a6a1f1dSLionel Sambuc     if (!ssl3_new(s))
155*0a6a1f1dSLionel Sambuc         return (0);
156ebfedea0SLionel Sambuc     s->method->ssl_clear(s);
157ebfedea0SLionel Sambuc     return (1);
158ebfedea0SLionel Sambuc }
159ebfedea0SLionel Sambuc 
tls1_free(SSL * s)160ebfedea0SLionel Sambuc void tls1_free(SSL *s)
161ebfedea0SLionel Sambuc {
162ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
163*0a6a1f1dSLionel Sambuc     if (s->tlsext_session_ticket) {
164ebfedea0SLionel Sambuc         OPENSSL_free(s->tlsext_session_ticket);
165ebfedea0SLionel Sambuc     }
166ebfedea0SLionel Sambuc #endif                          /* OPENSSL_NO_TLSEXT */
167ebfedea0SLionel Sambuc     ssl3_free(s);
168ebfedea0SLionel Sambuc }
169ebfedea0SLionel Sambuc 
tls1_clear(SSL * s)170ebfedea0SLionel Sambuc void tls1_clear(SSL *s)
171ebfedea0SLionel Sambuc {
172ebfedea0SLionel Sambuc     ssl3_clear(s);
173ebfedea0SLionel Sambuc     s->version = s->method->version;
174ebfedea0SLionel Sambuc }
175ebfedea0SLionel Sambuc 
176ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC
177ebfedea0SLionel Sambuc 
178*0a6a1f1dSLionel Sambuc static int nid_list[] = {
179ebfedea0SLionel Sambuc     NID_sect163k1,              /* sect163k1 (1) */
180ebfedea0SLionel Sambuc     NID_sect163r1,              /* sect163r1 (2) */
181ebfedea0SLionel Sambuc     NID_sect163r2,              /* sect163r2 (3) */
182ebfedea0SLionel Sambuc     NID_sect193r1,              /* sect193r1 (4) */
183ebfedea0SLionel Sambuc     NID_sect193r2,              /* sect193r2 (5) */
184ebfedea0SLionel Sambuc     NID_sect233k1,              /* sect233k1 (6) */
185ebfedea0SLionel Sambuc     NID_sect233r1,              /* sect233r1 (7) */
186ebfedea0SLionel Sambuc     NID_sect239k1,              /* sect239k1 (8) */
187ebfedea0SLionel Sambuc     NID_sect283k1,              /* sect283k1 (9) */
188ebfedea0SLionel Sambuc     NID_sect283r1,              /* sect283r1 (10) */
189ebfedea0SLionel Sambuc     NID_sect409k1,              /* sect409k1 (11) */
190ebfedea0SLionel Sambuc     NID_sect409r1,              /* sect409r1 (12) */
191ebfedea0SLionel Sambuc     NID_sect571k1,              /* sect571k1 (13) */
192ebfedea0SLionel Sambuc     NID_sect571r1,              /* sect571r1 (14) */
193ebfedea0SLionel Sambuc     NID_secp160k1,              /* secp160k1 (15) */
194ebfedea0SLionel Sambuc     NID_secp160r1,              /* secp160r1 (16) */
195ebfedea0SLionel Sambuc     NID_secp160r2,              /* secp160r2 (17) */
196ebfedea0SLionel Sambuc     NID_secp192k1,              /* secp192k1 (18) */
197ebfedea0SLionel Sambuc     NID_X9_62_prime192v1,       /* secp192r1 (19) */
198ebfedea0SLionel Sambuc     NID_secp224k1,              /* secp224k1 (20) */
199ebfedea0SLionel Sambuc     NID_secp224r1,              /* secp224r1 (21) */
200ebfedea0SLionel Sambuc     NID_secp256k1,              /* secp256k1 (22) */
201ebfedea0SLionel Sambuc     NID_X9_62_prime256v1,       /* secp256r1 (23) */
202ebfedea0SLionel Sambuc     NID_secp384r1,              /* secp384r1 (24) */
203ebfedea0SLionel Sambuc     NID_secp521r1               /* secp521r1 (25) */
204ebfedea0SLionel Sambuc };
205ebfedea0SLionel Sambuc 
206*0a6a1f1dSLionel Sambuc static int pref_list[] = {
207*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
208ebfedea0SLionel Sambuc     NID_sect571r1,              /* sect571r1 (14) */
209ebfedea0SLionel Sambuc     NID_sect571k1,              /* sect571k1 (13) */
210*0a6a1f1dSLionel Sambuc # endif
211ebfedea0SLionel Sambuc     NID_secp521r1,              /* secp521r1 (25) */
212*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
213ebfedea0SLionel Sambuc     NID_sect409k1,              /* sect409k1 (11) */
214ebfedea0SLionel Sambuc     NID_sect409r1,              /* sect409r1 (12) */
215*0a6a1f1dSLionel Sambuc # endif
216ebfedea0SLionel Sambuc     NID_secp384r1,              /* secp384r1 (24) */
217*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
218ebfedea0SLionel Sambuc     NID_sect283k1,              /* sect283k1 (9) */
219ebfedea0SLionel Sambuc     NID_sect283r1,              /* sect283r1 (10) */
220*0a6a1f1dSLionel Sambuc # endif
221ebfedea0SLionel Sambuc     NID_secp256k1,              /* secp256k1 (22) */
222ebfedea0SLionel Sambuc     NID_X9_62_prime256v1,       /* secp256r1 (23) */
223*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
224ebfedea0SLionel Sambuc     NID_sect239k1,              /* sect239k1 (8) */
225ebfedea0SLionel Sambuc     NID_sect233k1,              /* sect233k1 (6) */
226ebfedea0SLionel Sambuc     NID_sect233r1,              /* sect233r1 (7) */
227*0a6a1f1dSLionel Sambuc # endif
228ebfedea0SLionel Sambuc     NID_secp224k1,              /* secp224k1 (20) */
229ebfedea0SLionel Sambuc     NID_secp224r1,              /* secp224r1 (21) */
230*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
231ebfedea0SLionel Sambuc     NID_sect193r1,              /* sect193r1 (4) */
232ebfedea0SLionel Sambuc     NID_sect193r2,              /* sect193r2 (5) */
233*0a6a1f1dSLionel Sambuc # endif
234ebfedea0SLionel Sambuc     NID_secp192k1,              /* secp192k1 (18) */
235ebfedea0SLionel Sambuc     NID_X9_62_prime192v1,       /* secp192r1 (19) */
236*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC2M
237ebfedea0SLionel Sambuc     NID_sect163k1,              /* sect163k1 (1) */
238ebfedea0SLionel Sambuc     NID_sect163r1,              /* sect163r1 (2) */
239ebfedea0SLionel Sambuc     NID_sect163r2,              /* sect163r2 (3) */
240*0a6a1f1dSLionel Sambuc # endif
241ebfedea0SLionel Sambuc     NID_secp160k1,              /* secp160k1 (15) */
242ebfedea0SLionel Sambuc     NID_secp160r1,              /* secp160r1 (16) */
243ebfedea0SLionel Sambuc     NID_secp160r2,              /* secp160r2 (17) */
244ebfedea0SLionel Sambuc };
245ebfedea0SLionel Sambuc 
tls1_ec_curve_id2nid(int curve_id)246ebfedea0SLionel Sambuc int tls1_ec_curve_id2nid(int curve_id)
247ebfedea0SLionel Sambuc {
248*0a6a1f1dSLionel Sambuc     /* ECC curves from RFC 4492 */
249ebfedea0SLionel Sambuc     if ((curve_id < 1) || ((unsigned int)curve_id >
250ebfedea0SLionel Sambuc                            sizeof(nid_list) / sizeof(nid_list[0])))
251ebfedea0SLionel Sambuc         return 0;
252ebfedea0SLionel Sambuc     return nid_list[curve_id - 1];
253ebfedea0SLionel Sambuc }
254ebfedea0SLionel Sambuc 
tls1_ec_nid2curve_id(int nid)255ebfedea0SLionel Sambuc int tls1_ec_nid2curve_id(int nid)
256ebfedea0SLionel Sambuc {
257*0a6a1f1dSLionel Sambuc     /* ECC curves from RFC 4492 */
258*0a6a1f1dSLionel Sambuc     switch (nid) {
259ebfedea0SLionel Sambuc     case NID_sect163k1:        /* sect163k1 (1) */
260ebfedea0SLionel Sambuc         return 1;
261ebfedea0SLionel Sambuc     case NID_sect163r1:        /* sect163r1 (2) */
262ebfedea0SLionel Sambuc         return 2;
263ebfedea0SLionel Sambuc     case NID_sect163r2:        /* sect163r2 (3) */
264ebfedea0SLionel Sambuc         return 3;
265ebfedea0SLionel Sambuc     case NID_sect193r1:        /* sect193r1 (4) */
266ebfedea0SLionel Sambuc         return 4;
267ebfedea0SLionel Sambuc     case NID_sect193r2:        /* sect193r2 (5) */
268ebfedea0SLionel Sambuc         return 5;
269ebfedea0SLionel Sambuc     case NID_sect233k1:        /* sect233k1 (6) */
270ebfedea0SLionel Sambuc         return 6;
271ebfedea0SLionel Sambuc     case NID_sect233r1:        /* sect233r1 (7) */
272ebfedea0SLionel Sambuc         return 7;
273ebfedea0SLionel Sambuc     case NID_sect239k1:        /* sect239k1 (8) */
274ebfedea0SLionel Sambuc         return 8;
275ebfedea0SLionel Sambuc     case NID_sect283k1:        /* sect283k1 (9) */
276ebfedea0SLionel Sambuc         return 9;
277ebfedea0SLionel Sambuc     case NID_sect283r1:        /* sect283r1 (10) */
278ebfedea0SLionel Sambuc         return 10;
279ebfedea0SLionel Sambuc     case NID_sect409k1:        /* sect409k1 (11) */
280ebfedea0SLionel Sambuc         return 11;
281ebfedea0SLionel Sambuc     case NID_sect409r1:        /* sect409r1 (12) */
282ebfedea0SLionel Sambuc         return 12;
283ebfedea0SLionel Sambuc     case NID_sect571k1:        /* sect571k1 (13) */
284ebfedea0SLionel Sambuc         return 13;
285ebfedea0SLionel Sambuc     case NID_sect571r1:        /* sect571r1 (14) */
286ebfedea0SLionel Sambuc         return 14;
287ebfedea0SLionel Sambuc     case NID_secp160k1:        /* secp160k1 (15) */
288ebfedea0SLionel Sambuc         return 15;
289ebfedea0SLionel Sambuc     case NID_secp160r1:        /* secp160r1 (16) */
290ebfedea0SLionel Sambuc         return 16;
291ebfedea0SLionel Sambuc     case NID_secp160r2:        /* secp160r2 (17) */
292ebfedea0SLionel Sambuc         return 17;
293ebfedea0SLionel Sambuc     case NID_secp192k1:        /* secp192k1 (18) */
294ebfedea0SLionel Sambuc         return 18;
295ebfedea0SLionel Sambuc     case NID_X9_62_prime192v1: /* secp192r1 (19) */
296ebfedea0SLionel Sambuc         return 19;
297ebfedea0SLionel Sambuc     case NID_secp224k1:        /* secp224k1 (20) */
298ebfedea0SLionel Sambuc         return 20;
299ebfedea0SLionel Sambuc     case NID_secp224r1:        /* secp224r1 (21) */
300ebfedea0SLionel Sambuc         return 21;
301ebfedea0SLionel Sambuc     case NID_secp256k1:        /* secp256k1 (22) */
302ebfedea0SLionel Sambuc         return 22;
303ebfedea0SLionel Sambuc     case NID_X9_62_prime256v1: /* secp256r1 (23) */
304ebfedea0SLionel Sambuc         return 23;
305ebfedea0SLionel Sambuc     case NID_secp384r1:        /* secp384r1 (24) */
306ebfedea0SLionel Sambuc         return 24;
307ebfedea0SLionel Sambuc     case NID_secp521r1:        /* secp521r1 (25) */
308ebfedea0SLionel Sambuc         return 25;
309ebfedea0SLionel Sambuc     default:
310ebfedea0SLionel Sambuc         return 0;
311ebfedea0SLionel Sambuc     }
312ebfedea0SLionel Sambuc }
313ebfedea0SLionel Sambuc #endif                          /* OPENSSL_NO_EC */
314ebfedea0SLionel Sambuc 
315ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
316ebfedea0SLionel Sambuc 
317*0a6a1f1dSLionel Sambuc /*
318*0a6a1f1dSLionel Sambuc  * List of supported signature algorithms and hashes. Should make this
319ebfedea0SLionel Sambuc  * customisable at some point, for now include everything we support.
320ebfedea0SLionel Sambuc  */
321ebfedea0SLionel Sambuc 
322ebfedea0SLionel Sambuc # ifdef OPENSSL_NO_RSA
323ebfedea0SLionel Sambuc #  define tlsext_sigalg_rsa(md) /* */
324ebfedea0SLionel Sambuc # else
325ebfedea0SLionel Sambuc #  define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa,
326ebfedea0SLionel Sambuc # endif
327ebfedea0SLionel Sambuc 
328ebfedea0SLionel Sambuc # ifdef OPENSSL_NO_DSA
329ebfedea0SLionel Sambuc #  define tlsext_sigalg_dsa(md) /* */
330ebfedea0SLionel Sambuc # else
331ebfedea0SLionel Sambuc #  define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa,
332ebfedea0SLionel Sambuc # endif
333ebfedea0SLionel Sambuc 
334ebfedea0SLionel Sambuc # ifdef OPENSSL_NO_ECDSA
335*0a6a1f1dSLionel Sambuc #  define tlsext_sigalg_ecdsa(md)
336*0a6a1f1dSLionel Sambuc                                 /* */
337ebfedea0SLionel Sambuc # else
338ebfedea0SLionel Sambuc #  define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa,
339ebfedea0SLionel Sambuc # endif
340ebfedea0SLionel Sambuc 
341ebfedea0SLionel Sambuc # define tlsext_sigalg(md) \
342ebfedea0SLionel Sambuc                 tlsext_sigalg_rsa(md) \
343ebfedea0SLionel Sambuc                 tlsext_sigalg_dsa(md) \
344ebfedea0SLionel Sambuc                 tlsext_sigalg_ecdsa(md)
345ebfedea0SLionel Sambuc 
346ebfedea0SLionel Sambuc static unsigned char tls12_sigalgs[] = {
347ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA512
348ebfedea0SLionel Sambuc     tlsext_sigalg(TLSEXT_hash_sha512)
349ebfedea0SLionel Sambuc         tlsext_sigalg(TLSEXT_hash_sha384)
350ebfedea0SLionel Sambuc # endif
351ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA256
352ebfedea0SLionel Sambuc         tlsext_sigalg(TLSEXT_hash_sha256)
353ebfedea0SLionel Sambuc         tlsext_sigalg(TLSEXT_hash_sha224)
354ebfedea0SLionel Sambuc # endif
355ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA
356ebfedea0SLionel Sambuc         tlsext_sigalg(TLSEXT_hash_sha1)
357ebfedea0SLionel Sambuc # endif
358ebfedea0SLionel Sambuc };
359ebfedea0SLionel Sambuc 
tls12_get_req_sig_algs(SSL * s,unsigned char * p)360ebfedea0SLionel Sambuc int tls12_get_req_sig_algs(SSL *s, unsigned char *p)
361ebfedea0SLionel Sambuc {
362ebfedea0SLionel Sambuc     size_t slen = sizeof(tls12_sigalgs);
363ebfedea0SLionel Sambuc     if (p)
364ebfedea0SLionel Sambuc         memcpy(p, tls12_sigalgs, slen);
365ebfedea0SLionel Sambuc     return (int)slen;
366ebfedea0SLionel Sambuc }
367ebfedea0SLionel Sambuc 
ssl_add_clienthello_tlsext(SSL * s,unsigned char * buf,unsigned char * limit)368*0a6a1f1dSLionel Sambuc unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf,
369*0a6a1f1dSLionel Sambuc                                           unsigned char *limit)
370ebfedea0SLionel Sambuc {
371ebfedea0SLionel Sambuc     int extdatalen = 0;
372*0a6a1f1dSLionel Sambuc     unsigned char *orig = buf;
373*0a6a1f1dSLionel Sambuc     unsigned char *ret = buf;
374ebfedea0SLionel Sambuc 
375ebfedea0SLionel Sambuc     /* don't add extensions for SSLv3 unless doing secure renegotiation */
376*0a6a1f1dSLionel Sambuc     if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding)
377*0a6a1f1dSLionel Sambuc         return orig;
378ebfedea0SLionel Sambuc 
379ebfedea0SLionel Sambuc     ret += 2;
380ebfedea0SLionel Sambuc 
381*0a6a1f1dSLionel Sambuc     if (ret >= limit)
382*0a6a1f1dSLionel Sambuc         return NULL;            /* this really never occurs, but ... */
383ebfedea0SLionel Sambuc 
384*0a6a1f1dSLionel Sambuc     if (s->tlsext_hostname != NULL) {
385ebfedea0SLionel Sambuc         /* Add TLS extension servername to the Client Hello message */
386ebfedea0SLionel Sambuc         unsigned long size_str;
387ebfedea0SLionel Sambuc         long lenmax;
388ebfedea0SLionel Sambuc 
389*0a6a1f1dSLionel Sambuc         /*-
390*0a6a1f1dSLionel Sambuc          * check for enough space.
391*0a6a1f1dSLionel Sambuc          * 4 for the servername type and entension length
392*0a6a1f1dSLionel Sambuc          * 2 for servernamelist length
393*0a6a1f1dSLionel Sambuc          * 1 for the hostname type
394*0a6a1f1dSLionel Sambuc          * 2 for hostname length
395*0a6a1f1dSLionel Sambuc          * + hostname length
396ebfedea0SLionel Sambuc          */
397ebfedea0SLionel Sambuc 
398ebfedea0SLionel Sambuc         if ((lenmax = limit - ret - 9) < 0
399*0a6a1f1dSLionel Sambuc             || (size_str =
400*0a6a1f1dSLionel Sambuc                 strlen(s->tlsext_hostname)) > (unsigned long)lenmax)
401ebfedea0SLionel Sambuc             return NULL;
402ebfedea0SLionel Sambuc 
403ebfedea0SLionel Sambuc         /* extension type and length */
404ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_server_name, ret);
405ebfedea0SLionel Sambuc         s2n(size_str + 5, ret);
406ebfedea0SLionel Sambuc 
407ebfedea0SLionel Sambuc         /* length of servername list */
408ebfedea0SLionel Sambuc         s2n(size_str + 3, ret);
409ebfedea0SLionel Sambuc 
410ebfedea0SLionel Sambuc         /* hostname type, length and hostname */
411ebfedea0SLionel Sambuc         *(ret++) = (unsigned char)TLSEXT_NAMETYPE_host_name;
412ebfedea0SLionel Sambuc         s2n(size_str, ret);
413ebfedea0SLionel Sambuc         memcpy(ret, s->tlsext_hostname, size_str);
414ebfedea0SLionel Sambuc         ret += size_str;
415ebfedea0SLionel Sambuc     }
416ebfedea0SLionel Sambuc 
417ebfedea0SLionel Sambuc     /* Add RI if renegotiating */
418*0a6a1f1dSLionel Sambuc     if (s->renegotiate) {
419ebfedea0SLionel Sambuc         int el;
420ebfedea0SLionel Sambuc 
421*0a6a1f1dSLionel Sambuc         if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
422ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
423ebfedea0SLionel Sambuc             return NULL;
424ebfedea0SLionel Sambuc         }
425ebfedea0SLionel Sambuc 
426*0a6a1f1dSLionel Sambuc         if ((limit - ret - 4 - el) < 0)
427*0a6a1f1dSLionel Sambuc             return NULL;
428ebfedea0SLionel Sambuc 
429ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_renegotiate, ret);
430ebfedea0SLionel Sambuc         s2n(el, ret);
431ebfedea0SLionel Sambuc 
432*0a6a1f1dSLionel Sambuc         if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
433ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
434ebfedea0SLionel Sambuc             return NULL;
435ebfedea0SLionel Sambuc         }
436ebfedea0SLionel Sambuc 
437ebfedea0SLionel Sambuc         ret += el;
438ebfedea0SLionel Sambuc     }
439ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRP
440ebfedea0SLionel Sambuc     /* Add SRP username if there is one */
441*0a6a1f1dSLionel Sambuc     if (s->srp_ctx.login != NULL) { /* Add TLS extension SRP username to the
442*0a6a1f1dSLionel Sambuc                                      * Client Hello message */
443ebfedea0SLionel Sambuc 
444ebfedea0SLionel Sambuc         int login_len = strlen(s->srp_ctx.login);
445*0a6a1f1dSLionel Sambuc         if (login_len > 255 || login_len == 0) {
446ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
447ebfedea0SLionel Sambuc             return NULL;
448ebfedea0SLionel Sambuc         }
449ebfedea0SLionel Sambuc 
450*0a6a1f1dSLionel Sambuc         /*-
451*0a6a1f1dSLionel Sambuc          * check for enough space.
452*0a6a1f1dSLionel Sambuc          * 4 for the srp type type and entension length
453*0a6a1f1dSLionel Sambuc          * 1 for the srp user identity
454*0a6a1f1dSLionel Sambuc          * + srp user identity length
455ebfedea0SLionel Sambuc          */
456*0a6a1f1dSLionel Sambuc         if ((limit - ret - 5 - login_len) < 0)
457*0a6a1f1dSLionel Sambuc             return NULL;
458ebfedea0SLionel Sambuc 
459ebfedea0SLionel Sambuc         /* fill in the extension */
460ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_srp, ret);
461ebfedea0SLionel Sambuc         s2n(login_len + 1, ret);
462ebfedea0SLionel Sambuc         (*ret++) = (unsigned char)login_len;
463ebfedea0SLionel Sambuc         memcpy(ret, s->srp_ctx.login, login_len);
464ebfedea0SLionel Sambuc         ret += login_len;
465ebfedea0SLionel Sambuc     }
466ebfedea0SLionel Sambuc # endif
467ebfedea0SLionel Sambuc 
468ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
469*0a6a1f1dSLionel Sambuc     if (s->tlsext_ecpointformatlist != NULL) {
470*0a6a1f1dSLionel Sambuc         /*
471*0a6a1f1dSLionel Sambuc          * Add TLS extension ECPointFormats to the ClientHello message
472*0a6a1f1dSLionel Sambuc          */
473ebfedea0SLionel Sambuc         long lenmax;
474ebfedea0SLionel Sambuc 
475*0a6a1f1dSLionel Sambuc         if ((lenmax = limit - ret - 5) < 0)
476*0a6a1f1dSLionel Sambuc             return NULL;
477*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax)
478*0a6a1f1dSLionel Sambuc             return NULL;
479*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist_length > 255) {
480ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
481ebfedea0SLionel Sambuc             return NULL;
482ebfedea0SLionel Sambuc         }
483ebfedea0SLionel Sambuc 
484ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_ec_point_formats, ret);
485ebfedea0SLionel Sambuc         s2n(s->tlsext_ecpointformatlist_length + 1, ret);
486ebfedea0SLionel Sambuc         *(ret++) = (unsigned char)s->tlsext_ecpointformatlist_length;
487*0a6a1f1dSLionel Sambuc         memcpy(ret, s->tlsext_ecpointformatlist,
488*0a6a1f1dSLionel Sambuc                s->tlsext_ecpointformatlist_length);
489ebfedea0SLionel Sambuc         ret += s->tlsext_ecpointformatlist_length;
490ebfedea0SLionel Sambuc     }
491*0a6a1f1dSLionel Sambuc     if (s->tlsext_ellipticcurvelist != NULL) {
492*0a6a1f1dSLionel Sambuc         /*
493*0a6a1f1dSLionel Sambuc          * Add TLS extension EllipticCurves to the ClientHello message
494*0a6a1f1dSLionel Sambuc          */
495ebfedea0SLionel Sambuc         long lenmax;
496ebfedea0SLionel Sambuc 
497*0a6a1f1dSLionel Sambuc         if ((lenmax = limit - ret - 6) < 0)
498*0a6a1f1dSLionel Sambuc             return NULL;
499*0a6a1f1dSLionel Sambuc         if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax)
500*0a6a1f1dSLionel Sambuc             return NULL;
501*0a6a1f1dSLionel Sambuc         if (s->tlsext_ellipticcurvelist_length > 65532) {
502ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
503ebfedea0SLionel Sambuc             return NULL;
504ebfedea0SLionel Sambuc         }
505ebfedea0SLionel Sambuc 
506ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_elliptic_curves, ret);
507ebfedea0SLionel Sambuc         s2n(s->tlsext_ellipticcurvelist_length + 2, ret);
508ebfedea0SLionel Sambuc 
509ebfedea0SLionel Sambuc         s2n(s->tlsext_ellipticcurvelist_length, ret);
510*0a6a1f1dSLionel Sambuc         memcpy(ret, s->tlsext_ellipticcurvelist,
511*0a6a1f1dSLionel Sambuc                s->tlsext_ellipticcurvelist_length);
512ebfedea0SLionel Sambuc         ret += s->tlsext_ellipticcurvelist_length;
513ebfedea0SLionel Sambuc     }
514ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
515ebfedea0SLionel Sambuc 
516*0a6a1f1dSLionel Sambuc     if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
517ebfedea0SLionel Sambuc         int ticklen;
518ebfedea0SLionel Sambuc         if (!s->new_session && s->session && s->session->tlsext_tick)
519ebfedea0SLionel Sambuc             ticklen = s->session->tlsext_ticklen;
520ebfedea0SLionel Sambuc         else if (s->session && s->tlsext_session_ticket &&
521*0a6a1f1dSLionel Sambuc                  s->tlsext_session_ticket->data) {
522ebfedea0SLionel Sambuc             ticklen = s->tlsext_session_ticket->length;
523ebfedea0SLionel Sambuc             s->session->tlsext_tick = OPENSSL_malloc(ticklen);
524ebfedea0SLionel Sambuc             if (!s->session->tlsext_tick)
525ebfedea0SLionel Sambuc                 return NULL;
526ebfedea0SLionel Sambuc             memcpy(s->session->tlsext_tick,
527*0a6a1f1dSLionel Sambuc                    s->tlsext_session_ticket->data, ticklen);
528ebfedea0SLionel Sambuc             s->session->tlsext_ticklen = ticklen;
529*0a6a1f1dSLionel Sambuc         } else
530ebfedea0SLionel Sambuc             ticklen = 0;
531ebfedea0SLionel Sambuc         if (ticklen == 0 && s->tlsext_session_ticket &&
532ebfedea0SLionel Sambuc             s->tlsext_session_ticket->data == NULL)
533ebfedea0SLionel Sambuc             goto skip_ext;
534*0a6a1f1dSLionel Sambuc         /*
535*0a6a1f1dSLionel Sambuc          * Check for enough room 2 for extension type, 2 for len rest for
536*0a6a1f1dSLionel Sambuc          * ticket
537ebfedea0SLionel Sambuc          */
538*0a6a1f1dSLionel Sambuc         if ((long)(limit - ret - 4 - ticklen) < 0)
539*0a6a1f1dSLionel Sambuc             return NULL;
540ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_session_ticket, ret);
541ebfedea0SLionel Sambuc         s2n(ticklen, ret);
542*0a6a1f1dSLionel Sambuc         if (ticklen) {
543ebfedea0SLionel Sambuc             memcpy(ret, s->session->tlsext_tick, ticklen);
544ebfedea0SLionel Sambuc             ret += ticklen;
545ebfedea0SLionel Sambuc         }
546ebfedea0SLionel Sambuc     }
547ebfedea0SLionel Sambuc  skip_ext:
548ebfedea0SLionel Sambuc 
549*0a6a1f1dSLionel Sambuc     if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
550ebfedea0SLionel Sambuc         if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6)
551ebfedea0SLionel Sambuc             return NULL;
552ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_signature_algorithms, ret);
553ebfedea0SLionel Sambuc         s2n(sizeof(tls12_sigalgs) + 2, ret);
554ebfedea0SLionel Sambuc         s2n(sizeof(tls12_sigalgs), ret);
555ebfedea0SLionel Sambuc         memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs));
556ebfedea0SLionel Sambuc         ret += sizeof(tls12_sigalgs);
557ebfedea0SLionel Sambuc     }
558ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
559*0a6a1f1dSLionel Sambuc     if (s->s3->client_opaque_prf_input != NULL && s->version != DTLS1_VERSION) {
560ebfedea0SLionel Sambuc         size_t col = s->s3->client_opaque_prf_input_len;
561ebfedea0SLionel Sambuc 
562ebfedea0SLionel Sambuc         if ((long)(limit - ret - 6 - col < 0))
563ebfedea0SLionel Sambuc             return NULL;
564ebfedea0SLionel Sambuc         if (col > 0xFFFD)       /* can't happen */
565ebfedea0SLionel Sambuc             return NULL;
566ebfedea0SLionel Sambuc 
567ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_opaque_prf_input, ret);
568ebfedea0SLionel Sambuc         s2n(col + 2, ret);
569ebfedea0SLionel Sambuc         s2n(col, ret);
570ebfedea0SLionel Sambuc         memcpy(ret, s->s3->client_opaque_prf_input, col);
571ebfedea0SLionel Sambuc         ret += col;
572ebfedea0SLionel Sambuc     }
573ebfedea0SLionel Sambuc # endif
574ebfedea0SLionel Sambuc 
575ebfedea0SLionel Sambuc     if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp &&
576*0a6a1f1dSLionel Sambuc         s->version != DTLS1_VERSION) {
577ebfedea0SLionel Sambuc         int i;
578ebfedea0SLionel Sambuc         long extlen, idlen, itmp;
579ebfedea0SLionel Sambuc         OCSP_RESPID *id;
580ebfedea0SLionel Sambuc 
581ebfedea0SLionel Sambuc         idlen = 0;
582*0a6a1f1dSLionel Sambuc         for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
583ebfedea0SLionel Sambuc             id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
584ebfedea0SLionel Sambuc             itmp = i2d_OCSP_RESPID(id, NULL);
585ebfedea0SLionel Sambuc             if (itmp <= 0)
586ebfedea0SLionel Sambuc                 return NULL;
587ebfedea0SLionel Sambuc             idlen += itmp + 2;
588ebfedea0SLionel Sambuc         }
589ebfedea0SLionel Sambuc 
590*0a6a1f1dSLionel Sambuc         if (s->tlsext_ocsp_exts) {
591ebfedea0SLionel Sambuc             extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL);
592ebfedea0SLionel Sambuc             if (extlen < 0)
593ebfedea0SLionel Sambuc                 return NULL;
594*0a6a1f1dSLionel Sambuc         } else
595ebfedea0SLionel Sambuc             extlen = 0;
596ebfedea0SLionel Sambuc 
597*0a6a1f1dSLionel Sambuc         if ((long)(limit - ret - 7 - extlen - idlen) < 0)
598*0a6a1f1dSLionel Sambuc             return NULL;
599ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_status_request, ret);
600ebfedea0SLionel Sambuc         if (extlen + idlen > 0xFFF0)
601ebfedea0SLionel Sambuc             return NULL;
602ebfedea0SLionel Sambuc         s2n(extlen + idlen + 5, ret);
603ebfedea0SLionel Sambuc         *(ret++) = TLSEXT_STATUSTYPE_ocsp;
604ebfedea0SLionel Sambuc         s2n(idlen, ret);
605*0a6a1f1dSLionel Sambuc         for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) {
606ebfedea0SLionel Sambuc             /* save position of id len */
607ebfedea0SLionel Sambuc             unsigned char *q = ret;
608ebfedea0SLionel Sambuc             id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i);
609ebfedea0SLionel Sambuc             /* skip over id len */
610ebfedea0SLionel Sambuc             ret += 2;
611ebfedea0SLionel Sambuc             itmp = i2d_OCSP_RESPID(id, &ret);
612ebfedea0SLionel Sambuc             /* write id len */
613ebfedea0SLionel Sambuc             s2n(itmp, q);
614ebfedea0SLionel Sambuc         }
615ebfedea0SLionel Sambuc         s2n(extlen, ret);
616ebfedea0SLionel Sambuc         if (extlen > 0)
617ebfedea0SLionel Sambuc             i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret);
618ebfedea0SLionel Sambuc     }
619ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
620ebfedea0SLionel Sambuc     /* Add Heartbeat extension */
621*0a6a1f1dSLionel Sambuc     if ((limit - ret - 4 - 1) < 0)
622*0a6a1f1dSLionel Sambuc         return NULL;
623ebfedea0SLionel Sambuc     s2n(TLSEXT_TYPE_heartbeat, ret);
624ebfedea0SLionel Sambuc     s2n(1, ret);
625*0a6a1f1dSLionel Sambuc     /*-
626*0a6a1f1dSLionel Sambuc      * Set mode:
627ebfedea0SLionel Sambuc      * 1: peer may send requests
628ebfedea0SLionel Sambuc      * 2: peer not allowed to send requests
629ebfedea0SLionel Sambuc      */
630ebfedea0SLionel Sambuc     if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
631ebfedea0SLionel Sambuc         *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
632ebfedea0SLionel Sambuc     else
633ebfedea0SLionel Sambuc         *(ret++) = SSL_TLSEXT_HB_ENABLED;
634ebfedea0SLionel Sambuc # endif
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
637*0a6a1f1dSLionel Sambuc     if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) {
638*0a6a1f1dSLionel Sambuc         /*
639*0a6a1f1dSLionel Sambuc          * The client advertises an emtpy extension to indicate its support
640*0a6a1f1dSLionel Sambuc          * for Next Protocol Negotiation
641*0a6a1f1dSLionel Sambuc          */
642ebfedea0SLionel Sambuc         if (limit - ret - 4 < 0)
643ebfedea0SLionel Sambuc             return NULL;
644ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_next_proto_neg, ret);
645ebfedea0SLionel Sambuc         s2n(0, ret);
646ebfedea0SLionel Sambuc     }
647ebfedea0SLionel Sambuc # endif
648ebfedea0SLionel Sambuc 
649ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRTP
650*0a6a1f1dSLionel Sambuc     if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)) {
651ebfedea0SLionel Sambuc         int el;
652ebfedea0SLionel Sambuc 
653ebfedea0SLionel Sambuc         ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
654ebfedea0SLionel Sambuc 
655*0a6a1f1dSLionel Sambuc         if ((limit - ret - 4 - el) < 0)
656*0a6a1f1dSLionel Sambuc             return NULL;
657ebfedea0SLionel Sambuc 
658ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_use_srtp, ret);
659ebfedea0SLionel Sambuc         s2n(el, ret);
660ebfedea0SLionel Sambuc 
661*0a6a1f1dSLionel Sambuc         if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
662ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
663ebfedea0SLionel Sambuc             return NULL;
664ebfedea0SLionel Sambuc         }
665ebfedea0SLionel Sambuc         ret += el;
666ebfedea0SLionel Sambuc     }
667ebfedea0SLionel Sambuc # endif
668*0a6a1f1dSLionel Sambuc     /*
669*0a6a1f1dSLionel Sambuc      * Add padding to workaround bugs in F5 terminators. See
670*0a6a1f1dSLionel Sambuc      * https://tools.ietf.org/html/draft-agl-tls-padding-03 NB: because this
671*0a6a1f1dSLionel Sambuc      * code works out the length of all existing extensions it MUST always
672*0a6a1f1dSLionel Sambuc      * appear last.
673*0a6a1f1dSLionel Sambuc      */
674*0a6a1f1dSLionel Sambuc     if (s->options & SSL_OP_TLSEXT_PADDING) {
675*0a6a1f1dSLionel Sambuc         int hlen = ret - (unsigned char *)s->init_buf->data;
676*0a6a1f1dSLionel Sambuc         /*
677*0a6a1f1dSLionel Sambuc          * The code in s23_clnt.c to build ClientHello messages includes the
678*0a6a1f1dSLionel Sambuc          * 5-byte record header in the buffer, while the code in s3_clnt.c
679*0a6a1f1dSLionel Sambuc          * does not.
680*0a6a1f1dSLionel Sambuc          */
681*0a6a1f1dSLionel Sambuc         if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
682*0a6a1f1dSLionel Sambuc             hlen -= 5;
683*0a6a1f1dSLionel Sambuc         if (hlen > 0xff && hlen < 0x200) {
684*0a6a1f1dSLionel Sambuc             hlen = 0x200 - hlen;
685*0a6a1f1dSLionel Sambuc             if (hlen >= 4)
686*0a6a1f1dSLionel Sambuc                 hlen -= 4;
687*0a6a1f1dSLionel Sambuc             else
688*0a6a1f1dSLionel Sambuc                 hlen = 0;
689ebfedea0SLionel Sambuc 
690*0a6a1f1dSLionel Sambuc             s2n(TLSEXT_TYPE_padding, ret);
691*0a6a1f1dSLionel Sambuc             s2n(hlen, ret);
692*0a6a1f1dSLionel Sambuc             memset(ret, 0, hlen);
693*0a6a1f1dSLionel Sambuc             ret += hlen;
694*0a6a1f1dSLionel Sambuc         }
695*0a6a1f1dSLionel Sambuc     }
696ebfedea0SLionel Sambuc 
697*0a6a1f1dSLionel Sambuc     if ((extdatalen = ret - orig - 2) == 0)
698*0a6a1f1dSLionel Sambuc         return orig;
699*0a6a1f1dSLionel Sambuc 
700*0a6a1f1dSLionel Sambuc     s2n(extdatalen, orig);
701ebfedea0SLionel Sambuc     return ret;
702ebfedea0SLionel Sambuc }
703ebfedea0SLionel Sambuc 
ssl_add_serverhello_tlsext(SSL * s,unsigned char * buf,unsigned char * limit)704*0a6a1f1dSLionel Sambuc unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf,
705*0a6a1f1dSLionel Sambuc                                           unsigned char *limit)
706ebfedea0SLionel Sambuc {
707ebfedea0SLionel Sambuc     int extdatalen = 0;
708*0a6a1f1dSLionel Sambuc     unsigned char *orig = buf;
709*0a6a1f1dSLionel Sambuc     unsigned char *ret = buf;
710ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
711ebfedea0SLionel Sambuc     int next_proto_neg_seen;
712ebfedea0SLionel Sambuc # endif
713ebfedea0SLionel Sambuc 
714*0a6a1f1dSLionel Sambuc     /*
715*0a6a1f1dSLionel Sambuc      * don't add extensions for SSLv3, unless doing secure renegotiation
716*0a6a1f1dSLionel Sambuc      */
717ebfedea0SLionel Sambuc     if (s->version == SSL3_VERSION && !s->s3->send_connection_binding)
718*0a6a1f1dSLionel Sambuc         return orig;
719ebfedea0SLionel Sambuc 
720ebfedea0SLionel Sambuc     ret += 2;
721*0a6a1f1dSLionel Sambuc     if (ret >= limit)
722*0a6a1f1dSLionel Sambuc         return NULL;            /* this really never occurs, but ... */
723ebfedea0SLionel Sambuc 
724*0a6a1f1dSLionel Sambuc     if (!s->hit && s->servername_done == 1
725*0a6a1f1dSLionel Sambuc         && s->session->tlsext_hostname != NULL) {
726*0a6a1f1dSLionel Sambuc         if ((long)(limit - ret - 4) < 0)
727*0a6a1f1dSLionel Sambuc             return NULL;
728ebfedea0SLionel Sambuc 
729ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_server_name, ret);
730ebfedea0SLionel Sambuc         s2n(0, ret);
731ebfedea0SLionel Sambuc     }
732ebfedea0SLionel Sambuc 
733*0a6a1f1dSLionel Sambuc     if (s->s3->send_connection_binding) {
734ebfedea0SLionel Sambuc         int el;
735ebfedea0SLionel Sambuc 
736*0a6a1f1dSLionel Sambuc         if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
737ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
738ebfedea0SLionel Sambuc             return NULL;
739ebfedea0SLionel Sambuc         }
740ebfedea0SLionel Sambuc 
741*0a6a1f1dSLionel Sambuc         if ((limit - ret - 4 - el) < 0)
742*0a6a1f1dSLionel Sambuc             return NULL;
743ebfedea0SLionel Sambuc 
744ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_renegotiate, ret);
745ebfedea0SLionel Sambuc         s2n(el, ret);
746ebfedea0SLionel Sambuc 
747*0a6a1f1dSLionel Sambuc         if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
748ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
749ebfedea0SLionel Sambuc             return NULL;
750ebfedea0SLionel Sambuc         }
751ebfedea0SLionel Sambuc 
752ebfedea0SLionel Sambuc         ret += el;
753ebfedea0SLionel Sambuc     }
754ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
755*0a6a1f1dSLionel Sambuc     if (s->tlsext_ecpointformatlist != NULL) {
756*0a6a1f1dSLionel Sambuc         /*
757*0a6a1f1dSLionel Sambuc          * Add TLS extension ECPointFormats to the ServerHello message
758*0a6a1f1dSLionel Sambuc          */
759ebfedea0SLionel Sambuc         long lenmax;
760ebfedea0SLionel Sambuc 
761*0a6a1f1dSLionel Sambuc         if ((lenmax = limit - ret - 5) < 0)
762*0a6a1f1dSLionel Sambuc             return NULL;
763*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax)
764*0a6a1f1dSLionel Sambuc             return NULL;
765*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist_length > 255) {
766ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
767ebfedea0SLionel Sambuc             return NULL;
768ebfedea0SLionel Sambuc         }
769ebfedea0SLionel Sambuc 
770ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_ec_point_formats, ret);
771ebfedea0SLionel Sambuc         s2n(s->tlsext_ecpointformatlist_length + 1, ret);
772ebfedea0SLionel Sambuc         *(ret++) = (unsigned char)s->tlsext_ecpointformatlist_length;
773*0a6a1f1dSLionel Sambuc         memcpy(ret, s->tlsext_ecpointformatlist,
774*0a6a1f1dSLionel Sambuc                s->tlsext_ecpointformatlist_length);
775ebfedea0SLionel Sambuc         ret += s->tlsext_ecpointformatlist_length;
776ebfedea0SLionel Sambuc 
777ebfedea0SLionel Sambuc     }
778*0a6a1f1dSLionel Sambuc     /*
779*0a6a1f1dSLionel Sambuc      * Currently the server should not respond with a SupportedCurves
780*0a6a1f1dSLionel Sambuc      * extension
781*0a6a1f1dSLionel Sambuc      */
782ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
783ebfedea0SLionel Sambuc 
784*0a6a1f1dSLionel Sambuc     if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
785*0a6a1f1dSLionel Sambuc         if ((long)(limit - ret - 4) < 0)
786*0a6a1f1dSLionel Sambuc             return NULL;
787ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_session_ticket, ret);
788ebfedea0SLionel Sambuc         s2n(0, ret);
789ebfedea0SLionel Sambuc     }
790ebfedea0SLionel Sambuc 
791*0a6a1f1dSLionel Sambuc     if (s->tlsext_status_expected) {
792*0a6a1f1dSLionel Sambuc         if ((long)(limit - ret - 4) < 0)
793*0a6a1f1dSLionel Sambuc             return NULL;
794ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_status_request, ret);
795ebfedea0SLionel Sambuc         s2n(0, ret);
796ebfedea0SLionel Sambuc     }
797ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
798*0a6a1f1dSLionel Sambuc     if (s->s3->server_opaque_prf_input != NULL && s->version != DTLS1_VERSION) {
799ebfedea0SLionel Sambuc         size_t sol = s->s3->server_opaque_prf_input_len;
800ebfedea0SLionel Sambuc 
801ebfedea0SLionel Sambuc         if ((long)(limit - ret - 6 - sol) < 0)
802ebfedea0SLionel Sambuc             return NULL;
803ebfedea0SLionel Sambuc         if (sol > 0xFFFD)       /* can't happen */
804ebfedea0SLionel Sambuc             return NULL;
805ebfedea0SLionel Sambuc 
806ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_opaque_prf_input, ret);
807ebfedea0SLionel Sambuc         s2n(sol + 2, ret);
808ebfedea0SLionel Sambuc         s2n(sol, ret);
809ebfedea0SLionel Sambuc         memcpy(ret, s->s3->server_opaque_prf_input, sol);
810ebfedea0SLionel Sambuc         ret += sol;
811ebfedea0SLionel Sambuc     }
812ebfedea0SLionel Sambuc # endif
813ebfedea0SLionel Sambuc 
814ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRTP
815*0a6a1f1dSLionel Sambuc     if (SSL_IS_DTLS(s) && s->srtp_profile) {
816ebfedea0SLionel Sambuc         int el;
817ebfedea0SLionel Sambuc 
818ebfedea0SLionel Sambuc         ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
819ebfedea0SLionel Sambuc 
820*0a6a1f1dSLionel Sambuc         if ((limit - ret - 4 - el) < 0)
821*0a6a1f1dSLionel Sambuc             return NULL;
822ebfedea0SLionel Sambuc 
823ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_use_srtp, ret);
824ebfedea0SLionel Sambuc         s2n(el, ret);
825ebfedea0SLionel Sambuc 
826*0a6a1f1dSLionel Sambuc         if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
827ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR);
828ebfedea0SLionel Sambuc             return NULL;
829ebfedea0SLionel Sambuc         }
830ebfedea0SLionel Sambuc         ret += el;
831ebfedea0SLionel Sambuc     }
832ebfedea0SLionel Sambuc # endif
833ebfedea0SLionel Sambuc 
834*0a6a1f1dSLionel Sambuc     if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80
835*0a6a1f1dSLionel Sambuc          || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81)
836*0a6a1f1dSLionel Sambuc         && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) {
837*0a6a1f1dSLionel Sambuc         const unsigned char cryptopro_ext[36] = {
838ebfedea0SLionel Sambuc             0xfd, 0xe8,         /* 65000 */
839ebfedea0SLionel Sambuc             0x00, 0x20,         /* 32 bytes length */
840ebfedea0SLionel Sambuc             0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85,
841ebfedea0SLionel Sambuc             0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06,
842ebfedea0SLionel Sambuc             0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08,
843*0a6a1f1dSLionel Sambuc             0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17
844*0a6a1f1dSLionel Sambuc         };
845*0a6a1f1dSLionel Sambuc         if (limit - ret < 36)
846*0a6a1f1dSLionel Sambuc             return NULL;
847ebfedea0SLionel Sambuc         memcpy(ret, cryptopro_ext, 36);
848ebfedea0SLionel Sambuc         ret += 36;
849ebfedea0SLionel Sambuc 
850ebfedea0SLionel Sambuc     }
851ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
852ebfedea0SLionel Sambuc     /* Add Heartbeat extension if we've received one */
853*0a6a1f1dSLionel Sambuc     if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) {
854*0a6a1f1dSLionel Sambuc         if ((limit - ret - 4 - 1) < 0)
855*0a6a1f1dSLionel Sambuc             return NULL;
856ebfedea0SLionel Sambuc         s2n(TLSEXT_TYPE_heartbeat, ret);
857ebfedea0SLionel Sambuc         s2n(1, ret);
858*0a6a1f1dSLionel Sambuc         /*-
859*0a6a1f1dSLionel Sambuc          * Set mode:
860ebfedea0SLionel Sambuc          * 1: peer may send requests
861ebfedea0SLionel Sambuc          * 2: peer not allowed to send requests
862ebfedea0SLionel Sambuc          */
863ebfedea0SLionel Sambuc         if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS)
864ebfedea0SLionel Sambuc             *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
865ebfedea0SLionel Sambuc         else
866ebfedea0SLionel Sambuc             *(ret++) = SSL_TLSEXT_HB_ENABLED;
867ebfedea0SLionel Sambuc 
868ebfedea0SLionel Sambuc     }
869ebfedea0SLionel Sambuc # endif
870ebfedea0SLionel Sambuc 
871ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
872ebfedea0SLionel Sambuc     next_proto_neg_seen = s->s3->next_proto_neg_seen;
873ebfedea0SLionel Sambuc     s->s3->next_proto_neg_seen = 0;
874*0a6a1f1dSLionel Sambuc     if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
875ebfedea0SLionel Sambuc         const unsigned char *npa;
876ebfedea0SLionel Sambuc         unsigned int npalen;
877ebfedea0SLionel Sambuc         int r;
878ebfedea0SLionel Sambuc 
879*0a6a1f1dSLionel Sambuc         r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen,
880*0a6a1f1dSLionel Sambuc                                               s->
881*0a6a1f1dSLionel Sambuc                                               ctx->next_protos_advertised_cb_arg);
882*0a6a1f1dSLionel Sambuc         if (r == SSL_TLSEXT_ERR_OK) {
883*0a6a1f1dSLionel Sambuc             if ((long)(limit - ret - 4 - npalen) < 0)
884*0a6a1f1dSLionel Sambuc                 return NULL;
885ebfedea0SLionel Sambuc             s2n(TLSEXT_TYPE_next_proto_neg, ret);
886ebfedea0SLionel Sambuc             s2n(npalen, ret);
887ebfedea0SLionel Sambuc             memcpy(ret, npa, npalen);
888ebfedea0SLionel Sambuc             ret += npalen;
889ebfedea0SLionel Sambuc             s->s3->next_proto_neg_seen = 1;
890ebfedea0SLionel Sambuc         }
891ebfedea0SLionel Sambuc     }
892ebfedea0SLionel Sambuc # endif
893ebfedea0SLionel Sambuc 
894*0a6a1f1dSLionel Sambuc     if ((extdatalen = ret - orig - 2) == 0)
895*0a6a1f1dSLionel Sambuc         return orig;
896ebfedea0SLionel Sambuc 
897*0a6a1f1dSLionel Sambuc     s2n(extdatalen, orig);
898ebfedea0SLionel Sambuc     return ret;
899ebfedea0SLionel Sambuc }
900ebfedea0SLionel Sambuc 
901*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC
902*0a6a1f1dSLionel Sambuc /*-
903*0a6a1f1dSLionel Sambuc  * ssl_check_for_safari attempts to fingerprint Safari using OS X
904*0a6a1f1dSLionel Sambuc  * SecureTransport using the TLS extension block in |d|, of length |n|.
905*0a6a1f1dSLionel Sambuc  * Safari, since 10.6, sends exactly these extensions, in this order:
906*0a6a1f1dSLionel Sambuc  *   SNI,
907*0a6a1f1dSLionel Sambuc  *   elliptic_curves
908*0a6a1f1dSLionel Sambuc  *   ec_point_formats
909*0a6a1f1dSLionel Sambuc  *
910*0a6a1f1dSLionel Sambuc  * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8,
911*0a6a1f1dSLionel Sambuc  * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them.
912*0a6a1f1dSLionel Sambuc  * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from
913*0a6a1f1dSLionel Sambuc  * 10.8..10.8.3 (which don't work).
914*0a6a1f1dSLionel Sambuc  */
ssl_check_for_safari(SSL * s,const unsigned char * data,const unsigned char * d,int n)915*0a6a1f1dSLionel Sambuc static void ssl_check_for_safari(SSL *s, const unsigned char *data,
916*0a6a1f1dSLionel Sambuc                                  const unsigned char *d, int n)
917*0a6a1f1dSLionel Sambuc {
918*0a6a1f1dSLionel Sambuc     unsigned short type, size;
919*0a6a1f1dSLionel Sambuc     static const unsigned char kSafariExtensionsBlock[] = {
920*0a6a1f1dSLionel Sambuc         0x00, 0x0a,             /* elliptic_curves extension */
921*0a6a1f1dSLionel Sambuc         0x00, 0x08,             /* 8 bytes */
922*0a6a1f1dSLionel Sambuc         0x00, 0x06,             /* 6 bytes of curve ids */
923*0a6a1f1dSLionel Sambuc         0x00, 0x17,             /* P-256 */
924*0a6a1f1dSLionel Sambuc         0x00, 0x18,             /* P-384 */
925*0a6a1f1dSLionel Sambuc         0x00, 0x19,             /* P-521 */
926*0a6a1f1dSLionel Sambuc 
927*0a6a1f1dSLionel Sambuc         0x00, 0x0b,             /* ec_point_formats */
928*0a6a1f1dSLionel Sambuc         0x00, 0x02,             /* 2 bytes */
929*0a6a1f1dSLionel Sambuc         0x01,                   /* 1 point format */
930*0a6a1f1dSLionel Sambuc         0x00,                   /* uncompressed */
931*0a6a1f1dSLionel Sambuc     };
932*0a6a1f1dSLionel Sambuc 
933*0a6a1f1dSLionel Sambuc     /* The following is only present in TLS 1.2 */
934*0a6a1f1dSLionel Sambuc     static const unsigned char kSafariTLS12ExtensionsBlock[] = {
935*0a6a1f1dSLionel Sambuc         0x00, 0x0d,             /* signature_algorithms */
936*0a6a1f1dSLionel Sambuc         0x00, 0x0c,             /* 12 bytes */
937*0a6a1f1dSLionel Sambuc         0x00, 0x0a,             /* 10 bytes */
938*0a6a1f1dSLionel Sambuc         0x05, 0x01,             /* SHA-384/RSA */
939*0a6a1f1dSLionel Sambuc         0x04, 0x01,             /* SHA-256/RSA */
940*0a6a1f1dSLionel Sambuc         0x02, 0x01,             /* SHA-1/RSA */
941*0a6a1f1dSLionel Sambuc         0x04, 0x03,             /* SHA-256/ECDSA */
942*0a6a1f1dSLionel Sambuc         0x02, 0x03,             /* SHA-1/ECDSA */
943*0a6a1f1dSLionel Sambuc     };
944*0a6a1f1dSLionel Sambuc 
945*0a6a1f1dSLionel Sambuc     if (data >= (d + n - 2))
946*0a6a1f1dSLionel Sambuc         return;
947*0a6a1f1dSLionel Sambuc     data += 2;
948*0a6a1f1dSLionel Sambuc 
949*0a6a1f1dSLionel Sambuc     if (data > (d + n - 4))
950*0a6a1f1dSLionel Sambuc         return;
951*0a6a1f1dSLionel Sambuc     n2s(data, type);
952*0a6a1f1dSLionel Sambuc     n2s(data, size);
953*0a6a1f1dSLionel Sambuc 
954*0a6a1f1dSLionel Sambuc     if (type != TLSEXT_TYPE_server_name)
955*0a6a1f1dSLionel Sambuc         return;
956*0a6a1f1dSLionel Sambuc 
957*0a6a1f1dSLionel Sambuc     if (data + size > d + n)
958*0a6a1f1dSLionel Sambuc         return;
959*0a6a1f1dSLionel Sambuc     data += size;
960*0a6a1f1dSLionel Sambuc 
961*0a6a1f1dSLionel Sambuc     if (TLS1_get_client_version(s) >= TLS1_2_VERSION) {
962*0a6a1f1dSLionel Sambuc         const size_t len1 = sizeof(kSafariExtensionsBlock);
963*0a6a1f1dSLionel Sambuc         const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock);
964*0a6a1f1dSLionel Sambuc 
965*0a6a1f1dSLionel Sambuc         if (data + len1 + len2 != d + n)
966*0a6a1f1dSLionel Sambuc             return;
967*0a6a1f1dSLionel Sambuc         if (memcmp(data, kSafariExtensionsBlock, len1) != 0)
968*0a6a1f1dSLionel Sambuc             return;
969*0a6a1f1dSLionel Sambuc         if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0)
970*0a6a1f1dSLionel Sambuc             return;
971*0a6a1f1dSLionel Sambuc     } else {
972*0a6a1f1dSLionel Sambuc         const size_t len = sizeof(kSafariExtensionsBlock);
973*0a6a1f1dSLionel Sambuc 
974*0a6a1f1dSLionel Sambuc         if (data + len != d + n)
975*0a6a1f1dSLionel Sambuc             return;
976*0a6a1f1dSLionel Sambuc         if (memcmp(data, kSafariExtensionsBlock, len) != 0)
977*0a6a1f1dSLionel Sambuc             return;
978*0a6a1f1dSLionel Sambuc     }
979*0a6a1f1dSLionel Sambuc 
980*0a6a1f1dSLionel Sambuc     s->s3->is_probably_safari = 1;
981*0a6a1f1dSLionel Sambuc }
982*0a6a1f1dSLionel Sambuc # endif                         /* !OPENSSL_NO_EC */
983*0a6a1f1dSLionel Sambuc 
ssl_parse_clienthello_tlsext(SSL * s,unsigned char ** p,unsigned char * d,int n,int * al)984*0a6a1f1dSLionel Sambuc int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
985*0a6a1f1dSLionel Sambuc                                  int n, int *al)
986ebfedea0SLionel Sambuc {
987ebfedea0SLionel Sambuc     unsigned short type;
988ebfedea0SLionel Sambuc     unsigned short size;
989ebfedea0SLionel Sambuc     unsigned short len;
990ebfedea0SLionel Sambuc     unsigned char *data = *p;
991ebfedea0SLionel Sambuc     int renegotiate_seen = 0;
992ebfedea0SLionel Sambuc     int sigalg_seen = 0;
993ebfedea0SLionel Sambuc 
994ebfedea0SLionel Sambuc     s->servername_done = 0;
995ebfedea0SLionel Sambuc     s->tlsext_status_type = -1;
996ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
997ebfedea0SLionel Sambuc     s->s3->next_proto_neg_seen = 0;
998ebfedea0SLionel Sambuc # endif
999ebfedea0SLionel Sambuc 
1000ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
1001ebfedea0SLionel Sambuc     s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1002ebfedea0SLionel Sambuc                              SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1003ebfedea0SLionel Sambuc # endif
1004ebfedea0SLionel Sambuc 
1005*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_EC
1006*0a6a1f1dSLionel Sambuc     if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG)
1007*0a6a1f1dSLionel Sambuc         ssl_check_for_safari(s, data, d, n);
1008*0a6a1f1dSLionel Sambuc # endif                         /* !OPENSSL_NO_EC */
1009*0a6a1f1dSLionel Sambuc 
1010*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_SRP
1011*0a6a1f1dSLionel Sambuc     if (s->srp_ctx.login != NULL) {
1012*0a6a1f1dSLionel Sambuc         OPENSSL_free(s->srp_ctx.login);
1013*0a6a1f1dSLionel Sambuc         s->srp_ctx.login = NULL;
1014*0a6a1f1dSLionel Sambuc     }
1015*0a6a1f1dSLionel Sambuc # endif
1016*0a6a1f1dSLionel Sambuc 
1017*0a6a1f1dSLionel Sambuc     s->srtp_profile = NULL;
1018*0a6a1f1dSLionel Sambuc 
1019*0a6a1f1dSLionel Sambuc     if (data == d + n)
1020ebfedea0SLionel Sambuc         goto ri_check;
1021*0a6a1f1dSLionel Sambuc 
1022*0a6a1f1dSLionel Sambuc     if (data > (d + n - 2))
1023*0a6a1f1dSLionel Sambuc         goto err;
1024*0a6a1f1dSLionel Sambuc 
1025ebfedea0SLionel Sambuc     n2s(data, len);
1026ebfedea0SLionel Sambuc 
1027ebfedea0SLionel Sambuc     if (data > (d + n - len))
1028*0a6a1f1dSLionel Sambuc         goto err;
1029ebfedea0SLionel Sambuc 
1030*0a6a1f1dSLionel Sambuc     while (data <= (d + n - 4)) {
1031ebfedea0SLionel Sambuc         n2s(data, type);
1032ebfedea0SLionel Sambuc         n2s(data, size);
1033ebfedea0SLionel Sambuc 
1034ebfedea0SLionel Sambuc         if (data + size > (d + n))
1035*0a6a1f1dSLionel Sambuc             goto err;
1036ebfedea0SLionel Sambuc # if 0
1037ebfedea0SLionel Sambuc         fprintf(stderr, "Received extension type %d size %d\n", type, size);
1038ebfedea0SLionel Sambuc # endif
1039ebfedea0SLionel Sambuc         if (s->tlsext_debug_cb)
1040*0a6a1f1dSLionel Sambuc             s->tlsext_debug_cb(s, 0, type, data, size, s->tlsext_debug_arg);
1041*0a6a1f1dSLionel Sambuc /*-
1042*0a6a1f1dSLionel Sambuc  * The servername extension is treated as follows:
1043*0a6a1f1dSLionel Sambuc  *
1044*0a6a1f1dSLionel Sambuc  * - Only the hostname type is supported with a maximum length of 255.
1045*0a6a1f1dSLionel Sambuc  * - The servername is rejected if too long or if it contains zeros,
1046*0a6a1f1dSLionel Sambuc  *   in which case an fatal alert is generated.
1047*0a6a1f1dSLionel Sambuc  * - The servername field is maintained together with the session cache.
1048*0a6a1f1dSLionel Sambuc  * - When a session is resumed, the servername call back invoked in order
1049*0a6a1f1dSLionel Sambuc  *   to allow the application to position itself to the right context.
1050*0a6a1f1dSLionel Sambuc  * - The servername is acknowledged if it is new for a session or when
1051*0a6a1f1dSLionel Sambuc  *   it is identical to a previously used for the same session.
1052*0a6a1f1dSLionel Sambuc  *   Applications can control the behaviour.  They can at any time
1053*0a6a1f1dSLionel Sambuc  *   set a 'desirable' servername for a new SSL object. This can be the
1054*0a6a1f1dSLionel Sambuc  *   case for example with HTTPS when a Host: header field is received and
1055*0a6a1f1dSLionel Sambuc  *   a renegotiation is requested. In this case, a possible servername
1056*0a6a1f1dSLionel Sambuc  *   presented in the new client hello is only acknowledged if it matches
1057*0a6a1f1dSLionel Sambuc  *   the value of the Host: field.
1058*0a6a1f1dSLionel Sambuc  * - Applications must  use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
1059*0a6a1f1dSLionel Sambuc  *   if they provide for changing an explicit servername context for the
1060*0a6a1f1dSLionel Sambuc  *   session, i.e. when the session has been established with a servername
1061*0a6a1f1dSLionel Sambuc  *   extension.
1062*0a6a1f1dSLionel Sambuc  * - On session reconnect, the servername extension may be absent.
1063*0a6a1f1dSLionel Sambuc  *
1064ebfedea0SLionel Sambuc  */
1065ebfedea0SLionel Sambuc 
1066*0a6a1f1dSLionel Sambuc         if (type == TLSEXT_TYPE_server_name) {
1067ebfedea0SLionel Sambuc             unsigned char *sdata;
1068ebfedea0SLionel Sambuc             int servname_type;
1069ebfedea0SLionel Sambuc             int dsize;
1070ebfedea0SLionel Sambuc 
1071ebfedea0SLionel Sambuc             if (size < 2)
1072*0a6a1f1dSLionel Sambuc                 goto err;
1073ebfedea0SLionel Sambuc             n2s(data, dsize);
1074ebfedea0SLionel Sambuc             size -= 2;
1075ebfedea0SLionel Sambuc             if (dsize > size)
1076*0a6a1f1dSLionel Sambuc                 goto err;
1077ebfedea0SLionel Sambuc 
1078ebfedea0SLionel Sambuc             sdata = data;
1079*0a6a1f1dSLionel Sambuc             while (dsize > 3) {
1080ebfedea0SLionel Sambuc                 servname_type = *(sdata++);
1081ebfedea0SLionel Sambuc                 n2s(sdata, len);
1082ebfedea0SLionel Sambuc                 dsize -= 3;
1083ebfedea0SLionel Sambuc 
1084ebfedea0SLionel Sambuc                 if (len > dsize)
1085*0a6a1f1dSLionel Sambuc                     goto err;
1086*0a6a1f1dSLionel Sambuc 
1087ebfedea0SLionel Sambuc                 if (s->servername_done == 0)
1088*0a6a1f1dSLionel Sambuc                     switch (servname_type) {
1089ebfedea0SLionel Sambuc                     case TLSEXT_NAMETYPE_host_name:
1090*0a6a1f1dSLionel Sambuc                         if (!s->hit) {
1091ebfedea0SLionel Sambuc                             if (s->session->tlsext_hostname)
1092*0a6a1f1dSLionel Sambuc                                 goto err;
1093*0a6a1f1dSLionel Sambuc 
1094*0a6a1f1dSLionel Sambuc                             if (len > TLSEXT_MAXLEN_host_name) {
1095ebfedea0SLionel Sambuc                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
1096ebfedea0SLionel Sambuc                                 return 0;
1097ebfedea0SLionel Sambuc                             }
1098*0a6a1f1dSLionel Sambuc                             if ((s->session->tlsext_hostname =
1099*0a6a1f1dSLionel Sambuc                                  OPENSSL_malloc(len + 1)) == NULL) {
1100ebfedea0SLionel Sambuc                                 *al = TLS1_AD_INTERNAL_ERROR;
1101ebfedea0SLionel Sambuc                                 return 0;
1102ebfedea0SLionel Sambuc                             }
1103ebfedea0SLionel Sambuc                             memcpy(s->session->tlsext_hostname, sdata, len);
1104ebfedea0SLionel Sambuc                             s->session->tlsext_hostname[len] = '\0';
1105ebfedea0SLionel Sambuc                             if (strlen(s->session->tlsext_hostname) != len) {
1106ebfedea0SLionel Sambuc                                 OPENSSL_free(s->session->tlsext_hostname);
1107ebfedea0SLionel Sambuc                                 s->session->tlsext_hostname = NULL;
1108ebfedea0SLionel Sambuc                                 *al = TLS1_AD_UNRECOGNIZED_NAME;
1109ebfedea0SLionel Sambuc                                 return 0;
1110ebfedea0SLionel Sambuc                             }
1111ebfedea0SLionel Sambuc                             s->servername_done = 1;
1112ebfedea0SLionel Sambuc 
1113*0a6a1f1dSLionel Sambuc                         } else
1114ebfedea0SLionel Sambuc                             s->servername_done = s->session->tlsext_hostname
1115ebfedea0SLionel Sambuc                                 && strlen(s->session->tlsext_hostname) == len
1116*0a6a1f1dSLionel Sambuc                                 && strncmp(s->session->tlsext_hostname,
1117*0a6a1f1dSLionel Sambuc                                            (char *)sdata, len) == 0;
1118ebfedea0SLionel Sambuc 
1119ebfedea0SLionel Sambuc                         break;
1120ebfedea0SLionel Sambuc 
1121ebfedea0SLionel Sambuc                     default:
1122ebfedea0SLionel Sambuc                         break;
1123ebfedea0SLionel Sambuc                     }
1124ebfedea0SLionel Sambuc 
1125ebfedea0SLionel Sambuc                 dsize -= len;
1126ebfedea0SLionel Sambuc             }
1127ebfedea0SLionel Sambuc             if (dsize != 0)
1128*0a6a1f1dSLionel Sambuc                 goto err;
1129ebfedea0SLionel Sambuc 
1130ebfedea0SLionel Sambuc         }
1131ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRP
1132*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_srp) {
1133*0a6a1f1dSLionel Sambuc             if (size == 0 || ((len = data[0])) != (size - 1))
1134*0a6a1f1dSLionel Sambuc                 goto err;
1135ebfedea0SLionel Sambuc             if (s->srp_ctx.login != NULL)
1136*0a6a1f1dSLionel Sambuc                 goto err;
1137ebfedea0SLionel Sambuc             if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL)
1138ebfedea0SLionel Sambuc                 return -1;
1139ebfedea0SLionel Sambuc             memcpy(s->srp_ctx.login, &data[1], len);
1140ebfedea0SLionel Sambuc             s->srp_ctx.login[len] = '\0';
1141ebfedea0SLionel Sambuc 
1142ebfedea0SLionel Sambuc             if (strlen(s->srp_ctx.login) != len)
1143*0a6a1f1dSLionel Sambuc                 goto err;
1144ebfedea0SLionel Sambuc         }
1145ebfedea0SLionel Sambuc # endif
1146ebfedea0SLionel Sambuc 
1147ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
1148*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_ec_point_formats) {
1149ebfedea0SLionel Sambuc             unsigned char *sdata = data;
1150ebfedea0SLionel Sambuc             int ecpointformatlist_length = *(sdata++);
1151ebfedea0SLionel Sambuc 
1152ebfedea0SLionel Sambuc             if (ecpointformatlist_length != size - 1)
1153*0a6a1f1dSLionel Sambuc                 goto err;
1154*0a6a1f1dSLionel Sambuc             if (!s->hit) {
1155*0a6a1f1dSLionel Sambuc                 if (s->session->tlsext_ecpointformatlist) {
1156ebfedea0SLionel Sambuc                     OPENSSL_free(s->session->tlsext_ecpointformatlist);
1157ebfedea0SLionel Sambuc                     s->session->tlsext_ecpointformatlist = NULL;
1158ebfedea0SLionel Sambuc                 }
1159ebfedea0SLionel Sambuc                 s->session->tlsext_ecpointformatlist_length = 0;
1160*0a6a1f1dSLionel Sambuc                 if ((s->session->tlsext_ecpointformatlist =
1161*0a6a1f1dSLionel Sambuc                      OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
1162ebfedea0SLionel Sambuc                     *al = TLS1_AD_INTERNAL_ERROR;
1163ebfedea0SLionel Sambuc                     return 0;
1164ebfedea0SLionel Sambuc                 }
1165*0a6a1f1dSLionel Sambuc                 s->session->tlsext_ecpointformatlist_length =
1166*0a6a1f1dSLionel Sambuc                     ecpointformatlist_length;
1167*0a6a1f1dSLionel Sambuc                 memcpy(s->session->tlsext_ecpointformatlist, sdata,
1168*0a6a1f1dSLionel Sambuc                        ecpointformatlist_length);
1169ebfedea0SLionel Sambuc             }
1170ebfedea0SLionel Sambuc #  if 0
1171*0a6a1f1dSLionel Sambuc             fprintf(stderr,
1172*0a6a1f1dSLionel Sambuc                     "ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ",
1173*0a6a1f1dSLionel Sambuc                     s->session->tlsext_ecpointformatlist_length);
1174ebfedea0SLionel Sambuc             sdata = s->session->tlsext_ecpointformatlist;
1175ebfedea0SLionel Sambuc             for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1176ebfedea0SLionel Sambuc                 fprintf(stderr, "%i ", *(sdata++));
1177ebfedea0SLionel Sambuc             fprintf(stderr, "\n");
1178ebfedea0SLionel Sambuc #  endif
1179*0a6a1f1dSLionel Sambuc         } else if (type == TLSEXT_TYPE_elliptic_curves) {
1180ebfedea0SLionel Sambuc             unsigned char *sdata = data;
1181ebfedea0SLionel Sambuc             int ellipticcurvelist_length = (*(sdata++) << 8);
1182ebfedea0SLionel Sambuc             ellipticcurvelist_length += (*(sdata++));
1183ebfedea0SLionel Sambuc 
1184ebfedea0SLionel Sambuc             if (ellipticcurvelist_length != size - 2 ||
1185*0a6a1f1dSLionel Sambuc                 ellipticcurvelist_length < 1 ||
1186*0a6a1f1dSLionel Sambuc                 /* Each NamedCurve is 2 bytes. */
1187*0a6a1f1dSLionel Sambuc                 ellipticcurvelist_length & 1)
1188*0a6a1f1dSLionel Sambuc                     goto err;
1189*0a6a1f1dSLionel Sambuc 
1190*0a6a1f1dSLionel Sambuc             if (!s->hit) {
1191ebfedea0SLionel Sambuc                 if (s->session->tlsext_ellipticcurvelist)
1192*0a6a1f1dSLionel Sambuc                     goto err;
1193*0a6a1f1dSLionel Sambuc 
1194ebfedea0SLionel Sambuc                 s->session->tlsext_ellipticcurvelist_length = 0;
1195*0a6a1f1dSLionel Sambuc                 if ((s->session->tlsext_ellipticcurvelist =
1196*0a6a1f1dSLionel Sambuc                      OPENSSL_malloc(ellipticcurvelist_length)) == NULL) {
1197ebfedea0SLionel Sambuc                     *al = TLS1_AD_INTERNAL_ERROR;
1198ebfedea0SLionel Sambuc                     return 0;
1199ebfedea0SLionel Sambuc                 }
1200*0a6a1f1dSLionel Sambuc                 s->session->tlsext_ellipticcurvelist_length =
1201*0a6a1f1dSLionel Sambuc                     ellipticcurvelist_length;
1202*0a6a1f1dSLionel Sambuc                 memcpy(s->session->tlsext_ellipticcurvelist, sdata,
1203*0a6a1f1dSLionel Sambuc                        ellipticcurvelist_length);
1204ebfedea0SLionel Sambuc             }
1205ebfedea0SLionel Sambuc #  if 0
1206*0a6a1f1dSLionel Sambuc             fprintf(stderr,
1207*0a6a1f1dSLionel Sambuc                     "ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ",
1208*0a6a1f1dSLionel Sambuc                     s->session->tlsext_ellipticcurvelist_length);
1209ebfedea0SLionel Sambuc             sdata = s->session->tlsext_ellipticcurvelist;
1210ebfedea0SLionel Sambuc             for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++)
1211ebfedea0SLionel Sambuc                 fprintf(stderr, "%i ", *(sdata++));
1212ebfedea0SLionel Sambuc             fprintf(stderr, "\n");
1213ebfedea0SLionel Sambuc #  endif
1214ebfedea0SLionel Sambuc         }
1215ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
1216ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
1217ebfedea0SLionel Sambuc         else if (type == TLSEXT_TYPE_opaque_prf_input &&
1218*0a6a1f1dSLionel Sambuc                  s->version != DTLS1_VERSION) {
1219ebfedea0SLionel Sambuc             unsigned char *sdata = data;
1220ebfedea0SLionel Sambuc 
1221*0a6a1f1dSLionel Sambuc             if (size < 2) {
1222ebfedea0SLionel Sambuc                 *al = SSL_AD_DECODE_ERROR;
1223ebfedea0SLionel Sambuc                 return 0;
1224ebfedea0SLionel Sambuc             }
1225ebfedea0SLionel Sambuc             n2s(sdata, s->s3->client_opaque_prf_input_len);
1226*0a6a1f1dSLionel Sambuc             if (s->s3->client_opaque_prf_input_len != size - 2) {
1227ebfedea0SLionel Sambuc                 *al = SSL_AD_DECODE_ERROR;
1228ebfedea0SLionel Sambuc                 return 0;
1229ebfedea0SLionel Sambuc             }
1230ebfedea0SLionel Sambuc 
1231*0a6a1f1dSLionel Sambuc             if (s->s3->client_opaque_prf_input != NULL) {
1232*0a6a1f1dSLionel Sambuc                 /* shouldn't really happen */
1233ebfedea0SLionel Sambuc                 OPENSSL_free(s->s3->client_opaque_prf_input);
1234*0a6a1f1dSLionel Sambuc             }
1235*0a6a1f1dSLionel Sambuc 
1236*0a6a1f1dSLionel Sambuc             /* dummy byte just to get non-NULL */
1237ebfedea0SLionel Sambuc             if (s->s3->client_opaque_prf_input_len == 0)
1238*0a6a1f1dSLionel Sambuc                 s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
1239ebfedea0SLionel Sambuc             else
1240*0a6a1f1dSLionel Sambuc                 s->s3->client_opaque_prf_input =
1241*0a6a1f1dSLionel Sambuc                     BUF_memdup(sdata, s->s3->client_opaque_prf_input_len);
1242*0a6a1f1dSLionel Sambuc             if (s->s3->client_opaque_prf_input == NULL) {
1243ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1244ebfedea0SLionel Sambuc                 return 0;
1245ebfedea0SLionel Sambuc             }
1246ebfedea0SLionel Sambuc         }
1247ebfedea0SLionel Sambuc # endif
1248*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_session_ticket) {
1249ebfedea0SLionel Sambuc             if (s->tls_session_ticket_ext_cb &&
1250*0a6a1f1dSLionel Sambuc                 !s->tls_session_ticket_ext_cb(s, data, size,
1251*0a6a1f1dSLionel Sambuc                                               s->tls_session_ticket_ext_cb_arg))
1252ebfedea0SLionel Sambuc             {
1253ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1254ebfedea0SLionel Sambuc                 return 0;
1255ebfedea0SLionel Sambuc             }
1256*0a6a1f1dSLionel Sambuc         } else if (type == TLSEXT_TYPE_renegotiate) {
1257ebfedea0SLionel Sambuc             if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al))
1258ebfedea0SLionel Sambuc                 return 0;
1259ebfedea0SLionel Sambuc             renegotiate_seen = 1;
1260*0a6a1f1dSLionel Sambuc         } else if (type == TLSEXT_TYPE_signature_algorithms) {
1261ebfedea0SLionel Sambuc             int dsize;
1262ebfedea0SLionel Sambuc             if (sigalg_seen || size < 2)
1263*0a6a1f1dSLionel Sambuc                 goto err;
1264ebfedea0SLionel Sambuc             sigalg_seen = 1;
1265ebfedea0SLionel Sambuc             n2s(data, dsize);
1266ebfedea0SLionel Sambuc             size -= 2;
1267ebfedea0SLionel Sambuc             if (dsize != size || dsize & 1)
1268*0a6a1f1dSLionel Sambuc                 goto err;
1269ebfedea0SLionel Sambuc             if (!tls1_process_sigalgs(s, data, dsize))
1270*0a6a1f1dSLionel Sambuc                 goto err;
1271*0a6a1f1dSLionel Sambuc         } else if (type == TLSEXT_TYPE_status_request &&
1272*0a6a1f1dSLionel Sambuc                    s->version != DTLS1_VERSION) {
1273ebfedea0SLionel Sambuc 
1274ebfedea0SLionel Sambuc             if (size < 5)
1275*0a6a1f1dSLionel Sambuc                 goto err;
1276ebfedea0SLionel Sambuc 
1277ebfedea0SLionel Sambuc             s->tlsext_status_type = *data++;
1278ebfedea0SLionel Sambuc             size--;
1279*0a6a1f1dSLionel Sambuc             if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) {
1280ebfedea0SLionel Sambuc                 const unsigned char *sdata;
1281ebfedea0SLionel Sambuc                 int dsize;
1282ebfedea0SLionel Sambuc                 /* Read in responder_id_list */
1283ebfedea0SLionel Sambuc                 n2s(data, dsize);
1284ebfedea0SLionel Sambuc                 size -= 2;
1285ebfedea0SLionel Sambuc                 if (dsize > size)
1286*0a6a1f1dSLionel Sambuc                     goto err;
1287*0a6a1f1dSLionel Sambuc                 while (dsize > 0) {
1288ebfedea0SLionel Sambuc                     OCSP_RESPID *id;
1289ebfedea0SLionel Sambuc                     int idsize;
1290ebfedea0SLionel Sambuc                     if (dsize < 4)
1291*0a6a1f1dSLionel Sambuc                         goto err;
1292ebfedea0SLionel Sambuc                     n2s(data, idsize);
1293ebfedea0SLionel Sambuc                     dsize -= 2 + idsize;
1294ebfedea0SLionel Sambuc                     size -= 2 + idsize;
1295ebfedea0SLionel Sambuc                     if (dsize < 0)
1296*0a6a1f1dSLionel Sambuc                         goto err;
1297ebfedea0SLionel Sambuc                     sdata = data;
1298ebfedea0SLionel Sambuc                     data += idsize;
1299*0a6a1f1dSLionel Sambuc                     id = d2i_OCSP_RESPID(NULL, &sdata, idsize);
1300ebfedea0SLionel Sambuc                     if (!id)
1301*0a6a1f1dSLionel Sambuc                         goto err;
1302*0a6a1f1dSLionel Sambuc                     if (data != sdata) {
1303ebfedea0SLionel Sambuc                         OCSP_RESPID_free(id);
1304*0a6a1f1dSLionel Sambuc                         goto err;
1305ebfedea0SLionel Sambuc                     }
1306ebfedea0SLionel Sambuc                     if (!s->tlsext_ocsp_ids
1307ebfedea0SLionel Sambuc                         && !(s->tlsext_ocsp_ids =
1308*0a6a1f1dSLionel Sambuc                              sk_OCSP_RESPID_new_null())) {
1309ebfedea0SLionel Sambuc                         OCSP_RESPID_free(id);
1310ebfedea0SLionel Sambuc                         *al = SSL_AD_INTERNAL_ERROR;
1311ebfedea0SLionel Sambuc                         return 0;
1312ebfedea0SLionel Sambuc                     }
1313*0a6a1f1dSLionel Sambuc                     if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) {
1314ebfedea0SLionel Sambuc                         OCSP_RESPID_free(id);
1315ebfedea0SLionel Sambuc                         *al = SSL_AD_INTERNAL_ERROR;
1316ebfedea0SLionel Sambuc                         return 0;
1317ebfedea0SLionel Sambuc                     }
1318ebfedea0SLionel Sambuc                 }
1319ebfedea0SLionel Sambuc 
1320ebfedea0SLionel Sambuc                 /* Read in request_extensions */
1321ebfedea0SLionel Sambuc                 if (size < 2)
1322*0a6a1f1dSLionel Sambuc                     goto err;
1323ebfedea0SLionel Sambuc                 n2s(data, dsize);
1324ebfedea0SLionel Sambuc                 size -= 2;
1325ebfedea0SLionel Sambuc                 if (dsize != size)
1326*0a6a1f1dSLionel Sambuc                     goto err;
1327ebfedea0SLionel Sambuc                 sdata = data;
1328*0a6a1f1dSLionel Sambuc                 if (dsize > 0) {
1329*0a6a1f1dSLionel Sambuc                     if (s->tlsext_ocsp_exts) {
1330ebfedea0SLionel Sambuc                         sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
1331ebfedea0SLionel Sambuc                                                    X509_EXTENSION_free);
1332ebfedea0SLionel Sambuc                     }
1333ebfedea0SLionel Sambuc 
1334ebfedea0SLionel Sambuc                     s->tlsext_ocsp_exts =
1335*0a6a1f1dSLionel Sambuc                         d2i_X509_EXTENSIONS(NULL, &sdata, dsize);
1336*0a6a1f1dSLionel Sambuc                     if (!s->tlsext_ocsp_exts || (data + dsize != sdata))
1337*0a6a1f1dSLionel Sambuc                         goto err;
1338ebfedea0SLionel Sambuc                 }
1339ebfedea0SLionel Sambuc             }
1340*0a6a1f1dSLionel Sambuc             /*
1341*0a6a1f1dSLionel Sambuc              * We don't know what to do with any other type * so ignore it.
1342ebfedea0SLionel Sambuc              */
1343ebfedea0SLionel Sambuc             else
1344ebfedea0SLionel Sambuc                 s->tlsext_status_type = -1;
1345ebfedea0SLionel Sambuc         }
1346ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
1347*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_heartbeat) {
1348*0a6a1f1dSLionel Sambuc             switch (data[0]) {
1349ebfedea0SLionel Sambuc             case 0x01:         /* Client allows us to send HB requests */
1350ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1351ebfedea0SLionel Sambuc                 break;
1352ebfedea0SLionel Sambuc             case 0x02:         /* Client doesn't accept HB requests */
1353ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1354ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1355ebfedea0SLionel Sambuc                 break;
1356*0a6a1f1dSLionel Sambuc             default:
1357*0a6a1f1dSLionel Sambuc                 *al = SSL_AD_ILLEGAL_PARAMETER;
1358ebfedea0SLionel Sambuc                 return 0;
1359ebfedea0SLionel Sambuc             }
1360ebfedea0SLionel Sambuc         }
1361ebfedea0SLionel Sambuc # endif
1362ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1363ebfedea0SLionel Sambuc         else if (type == TLSEXT_TYPE_next_proto_neg &&
1364*0a6a1f1dSLionel Sambuc                  s->s3->tmp.finish_md_len == 0) {
1365*0a6a1f1dSLionel Sambuc             /*-
1366*0a6a1f1dSLionel Sambuc              * We shouldn't accept this extension on a
1367ebfedea0SLionel Sambuc              * renegotiation.
1368ebfedea0SLionel Sambuc              *
1369ebfedea0SLionel Sambuc              * s->new_session will be set on renegotiation, but we
1370ebfedea0SLionel Sambuc              * probably shouldn't rely that it couldn't be set on
1371ebfedea0SLionel Sambuc              * the initial renegotation too in certain cases (when
1372ebfedea0SLionel Sambuc              * there's some other reason to disallow resuming an
1373ebfedea0SLionel Sambuc              * earlier session -- the current code won't be doing
1374ebfedea0SLionel Sambuc              * anything like that, but this might change).
1375*0a6a1f1dSLionel Sambuc              *
1376ebfedea0SLionel Sambuc              * A valid sign that there's been a previous handshake
1377ebfedea0SLionel Sambuc              * in this connection is if s->s3->tmp.finish_md_len >
1378ebfedea0SLionel Sambuc              * 0.  (We are talking about a check that will happen
1379ebfedea0SLionel Sambuc              * in the Hello protocol round, well before a new
1380*0a6a1f1dSLionel Sambuc              * Finished message could have been computed.)
1381*0a6a1f1dSLionel Sambuc              */
1382ebfedea0SLionel Sambuc             s->s3->next_proto_neg_seen = 1;
1383ebfedea0SLionel Sambuc         }
1384ebfedea0SLionel Sambuc # endif
1385ebfedea0SLionel Sambuc 
1386ebfedea0SLionel Sambuc         /* session ticket processed earlier */
1387ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRTP
1388*0a6a1f1dSLionel Sambuc         else if (SSL_IS_DTLS(s) && SSL_get_srtp_profiles(s)
1389*0a6a1f1dSLionel Sambuc                  && type == TLSEXT_TYPE_use_srtp) {
1390*0a6a1f1dSLionel Sambuc             if (ssl_parse_clienthello_use_srtp_ext(s, data, size, al))
1391ebfedea0SLionel Sambuc                 return 0;
1392ebfedea0SLionel Sambuc         }
1393ebfedea0SLionel Sambuc # endif
1394ebfedea0SLionel Sambuc 
1395ebfedea0SLionel Sambuc         data += size;
1396ebfedea0SLionel Sambuc     }
1397ebfedea0SLionel Sambuc 
1398*0a6a1f1dSLionel Sambuc     /* Spurious data on the end */
1399*0a6a1f1dSLionel Sambuc     if (data != d + n)
1400*0a6a1f1dSLionel Sambuc         goto err;
1401*0a6a1f1dSLionel Sambuc 
1402ebfedea0SLionel Sambuc     *p = data;
1403ebfedea0SLionel Sambuc 
1404ebfedea0SLionel Sambuc  ri_check:
1405ebfedea0SLionel Sambuc 
1406ebfedea0SLionel Sambuc     /* Need RI if renegotiating */
1407ebfedea0SLionel Sambuc 
1408ebfedea0SLionel Sambuc     if (!renegotiate_seen && s->renegotiate &&
1409*0a6a1f1dSLionel Sambuc         !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1410ebfedea0SLionel Sambuc         *al = SSL_AD_HANDSHAKE_FAILURE;
1411ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT,
1412ebfedea0SLionel Sambuc                SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1413ebfedea0SLionel Sambuc         return 0;
1414ebfedea0SLionel Sambuc     }
1415ebfedea0SLionel Sambuc 
1416ebfedea0SLionel Sambuc     return 1;
1417*0a6a1f1dSLionel Sambuc err:
1418*0a6a1f1dSLionel Sambuc     *al = SSL_AD_DECODE_ERROR;
1419*0a6a1f1dSLionel Sambuc     return 0;
1420ebfedea0SLionel Sambuc }
1421ebfedea0SLionel Sambuc 
1422ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1423*0a6a1f1dSLionel Sambuc /*
1424*0a6a1f1dSLionel Sambuc  * ssl_next_proto_validate validates a Next Protocol Negotiation block. No
1425*0a6a1f1dSLionel Sambuc  * elements of zero length are allowed and the set of elements must exactly
1426*0a6a1f1dSLionel Sambuc  * fill the length of the block.
1427*0a6a1f1dSLionel Sambuc  */
ssl_next_proto_validate(unsigned char * d,unsigned len)1428ebfedea0SLionel Sambuc static char ssl_next_proto_validate(unsigned char *d, unsigned len)
1429ebfedea0SLionel Sambuc {
1430ebfedea0SLionel Sambuc     unsigned int off = 0;
1431ebfedea0SLionel Sambuc 
1432*0a6a1f1dSLionel Sambuc     while (off < len) {
1433ebfedea0SLionel Sambuc         if (d[off] == 0)
1434ebfedea0SLionel Sambuc             return 0;
1435ebfedea0SLionel Sambuc         off += d[off];
1436ebfedea0SLionel Sambuc         off++;
1437ebfedea0SLionel Sambuc     }
1438ebfedea0SLionel Sambuc 
1439ebfedea0SLionel Sambuc     return off == len;
1440ebfedea0SLionel Sambuc }
1441ebfedea0SLionel Sambuc # endif
1442ebfedea0SLionel Sambuc 
ssl_parse_serverhello_tlsext(SSL * s,unsigned char ** p,unsigned char * d,int n,int * al)1443*0a6a1f1dSLionel Sambuc int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d,
1444*0a6a1f1dSLionel Sambuc                                  int n, int *al)
1445ebfedea0SLionel Sambuc {
1446ebfedea0SLionel Sambuc     unsigned short length;
1447ebfedea0SLionel Sambuc     unsigned short type;
1448ebfedea0SLionel Sambuc     unsigned short size;
1449ebfedea0SLionel Sambuc     unsigned char *data = *p;
1450ebfedea0SLionel Sambuc     int tlsext_servername = 0;
1451ebfedea0SLionel Sambuc     int renegotiate_seen = 0;
1452ebfedea0SLionel Sambuc 
1453ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1454ebfedea0SLionel Sambuc     s->s3->next_proto_neg_seen = 0;
1455ebfedea0SLionel Sambuc # endif
1456*0a6a1f1dSLionel Sambuc     s->tlsext_ticket_expected = 0;
1457ebfedea0SLionel Sambuc 
1458ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
1459ebfedea0SLionel Sambuc     s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED |
1460ebfedea0SLionel Sambuc                              SSL_TLSEXT_HB_DONT_SEND_REQUESTS);
1461ebfedea0SLionel Sambuc # endif
1462ebfedea0SLionel Sambuc 
1463ebfedea0SLionel Sambuc     if (data >= (d + n - 2))
1464ebfedea0SLionel Sambuc         goto ri_check;
1465ebfedea0SLionel Sambuc 
1466ebfedea0SLionel Sambuc     n2s(data, length);
1467*0a6a1f1dSLionel Sambuc     if (data + length != d + n) {
1468ebfedea0SLionel Sambuc         *al = SSL_AD_DECODE_ERROR;
1469ebfedea0SLionel Sambuc         return 0;
1470ebfedea0SLionel Sambuc     }
1471ebfedea0SLionel Sambuc 
1472*0a6a1f1dSLionel Sambuc     while (data <= (d + n - 4)) {
1473ebfedea0SLionel Sambuc         n2s(data, type);
1474ebfedea0SLionel Sambuc         n2s(data, size);
1475ebfedea0SLionel Sambuc 
1476ebfedea0SLionel Sambuc         if (data + size > (d + n))
1477ebfedea0SLionel Sambuc             goto ri_check;
1478ebfedea0SLionel Sambuc 
1479ebfedea0SLionel Sambuc         if (s->tlsext_debug_cb)
1480*0a6a1f1dSLionel Sambuc             s->tlsext_debug_cb(s, 1, type, data, size, s->tlsext_debug_arg);
1481ebfedea0SLionel Sambuc 
1482*0a6a1f1dSLionel Sambuc         if (type == TLSEXT_TYPE_server_name) {
1483*0a6a1f1dSLionel Sambuc             if (s->tlsext_hostname == NULL || size > 0) {
1484ebfedea0SLionel Sambuc                 *al = TLS1_AD_UNRECOGNIZED_NAME;
1485ebfedea0SLionel Sambuc                 return 0;
1486ebfedea0SLionel Sambuc             }
1487ebfedea0SLionel Sambuc             tlsext_servername = 1;
1488ebfedea0SLionel Sambuc         }
1489ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
1490*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_ec_point_formats) {
1491ebfedea0SLionel Sambuc             unsigned char *sdata = data;
1492ebfedea0SLionel Sambuc             int ecpointformatlist_length = *(sdata++);
1493ebfedea0SLionel Sambuc 
1494ebfedea0SLionel Sambuc             if (ecpointformatlist_length != size - 1 ||
1495*0a6a1f1dSLionel Sambuc                 ecpointformatlist_length < 1) {
1496ebfedea0SLionel Sambuc                 *al = TLS1_AD_DECODE_ERROR;
1497ebfedea0SLionel Sambuc                 return 0;
1498ebfedea0SLionel Sambuc             }
1499*0a6a1f1dSLionel Sambuc             if (!s->hit) {
1500ebfedea0SLionel Sambuc                 s->session->tlsext_ecpointformatlist_length = 0;
1501*0a6a1f1dSLionel Sambuc                 if (s->session->tlsext_ecpointformatlist != NULL)
1502*0a6a1f1dSLionel Sambuc                     OPENSSL_free(s->session->tlsext_ecpointformatlist);
1503*0a6a1f1dSLionel Sambuc                 if ((s->session->tlsext_ecpointformatlist =
1504*0a6a1f1dSLionel Sambuc                      OPENSSL_malloc(ecpointformatlist_length)) == NULL) {
1505ebfedea0SLionel Sambuc                     *al = TLS1_AD_INTERNAL_ERROR;
1506ebfedea0SLionel Sambuc                     return 0;
1507ebfedea0SLionel Sambuc                 }
1508*0a6a1f1dSLionel Sambuc                 s->session->tlsext_ecpointformatlist_length =
1509*0a6a1f1dSLionel Sambuc                     ecpointformatlist_length;
1510*0a6a1f1dSLionel Sambuc                 memcpy(s->session->tlsext_ecpointformatlist, sdata,
1511*0a6a1f1dSLionel Sambuc                        ecpointformatlist_length);
1512*0a6a1f1dSLionel Sambuc             }
1513ebfedea0SLionel Sambuc #  if 0
1514*0a6a1f1dSLionel Sambuc             fprintf(stderr,
1515*0a6a1f1dSLionel Sambuc                     "ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist ");
1516ebfedea0SLionel Sambuc             sdata = s->session->tlsext_ecpointformatlist;
1517ebfedea0SLionel Sambuc             for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++)
1518ebfedea0SLionel Sambuc                 fprintf(stderr, "%i ", *(sdata++));
1519ebfedea0SLionel Sambuc             fprintf(stderr, "\n");
1520ebfedea0SLionel Sambuc #  endif
1521ebfedea0SLionel Sambuc         }
1522ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
1523ebfedea0SLionel Sambuc 
1524*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_session_ticket) {
1525ebfedea0SLionel Sambuc             if (s->tls_session_ticket_ext_cb &&
1526*0a6a1f1dSLionel Sambuc                 !s->tls_session_ticket_ext_cb(s, data, size,
1527*0a6a1f1dSLionel Sambuc                                               s->tls_session_ticket_ext_cb_arg))
1528ebfedea0SLionel Sambuc             {
1529ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1530ebfedea0SLionel Sambuc                 return 0;
1531ebfedea0SLionel Sambuc             }
1532ebfedea0SLionel Sambuc             if ((SSL_get_options(s) & SSL_OP_NO_TICKET)
1533*0a6a1f1dSLionel Sambuc                 || (size > 0)) {
1534ebfedea0SLionel Sambuc                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1535ebfedea0SLionel Sambuc                 return 0;
1536ebfedea0SLionel Sambuc             }
1537ebfedea0SLionel Sambuc             s->tlsext_ticket_expected = 1;
1538ebfedea0SLionel Sambuc         }
1539ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
1540ebfedea0SLionel Sambuc         else if (type == TLSEXT_TYPE_opaque_prf_input &&
1541*0a6a1f1dSLionel Sambuc                  s->version != DTLS1_VERSION) {
1542ebfedea0SLionel Sambuc             unsigned char *sdata = data;
1543ebfedea0SLionel Sambuc 
1544*0a6a1f1dSLionel Sambuc             if (size < 2) {
1545ebfedea0SLionel Sambuc                 *al = SSL_AD_DECODE_ERROR;
1546ebfedea0SLionel Sambuc                 return 0;
1547ebfedea0SLionel Sambuc             }
1548ebfedea0SLionel Sambuc             n2s(sdata, s->s3->server_opaque_prf_input_len);
1549*0a6a1f1dSLionel Sambuc             if (s->s3->server_opaque_prf_input_len != size - 2) {
1550ebfedea0SLionel Sambuc                 *al = SSL_AD_DECODE_ERROR;
1551ebfedea0SLionel Sambuc                 return 0;
1552ebfedea0SLionel Sambuc             }
1553ebfedea0SLionel Sambuc 
1554*0a6a1f1dSLionel Sambuc             if (s->s3->server_opaque_prf_input != NULL) {
1555*0a6a1f1dSLionel Sambuc                 /* shouldn't really happen */
1556ebfedea0SLionel Sambuc                 OPENSSL_free(s->s3->server_opaque_prf_input);
1557*0a6a1f1dSLionel Sambuc             }
1558*0a6a1f1dSLionel Sambuc             if (s->s3->server_opaque_prf_input_len == 0) {
1559*0a6a1f1dSLionel Sambuc                 /* dummy byte just to get non-NULL */
1560*0a6a1f1dSLionel Sambuc                 s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
1561*0a6a1f1dSLionel Sambuc             } else {
1562*0a6a1f1dSLionel Sambuc                 s->s3->server_opaque_prf_input =
1563*0a6a1f1dSLionel Sambuc                     BUF_memdup(sdata, s->s3->server_opaque_prf_input_len);
1564*0a6a1f1dSLionel Sambuc             }
1565ebfedea0SLionel Sambuc 
1566*0a6a1f1dSLionel Sambuc             if (s->s3->server_opaque_prf_input == NULL) {
1567ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1568ebfedea0SLionel Sambuc                 return 0;
1569ebfedea0SLionel Sambuc             }
1570ebfedea0SLionel Sambuc         }
1571ebfedea0SLionel Sambuc # endif
1572ebfedea0SLionel Sambuc         else if (type == TLSEXT_TYPE_status_request &&
1573*0a6a1f1dSLionel Sambuc                  s->version != DTLS1_VERSION) {
1574*0a6a1f1dSLionel Sambuc             /*
1575*0a6a1f1dSLionel Sambuc              * MUST be empty and only sent if we've requested a status
1576*0a6a1f1dSLionel Sambuc              * request message.
1577ebfedea0SLionel Sambuc              */
1578*0a6a1f1dSLionel Sambuc             if ((s->tlsext_status_type == -1) || (size > 0)) {
1579ebfedea0SLionel Sambuc                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1580ebfedea0SLionel Sambuc                 return 0;
1581ebfedea0SLionel Sambuc             }
1582ebfedea0SLionel Sambuc             /* Set flag to expect CertificateStatus message */
1583ebfedea0SLionel Sambuc             s->tlsext_status_expected = 1;
1584ebfedea0SLionel Sambuc         }
1585ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1586ebfedea0SLionel Sambuc         else if (type == TLSEXT_TYPE_next_proto_neg &&
1587*0a6a1f1dSLionel Sambuc                  s->s3->tmp.finish_md_len == 0) {
1588ebfedea0SLionel Sambuc             unsigned char *selected;
1589ebfedea0SLionel Sambuc             unsigned char selected_len;
1590ebfedea0SLionel Sambuc 
1591ebfedea0SLionel Sambuc             /* We must have requested it. */
1592*0a6a1f1dSLionel Sambuc             if (s->ctx->next_proto_select_cb == NULL) {
1593ebfedea0SLionel Sambuc                 *al = TLS1_AD_UNSUPPORTED_EXTENSION;
1594ebfedea0SLionel Sambuc                 return 0;
1595ebfedea0SLionel Sambuc             }
1596ebfedea0SLionel Sambuc             /* The data must be valid */
1597*0a6a1f1dSLionel Sambuc             if (!ssl_next_proto_validate(data, size)) {
1598ebfedea0SLionel Sambuc                 *al = TLS1_AD_DECODE_ERROR;
1599ebfedea0SLionel Sambuc                 return 0;
1600ebfedea0SLionel Sambuc             }
1601*0a6a1f1dSLionel Sambuc             if (s->
1602*0a6a1f1dSLionel Sambuc                 ctx->next_proto_select_cb(s, &selected, &selected_len, data,
1603*0a6a1f1dSLionel Sambuc                                           size,
1604*0a6a1f1dSLionel Sambuc                                           s->ctx->next_proto_select_cb_arg) !=
1605*0a6a1f1dSLionel Sambuc                 SSL_TLSEXT_ERR_OK) {
1606ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1607ebfedea0SLionel Sambuc                 return 0;
1608ebfedea0SLionel Sambuc             }
1609ebfedea0SLionel Sambuc             s->next_proto_negotiated = OPENSSL_malloc(selected_len);
1610*0a6a1f1dSLionel Sambuc             if (!s->next_proto_negotiated) {
1611ebfedea0SLionel Sambuc                 *al = TLS1_AD_INTERNAL_ERROR;
1612ebfedea0SLionel Sambuc                 return 0;
1613ebfedea0SLionel Sambuc             }
1614ebfedea0SLionel Sambuc             memcpy(s->next_proto_negotiated, selected, selected_len);
1615ebfedea0SLionel Sambuc             s->next_proto_negotiated_len = selected_len;
1616ebfedea0SLionel Sambuc             s->s3->next_proto_neg_seen = 1;
1617ebfedea0SLionel Sambuc         }
1618ebfedea0SLionel Sambuc # endif
1619*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_renegotiate) {
1620ebfedea0SLionel Sambuc             if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al))
1621ebfedea0SLionel Sambuc                 return 0;
1622ebfedea0SLionel Sambuc             renegotiate_seen = 1;
1623ebfedea0SLionel Sambuc         }
1624ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_HEARTBEATS
1625*0a6a1f1dSLionel Sambuc         else if (type == TLSEXT_TYPE_heartbeat) {
1626*0a6a1f1dSLionel Sambuc             switch (data[0]) {
1627ebfedea0SLionel Sambuc             case 0x01:         /* Server allows us to send HB requests */
1628ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1629ebfedea0SLionel Sambuc                 break;
1630ebfedea0SLionel Sambuc             case 0x02:         /* Server doesn't accept HB requests */
1631ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED;
1632ebfedea0SLionel Sambuc                 s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS;
1633ebfedea0SLionel Sambuc                 break;
1634*0a6a1f1dSLionel Sambuc             default:
1635*0a6a1f1dSLionel Sambuc                 *al = SSL_AD_ILLEGAL_PARAMETER;
1636ebfedea0SLionel Sambuc                 return 0;
1637ebfedea0SLionel Sambuc             }
1638ebfedea0SLionel Sambuc         }
1639ebfedea0SLionel Sambuc # endif
1640ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SRTP
1641*0a6a1f1dSLionel Sambuc         else if (SSL_IS_DTLS(s) && type == TLSEXT_TYPE_use_srtp) {
1642*0a6a1f1dSLionel Sambuc             if (ssl_parse_serverhello_use_srtp_ext(s, data, size, al))
1643ebfedea0SLionel Sambuc                 return 0;
1644ebfedea0SLionel Sambuc         }
1645ebfedea0SLionel Sambuc # endif
1646ebfedea0SLionel Sambuc 
1647ebfedea0SLionel Sambuc         data += size;
1648ebfedea0SLionel Sambuc     }
1649ebfedea0SLionel Sambuc 
1650*0a6a1f1dSLionel Sambuc     if (data != d + n) {
1651ebfedea0SLionel Sambuc         *al = SSL_AD_DECODE_ERROR;
1652ebfedea0SLionel Sambuc         return 0;
1653ebfedea0SLionel Sambuc     }
1654ebfedea0SLionel Sambuc 
1655*0a6a1f1dSLionel Sambuc     if (!s->hit && tlsext_servername == 1) {
1656*0a6a1f1dSLionel Sambuc         if (s->tlsext_hostname) {
1657*0a6a1f1dSLionel Sambuc             if (s->session->tlsext_hostname == NULL) {
1658ebfedea0SLionel Sambuc                 s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
1659*0a6a1f1dSLionel Sambuc                 if (!s->session->tlsext_hostname) {
1660ebfedea0SLionel Sambuc                     *al = SSL_AD_UNRECOGNIZED_NAME;
1661ebfedea0SLionel Sambuc                     return 0;
1662ebfedea0SLionel Sambuc                 }
1663*0a6a1f1dSLionel Sambuc             } else {
1664ebfedea0SLionel Sambuc                 *al = SSL_AD_DECODE_ERROR;
1665ebfedea0SLionel Sambuc                 return 0;
1666ebfedea0SLionel Sambuc             }
1667ebfedea0SLionel Sambuc         }
1668ebfedea0SLionel Sambuc     }
1669ebfedea0SLionel Sambuc 
1670ebfedea0SLionel Sambuc     *p = data;
1671ebfedea0SLionel Sambuc 
1672ebfedea0SLionel Sambuc  ri_check:
1673ebfedea0SLionel Sambuc 
1674*0a6a1f1dSLionel Sambuc     /*
1675*0a6a1f1dSLionel Sambuc      * Determine if we need to see RI. Strictly speaking if we want to avoid
1676*0a6a1f1dSLionel Sambuc      * an attack we should *always* see RI even on initial server hello
1677*0a6a1f1dSLionel Sambuc      * because the client doesn't see any renegotiation during an attack.
1678*0a6a1f1dSLionel Sambuc      * However this would mean we could not connect to any server which
1679*0a6a1f1dSLionel Sambuc      * doesn't support RI so for the immediate future tolerate RI absence on
1680*0a6a1f1dSLionel Sambuc      * initial connect only.
1681ebfedea0SLionel Sambuc      */
1682*0a6a1f1dSLionel Sambuc     if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
1683*0a6a1f1dSLionel Sambuc         && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
1684ebfedea0SLionel Sambuc         *al = SSL_AD_HANDSHAKE_FAILURE;
1685ebfedea0SLionel Sambuc         SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT,
1686ebfedea0SLionel Sambuc                SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
1687ebfedea0SLionel Sambuc         return 0;
1688ebfedea0SLionel Sambuc     }
1689ebfedea0SLionel Sambuc 
1690ebfedea0SLionel Sambuc     return 1;
1691ebfedea0SLionel Sambuc }
1692ebfedea0SLionel Sambuc 
ssl_prepare_clienthello_tlsext(SSL * s)1693ebfedea0SLionel Sambuc int ssl_prepare_clienthello_tlsext(SSL *s)
1694ebfedea0SLionel Sambuc {
1695ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
1696*0a6a1f1dSLionel Sambuc     /*
1697*0a6a1f1dSLionel Sambuc      * If we are client and using an elliptic curve cryptography cipher
1698*0a6a1f1dSLionel Sambuc      * suite, send the point formats and elliptic curves we support.
1699ebfedea0SLionel Sambuc      */
1700ebfedea0SLionel Sambuc     int using_ecc = 0;
1701ebfedea0SLionel Sambuc     int i;
1702ebfedea0SLionel Sambuc     unsigned char *j;
1703ebfedea0SLionel Sambuc     unsigned long alg_k, alg_a;
1704ebfedea0SLionel Sambuc     STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
1705ebfedea0SLionel Sambuc 
1706*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
1707ebfedea0SLionel Sambuc         SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
1708ebfedea0SLionel Sambuc 
1709ebfedea0SLionel Sambuc         alg_k = c->algorithm_mkey;
1710ebfedea0SLionel Sambuc         alg_a = c->algorithm_auth;
1711*0a6a1f1dSLionel Sambuc         if ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)
1712*0a6a1f1dSLionel Sambuc              || (alg_a & SSL_aECDSA))) {
1713ebfedea0SLionel Sambuc             using_ecc = 1;
1714ebfedea0SLionel Sambuc             break;
1715ebfedea0SLionel Sambuc         }
1716ebfedea0SLionel Sambuc     }
1717ebfedea0SLionel Sambuc     using_ecc = using_ecc && (s->version >= TLS1_VERSION);
1718*0a6a1f1dSLionel Sambuc     if (using_ecc) {
1719*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist != NULL)
1720*0a6a1f1dSLionel Sambuc             OPENSSL_free(s->tlsext_ecpointformatlist);
1721*0a6a1f1dSLionel Sambuc         if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) {
1722*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
1723*0a6a1f1dSLionel Sambuc                    ERR_R_MALLOC_FAILURE);
1724ebfedea0SLionel Sambuc             return -1;
1725ebfedea0SLionel Sambuc         }
1726ebfedea0SLionel Sambuc         s->tlsext_ecpointformatlist_length = 3;
1727ebfedea0SLionel Sambuc         s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1728*0a6a1f1dSLionel Sambuc         s->tlsext_ecpointformatlist[1] =
1729*0a6a1f1dSLionel Sambuc             TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1730*0a6a1f1dSLionel Sambuc         s->tlsext_ecpointformatlist[2] =
1731*0a6a1f1dSLionel Sambuc             TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1732ebfedea0SLionel Sambuc 
1733*0a6a1f1dSLionel Sambuc         /* we support all named elliptic curves in RFC 4492 */
1734*0a6a1f1dSLionel Sambuc         if (s->tlsext_ellipticcurvelist != NULL)
1735*0a6a1f1dSLionel Sambuc             OPENSSL_free(s->tlsext_ellipticcurvelist);
1736*0a6a1f1dSLionel Sambuc         s->tlsext_ellipticcurvelist_length =
1737*0a6a1f1dSLionel Sambuc             sizeof(pref_list) / sizeof(pref_list[0]) * 2;
1738*0a6a1f1dSLionel Sambuc         if ((s->tlsext_ellipticcurvelist =
1739*0a6a1f1dSLionel Sambuc              OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL) {
1740ebfedea0SLionel Sambuc             s->tlsext_ellipticcurvelist_length = 0;
1741*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
1742*0a6a1f1dSLionel Sambuc                    ERR_R_MALLOC_FAILURE);
1743ebfedea0SLionel Sambuc             return -1;
1744ebfedea0SLionel Sambuc         }
1745ebfedea0SLionel Sambuc         for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i <
1746*0a6a1f1dSLionel Sambuc              sizeof(pref_list) / sizeof(pref_list[0]); i++) {
1747ebfedea0SLionel Sambuc             int id = tls1_ec_nid2curve_id(pref_list[i]);
1748ebfedea0SLionel Sambuc             s2n(id, j);
1749ebfedea0SLionel Sambuc         }
1750ebfedea0SLionel Sambuc     }
1751ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
1752ebfedea0SLionel Sambuc 
1753ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
1754ebfedea0SLionel Sambuc     {
1755ebfedea0SLionel Sambuc         int r = 1;
1756ebfedea0SLionel Sambuc 
1757*0a6a1f1dSLionel Sambuc         if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
1758*0a6a1f1dSLionel Sambuc             r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
1759*0a6a1f1dSLionel Sambuc                                                          s->
1760*0a6a1f1dSLionel Sambuc                                                          ctx->tlsext_opaque_prf_input_callback_arg);
1761ebfedea0SLionel Sambuc             if (!r)
1762ebfedea0SLionel Sambuc                 return -1;
1763ebfedea0SLionel Sambuc         }
1764ebfedea0SLionel Sambuc 
1765*0a6a1f1dSLionel Sambuc         if (s->tlsext_opaque_prf_input != NULL) {
1766*0a6a1f1dSLionel Sambuc             if (s->s3->client_opaque_prf_input != NULL) {
1767*0a6a1f1dSLionel Sambuc                 /* shouldn't really happen */
1768ebfedea0SLionel Sambuc                 OPENSSL_free(s->s3->client_opaque_prf_input);
1769*0a6a1f1dSLionel Sambuc             }
1770ebfedea0SLionel Sambuc 
1771*0a6a1f1dSLionel Sambuc             if (s->tlsext_opaque_prf_input_len == 0) {
1772*0a6a1f1dSLionel Sambuc                 /* dummy byte just to get non-NULL */
1773*0a6a1f1dSLionel Sambuc                 s->s3->client_opaque_prf_input = OPENSSL_malloc(1);
1774*0a6a1f1dSLionel Sambuc             } else {
1775*0a6a1f1dSLionel Sambuc                 s->s3->client_opaque_prf_input =
1776*0a6a1f1dSLionel Sambuc                     BUF_memdup(s->tlsext_opaque_prf_input,
1777*0a6a1f1dSLionel Sambuc                                s->tlsext_opaque_prf_input_len);
1778*0a6a1f1dSLionel Sambuc             }
1779*0a6a1f1dSLionel Sambuc             if (s->s3->client_opaque_prf_input == NULL) {
1780*0a6a1f1dSLionel Sambuc                 SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,
1781*0a6a1f1dSLionel Sambuc                        ERR_R_MALLOC_FAILURE);
1782ebfedea0SLionel Sambuc                 return -1;
1783ebfedea0SLionel Sambuc             }
1784*0a6a1f1dSLionel Sambuc             s->s3->client_opaque_prf_input_len =
1785*0a6a1f1dSLionel Sambuc                 s->tlsext_opaque_prf_input_len;
1786ebfedea0SLionel Sambuc         }
1787ebfedea0SLionel Sambuc 
1788ebfedea0SLionel Sambuc         if (r == 2)
1789*0a6a1f1dSLionel Sambuc             /*
1790*0a6a1f1dSLionel Sambuc              * at callback's request, insist on receiving an appropriate
1791*0a6a1f1dSLionel Sambuc              * server opaque PRF input
1792*0a6a1f1dSLionel Sambuc              */
1793*0a6a1f1dSLionel Sambuc             s->s3->server_opaque_prf_input_len =
1794*0a6a1f1dSLionel Sambuc                 s->tlsext_opaque_prf_input_len;
1795ebfedea0SLionel Sambuc     }
1796ebfedea0SLionel Sambuc # endif
1797ebfedea0SLionel Sambuc 
1798ebfedea0SLionel Sambuc     return 1;
1799ebfedea0SLionel Sambuc }
1800ebfedea0SLionel Sambuc 
ssl_prepare_serverhello_tlsext(SSL * s)1801ebfedea0SLionel Sambuc int ssl_prepare_serverhello_tlsext(SSL *s)
1802ebfedea0SLionel Sambuc {
1803ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
1804*0a6a1f1dSLionel Sambuc     /*
1805*0a6a1f1dSLionel Sambuc      * If we are server and using an ECC cipher suite, send the point formats
1806*0a6a1f1dSLionel Sambuc      * we support if the client sent us an ECPointsFormat extension.  Note
1807*0a6a1f1dSLionel Sambuc      * that the server is not supposed to send an EllipticCurves extension.
1808ebfedea0SLionel Sambuc      */
1809ebfedea0SLionel Sambuc 
1810ebfedea0SLionel Sambuc     unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1811ebfedea0SLionel Sambuc     unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1812*0a6a1f1dSLionel Sambuc     int using_ecc = (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
1813*0a6a1f1dSLionel Sambuc         || (alg_a & SSL_aECDSA);
1814ebfedea0SLionel Sambuc     using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL);
1815ebfedea0SLionel Sambuc 
1816*0a6a1f1dSLionel Sambuc     if (using_ecc) {
1817*0a6a1f1dSLionel Sambuc         if (s->tlsext_ecpointformatlist != NULL)
1818*0a6a1f1dSLionel Sambuc             OPENSSL_free(s->tlsext_ecpointformatlist);
1819*0a6a1f1dSLionel Sambuc         if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) {
1820*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,
1821*0a6a1f1dSLionel Sambuc                    ERR_R_MALLOC_FAILURE);
1822ebfedea0SLionel Sambuc             return -1;
1823ebfedea0SLionel Sambuc         }
1824ebfedea0SLionel Sambuc         s->tlsext_ecpointformatlist_length = 3;
1825ebfedea0SLionel Sambuc         s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed;
1826*0a6a1f1dSLionel Sambuc         s->tlsext_ecpointformatlist[1] =
1827*0a6a1f1dSLionel Sambuc             TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime;
1828*0a6a1f1dSLionel Sambuc         s->tlsext_ecpointformatlist[2] =
1829*0a6a1f1dSLionel Sambuc             TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2;
1830ebfedea0SLionel Sambuc     }
1831ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
1832ebfedea0SLionel Sambuc 
1833ebfedea0SLionel Sambuc     return 1;
1834ebfedea0SLionel Sambuc }
1835ebfedea0SLionel Sambuc 
ssl_check_clienthello_tlsext_early(SSL * s)1836ebfedea0SLionel Sambuc int ssl_check_clienthello_tlsext_early(SSL *s)
1837ebfedea0SLionel Sambuc {
1838ebfedea0SLionel Sambuc     int ret = SSL_TLSEXT_ERR_NOACK;
1839ebfedea0SLionel Sambuc     int al = SSL_AD_UNRECOGNIZED_NAME;
1840ebfedea0SLionel Sambuc 
1841ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
1842*0a6a1f1dSLionel Sambuc     /*
1843*0a6a1f1dSLionel Sambuc      * The handling of the ECPointFormats extension is done elsewhere, namely
1844*0a6a1f1dSLionel Sambuc      * in ssl3_choose_cipher in s3_lib.c.
1845ebfedea0SLionel Sambuc      */
1846*0a6a1f1dSLionel Sambuc     /*
1847*0a6a1f1dSLionel Sambuc      * The handling of the EllipticCurves extension is done elsewhere, namely
1848*0a6a1f1dSLionel Sambuc      * in ssl3_choose_cipher in s3_lib.c.
1849ebfedea0SLionel Sambuc      */
1850ebfedea0SLionel Sambuc # endif
1851ebfedea0SLionel Sambuc 
1852ebfedea0SLionel Sambuc     if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
1853*0a6a1f1dSLionel Sambuc         ret =
1854*0a6a1f1dSLionel Sambuc             s->ctx->tlsext_servername_callback(s, &al,
1855*0a6a1f1dSLionel Sambuc                                                s->ctx->tlsext_servername_arg);
1856*0a6a1f1dSLionel Sambuc     else if (s->initial_ctx != NULL
1857*0a6a1f1dSLionel Sambuc              && s->initial_ctx->tlsext_servername_callback != 0)
1858*0a6a1f1dSLionel Sambuc         ret =
1859*0a6a1f1dSLionel Sambuc             s->initial_ctx->tlsext_servername_callback(s, &al,
1860*0a6a1f1dSLionel Sambuc                                                        s->
1861*0a6a1f1dSLionel Sambuc                                                        initial_ctx->tlsext_servername_arg);
1862ebfedea0SLionel Sambuc 
1863ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
1864ebfedea0SLionel Sambuc     {
1865*0a6a1f1dSLionel Sambuc         /*
1866*0a6a1f1dSLionel Sambuc          * This sort of belongs into ssl_prepare_serverhello_tlsext(), but we
1867*0a6a1f1dSLionel Sambuc          * might be sending an alert in response to the client hello, so this
1868*0a6a1f1dSLionel Sambuc          * has to happen here in ssl_check_clienthello_tlsext_early().
1869*0a6a1f1dSLionel Sambuc          */
1870ebfedea0SLionel Sambuc 
1871ebfedea0SLionel Sambuc         int r = 1;
1872ebfedea0SLionel Sambuc 
1873*0a6a1f1dSLionel Sambuc         if (s->ctx->tlsext_opaque_prf_input_callback != 0) {
1874*0a6a1f1dSLionel Sambuc             r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0,
1875*0a6a1f1dSLionel Sambuc                                                          s->
1876*0a6a1f1dSLionel Sambuc                                                          ctx->tlsext_opaque_prf_input_callback_arg);
1877*0a6a1f1dSLionel Sambuc             if (!r) {
1878ebfedea0SLionel Sambuc                 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1879ebfedea0SLionel Sambuc                 al = SSL_AD_INTERNAL_ERROR;
1880ebfedea0SLionel Sambuc                 goto err;
1881ebfedea0SLionel Sambuc             }
1882ebfedea0SLionel Sambuc         }
1883ebfedea0SLionel Sambuc 
1884*0a6a1f1dSLionel Sambuc         if (s->s3->server_opaque_prf_input != NULL) {
1885*0a6a1f1dSLionel Sambuc             /* shouldn't really happen */
1886ebfedea0SLionel Sambuc             OPENSSL_free(s->s3->server_opaque_prf_input);
1887*0a6a1f1dSLionel Sambuc         }
1888ebfedea0SLionel Sambuc         s->s3->server_opaque_prf_input = NULL;
1889ebfedea0SLionel Sambuc 
1890*0a6a1f1dSLionel Sambuc         if (s->tlsext_opaque_prf_input != NULL) {
1891ebfedea0SLionel Sambuc             if (s->s3->client_opaque_prf_input != NULL &&
1892*0a6a1f1dSLionel Sambuc                 s->s3->client_opaque_prf_input_len ==
1893*0a6a1f1dSLionel Sambuc                 s->tlsext_opaque_prf_input_len) {
1894*0a6a1f1dSLionel Sambuc                 /*
1895*0a6a1f1dSLionel Sambuc                  * can only use this extension if we have a server opaque PRF
1896*0a6a1f1dSLionel Sambuc                  * input of the same length as the client opaque PRF input!
1897*0a6a1f1dSLionel Sambuc                  */
1898ebfedea0SLionel Sambuc 
1899*0a6a1f1dSLionel Sambuc                 if (s->tlsext_opaque_prf_input_len == 0) {
1900*0a6a1f1dSLionel Sambuc                     /* dummy byte just to get non-NULL */
1901*0a6a1f1dSLionel Sambuc                     s->s3->server_opaque_prf_input = OPENSSL_malloc(1);
1902*0a6a1f1dSLionel Sambuc                 } else {
1903*0a6a1f1dSLionel Sambuc                     s->s3->server_opaque_prf_input =
1904*0a6a1f1dSLionel Sambuc                         BUF_memdup(s->tlsext_opaque_prf_input,
1905*0a6a1f1dSLionel Sambuc                                    s->tlsext_opaque_prf_input_len);
1906*0a6a1f1dSLionel Sambuc                 }
1907*0a6a1f1dSLionel Sambuc                 if (s->s3->server_opaque_prf_input == NULL) {
1908ebfedea0SLionel Sambuc                     ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1909ebfedea0SLionel Sambuc                     al = SSL_AD_INTERNAL_ERROR;
1910ebfedea0SLionel Sambuc                     goto err;
1911ebfedea0SLionel Sambuc                 }
1912*0a6a1f1dSLionel Sambuc                 s->s3->server_opaque_prf_input_len =
1913*0a6a1f1dSLionel Sambuc                     s->tlsext_opaque_prf_input_len;
1914ebfedea0SLionel Sambuc             }
1915ebfedea0SLionel Sambuc         }
1916ebfedea0SLionel Sambuc 
1917*0a6a1f1dSLionel Sambuc         if (r == 2 && s->s3->server_opaque_prf_input == NULL) {
1918*0a6a1f1dSLionel Sambuc             /*
1919*0a6a1f1dSLionel Sambuc              * The callback wants to enforce use of the extension, but we
1920*0a6a1f1dSLionel Sambuc              * can't do that with the client opaque PRF input; abort the
1921*0a6a1f1dSLionel Sambuc              * handshake.
1922ebfedea0SLionel Sambuc              */
1923ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1924ebfedea0SLionel Sambuc             al = SSL_AD_HANDSHAKE_FAILURE;
1925ebfedea0SLionel Sambuc         }
1926ebfedea0SLionel Sambuc     }
1927ebfedea0SLionel Sambuc 
1928ebfedea0SLionel Sambuc  err:
1929ebfedea0SLionel Sambuc # endif
1930*0a6a1f1dSLionel Sambuc     switch (ret) {
1931ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_FATAL:
1932ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_FATAL, al);
1933ebfedea0SLionel Sambuc         return -1;
1934ebfedea0SLionel Sambuc 
1935ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_WARNING:
1936ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_WARNING, al);
1937ebfedea0SLionel Sambuc         return 1;
1938ebfedea0SLionel Sambuc 
1939ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_NOACK:
1940ebfedea0SLionel Sambuc         s->servername_done = 0;
1941ebfedea0SLionel Sambuc     default:
1942ebfedea0SLionel Sambuc         return 1;
1943ebfedea0SLionel Sambuc     }
1944ebfedea0SLionel Sambuc }
1945ebfedea0SLionel Sambuc 
ssl_check_clienthello_tlsext_late(SSL * s)1946ebfedea0SLionel Sambuc int ssl_check_clienthello_tlsext_late(SSL *s)
1947ebfedea0SLionel Sambuc {
1948ebfedea0SLionel Sambuc     int ret = SSL_TLSEXT_ERR_OK;
1949ebfedea0SLionel Sambuc     int al;
1950ebfedea0SLionel Sambuc 
1951*0a6a1f1dSLionel Sambuc     /*
1952*0a6a1f1dSLionel Sambuc      * If status request then ask callback what to do. Note: this must be
1953*0a6a1f1dSLionel Sambuc      * called after servername callbacks in case the certificate has
1954*0a6a1f1dSLionel Sambuc      * changed, and must be called after the cipher has been chosen because
1955*0a6a1f1dSLionel Sambuc      * this may influence which certificate is sent
1956ebfedea0SLionel Sambuc      */
1957*0a6a1f1dSLionel Sambuc     if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) {
1958ebfedea0SLionel Sambuc         int r;
1959ebfedea0SLionel Sambuc         CERT_PKEY *certpkey;
1960ebfedea0SLionel Sambuc         certpkey = ssl_get_server_send_pkey(s);
1961ebfedea0SLionel Sambuc         /* If no certificate can't return certificate status */
1962*0a6a1f1dSLionel Sambuc         if (certpkey == NULL) {
1963ebfedea0SLionel Sambuc             s->tlsext_status_expected = 0;
1964ebfedea0SLionel Sambuc             return 1;
1965ebfedea0SLionel Sambuc         }
1966*0a6a1f1dSLionel Sambuc         /*
1967*0a6a1f1dSLionel Sambuc          * Set current certificate to one we will use so SSL_get_certificate
1968*0a6a1f1dSLionel Sambuc          * et al can pick it up.
1969ebfedea0SLionel Sambuc          */
1970ebfedea0SLionel Sambuc         s->cert->key = certpkey;
1971ebfedea0SLionel Sambuc         r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
1972*0a6a1f1dSLionel Sambuc         switch (r) {
1973ebfedea0SLionel Sambuc             /* We don't want to send a status request response */
1974ebfedea0SLionel Sambuc         case SSL_TLSEXT_ERR_NOACK:
1975ebfedea0SLionel Sambuc             s->tlsext_status_expected = 0;
1976ebfedea0SLionel Sambuc             break;
1977ebfedea0SLionel Sambuc             /* status request response should be sent */
1978ebfedea0SLionel Sambuc         case SSL_TLSEXT_ERR_OK:
1979ebfedea0SLionel Sambuc             if (s->tlsext_ocsp_resp)
1980ebfedea0SLionel Sambuc                 s->tlsext_status_expected = 1;
1981ebfedea0SLionel Sambuc             else
1982ebfedea0SLionel Sambuc                 s->tlsext_status_expected = 0;
1983ebfedea0SLionel Sambuc             break;
1984ebfedea0SLionel Sambuc             /* something bad happened */
1985ebfedea0SLionel Sambuc         case SSL_TLSEXT_ERR_ALERT_FATAL:
1986ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
1987ebfedea0SLionel Sambuc             al = SSL_AD_INTERNAL_ERROR;
1988ebfedea0SLionel Sambuc             goto err;
1989ebfedea0SLionel Sambuc         }
1990*0a6a1f1dSLionel Sambuc     } else
1991ebfedea0SLionel Sambuc         s->tlsext_status_expected = 0;
1992ebfedea0SLionel Sambuc 
1993ebfedea0SLionel Sambuc  err:
1994*0a6a1f1dSLionel Sambuc     switch (ret) {
1995ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_FATAL:
1996ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_FATAL, al);
1997ebfedea0SLionel Sambuc         return -1;
1998ebfedea0SLionel Sambuc 
1999ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_WARNING:
2000ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_WARNING, al);
2001ebfedea0SLionel Sambuc         return 1;
2002ebfedea0SLionel Sambuc 
2003ebfedea0SLionel Sambuc     default:
2004ebfedea0SLionel Sambuc         return 1;
2005ebfedea0SLionel Sambuc     }
2006ebfedea0SLionel Sambuc }
2007ebfedea0SLionel Sambuc 
ssl_check_serverhello_tlsext(SSL * s)2008ebfedea0SLionel Sambuc int ssl_check_serverhello_tlsext(SSL *s)
2009ebfedea0SLionel Sambuc {
2010ebfedea0SLionel Sambuc     int ret = SSL_TLSEXT_ERR_NOACK;
2011ebfedea0SLionel Sambuc     int al = SSL_AD_UNRECOGNIZED_NAME;
2012ebfedea0SLionel Sambuc 
2013ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
2014*0a6a1f1dSLionel Sambuc     /*
2015*0a6a1f1dSLionel Sambuc      * If we are client and using an elliptic curve cryptography cipher
2016*0a6a1f1dSLionel Sambuc      * suite, then if server returns an EC point formats lists extension it
2017*0a6a1f1dSLionel Sambuc      * must contain uncompressed.
2018ebfedea0SLionel Sambuc      */
2019ebfedea0SLionel Sambuc     unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2020ebfedea0SLionel Sambuc     unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2021*0a6a1f1dSLionel Sambuc     if ((s->tlsext_ecpointformatlist != NULL)
2022*0a6a1f1dSLionel Sambuc         && (s->tlsext_ecpointformatlist_length > 0)
2023*0a6a1f1dSLionel Sambuc         && (s->session->tlsext_ecpointformatlist != NULL)
2024*0a6a1f1dSLionel Sambuc         && (s->session->tlsext_ecpointformatlist_length > 0)
2025*0a6a1f1dSLionel Sambuc         && ((alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe))
2026*0a6a1f1dSLionel Sambuc             || (alg_a & SSL_aECDSA))) {
2027ebfedea0SLionel Sambuc         /* we are using an ECC cipher */
2028ebfedea0SLionel Sambuc         size_t i;
2029ebfedea0SLionel Sambuc         unsigned char *list;
2030ebfedea0SLionel Sambuc         int found_uncompressed = 0;
2031ebfedea0SLionel Sambuc         list = s->session->tlsext_ecpointformatlist;
2032*0a6a1f1dSLionel Sambuc         for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) {
2033*0a6a1f1dSLionel Sambuc             if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) {
2034ebfedea0SLionel Sambuc                 found_uncompressed = 1;
2035ebfedea0SLionel Sambuc                 break;
2036ebfedea0SLionel Sambuc             }
2037ebfedea0SLionel Sambuc         }
2038*0a6a1f1dSLionel Sambuc         if (!found_uncompressed) {
2039*0a6a1f1dSLionel Sambuc             SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,
2040*0a6a1f1dSLionel Sambuc                    SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
2041ebfedea0SLionel Sambuc             return -1;
2042ebfedea0SLionel Sambuc         }
2043ebfedea0SLionel Sambuc     }
2044ebfedea0SLionel Sambuc     ret = SSL_TLSEXT_ERR_OK;
2045ebfedea0SLionel Sambuc # endif                         /* OPENSSL_NO_EC */
2046ebfedea0SLionel Sambuc 
2047ebfedea0SLionel Sambuc     if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0)
2048*0a6a1f1dSLionel Sambuc         ret =
2049*0a6a1f1dSLionel Sambuc             s->ctx->tlsext_servername_callback(s, &al,
2050*0a6a1f1dSLionel Sambuc                                                s->ctx->tlsext_servername_arg);
2051*0a6a1f1dSLionel Sambuc     else if (s->initial_ctx != NULL
2052*0a6a1f1dSLionel Sambuc              && s->initial_ctx->tlsext_servername_callback != 0)
2053*0a6a1f1dSLionel Sambuc         ret =
2054*0a6a1f1dSLionel Sambuc             s->initial_ctx->tlsext_servername_callback(s, &al,
2055*0a6a1f1dSLionel Sambuc                                                        s->
2056*0a6a1f1dSLionel Sambuc                                                        initial_ctx->tlsext_servername_arg);
2057ebfedea0SLionel Sambuc 
2058ebfedea0SLionel Sambuc # ifdef TLSEXT_TYPE_opaque_prf_input
2059*0a6a1f1dSLionel Sambuc     if (s->s3->server_opaque_prf_input_len > 0) {
2060*0a6a1f1dSLionel Sambuc         /*
2061*0a6a1f1dSLionel Sambuc          * This case may indicate that we, as a client, want to insist on
2062*0a6a1f1dSLionel Sambuc          * using opaque PRF inputs. So first verify that we really have a
2063*0a6a1f1dSLionel Sambuc          * value from the server too.
2064*0a6a1f1dSLionel Sambuc          */
2065ebfedea0SLionel Sambuc 
2066*0a6a1f1dSLionel Sambuc         if (s->s3->server_opaque_prf_input == NULL) {
2067ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2068ebfedea0SLionel Sambuc             al = SSL_AD_HANDSHAKE_FAILURE;
2069ebfedea0SLionel Sambuc         }
2070ebfedea0SLionel Sambuc 
2071*0a6a1f1dSLionel Sambuc         /*
2072*0a6a1f1dSLionel Sambuc          * Anytime the server *has* sent an opaque PRF input, we need to
2073*0a6a1f1dSLionel Sambuc          * check that we have a client opaque PRF input of the same size.
2074*0a6a1f1dSLionel Sambuc          */
2075ebfedea0SLionel Sambuc         if (s->s3->client_opaque_prf_input == NULL ||
2076*0a6a1f1dSLionel Sambuc             s->s3->client_opaque_prf_input_len !=
2077*0a6a1f1dSLionel Sambuc             s->s3->server_opaque_prf_input_len) {
2078ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2079ebfedea0SLionel Sambuc             al = SSL_AD_ILLEGAL_PARAMETER;
2080ebfedea0SLionel Sambuc         }
2081ebfedea0SLionel Sambuc     }
2082ebfedea0SLionel Sambuc # endif
2083ebfedea0SLionel Sambuc 
2084*0a6a1f1dSLionel Sambuc     /*
2085*0a6a1f1dSLionel Sambuc      * If we've requested certificate status and we wont get one tell the
2086*0a6a1f1dSLionel Sambuc      * callback
2087ebfedea0SLionel Sambuc      */
2088ebfedea0SLionel Sambuc     if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected)
2089*0a6a1f1dSLionel Sambuc         && s->ctx && s->ctx->tlsext_status_cb) {
2090ebfedea0SLionel Sambuc         int r;
2091*0a6a1f1dSLionel Sambuc         /*
2092*0a6a1f1dSLionel Sambuc          * Set resp to NULL, resplen to -1 so callback knows there is no
2093*0a6a1f1dSLionel Sambuc          * response.
2094ebfedea0SLionel Sambuc          */
2095*0a6a1f1dSLionel Sambuc         if (s->tlsext_ocsp_resp) {
2096ebfedea0SLionel Sambuc             OPENSSL_free(s->tlsext_ocsp_resp);
2097ebfedea0SLionel Sambuc             s->tlsext_ocsp_resp = NULL;
2098ebfedea0SLionel Sambuc         }
2099ebfedea0SLionel Sambuc         s->tlsext_ocsp_resplen = -1;
2100ebfedea0SLionel Sambuc         r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2101*0a6a1f1dSLionel Sambuc         if (r == 0) {
2102ebfedea0SLionel Sambuc             al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2103ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2104ebfedea0SLionel Sambuc         }
2105*0a6a1f1dSLionel Sambuc         if (r < 0) {
2106ebfedea0SLionel Sambuc             al = SSL_AD_INTERNAL_ERROR;
2107ebfedea0SLionel Sambuc             ret = SSL_TLSEXT_ERR_ALERT_FATAL;
2108ebfedea0SLionel Sambuc         }
2109ebfedea0SLionel Sambuc     }
2110ebfedea0SLionel Sambuc 
2111*0a6a1f1dSLionel Sambuc     switch (ret) {
2112ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_FATAL:
2113ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_FATAL, al);
2114ebfedea0SLionel Sambuc         return -1;
2115ebfedea0SLionel Sambuc 
2116ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_ALERT_WARNING:
2117ebfedea0SLionel Sambuc         ssl3_send_alert(s, SSL3_AL_WARNING, al);
2118ebfedea0SLionel Sambuc         return 1;
2119ebfedea0SLionel Sambuc 
2120ebfedea0SLionel Sambuc     case SSL_TLSEXT_ERR_NOACK:
2121ebfedea0SLionel Sambuc         s->servername_done = 0;
2122ebfedea0SLionel Sambuc     default:
2123ebfedea0SLionel Sambuc         return 1;
2124ebfedea0SLionel Sambuc     }
2125ebfedea0SLionel Sambuc }
2126ebfedea0SLionel Sambuc 
2127*0a6a1f1dSLionel Sambuc /*-
2128*0a6a1f1dSLionel Sambuc  * Since the server cache lookup is done early on in the processing of the
2129ebfedea0SLionel Sambuc  * ClientHello, and other operations depend on the result, we need to handle
2130ebfedea0SLionel Sambuc  * any TLS session ticket extension at the same time.
2131ebfedea0SLionel Sambuc  *
2132ebfedea0SLionel Sambuc  *   session_id: points at the session ID in the ClientHello. This code will
2133ebfedea0SLionel Sambuc  *       read past the end of this in order to parse out the session ticket
2134ebfedea0SLionel Sambuc  *       extension, if any.
2135ebfedea0SLionel Sambuc  *   len: the length of the session ID.
2136ebfedea0SLionel Sambuc  *   limit: a pointer to the first byte after the ClientHello.
2137ebfedea0SLionel Sambuc  *   ret: (output) on return, if a ticket was decrypted, then this is set to
2138ebfedea0SLionel Sambuc  *       point to the resulting session.
2139ebfedea0SLionel Sambuc  *
2140ebfedea0SLionel Sambuc  * If s->tls_session_secret_cb is set then we are expecting a pre-shared key
2141ebfedea0SLionel Sambuc  * ciphersuite, in which case we have no use for session tickets and one will
2142ebfedea0SLionel Sambuc  * never be decrypted, nor will s->tlsext_ticket_expected be set to 1.
2143ebfedea0SLionel Sambuc  *
2144ebfedea0SLionel Sambuc  * Returns:
2145ebfedea0SLionel Sambuc  *   -1: fatal error, either from parsing or decrypting the ticket.
2146ebfedea0SLionel Sambuc  *    0: no ticket was found (or was ignored, based on settings).
2147ebfedea0SLionel Sambuc  *    1: a zero length extension was found, indicating that the client supports
2148ebfedea0SLionel Sambuc  *       session tickets but doesn't currently have one to offer.
2149ebfedea0SLionel Sambuc  *    2: either s->tls_session_secret_cb was set, or a ticket was offered but
2150ebfedea0SLionel Sambuc  *       couldn't be decrypted because of a non-fatal error.
2151ebfedea0SLionel Sambuc  *    3: a ticket was successfully decrypted and *ret was set.
2152ebfedea0SLionel Sambuc  *
2153ebfedea0SLionel Sambuc  * Side effects:
2154ebfedea0SLionel Sambuc  *   Sets s->tlsext_ticket_expected to 1 if the server will have to issue
2155ebfedea0SLionel Sambuc  *   a new session ticket to the client because the client indicated support
2156ebfedea0SLionel Sambuc  *   (and s->tls_session_secret_cb is NULL) but the client either doesn't have
2157ebfedea0SLionel Sambuc  *   a session ticket or we couldn't use the one it gave us, or if
2158ebfedea0SLionel Sambuc  *   s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket.
2159ebfedea0SLionel Sambuc  *   Otherwise, s->tlsext_ticket_expected is set to 0.
2160ebfedea0SLionel Sambuc  */
tls1_process_ticket(SSL * s,unsigned char * session_id,int len,const unsigned char * limit,SSL_SESSION ** ret)2161ebfedea0SLionel Sambuc int tls1_process_ticket(SSL *s, unsigned char *session_id, int len,
2162ebfedea0SLionel Sambuc                         const unsigned char *limit, SSL_SESSION **ret)
2163ebfedea0SLionel Sambuc {
2164ebfedea0SLionel Sambuc     /* Point after session ID in client hello */
2165ebfedea0SLionel Sambuc     const unsigned char *p = session_id + len;
2166ebfedea0SLionel Sambuc     unsigned short i;
2167ebfedea0SLionel Sambuc 
2168ebfedea0SLionel Sambuc     *ret = NULL;
2169ebfedea0SLionel Sambuc     s->tlsext_ticket_expected = 0;
2170ebfedea0SLionel Sambuc 
2171*0a6a1f1dSLionel Sambuc     /*
2172*0a6a1f1dSLionel Sambuc      * If tickets disabled behave as if no ticket present to permit stateful
2173*0a6a1f1dSLionel Sambuc      * resumption.
2174ebfedea0SLionel Sambuc      */
2175ebfedea0SLionel Sambuc     if (SSL_get_options(s) & SSL_OP_NO_TICKET)
2176ebfedea0SLionel Sambuc         return 0;
2177ebfedea0SLionel Sambuc     if ((s->version <= SSL3_VERSION) || !limit)
2178ebfedea0SLionel Sambuc         return 0;
2179ebfedea0SLionel Sambuc     if (p >= limit)
2180ebfedea0SLionel Sambuc         return -1;
2181ebfedea0SLionel Sambuc     /* Skip past DTLS cookie */
2182*0a6a1f1dSLionel Sambuc     if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
2183ebfedea0SLionel Sambuc         i = *(p++);
2184ebfedea0SLionel Sambuc         p += i;
2185ebfedea0SLionel Sambuc         if (p >= limit)
2186ebfedea0SLionel Sambuc             return -1;
2187ebfedea0SLionel Sambuc     }
2188ebfedea0SLionel Sambuc     /* Skip past cipher list */
2189ebfedea0SLionel Sambuc     n2s(p, i);
2190ebfedea0SLionel Sambuc     p += i;
2191ebfedea0SLionel Sambuc     if (p >= limit)
2192ebfedea0SLionel Sambuc         return -1;
2193ebfedea0SLionel Sambuc     /* Skip past compression algorithm list */
2194ebfedea0SLionel Sambuc     i = *(p++);
2195ebfedea0SLionel Sambuc     p += i;
2196ebfedea0SLionel Sambuc     if (p > limit)
2197ebfedea0SLionel Sambuc         return -1;
2198ebfedea0SLionel Sambuc     /* Now at start of extensions */
2199ebfedea0SLionel Sambuc     if ((p + 2) >= limit)
2200ebfedea0SLionel Sambuc         return 0;
2201ebfedea0SLionel Sambuc     n2s(p, i);
2202*0a6a1f1dSLionel Sambuc     while ((p + 4) <= limit) {
2203ebfedea0SLionel Sambuc         unsigned short type, size;
2204ebfedea0SLionel Sambuc         n2s(p, type);
2205ebfedea0SLionel Sambuc         n2s(p, size);
2206ebfedea0SLionel Sambuc         if (p + size > limit)
2207ebfedea0SLionel Sambuc             return 0;
2208*0a6a1f1dSLionel Sambuc         if (type == TLSEXT_TYPE_session_ticket) {
2209ebfedea0SLionel Sambuc             int r;
2210*0a6a1f1dSLionel Sambuc             if (size == 0) {
2211*0a6a1f1dSLionel Sambuc                 /*
2212*0a6a1f1dSLionel Sambuc                  * The client will accept a ticket but doesn't currently have
2213*0a6a1f1dSLionel Sambuc                  * one.
2214*0a6a1f1dSLionel Sambuc                  */
2215ebfedea0SLionel Sambuc                 s->tlsext_ticket_expected = 1;
2216ebfedea0SLionel Sambuc                 return 1;
2217ebfedea0SLionel Sambuc             }
2218*0a6a1f1dSLionel Sambuc             if (s->tls_session_secret_cb) {
2219*0a6a1f1dSLionel Sambuc                 /*
2220*0a6a1f1dSLionel Sambuc                  * Indicate that the ticket couldn't be decrypted rather than
2221*0a6a1f1dSLionel Sambuc                  * generating the session from ticket now, trigger
2222*0a6a1f1dSLionel Sambuc                  * abbreviated handshake based on external mechanism to
2223*0a6a1f1dSLionel Sambuc                  * calculate the master secret later.
2224*0a6a1f1dSLionel Sambuc                  */
2225ebfedea0SLionel Sambuc                 return 2;
2226ebfedea0SLionel Sambuc             }
2227ebfedea0SLionel Sambuc             r = tls_decrypt_ticket(s, p, size, session_id, len, ret);
2228*0a6a1f1dSLionel Sambuc             switch (r) {
2229ebfedea0SLionel Sambuc             case 2:            /* ticket couldn't be decrypted */
2230ebfedea0SLionel Sambuc                 s->tlsext_ticket_expected = 1;
2231ebfedea0SLionel Sambuc                 return 2;
2232ebfedea0SLionel Sambuc             case 3:            /* ticket was decrypted */
2233ebfedea0SLionel Sambuc                 return r;
2234ebfedea0SLionel Sambuc             case 4:            /* ticket decrypted but need to renew */
2235ebfedea0SLionel Sambuc                 s->tlsext_ticket_expected = 1;
2236ebfedea0SLionel Sambuc                 return 3;
2237ebfedea0SLionel Sambuc             default:           /* fatal error */
2238ebfedea0SLionel Sambuc                 return -1;
2239ebfedea0SLionel Sambuc             }
2240ebfedea0SLionel Sambuc         }
2241ebfedea0SLionel Sambuc         p += size;
2242ebfedea0SLionel Sambuc     }
2243ebfedea0SLionel Sambuc     return 0;
2244ebfedea0SLionel Sambuc }
2245ebfedea0SLionel Sambuc 
2246*0a6a1f1dSLionel Sambuc /*-
2247*0a6a1f1dSLionel Sambuc  * tls_decrypt_ticket attempts to decrypt a session ticket.
2248ebfedea0SLionel Sambuc  *
2249ebfedea0SLionel Sambuc  *   etick: points to the body of the session ticket extension.
2250ebfedea0SLionel Sambuc  *   eticklen: the length of the session tickets extenion.
2251ebfedea0SLionel Sambuc  *   sess_id: points at the session ID.
2252ebfedea0SLionel Sambuc  *   sesslen: the length of the session ID.
2253ebfedea0SLionel Sambuc  *   psess: (output) on return, if a ticket was decrypted, then this is set to
2254ebfedea0SLionel Sambuc  *       point to the resulting session.
2255ebfedea0SLionel Sambuc  *
2256ebfedea0SLionel Sambuc  * Returns:
2257ebfedea0SLionel Sambuc  *   -1: fatal error, either from parsing or decrypting the ticket.
2258ebfedea0SLionel Sambuc  *    2: the ticket couldn't be decrypted.
2259ebfedea0SLionel Sambuc  *    3: a ticket was successfully decrypted and *psess was set.
2260ebfedea0SLionel Sambuc  *    4: same as 3, but the ticket needs to be renewed.
2261ebfedea0SLionel Sambuc  */
tls_decrypt_ticket(SSL * s,const unsigned char * etick,int eticklen,const unsigned char * sess_id,int sesslen,SSL_SESSION ** psess)2262*0a6a1f1dSLionel Sambuc static int tls_decrypt_ticket(SSL *s, const unsigned char *etick,
2263*0a6a1f1dSLionel Sambuc                               int eticklen, const unsigned char *sess_id,
2264*0a6a1f1dSLionel Sambuc                               int sesslen, SSL_SESSION **psess)
2265ebfedea0SLionel Sambuc {
2266ebfedea0SLionel Sambuc     SSL_SESSION *sess;
2267ebfedea0SLionel Sambuc     unsigned char *sdec;
2268ebfedea0SLionel Sambuc     const unsigned char *p;
2269ebfedea0SLionel Sambuc     int slen, mlen, renew_ticket = 0;
2270ebfedea0SLionel Sambuc     unsigned char tick_hmac[EVP_MAX_MD_SIZE];
2271ebfedea0SLionel Sambuc     HMAC_CTX hctx;
2272ebfedea0SLionel Sambuc     EVP_CIPHER_CTX ctx;
2273ebfedea0SLionel Sambuc     SSL_CTX *tctx = s->initial_ctx;
2274ebfedea0SLionel Sambuc     /* Need at least keyname + iv + some encrypted data */
2275ebfedea0SLionel Sambuc     if (eticklen < 48)
2276ebfedea0SLionel Sambuc         return 2;
2277ebfedea0SLionel Sambuc     /* Initialize session ticket encryption and HMAC contexts */
2278ebfedea0SLionel Sambuc     HMAC_CTX_init(&hctx);
2279ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_init(&ctx);
2280*0a6a1f1dSLionel Sambuc     if (tctx->tlsext_ticket_key_cb) {
2281ebfedea0SLionel Sambuc         unsigned char *nctick = (unsigned char *)etick;
2282ebfedea0SLionel Sambuc         int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16,
2283ebfedea0SLionel Sambuc                                             &ctx, &hctx, 0);
2284ebfedea0SLionel Sambuc         if (rv < 0)
2285ebfedea0SLionel Sambuc             return -1;
2286ebfedea0SLionel Sambuc         if (rv == 0)
2287ebfedea0SLionel Sambuc             return 2;
2288ebfedea0SLionel Sambuc         if (rv == 2)
2289ebfedea0SLionel Sambuc             renew_ticket = 1;
2290*0a6a1f1dSLionel Sambuc     } else {
2291ebfedea0SLionel Sambuc         /* Check key name matches */
2292ebfedea0SLionel Sambuc         if (memcmp(etick, tctx->tlsext_tick_key_name, 16))
2293ebfedea0SLionel Sambuc             return 2;
2294ebfedea0SLionel Sambuc         HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
2295ebfedea0SLionel Sambuc                      tlsext_tick_md(), NULL);
2296ebfedea0SLionel Sambuc         EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
2297ebfedea0SLionel Sambuc                            tctx->tlsext_tick_aes_key, etick + 16);
2298ebfedea0SLionel Sambuc     }
2299*0a6a1f1dSLionel Sambuc     /*
2300*0a6a1f1dSLionel Sambuc      * Attempt to process session ticket, first conduct sanity and integrity
2301*0a6a1f1dSLionel Sambuc      * checks on ticket.
2302ebfedea0SLionel Sambuc      */
2303ebfedea0SLionel Sambuc     mlen = HMAC_size(&hctx);
2304*0a6a1f1dSLionel Sambuc     if (mlen < 0) {
2305ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_cleanup(&ctx);
2306ebfedea0SLionel Sambuc         return -1;
2307ebfedea0SLionel Sambuc     }
2308ebfedea0SLionel Sambuc     eticklen -= mlen;
2309ebfedea0SLionel Sambuc     /* Check HMAC of encrypted ticket */
2310ebfedea0SLionel Sambuc     HMAC_Update(&hctx, etick, eticklen);
2311ebfedea0SLionel Sambuc     HMAC_Final(&hctx, tick_hmac, NULL);
2312ebfedea0SLionel Sambuc     HMAC_CTX_cleanup(&hctx);
2313*0a6a1f1dSLionel Sambuc     if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
2314*0a6a1f1dSLionel Sambuc         EVP_CIPHER_CTX_cleanup(&ctx);
2315ebfedea0SLionel Sambuc         return 2;
2316*0a6a1f1dSLionel Sambuc     }
2317ebfedea0SLionel Sambuc     /* Attempt to decrypt session data */
2318ebfedea0SLionel Sambuc     /* Move p after IV to start of encrypted ticket, update length */
2319ebfedea0SLionel Sambuc     p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2320ebfedea0SLionel Sambuc     eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
2321ebfedea0SLionel Sambuc     sdec = OPENSSL_malloc(eticklen);
2322*0a6a1f1dSLionel Sambuc     if (!sdec) {
2323ebfedea0SLionel Sambuc         EVP_CIPHER_CTX_cleanup(&ctx);
2324ebfedea0SLionel Sambuc         return -1;
2325ebfedea0SLionel Sambuc     }
2326ebfedea0SLionel Sambuc     EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
2327*0a6a1f1dSLionel Sambuc     if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) {
2328*0a6a1f1dSLionel Sambuc         EVP_CIPHER_CTX_cleanup(&ctx);
2329*0a6a1f1dSLionel Sambuc         OPENSSL_free(sdec);
2330ebfedea0SLionel Sambuc         return 2;
2331*0a6a1f1dSLionel Sambuc     }
2332ebfedea0SLionel Sambuc     slen += mlen;
2333ebfedea0SLionel Sambuc     EVP_CIPHER_CTX_cleanup(&ctx);
2334ebfedea0SLionel Sambuc     p = sdec;
2335ebfedea0SLionel Sambuc 
2336ebfedea0SLionel Sambuc     sess = d2i_SSL_SESSION(NULL, &p, slen);
2337ebfedea0SLionel Sambuc     OPENSSL_free(sdec);
2338*0a6a1f1dSLionel Sambuc     if (sess) {
2339*0a6a1f1dSLionel Sambuc         /*
2340*0a6a1f1dSLionel Sambuc          * The session ID, if non-empty, is used by some clients to detect
2341*0a6a1f1dSLionel Sambuc          * that the ticket has been accepted. So we copy it to the session
2342*0a6a1f1dSLionel Sambuc          * structure. If it is empty set length to zero as required by
2343*0a6a1f1dSLionel Sambuc          * standard.
2344ebfedea0SLionel Sambuc          */
2345ebfedea0SLionel Sambuc         if (sesslen)
2346ebfedea0SLionel Sambuc             memcpy(sess->session_id, sess_id, sesslen);
2347ebfedea0SLionel Sambuc         sess->session_id_length = sesslen;
2348ebfedea0SLionel Sambuc         *psess = sess;
2349ebfedea0SLionel Sambuc         if (renew_ticket)
2350ebfedea0SLionel Sambuc             return 4;
2351ebfedea0SLionel Sambuc         else
2352ebfedea0SLionel Sambuc             return 3;
2353ebfedea0SLionel Sambuc     }
2354ebfedea0SLionel Sambuc     ERR_clear_error();
2355*0a6a1f1dSLionel Sambuc     /*
2356*0a6a1f1dSLionel Sambuc      * For session parse failure, indicate that we need to send a new ticket.
2357*0a6a1f1dSLionel Sambuc      */
2358ebfedea0SLionel Sambuc     return 2;
2359ebfedea0SLionel Sambuc }
2360ebfedea0SLionel Sambuc 
2361ebfedea0SLionel Sambuc /* Tables to translate from NIDs to TLS v1.2 ids */
2362ebfedea0SLionel Sambuc 
2363*0a6a1f1dSLionel Sambuc typedef struct {
2364ebfedea0SLionel Sambuc     int nid;
2365ebfedea0SLionel Sambuc     int id;
2366ebfedea0SLionel Sambuc } tls12_lookup;
2367ebfedea0SLionel Sambuc 
2368ebfedea0SLionel Sambuc static tls12_lookup tls12_md[] = {
2369ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_MD5
2370ebfedea0SLionel Sambuc     {NID_md5, TLSEXT_hash_md5},
2371ebfedea0SLionel Sambuc # endif
2372ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA
2373ebfedea0SLionel Sambuc     {NID_sha1, TLSEXT_hash_sha1},
2374ebfedea0SLionel Sambuc # endif
2375ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA256
2376ebfedea0SLionel Sambuc     {NID_sha224, TLSEXT_hash_sha224},
2377ebfedea0SLionel Sambuc     {NID_sha256, TLSEXT_hash_sha256},
2378ebfedea0SLionel Sambuc # endif
2379ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA512
2380ebfedea0SLionel Sambuc     {NID_sha384, TLSEXT_hash_sha384},
2381ebfedea0SLionel Sambuc     {NID_sha512, TLSEXT_hash_sha512}
2382ebfedea0SLionel Sambuc # endif
2383ebfedea0SLionel Sambuc };
2384ebfedea0SLionel Sambuc 
2385ebfedea0SLionel Sambuc static tls12_lookup tls12_sig[] = {
2386ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
2387ebfedea0SLionel Sambuc     {EVP_PKEY_RSA, TLSEXT_signature_rsa},
2388ebfedea0SLionel Sambuc # endif
2389ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
2390ebfedea0SLionel Sambuc     {EVP_PKEY_DSA, TLSEXT_signature_dsa},
2391ebfedea0SLionel Sambuc # endif
2392ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ECDSA
2393ebfedea0SLionel Sambuc     {EVP_PKEY_EC, TLSEXT_signature_ecdsa}
2394ebfedea0SLionel Sambuc # endif
2395ebfedea0SLionel Sambuc };
2396ebfedea0SLionel Sambuc 
tls12_find_id(int nid,tls12_lookup * table,size_t tlen)2397ebfedea0SLionel Sambuc static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen)
2398ebfedea0SLionel Sambuc {
2399ebfedea0SLionel Sambuc     size_t i;
2400*0a6a1f1dSLionel Sambuc     for (i = 0; i < tlen; i++) {
2401ebfedea0SLionel Sambuc         if (table[i].nid == nid)
2402ebfedea0SLionel Sambuc             return table[i].id;
2403ebfedea0SLionel Sambuc     }
2404ebfedea0SLionel Sambuc     return -1;
2405ebfedea0SLionel Sambuc }
2406*0a6a1f1dSLionel Sambuc 
2407ebfedea0SLionel Sambuc # if 0
2408ebfedea0SLionel Sambuc static int tls12_find_nid(int id, tls12_lookup *table, size_t tlen)
2409ebfedea0SLionel Sambuc {
2410ebfedea0SLionel Sambuc     size_t i;
2411*0a6a1f1dSLionel Sambuc     for (i = 0; i < tlen; i++) {
2412ebfedea0SLionel Sambuc         if (table[i].id == id)
2413ebfedea0SLionel Sambuc             return table[i].nid;
2414ebfedea0SLionel Sambuc     }
2415ebfedea0SLionel Sambuc     return -1;
2416ebfedea0SLionel Sambuc }
2417ebfedea0SLionel Sambuc # endif
2418ebfedea0SLionel Sambuc 
tls12_get_sigandhash(unsigned char * p,const EVP_PKEY * pk,const EVP_MD * md)2419*0a6a1f1dSLionel Sambuc int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk,
2420*0a6a1f1dSLionel Sambuc                          const EVP_MD *md)
2421ebfedea0SLionel Sambuc {
2422ebfedea0SLionel Sambuc     int sig_id, md_id;
2423ebfedea0SLionel Sambuc     if (!md)
2424ebfedea0SLionel Sambuc         return 0;
2425ebfedea0SLionel Sambuc     md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
2426ebfedea0SLionel Sambuc                           sizeof(tls12_md) / sizeof(tls12_lookup));
2427ebfedea0SLionel Sambuc     if (md_id == -1)
2428ebfedea0SLionel Sambuc         return 0;
2429ebfedea0SLionel Sambuc     sig_id = tls12_get_sigid(pk);
2430ebfedea0SLionel Sambuc     if (sig_id == -1)
2431ebfedea0SLionel Sambuc         return 0;
2432ebfedea0SLionel Sambuc     p[0] = (unsigned char)md_id;
2433ebfedea0SLionel Sambuc     p[1] = (unsigned char)sig_id;
2434ebfedea0SLionel Sambuc     return 1;
2435ebfedea0SLionel Sambuc }
2436ebfedea0SLionel Sambuc 
tls12_get_sigid(const EVP_PKEY * pk)2437ebfedea0SLionel Sambuc int tls12_get_sigid(const EVP_PKEY *pk)
2438ebfedea0SLionel Sambuc {
2439ebfedea0SLionel Sambuc     return tls12_find_id(pk->type, tls12_sig,
2440ebfedea0SLionel Sambuc                          sizeof(tls12_sig) / sizeof(tls12_lookup));
2441ebfedea0SLionel Sambuc }
2442ebfedea0SLionel Sambuc 
tls12_get_hash(unsigned char hash_alg)2443ebfedea0SLionel Sambuc const EVP_MD *tls12_get_hash(unsigned char hash_alg)
2444ebfedea0SLionel Sambuc {
2445*0a6a1f1dSLionel Sambuc     switch (hash_alg) {
2446ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA
2447ebfedea0SLionel Sambuc     case TLSEXT_hash_sha1:
2448ebfedea0SLionel Sambuc         return EVP_sha1();
2449ebfedea0SLionel Sambuc # endif
2450ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA256
2451ebfedea0SLionel Sambuc     case TLSEXT_hash_sha224:
2452ebfedea0SLionel Sambuc         return EVP_sha224();
2453ebfedea0SLionel Sambuc 
2454ebfedea0SLionel Sambuc     case TLSEXT_hash_sha256:
2455ebfedea0SLionel Sambuc         return EVP_sha256();
2456ebfedea0SLionel Sambuc # endif
2457ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA512
2458ebfedea0SLionel Sambuc     case TLSEXT_hash_sha384:
2459ebfedea0SLionel Sambuc         return EVP_sha384();
2460ebfedea0SLionel Sambuc 
2461ebfedea0SLionel Sambuc     case TLSEXT_hash_sha512:
2462ebfedea0SLionel Sambuc         return EVP_sha512();
2463ebfedea0SLionel Sambuc # endif
2464ebfedea0SLionel Sambuc     default:
2465ebfedea0SLionel Sambuc         return NULL;
2466ebfedea0SLionel Sambuc 
2467ebfedea0SLionel Sambuc     }
2468ebfedea0SLionel Sambuc }
2469ebfedea0SLionel Sambuc 
2470ebfedea0SLionel Sambuc /* Set preferred digest for each key type */
2471ebfedea0SLionel Sambuc 
tls1_process_sigalgs(SSL * s,const unsigned char * data,int dsize)2472ebfedea0SLionel Sambuc int tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize)
2473ebfedea0SLionel Sambuc {
2474ebfedea0SLionel Sambuc     int i, idx;
2475ebfedea0SLionel Sambuc     const EVP_MD *md;
2476ebfedea0SLionel Sambuc     CERT *c = s->cert;
2477ebfedea0SLionel Sambuc     /* Extension ignored for TLS versions below 1.2 */
2478ebfedea0SLionel Sambuc     if (TLS1_get_version(s) < TLS1_2_VERSION)
2479ebfedea0SLionel Sambuc         return 1;
2480ebfedea0SLionel Sambuc     /* Should never happen */
2481ebfedea0SLionel Sambuc     if (!c)
2482ebfedea0SLionel Sambuc         return 0;
2483ebfedea0SLionel Sambuc 
2484ebfedea0SLionel Sambuc     c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL;
2485ebfedea0SLionel Sambuc     c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL;
2486ebfedea0SLionel Sambuc     c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL;
2487ebfedea0SLionel Sambuc     c->pkeys[SSL_PKEY_ECC].digest = NULL;
2488ebfedea0SLionel Sambuc 
2489*0a6a1f1dSLionel Sambuc     for (i = 0; i < dsize; i += 2) {
2490ebfedea0SLionel Sambuc         unsigned char hash_alg = data[i], sig_alg = data[i + 1];
2491ebfedea0SLionel Sambuc 
2492*0a6a1f1dSLionel Sambuc         switch (sig_alg) {
2493ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
2494ebfedea0SLionel Sambuc         case TLSEXT_signature_rsa:
2495ebfedea0SLionel Sambuc             idx = SSL_PKEY_RSA_SIGN;
2496ebfedea0SLionel Sambuc             break;
2497ebfedea0SLionel Sambuc # endif
2498ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
2499ebfedea0SLionel Sambuc         case TLSEXT_signature_dsa:
2500ebfedea0SLionel Sambuc             idx = SSL_PKEY_DSA_SIGN;
2501ebfedea0SLionel Sambuc             break;
2502ebfedea0SLionel Sambuc # endif
2503ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ECDSA
2504ebfedea0SLionel Sambuc         case TLSEXT_signature_ecdsa:
2505ebfedea0SLionel Sambuc             idx = SSL_PKEY_ECC;
2506ebfedea0SLionel Sambuc             break;
2507ebfedea0SLionel Sambuc # endif
2508ebfedea0SLionel Sambuc         default:
2509ebfedea0SLionel Sambuc             continue;
2510ebfedea0SLionel Sambuc         }
2511ebfedea0SLionel Sambuc 
2512*0a6a1f1dSLionel Sambuc         if (c->pkeys[idx].digest == NULL) {
2513ebfedea0SLionel Sambuc             md = tls12_get_hash(hash_alg);
2514*0a6a1f1dSLionel Sambuc             if (md) {
2515ebfedea0SLionel Sambuc                 c->pkeys[idx].digest = md;
2516ebfedea0SLionel Sambuc                 if (idx == SSL_PKEY_RSA_SIGN)
2517ebfedea0SLionel Sambuc                     c->pkeys[SSL_PKEY_RSA_ENC].digest = md;
2518ebfedea0SLionel Sambuc             }
2519ebfedea0SLionel Sambuc         }
2520ebfedea0SLionel Sambuc 
2521ebfedea0SLionel Sambuc     }
2522ebfedea0SLionel Sambuc 
2523*0a6a1f1dSLionel Sambuc     /*
2524*0a6a1f1dSLionel Sambuc      * Set any remaining keys to default values. NOTE: if alg is not
2525ebfedea0SLionel Sambuc      * supported it stays as NULL.
2526ebfedea0SLionel Sambuc      */
2527ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
2528ebfedea0SLionel Sambuc     if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest)
2529ebfedea0SLionel Sambuc         c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
2530ebfedea0SLionel Sambuc # endif
2531ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
2532*0a6a1f1dSLionel Sambuc     if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) {
2533ebfedea0SLionel Sambuc         c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
2534ebfedea0SLionel Sambuc         c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
2535ebfedea0SLionel Sambuc     }
2536ebfedea0SLionel Sambuc # endif
2537ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ECDSA
2538ebfedea0SLionel Sambuc     if (!c->pkeys[SSL_PKEY_ECC].digest)
2539ebfedea0SLionel Sambuc         c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
2540ebfedea0SLionel Sambuc # endif
2541ebfedea0SLionel Sambuc     return 1;
2542ebfedea0SLionel Sambuc }
2543ebfedea0SLionel Sambuc 
2544ebfedea0SLionel Sambuc #endif
2545ebfedea0SLionel Sambuc 
2546ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
tls1_process_heartbeat(SSL * s)2547*0a6a1f1dSLionel Sambuc int tls1_process_heartbeat(SSL *s)
2548ebfedea0SLionel Sambuc {
2549ebfedea0SLionel Sambuc     unsigned char *p = &s->s3->rrec.data[0], *pl;
2550ebfedea0SLionel Sambuc     unsigned short hbtype;
2551ebfedea0SLionel Sambuc     unsigned int payload;
2552ebfedea0SLionel Sambuc     unsigned int padding = 16;  /* Use minimum padding */
2553ebfedea0SLionel Sambuc 
2554ebfedea0SLionel Sambuc     if (s->msg_callback)
2555ebfedea0SLionel Sambuc         s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT,
2556ebfedea0SLionel Sambuc                         &s->s3->rrec.data[0], s->s3->rrec.length,
2557ebfedea0SLionel Sambuc                         s, s->msg_callback_arg);
2558ebfedea0SLionel Sambuc 
2559*0a6a1f1dSLionel Sambuc     /* Read type and payload length first */
2560*0a6a1f1dSLionel Sambuc     if (1 + 2 + 16 > s->s3->rrec.length)
2561*0a6a1f1dSLionel Sambuc         return 0;               /* silently discard */
2562*0a6a1f1dSLionel Sambuc     hbtype = *p++;
2563*0a6a1f1dSLionel Sambuc     n2s(p, payload);
2564*0a6a1f1dSLionel Sambuc     if (1 + 2 + payload + 16 > s->s3->rrec.length)
2565*0a6a1f1dSLionel Sambuc         return 0;               /* silently discard per RFC 6520 sec. 4 */
2566*0a6a1f1dSLionel Sambuc     pl = p;
2567*0a6a1f1dSLionel Sambuc 
2568*0a6a1f1dSLionel Sambuc     if (hbtype == TLS1_HB_REQUEST) {
2569ebfedea0SLionel Sambuc         unsigned char *buffer, *bp;
2570ebfedea0SLionel Sambuc         int r;
2571ebfedea0SLionel Sambuc 
2572*0a6a1f1dSLionel Sambuc         /*
2573*0a6a1f1dSLionel Sambuc          * Allocate memory for the response, size is 1 bytes message type,
2574*0a6a1f1dSLionel Sambuc          * plus 2 bytes payload length, plus payload, plus padding
2575ebfedea0SLionel Sambuc          */
2576ebfedea0SLionel Sambuc         buffer = OPENSSL_malloc(1 + 2 + payload + padding);
2577ebfedea0SLionel Sambuc         bp = buffer;
2578ebfedea0SLionel Sambuc 
2579ebfedea0SLionel Sambuc         /* Enter response type, length and copy payload */
2580ebfedea0SLionel Sambuc         *bp++ = TLS1_HB_RESPONSE;
2581ebfedea0SLionel Sambuc         s2n(payload, bp);
2582ebfedea0SLionel Sambuc         memcpy(bp, pl, payload);
2583ebfedea0SLionel Sambuc         bp += payload;
2584ebfedea0SLionel Sambuc         /* Random padding */
2585*0a6a1f1dSLionel Sambuc         if (RAND_pseudo_bytes(bp, padding) < 0) {
2586*0a6a1f1dSLionel Sambuc             OPENSSL_free(buffer);
2587*0a6a1f1dSLionel Sambuc             return -1;
2588*0a6a1f1dSLionel Sambuc         }
2589ebfedea0SLionel Sambuc 
2590*0a6a1f1dSLionel Sambuc         r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer,
2591*0a6a1f1dSLionel Sambuc                              3 + payload + padding);
2592ebfedea0SLionel Sambuc 
2593ebfedea0SLionel Sambuc         if (r >= 0 && s->msg_callback)
2594ebfedea0SLionel Sambuc             s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
2595ebfedea0SLionel Sambuc                             buffer, 3 + payload + padding,
2596ebfedea0SLionel Sambuc                             s, s->msg_callback_arg);
2597ebfedea0SLionel Sambuc 
2598ebfedea0SLionel Sambuc         OPENSSL_free(buffer);
2599ebfedea0SLionel Sambuc 
2600ebfedea0SLionel Sambuc         if (r < 0)
2601ebfedea0SLionel Sambuc             return r;
2602*0a6a1f1dSLionel Sambuc     } else if (hbtype == TLS1_HB_RESPONSE) {
2603ebfedea0SLionel Sambuc         unsigned int seq;
2604ebfedea0SLionel Sambuc 
2605*0a6a1f1dSLionel Sambuc         /*
2606*0a6a1f1dSLionel Sambuc          * We only send sequence numbers (2 bytes unsigned int), and 16
2607*0a6a1f1dSLionel Sambuc          * random bytes, so we just try to read the sequence number
2608*0a6a1f1dSLionel Sambuc          */
2609ebfedea0SLionel Sambuc         n2s(pl, seq);
2610ebfedea0SLionel Sambuc 
2611*0a6a1f1dSLionel Sambuc         if (payload == 18 && seq == s->tlsext_hb_seq) {
2612ebfedea0SLionel Sambuc             s->tlsext_hb_seq++;
2613ebfedea0SLionel Sambuc             s->tlsext_hb_pending = 0;
2614ebfedea0SLionel Sambuc         }
2615ebfedea0SLionel Sambuc     }
2616ebfedea0SLionel Sambuc 
2617ebfedea0SLionel Sambuc     return 0;
2618ebfedea0SLionel Sambuc }
2619ebfedea0SLionel Sambuc 
tls1_heartbeat(SSL * s)2620*0a6a1f1dSLionel Sambuc int tls1_heartbeat(SSL *s)
2621ebfedea0SLionel Sambuc {
2622ebfedea0SLionel Sambuc     unsigned char *buf, *p;
2623*0a6a1f1dSLionel Sambuc     int ret = -1;
2624ebfedea0SLionel Sambuc     unsigned int payload = 18;  /* Sequence number + random bytes */
2625ebfedea0SLionel Sambuc     unsigned int padding = 16;  /* Use minimum padding */
2626ebfedea0SLionel Sambuc 
2627ebfedea0SLionel Sambuc     /* Only send if peer supports and accepts HB requests... */
2628ebfedea0SLionel Sambuc     if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) ||
2629*0a6a1f1dSLionel Sambuc         s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) {
2630ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT);
2631ebfedea0SLionel Sambuc         return -1;
2632ebfedea0SLionel Sambuc     }
2633ebfedea0SLionel Sambuc 
2634ebfedea0SLionel Sambuc     /* ...and there is none in flight yet... */
2635*0a6a1f1dSLionel Sambuc     if (s->tlsext_hb_pending) {
2636ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_TLS_HEARTBEAT_PENDING);
2637ebfedea0SLionel Sambuc         return -1;
2638ebfedea0SLionel Sambuc     }
2639ebfedea0SLionel Sambuc 
2640ebfedea0SLionel Sambuc     /* ...and no handshake in progress. */
2641*0a6a1f1dSLionel Sambuc     if (SSL_in_init(s) || s->in_handshake) {
2642ebfedea0SLionel Sambuc         SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_UNEXPECTED_MESSAGE);
2643ebfedea0SLionel Sambuc         return -1;
2644ebfedea0SLionel Sambuc     }
2645ebfedea0SLionel Sambuc 
2646*0a6a1f1dSLionel Sambuc     /*
2647*0a6a1f1dSLionel Sambuc      * Check if padding is too long, payload and padding must not exceed 2^14
2648*0a6a1f1dSLionel Sambuc      * - 3 = 16381 bytes in total.
2649ebfedea0SLionel Sambuc      */
2650ebfedea0SLionel Sambuc     OPENSSL_assert(payload + padding <= 16381);
2651ebfedea0SLionel Sambuc 
2652*0a6a1f1dSLionel Sambuc     /*-
2653*0a6a1f1dSLionel Sambuc      * Create HeartBeat message, we just use a sequence number
2654ebfedea0SLionel Sambuc      * as payload to distuingish different messages and add
2655ebfedea0SLionel Sambuc      * some random stuff.
2656ebfedea0SLionel Sambuc      *  - Message Type, 1 byte
2657ebfedea0SLionel Sambuc      *  - Payload Length, 2 bytes (unsigned int)
2658ebfedea0SLionel Sambuc      *  - Payload, the sequence number (2 bytes uint)
2659ebfedea0SLionel Sambuc      *  - Payload, random bytes (16 bytes uint)
2660ebfedea0SLionel Sambuc      *  - Padding
2661ebfedea0SLionel Sambuc      */
2662ebfedea0SLionel Sambuc     buf = OPENSSL_malloc(1 + 2 + payload + padding);
2663ebfedea0SLionel Sambuc     p = buf;
2664ebfedea0SLionel Sambuc     /* Message Type */
2665ebfedea0SLionel Sambuc     *p++ = TLS1_HB_REQUEST;
2666ebfedea0SLionel Sambuc     /* Payload length (18 bytes here) */
2667ebfedea0SLionel Sambuc     s2n(payload, p);
2668ebfedea0SLionel Sambuc     /* Sequence number */
2669ebfedea0SLionel Sambuc     s2n(s->tlsext_hb_seq, p);
2670ebfedea0SLionel Sambuc     /* 16 random bytes */
2671*0a6a1f1dSLionel Sambuc     if (RAND_pseudo_bytes(p, 16) < 0) {
2672*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
2673*0a6a1f1dSLionel Sambuc         goto err;
2674*0a6a1f1dSLionel Sambuc     }
2675ebfedea0SLionel Sambuc     p += 16;
2676ebfedea0SLionel Sambuc     /* Random padding */
2677*0a6a1f1dSLionel Sambuc     if (RAND_pseudo_bytes(p, padding) < 0) {
2678*0a6a1f1dSLionel Sambuc         SSLerr(SSL_F_TLS1_HEARTBEAT, ERR_R_INTERNAL_ERROR);
2679*0a6a1f1dSLionel Sambuc         goto err;
2680*0a6a1f1dSLionel Sambuc     }
2681ebfedea0SLionel Sambuc 
2682ebfedea0SLionel Sambuc     ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding);
2683*0a6a1f1dSLionel Sambuc     if (ret >= 0) {
2684ebfedea0SLionel Sambuc         if (s->msg_callback)
2685ebfedea0SLionel Sambuc             s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT,
2686ebfedea0SLionel Sambuc                             buf, 3 + payload + padding,
2687ebfedea0SLionel Sambuc                             s, s->msg_callback_arg);
2688ebfedea0SLionel Sambuc 
2689ebfedea0SLionel Sambuc         s->tlsext_hb_pending = 1;
2690ebfedea0SLionel Sambuc     }
2691ebfedea0SLionel Sambuc 
2692*0a6a1f1dSLionel Sambuc err:
2693ebfedea0SLionel Sambuc     OPENSSL_free(buf);
2694ebfedea0SLionel Sambuc 
2695ebfedea0SLionel Sambuc     return ret;
2696ebfedea0SLionel Sambuc }
2697ebfedea0SLionel Sambuc #endif
2698