blob: b1cd7cbcec0519113a92fbde88265102985f82e7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 client-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/ssl.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020038#else
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stdlib.h>
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +020040#define polarssl_malloc malloc
41#define polarssl_free free
42#endif
43
Paul Bakkerfa6a6202013-10-28 18:48:30 +010044#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045#include <basetsd.h>
46typedef UINT32 uint32_t;
47#else
48#include <inttypes.h>
49#endif
50
51#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Paul Bakker34617722014-06-13 17:20:13 +020055#if defined(POLARSSL_SSL_SESSION_TICKETS)
56/* Implementation that should never be optimized out by the compiler */
57static void polarssl_zeroize( void *v, size_t n ) {
58 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
59}
60#endif
61
Paul Bakker0be444a2013-08-27 21:55:01 +020062#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +010063static void ssl_write_hostname_ext( ssl_context *ssl,
64 unsigned char *buf,
65 size_t *olen )
66{
67 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +010068 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +010069
70 *olen = 0;
71
Paul Bakker66d5d072014-06-17 16:39:18 +020072 if( ssl->hostname == NULL )
Paul Bakkerd3edc862013-03-20 16:07:17 +010073 return;
74
75 SSL_DEBUG_MSG( 3, ( "client hello, adding server name extension: %s",
76 ssl->hostname ) );
77
Simon Butcher643a9222015-10-01 01:17:10 +010078 if( end < p || (size_t)( end - p ) < ssl->hostname_len + 9 )
Simon Butcherb1e325d2015-10-01 00:24:36 +010079 {
80 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
81 return;
82 }
83
Paul Bakkerd3edc862013-03-20 16:07:17 +010084 /*
85 * struct {
86 * NameType name_type;
87 * select (name_type) {
88 * case host_name: HostName;
89 * } name;
90 * } ServerName;
91 *
92 * enum {
93 * host_name(0), (255)
94 * } NameType;
95 *
96 * opaque HostName<1..2^16-1>;
97 *
98 * struct {
99 * ServerName server_name_list<1..2^16-1>
100 * } ServerNameList;
101 */
102 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME >> 8 ) & 0xFF );
103 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME ) & 0xFF );
104
105 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) >> 8 ) & 0xFF );
106 *p++ = (unsigned char)( ( (ssl->hostname_len + 5) ) & 0xFF );
107
108 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) >> 8 ) & 0xFF );
109 *p++ = (unsigned char)( ( (ssl->hostname_len + 3) ) & 0xFF );
110
111 *p++ = (unsigned char)( ( TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF );
112 *p++ = (unsigned char)( ( ssl->hostname_len >> 8 ) & 0xFF );
113 *p++ = (unsigned char)( ( ssl->hostname_len ) & 0xFF );
114
115 memcpy( p, ssl->hostname, ssl->hostname_len );
116
117 *olen = ssl->hostname_len + 9;
118}
Paul Bakker0be444a2013-08-27 21:55:01 +0200119#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100120
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100121#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100122static void ssl_write_renegotiation_ext( ssl_context *ssl,
123 unsigned char *buf,
124 size_t *olen )
125{
126 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100127 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100128
129 *olen = 0;
130
131 if( ssl->renegotiation != SSL_RENEGOTIATION )
132 return;
133
134 SSL_DEBUG_MSG( 3, ( "client hello, adding renegotiation extension" ) );
135
Manuel Pégourié-Gonnardf3e6e4b2015-10-02 09:53:52 +0200136 if( end < p || (size_t)(end - p) < 5 + ssl->verify_data_len )
Simon Butcherb1e325d2015-10-01 00:24:36 +0100137 {
138 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
139 return;
140 }
141
Paul Bakkerd3edc862013-03-20 16:07:17 +0100142 /*
143 * Secure renegotiation
144 */
145 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
146 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
147
148 *p++ = 0x00;
149 *p++ = ( ssl->verify_data_len + 1 ) & 0xFF;
150 *p++ = ssl->verify_data_len & 0xFF;
151
152 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
153
154 *olen = 5 + ssl->verify_data_len;
155}
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100156#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100157
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100158/*
159 * Only if we handle at least one key exchange that needs signatures.
160 */
161#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
162 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100163static void ssl_write_signature_algorithms_ext( ssl_context *ssl,
164 unsigned char *buf,
165 size_t *olen )
166{
167 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100168 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100169 size_t sig_alg_len = 0;
Manuel Pégourié-Gonnard5bfd9682014-06-24 15:18:11 +0200170#if defined(POLARSSL_RSA_C) || defined(POLARSSL_ECDSA_C)
171 unsigned char *sig_alg_list = buf + 6;
172#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100173
174 *olen = 0;
175
176 if( ssl->max_minor_ver != SSL_MINOR_VERSION_3 )
177 return;
178
179 SSL_DEBUG_MSG( 3, ( "client hello, adding signature_algorithms extension" ) );
180
Simon Butcherb1e325d2015-10-01 00:24:36 +0100181#if defined(POLARSSL_RSA_C)
182#if defined(POLARSSL_SHA512_C)
183 /* SHA512 + RSA signature, SHA384 + RSA signature */
184 sig_alg_len += 4;
185#endif
186#if defined(POLARSSL_SHA256_C)
187 /* SHA256 + RSA signature, SHA224 + RSA signature */
188 sig_alg_len += 4;
189#endif
190#if defined(POLARSSL_SHA1_C)
191 /* SHA1 + RSA signature */
192 sig_alg_len += 2;
193#endif
Simon Butcher14400c82016-01-02 00:08:13 +0000194#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Simon Butcherb1e325d2015-10-01 00:24:36 +0100195 /* MD5 + RSA signature */
196 sig_alg_len += 2;
197#endif
198#endif /* POLARSSL_RSA_C */
199#if defined(POLARSSL_ECDSA_C)
200#if defined(POLARSSL_SHA512_C)
201 /* SHA512 + ECDSA signature, SHA384 + ECDSA signature */
202 sig_alg_len += 4;
203#endif
204#if defined(POLARSSL_SHA256_C)
205 /* SHA256 + ECDSA signature, SHA224 + ECDSA signature */
206 sig_alg_len += 4;
207#endif
208#if defined(POLARSSL_SHA1_C)
209 /* SHA1 + ECDSA signature */
210 sig_alg_len += 2;
211#endif
Simon Butcher14400c82016-01-02 00:08:13 +0000212#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Simon Butcherb1e325d2015-10-01 00:24:36 +0100213 /* MD5 + ECDSA signature */
214 sig_alg_len += 2;
215#endif
216#endif /* POLARSSL_ECDSA_C */
217
218 if( end < p || (size_t)( end - p ) < sig_alg_len + 6 )
219 {
220 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
221 return;
222 }
223
Paul Bakkerd3edc862013-03-20 16:07:17 +0100224 /*
225 * Prepare signature_algorithms extension (TLS 1.2)
226 */
Simon Butcherb1e325d2015-10-01 00:24:36 +0100227 sig_alg_len = 0;
228
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200229#if defined(POLARSSL_RSA_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200230#if defined(POLARSSL_SHA512_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100231 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
232 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
233 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
234 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
235#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200236#if defined(POLARSSL_SHA256_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100237 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
238 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
239 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
240 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
241#endif
242#if defined(POLARSSL_SHA1_C)
243 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
244 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
245#endif
Simon Butcher14400c82016-01-02 00:08:13 +0000246#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100247 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
248 sig_alg_list[sig_alg_len++] = SSL_SIG_RSA;
249#endif
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200250#endif /* POLARSSL_RSA_C */
251#if defined(POLARSSL_ECDSA_C)
252#if defined(POLARSSL_SHA512_C)
253 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA512;
254 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
255 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA384;
256 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
257#endif
258#if defined(POLARSSL_SHA256_C)
259 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA256;
260 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
261 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA224;
262 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
263#endif
264#if defined(POLARSSL_SHA1_C)
265 sig_alg_list[sig_alg_len++] = SSL_HASH_SHA1;
266 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
267#endif
Simon Butcher14400c82016-01-02 00:08:13 +0000268#if defined(POLARSSL_MD5_C) && defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200269 sig_alg_list[sig_alg_len++] = SSL_HASH_MD5;
270 sig_alg_list[sig_alg_len++] = SSL_SIG_ECDSA;
271#endif
272#endif /* POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100273
274 /*
275 * enum {
276 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
277 * sha512(6), (255)
278 * } HashAlgorithm;
279 *
280 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
281 * SignatureAlgorithm;
282 *
283 * struct {
284 * HashAlgorithm hash;
285 * SignatureAlgorithm signature;
286 * } SignatureAndHashAlgorithm;
287 *
288 * SignatureAndHashAlgorithm
289 * supported_signature_algorithms<2..2^16-2>;
290 */
291 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG >> 8 ) & 0xFF );
292 *p++ = (unsigned char)( ( TLS_EXT_SIG_ALG ) & 0xFF );
293
294 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF );
295 *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF );
296
297 *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF );
298 *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF );
299
Paul Bakkerd3edc862013-03-20 16:07:17 +0100300 *olen = 6 + sig_alg_len;
301}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100302#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
303 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100304
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200305#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100306static void ssl_write_supported_elliptic_curves_ext( ssl_context *ssl,
307 unsigned char *buf,
308 size_t *olen )
309{
310 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100311 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard8e205fc2014-01-23 17:27:10 +0100312 unsigned char *elliptic_curve_list = p + 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100313 size_t elliptic_curve_len = 0;
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100314 const ecp_curve_info *info;
315#if defined(POLARSSL_SSL_SET_CURVES)
316 const ecp_group_id *grp_id;
Paul Bakker0910f322014-02-06 13:41:18 +0100317#else
318 ((void) ssl);
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100319#endif
Paul Bakkerd3edc862013-03-20 16:07:17 +0100320
321 *olen = 0;
322
323 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_elliptic_curves extension" ) );
324
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100325#if defined(POLARSSL_SSL_SET_CURVES)
326 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200327 {
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100328 info = ecp_curve_info_from_grp_id( *grp_id );
329#else
330 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
331 {
332#endif
Simon Butcherb1e325d2015-10-01 00:24:36 +0100333 elliptic_curve_len += 2;
334 }
335
336 if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
337 {
338 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
339 return;
340 }
341
342 elliptic_curve_len = 0;
343
344#if defined(POLARSSL_SSL_SET_CURVES)
345 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
346 {
347 info = ecp_curve_info_from_grp_id( *grp_id );
348#else
349 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
350 {
351#endif
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100352
353 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
354 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200355 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200356
357 if( elliptic_curve_len == 0 )
358 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100359
360 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
361 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
362
363 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
364 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
365
366 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
367 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
368
Paul Bakkerd3edc862013-03-20 16:07:17 +0100369 *olen = 6 + elliptic_curve_len;
370}
371
372static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
373 unsigned char *buf,
374 size_t *olen )
375{
376 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100377 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100378
379 *olen = 0;
380
381 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
382
Simon Butcherb1e325d2015-10-01 00:24:36 +0100383 if( end < p || (size_t)( end - p ) < 6 )
384 {
385 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
386 return;
387 }
388
Paul Bakkerd3edc862013-03-20 16:07:17 +0100389 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
390 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
391
392 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100393 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200394
395 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100396 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
397
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200398 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100399}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200400#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100401
Paul Bakker05decb22013-08-15 13:33:48 +0200402#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200403static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
404 unsigned char *buf,
405 size_t *olen )
406{
407 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100408 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200409
Simon Butcherb1e325d2015-10-01 00:24:36 +0100410 *olen = 0;
411
412 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200413 return;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200414
415 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
416
Simon Butcherb1e325d2015-10-01 00:24:36 +0100417 if( end < p || (size_t)( end - p ) < 5 )
418 {
419 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
420 return;
421 }
422
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200423 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
424 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
425
426 *p++ = 0x00;
427 *p++ = 1;
428
429 *p++ = ssl->mfl_code;
430
431 *olen = 5;
432}
Paul Bakker05decb22013-08-15 13:33:48 +0200433#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200434
Paul Bakker1f2bc622013-08-15 13:45:55 +0200435#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200436static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
437 unsigned char *buf, size_t *olen )
438{
439 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100440 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
441
442 *olen = 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200443
444 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200445 return;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200446
447 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
448
Simon Butcherb1e325d2015-10-01 00:24:36 +0100449 if( end < p || (size_t)( end - p ) < 4 )
450 {
451 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
452 return;
453 }
454
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200455 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
456 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
457
458 *p++ = 0x00;
459 *p++ = 0x00;
460
461 *olen = 4;
462}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200463#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200464
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100465#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
466static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
467 unsigned char *buf, size_t *olen )
468{
469 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100470 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
471
472 *olen = 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100473
474 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
475 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
476 {
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100477 return;
478 }
479
480 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
481 "extension" ) );
482
Simon Butcherb1e325d2015-10-01 00:24:36 +0100483 if( end < p || (size_t)( end - p ) < 4 )
484 {
485 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
486 return;
487 }
488
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100489 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
490 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
491
492 *p++ = 0x00;
493 *p++ = 0x00;
494
495 *olen = 4;
496}
497#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
498
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200499#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
500static void ssl_write_extended_ms_ext( ssl_context *ssl,
501 unsigned char *buf, size_t *olen )
502{
503 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100504 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
505
506 *olen = 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200507
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200508 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
509 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200510 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200511 return;
512 }
513
514 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
515 "extension" ) );
516
Simon Butcherb1e325d2015-10-01 00:24:36 +0100517 if( end < p || (size_t)( end - p ) < 4 )
518 {
519 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
520 return;
521 }
522
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200523 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
524 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
525
526 *p++ = 0x00;
527 *p++ = 0x00;
528
529 *olen = 4;
530}
531#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
532
Paul Bakkera503a632013-08-14 13:48:06 +0200533#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200534static void ssl_write_session_ticket_ext( ssl_context *ssl,
535 unsigned char *buf, size_t *olen )
536{
537 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100538 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200539 size_t tlen = ssl->session_negotiate->ticket_len;
540
Simon Butcherb1e325d2015-10-01 00:24:36 +0100541 *olen = 0;
542
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200543 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200544 return;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200545
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200546 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
547
Simon Butcherb1e325d2015-10-01 00:24:36 +0100548 if( end < p || (size_t)( end - p ) < 4 + tlen )
549 {
550 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
551 return;
552 }
553
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200554 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
555 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
556
557 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
558 *p++ = (unsigned char)( ( tlen ) & 0xFF );
559
560 *olen = 4;
561
562 if( ssl->session_negotiate->ticket == NULL ||
563 ssl->session_negotiate->ticket_len == 0 )
564 {
565 return;
566 }
567
568 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
569
570 memcpy( p, ssl->session_negotiate->ticket, tlen );
571
572 *olen += tlen;
573}
Paul Bakkera503a632013-08-14 13:48:06 +0200574#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200575
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200576#if defined(POLARSSL_SSL_ALPN)
577static void ssl_write_alpn_ext( ssl_context *ssl,
578 unsigned char *buf, size_t *olen )
579{
580 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100581 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
582 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200583 const char **cur;
584
Simon Butcherb1e325d2015-10-01 00:24:36 +0100585 *olen = 0;
586
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200587 if( ssl->alpn_list == NULL )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200588 return;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200589
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200590 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200591
Simon Butcherb1e325d2015-10-01 00:24:36 +0100592 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
593 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
594
595 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
596 {
597 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
598 return;
599 }
600
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200601 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
602 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
603
604 /*
605 * opaque ProtocolName<1..2^8-1>;
606 *
607 * struct {
608 * ProtocolName protocol_name_list<2..2^16-1>
609 * } ProtocolNameList;
610 */
611
612 /* Skip writing extension and list length for now */
613 p += 4;
614
615 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
616 {
617 *p = (unsigned char)( strlen( *cur ) & 0xFF );
618 memcpy( p + 1, *cur, *p );
619 p += 1 + *p;
620 }
621
622 *olen = p - buf;
623
624 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
625 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
626 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
627
628 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
629 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
630 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
631}
632#endif /* POLARSSL_SSL_ALPN */
633
Paul Bakker5121ce52009-01-03 21:22:43 +0000634static int ssl_write_client_hello( ssl_context *ssl )
635{
Paul Bakker23986e52011-04-24 08:57:21 +0000636 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100637 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200639 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200640#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000641 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200642#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200643 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200644 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000645
646 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
647
Paul Bakkera9a028e2013-11-21 17:31:06 +0100648 if( ssl->f_rng == NULL )
649 {
650 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
651 return( POLARSSL_ERR_SSL_NO_RNG );
652 }
653
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100654#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000655 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100656#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000657 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000658 ssl->major_ver = ssl->min_major_ver;
659 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000660 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Paul Bakker490ecc82011-10-06 13:04:09 +0000662 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
663 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200664 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
665 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000666 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668 /*
669 * 0 . 0 handshake type
670 * 1 . 3 handshake length
671 * 4 . 5 highest version supported
672 * 6 . 9 current UNIX time
673 * 10 . 37 random bytes
674 */
675 buf = ssl->out_msg;
676 p = buf + 4;
677
678 *p++ = (unsigned char) ssl->max_major_ver;
679 *p++ = (unsigned char) ssl->max_minor_ver;
680
681 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
682 buf[4], buf[5] ) );
683
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200684#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 t = time( NULL );
686 *p++ = (unsigned char)( t >> 24 );
687 *p++ = (unsigned char)( t >> 16 );
688 *p++ = (unsigned char)( t >> 8 );
689 *p++ = (unsigned char)( t );
690
691 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200692#else
693 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
694 return( ret );
695
696 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200697#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000698
Paul Bakkera3d195c2011-11-27 21:07:34 +0000699 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
700 return( ret );
701
702 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakker48916f92012-09-16 19:57:18 +0000704 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
707
708 /*
709 * 38 . 38 session id length
710 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000711 * 40+n . 41+n ciphersuitelist length
712 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000713 * .. . .. compression methods length
714 * .. . .. compression methods
715 * .. . .. extensions length
716 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000717 */
Paul Bakker48916f92012-09-16 19:57:18 +0000718 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100720 if( n < 16 || n > 32 ||
721#if defined(POLARSSL_SSL_RENEGOTIATION)
722 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
723#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000724 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200725 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200727 }
728
Paul Bakkera503a632013-08-14 13:48:06 +0200729#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200730 /*
731 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
732 * generate and include a Session ID in the TLS ClientHello."
733 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100734#if defined(POLARSSL_SSL_RENEGOTIATION)
735 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000736#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000737 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000738 if( ssl->session_negotiate->ticket != NULL &&
739 ssl->session_negotiate->ticket_len != 0 )
740 {
741 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200742
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000743 if( ret != 0 )
744 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200745
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000746 ssl->session_negotiate->length = n = 32;
747 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200748 }
Paul Bakkera503a632013-08-14 13:48:06 +0200749#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751 *p++ = (unsigned char) n;
752
753 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000754 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
757 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
758
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200759 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200760 n = 0;
761 q = p;
762
763 // Skip writing ciphersuite length for now
764 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000765
Paul Bakker2fbefde2013-06-29 16:01:15 +0200766 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000767 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200768 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
769
770 if( ciphersuite_info == NULL )
771 continue;
772
773 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
774 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
775 continue;
776
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100777 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
778 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
779 continue;
780
Paul Bakkere3166ce2011-01-27 17:40:50 +0000781 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200782 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000783
Paul Bakker2fbefde2013-06-29 16:01:15 +0200784 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200785 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
786 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 }
788
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000789 /*
790 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
791 */
792#if defined(POLARSSL_SSL_RENEGOTIATION)
793 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
794#endif
795 {
796 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
797 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
798 n++;
799 }
800
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200801 /* Some versions of OpenSSL don't handle it correctly if not at end */
802#if defined(POLARSSL_SSL_FALLBACK_SCSV)
803 if( ssl->fallback == SSL_IS_FALLBACK )
804 {
805 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
806 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
807 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
808 n++;
809 }
810#endif
811
Paul Bakker2fbefde2013-06-29 16:01:15 +0200812 *q++ = (unsigned char)( n >> 7 );
813 *q++ = (unsigned char)( n << 1 );
814
815 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
816
817
Paul Bakker2770fbd2012-07-03 13:30:23 +0000818#if defined(POLARSSL_ZLIB_SUPPORT)
819 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
820 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000821 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000822
823 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000824 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000825 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000826#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000828 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
830 *p++ = 1;
831 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200832#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
Paul Bakkerd3edc862013-03-20 16:07:17 +0100834 // First write extensions, then the total length
835 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200836#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100837 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
838 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100841#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100842 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
843 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100844#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000845
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100846#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
847 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100848 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
849 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200850#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000851
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200852#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100853 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
854 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100855
Paul Bakkerd3edc862013-03-20 16:07:17 +0100856 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
857 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100858#endif
859
Paul Bakker05decb22013-08-15 13:33:48 +0200860#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200861 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
862 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200863#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200864
Paul Bakker1f2bc622013-08-15 13:45:55 +0200865#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200866 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
867 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200868#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200869
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100870#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
871 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
872 ext_len += olen;
873#endif
874
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200875#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
876 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
877 ext_len += olen;
878#endif
879
Simon Butcher643a9222015-10-01 01:17:10 +0100880#if defined(POLARSSL_SSL_ALPN)
881 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200882 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200883#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200884
Simon Butcher643a9222015-10-01 01:17:10 +0100885#if defined(POLARSSL_SSL_SESSION_TICKETS)
886 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200887 ext_len += olen;
888#endif
889
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100890 /* olen unused if all extensions are disabled */
891 ((void) olen);
892
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000893 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
894 ext_len ) );
895
Paul Bakkera7036632014-04-30 10:15:38 +0200896 if( ext_len > 0 )
897 {
898 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
899 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
900 p += ext_len;
901 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100902
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 ssl->out_msglen = p - buf;
904 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
905 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
906
907 ssl->state++;
908
909 if( ( ret = ssl_write_record( ssl ) ) != 0 )
910 {
911 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
912 return( ret );
913 }
914
915 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
916
917 return( 0 );
918}
919
Paul Bakker48916f92012-09-16 19:57:18 +0000920static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200921 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000922 size_t len )
923{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000924 int ret;
925
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100926#if defined(POLARSSL_SSL_RENEGOTIATION)
927 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000928 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100929 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000930 if( len != 1 + ssl->verify_data_len * 2 ||
931 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100932 safer_memcmp( buf + 1,
933 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
934 safer_memcmp( buf + 1 + ssl->verify_data_len,
935 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000936 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100937 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000938
939 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
940 return( ret );
941
Paul Bakker48916f92012-09-16 19:57:18 +0000942 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
943 }
944 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100945 else
946#endif /* POLARSSL_SSL_RENEGOTIATION */
947 {
948 if( len != 1 || buf[0] != 0x00 )
949 {
950 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
951
952 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
953 return( ret );
954
955 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
956 }
957
958 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
959 }
Paul Bakker48916f92012-09-16 19:57:18 +0000960
961 return( 0 );
962}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200963
Paul Bakker05decb22013-08-15 13:33:48 +0200964#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200965static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200966 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200967 size_t len )
968{
969 /*
970 * server should use the extension only if we did,
971 * and if so the server's value should match ours (and len is always 1)
972 */
973 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
974 len != 1 ||
975 buf[0] != ssl->mfl_code )
976 {
977 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
978 }
979
980 return( 0 );
981}
Paul Bakker05decb22013-08-15 13:33:48 +0200982#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000983
Paul Bakker1f2bc622013-08-15 13:45:55 +0200984#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200985static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
986 const unsigned char *buf,
987 size_t len )
988{
989 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
990 len != 0 )
991 {
992 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
993 }
994
995 ((void) buf);
996
997 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
998
999 return( 0 );
1000}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001001#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001002
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001003#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1004static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
1005 const unsigned char *buf,
1006 size_t len )
1007{
1008 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
1009 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
1010 len != 0 )
1011 {
1012 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1013 }
1014
1015 ((void) buf);
1016
1017 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
1018
1019 return( 0 );
1020}
1021#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1022
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001023#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1024static int ssl_parse_extended_ms_ext( ssl_context *ssl,
1025 const unsigned char *buf,
1026 size_t len )
1027{
1028 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001029 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001030 len != 0 )
1031 {
1032 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1033 }
1034
1035 ((void) buf);
1036
1037 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
1038
1039 return( 0 );
1040}
1041#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1042
Paul Bakkera503a632013-08-14 13:48:06 +02001043#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001044static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1045 const unsigned char *buf,
1046 size_t len )
1047{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001048 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1049 len != 0 )
1050 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001051 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001052 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001053
1054 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001055
1056 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001057
1058 return( 0 );
1059}
Paul Bakkera503a632013-08-14 13:48:06 +02001060#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001061
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001062#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001063static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1064 const unsigned char *buf,
1065 size_t len )
1066{
1067 size_t list_size;
1068 const unsigned char *p;
1069
1070 list_size = buf[0];
1071 if( list_size + 1 != len )
1072 {
1073 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1074 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1075 }
1076
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001077 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001078 while( list_size > 0 )
1079 {
1080 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1081 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1082 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001083 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001084 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1085 return( 0 );
1086 }
1087
1088 list_size--;
1089 p++;
1090 }
1091
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001092 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1093 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001094}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001095#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001096
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001097#if defined(POLARSSL_SSL_ALPN)
1098static int ssl_parse_alpn_ext( ssl_context *ssl,
1099 const unsigned char *buf, size_t len )
1100{
1101 size_t list_len, name_len;
1102 const char **p;
1103
1104 /* If we didn't send it, the server shouldn't send it */
1105 if( ssl->alpn_list == NULL )
1106 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1107
1108 /*
1109 * opaque ProtocolName<1..2^8-1>;
1110 *
1111 * struct {
1112 * ProtocolName protocol_name_list<2..2^16-1>
1113 * } ProtocolNameList;
1114 *
1115 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1116 */
1117
1118 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1119 if( len < 4 )
1120 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1121
1122 list_len = ( buf[0] << 8 ) | buf[1];
1123 if( list_len != len - 2 )
1124 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1125
1126 name_len = buf[2];
1127 if( name_len != list_len - 1 )
1128 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1129
1130 /* Check that the server chosen protocol was in our list and save it */
1131 for( p = ssl->alpn_list; *p != NULL; p++ )
1132 {
1133 if( name_len == strlen( *p ) &&
1134 memcmp( buf + 3, *p, name_len ) == 0 )
1135 {
1136 ssl->alpn_chosen = *p;
1137 return( 0 );
1138 }
1139 }
1140
1141 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1142}
1143#endif /* POLARSSL_SSL_ALPN */
1144
Paul Bakker5121ce52009-01-03 21:22:43 +00001145static int ssl_parse_server_hello( ssl_context *ssl )
1146{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001147 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001148 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001149 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001150 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001151#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001152 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001153#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001154 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001155 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001156#if defined(POLARSSL_DEBUG_C)
1157 uint32_t t;
1158#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001159
1160 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1161
1162 /*
1163 * 0 . 0 handshake type
1164 * 1 . 3 handshake length
1165 * 4 . 5 protocol version
1166 * 6 . 9 UNIX time()
1167 * 10 . 37 random bytes
1168 */
1169 buf = ssl->in_msg;
1170
1171 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1172 {
1173 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1174 return( ret );
1175 }
1176
1177 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1178 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001179#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001180 if( ssl->renegotiation == SSL_RENEGOTIATION )
1181 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001182 ssl->renego_records_seen++;
1183
1184 if( ssl->renego_max_records >= 0 &&
1185 ssl->renego_records_seen > ssl->renego_max_records )
1186 {
1187 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1188 "but not honored by server" ) );
1189 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1190 }
1191
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001192 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1193 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1194 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001195#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001196
Paul Bakker5121ce52009-01-03 21:22:43 +00001197 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001198 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199 }
1200
1201 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1202 buf[4], buf[5] ) );
1203
1204 if( ssl->in_hslen < 42 ||
1205 buf[0] != SSL_HS_SERVER_HELLO ||
1206 buf[4] != SSL_MAJOR_VERSION_3 )
1207 {
1208 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001209 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 }
1211
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001212 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
1214 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001215 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 }
1217
1218 ssl->minor_ver = buf[5];
1219
Paul Bakker1d29fb52012-09-28 13:28:45 +00001220 if( ssl->minor_ver < ssl->min_minor_ver )
1221 {
1222 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001223 " [%d:%d] < [%d:%d]", ssl->major_ver,
1224 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001225
1226 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1227 SSL_ALERT_MSG_PROTOCOL_VERSION );
1228
1229 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1230 }
1231
Paul Bakker1504af52012-02-11 16:17:43 +00001232#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001233 t = ( (uint32_t) buf[6] << 24 )
1234 | ( (uint32_t) buf[7] << 16 )
1235 | ( (uint32_t) buf[8] << 8 )
1236 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001237 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Paul Bakker48916f92012-09-16 19:57:18 +00001240 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001241
1242 n = buf[38];
1243
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1245
Paul Bakker48916f92012-09-16 19:57:18 +00001246 if( n > 32 )
1247 {
1248 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1249 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1250 }
1251
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 /*
1253 * 38 . 38 session id length
1254 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001255 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 * 41+n . 41+n chosen compression alg.
1257 * 42+n . 43+n extensions length
1258 * 44+n . 44+n+m extensions
1259 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001260 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 {
1262 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001263 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Paul Bakker48916f92012-09-16 19:57:18 +00001265 if( ( ext_len > 0 && ext_len < 4 ) ||
1266 ssl->in_hslen != 44 + n + ext_len )
1267 {
1268 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1269 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1270 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001272 else if( ssl->in_hslen == 42 + n )
1273 {
1274 ext_len = 0;
1275 }
1276 else
1277 {
1278 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1279 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
1282 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001283 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001284
Paul Bakker380da532012-04-18 16:10:25 +00001285 /*
1286 * Initialize update checksum functions
1287 */
Paul Bakker68884e32013-01-07 18:20:04 +01001288 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1289
1290 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1291 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001292 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001293 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1294 }
Paul Bakker380da532012-04-18 16:10:25 +00001295
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001296 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1297
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1299 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1300
1301 /*
1302 * Check if the session can be resumed
1303 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001304 if( ssl->handshake->resume == 0 || n == 0 ||
1305#if defined(POLARSSL_SSL_RENEGOTIATION)
1306 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1307#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001308 ssl->session_negotiate->ciphersuite != i ||
1309 ssl->session_negotiate->compression != comp ||
1310 ssl->session_negotiate->length != n ||
1311 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001312 {
1313 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001314 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001315#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001316 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001317#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001318 ssl->session_negotiate->ciphersuite = i;
1319 ssl->session_negotiate->compression = comp;
1320 ssl->session_negotiate->length = n;
1321 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 }
1323 else
1324 {
1325 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001326
1327 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1328 {
1329 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1330 return( ret );
1331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333
1334 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001335 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
Paul Bakkere3166ce2011-01-27 17:40:50 +00001337 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1339
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001340 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1341 if( suite_info == NULL ||
1342 ( ssl->arc4_disabled &&
1343 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1344 {
1345 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1346 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1347 }
1348
1349
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 i = 0;
1351 while( 1 )
1352 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001353 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
1355 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001356 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
1358
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001359 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1360 ssl->session_negotiate->ciphersuite )
1361 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 }
1365
Paul Bakker2770fbd2012-07-03 13:30:23 +00001366 if( comp != SSL_COMPRESS_NULL
1367#if defined(POLARSSL_ZLIB_SUPPORT)
1368 && comp != SSL_COMPRESS_DEFLATE
1369#endif
1370 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
1372 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001373 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
Paul Bakker48916f92012-09-16 19:57:18 +00001375 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Paul Bakker48916f92012-09-16 19:57:18 +00001377 ext = buf + 44 + n;
1378
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001379 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1380
Paul Bakker48916f92012-09-16 19:57:18 +00001381 while( ext_len )
1382 {
1383 unsigned int ext_id = ( ( ext[0] << 8 )
1384 | ( ext[1] ) );
1385 unsigned int ext_size = ( ( ext[2] << 8 )
1386 | ( ext[3] ) );
1387
1388 if( ext_size + 4 > ext_len )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1391 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1392 }
1393
1394 switch( ext_id )
1395 {
1396 case TLS_EXT_RENEGOTIATION_INFO:
1397 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001398#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001399 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001400#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001401
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001402 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1403 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001404 return( ret );
1405
1406 break;
1407
Paul Bakker05decb22013-08-15 13:33:48 +02001408#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001409 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1410 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1411
1412 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1413 ext + 4, ext_size ) ) != 0 )
1414 {
1415 return( ret );
1416 }
1417
1418 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001419#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001420
Paul Bakker1f2bc622013-08-15 13:45:55 +02001421#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001422 case TLS_EXT_TRUNCATED_HMAC:
1423 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1424
1425 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1426 ext + 4, ext_size ) ) != 0 )
1427 {
1428 return( ret );
1429 }
1430
1431 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001432#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001433
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001434#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1435 case TLS_EXT_ENCRYPT_THEN_MAC:
1436 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1437
1438 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1439 ext + 4, ext_size ) ) != 0 )
1440 {
1441 return( ret );
1442 }
1443
1444 break;
1445#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1446
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001447#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1448 case TLS_EXT_EXTENDED_MASTER_SECRET:
1449 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1450
1451 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1452 ext + 4, ext_size ) ) != 0 )
1453 {
1454 return( ret );
1455 }
1456
1457 break;
1458#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1459
Paul Bakkera503a632013-08-14 13:48:06 +02001460#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001461 case TLS_EXT_SESSION_TICKET:
1462 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1463
1464 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1465 ext + 4, ext_size ) ) != 0 )
1466 {
1467 return( ret );
1468 }
1469
1470 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001471#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001472
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001473#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001474 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1475 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1476
1477 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1478 ext + 4, ext_size ) ) != 0 )
1479 {
1480 return( ret );
1481 }
1482
1483 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001484#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001485
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001486#if defined(POLARSSL_SSL_ALPN)
1487 case TLS_EXT_ALPN:
1488 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1489
1490 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1491 return( ret );
1492
1493 break;
1494#endif /* POLARSSL_SSL_ALPN */
1495
Paul Bakker48916f92012-09-16 19:57:18 +00001496 default:
1497 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1498 ext_id ) );
1499 }
1500
1501 ext_len -= 4 + ext_size;
1502 ext += 4 + ext_size;
1503
1504 if( ext_len > 0 && ext_len < 4 )
1505 {
1506 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1507 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1508 }
1509 }
1510
1511 /*
1512 * Renegotiation security checks
1513 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001514 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1515 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001516 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001517 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1518 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001519 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001520#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001521 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1522 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1523 renegotiation_info_seen == 0 )
1524 {
1525 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1526 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001527 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001528 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1529 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1530 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001531 {
1532 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001533 handshake_failure = 1;
1534 }
1535 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1536 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1537 renegotiation_info_seen == 1 )
1538 {
1539 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1540 handshake_failure = 1;
1541 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001542#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001543
1544 if( handshake_failure == 1 )
1545 {
1546 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1547 return( ret );
1548
Paul Bakker48916f92012-09-16 19:57:18 +00001549 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1550 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001551
1552 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1553
1554 return( 0 );
1555}
1556
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001557#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1558 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001559static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1560 unsigned char *end )
1561{
1562 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1563
Paul Bakker29e1f122013-04-16 13:07:56 +02001564 /*
1565 * Ephemeral DH parameters:
1566 *
1567 * struct {
1568 * opaque dh_p<1..2^16-1>;
1569 * opaque dh_g<1..2^16-1>;
1570 * opaque dh_Ys<1..2^16-1>;
1571 * } ServerDHParams;
1572 */
1573 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1574 {
1575 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1576 return( ret );
1577 }
1578
Manuel Pégourié-Gonnard9ea1b232015-06-29 15:27:52 +02001579 if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES ||
Paul Bakker29e1f122013-04-16 13:07:56 +02001580 ssl->handshake->dhm_ctx.len > 512 )
1581 {
1582 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1583 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1584 }
1585
1586 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1587 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1588 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001589
1590 return( ret );
1591}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001592#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1593 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001594
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001595#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001596 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001597 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1598 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1599 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1600static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1601{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001602 const ecp_curve_info *curve_info;
1603
1604 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1605 if( curve_info == NULL )
1606 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001607 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1608 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001609 }
1610
1611 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001612
Manuel Pégourié-Gonnard29f777e2015-04-03 17:26:50 +02001613#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001614 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1615#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001616 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1617 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001618#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001619 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001620
1621 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1622
1623 return( 0 );
1624}
Paul Bakker9af723c2014-05-01 13:03:14 +02001625#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1626 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1627 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1628 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1629 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001630
1631#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1632 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001633 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001634static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1635 unsigned char **p,
1636 unsigned char *end )
1637{
1638 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1639
Paul Bakker29e1f122013-04-16 13:07:56 +02001640 /*
1641 * Ephemeral ECDH parameters:
1642 *
1643 * struct {
1644 * ECParameters curve_params;
1645 * ECPoint public;
1646 * } ServerECDHParams;
1647 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001648 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1649 (const unsigned char **) p, end ) ) != 0 )
1650 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001651 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001652 return( ret );
1653 }
1654
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001655 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001656 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001657 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001658 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1659 }
1660
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 return( ret );
1662}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001663#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001664 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1665 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001666
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001667#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001668static int ssl_parse_server_psk_hint( ssl_context *ssl,
1669 unsigned char **p,
1670 unsigned char *end )
1671{
1672 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001673 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001674 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001675
1676 /*
1677 * PSK parameters:
1678 *
1679 * opaque psk_identity_hint<0..2^16-1>;
1680 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001681 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001682 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001683
1684 if( (*p) + len > end )
1685 {
1686 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1687 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1688 }
1689
1690 // TODO: Retrieve PSK identity hint and callback to app
1691 //
1692 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001693 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001694
1695 return( ret );
1696}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001697#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001698
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001699#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1700 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1701/*
1702 * Generate a pre-master secret and encrypt it with the server's RSA key
1703 */
1704static int ssl_write_encrypted_pms( ssl_context *ssl,
1705 size_t offset, size_t *olen,
1706 size_t pms_offset )
1707{
1708 int ret;
1709 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1710 unsigned char *p = ssl->handshake->premaster + pms_offset;
1711
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02001712 if( offset + len_bytes > SSL_MAX_CONTENT_LEN )
1713 {
1714 SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1715 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1716 }
1717
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001718 /*
1719 * Generate (part of) the pre-master as
1720 * struct {
1721 * ProtocolVersion client_version;
1722 * opaque random[46];
1723 * } PreMasterSecret;
1724 */
1725 p[0] = (unsigned char) ssl->max_major_ver;
1726 p[1] = (unsigned char) ssl->max_minor_ver;
1727
1728 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1729 {
1730 SSL_DEBUG_RET( 1, "f_rng", ret );
1731 return( ret );
1732 }
1733
1734 ssl->handshake->pmslen = 48;
1735
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001736 if( ssl->session_negotiate->peer_cert == NULL )
1737 {
1738 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1739 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1740 }
1741
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001742 /*
1743 * Now write it out, encrypted
1744 */
1745 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1746 POLARSSL_PK_RSA ) )
1747 {
1748 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1749 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1750 }
1751
1752 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1753 p, ssl->handshake->pmslen,
1754 ssl->out_msg + offset + len_bytes, olen,
1755 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1756 ssl->f_rng, ssl->p_rng ) ) != 0 )
1757 {
1758 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1759 return( ret );
1760 }
1761
1762#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1763 defined(POLARSSL_SSL_PROTO_TLS1_2)
1764 if( len_bytes == 2 )
1765 {
1766 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1767 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1768 *olen += 2;
1769 }
1770#endif
1771
1772 return( 0 );
1773}
1774#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1775 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001776
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001777#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001778#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001779 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1780 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001781static int ssl_parse_signature_algorithm( ssl_context *ssl,
1782 unsigned char **p,
1783 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001784 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001785 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001786{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001787 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001788 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001789 *pk_alg = POLARSSL_PK_NONE;
1790
1791 /* Only in TLS 1.2 */
1792 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1793 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001794 return( 0 );
1795 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001796
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001797 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001798 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1799
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001800 /*
1801 * Get hash algorithm
1802 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001803 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001804 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001805 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1806 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001807 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1808 }
1809
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001810 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001811 * Get signature algorithm
1812 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001813 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001814 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001815 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1816 "SignatureAlgorithm %d", (*p)[1] ) );
1817 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001818 }
1819
1820 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1821 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1822 *p += 2;
1823
1824 return( 0 );
1825}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001826#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001827 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1828 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001829#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001830
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001831
1832#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1833 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1834static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1835{
1836 int ret;
1837 const ecp_keypair *peer_key;
1838
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001839 if( ssl->session_negotiate->peer_cert == NULL )
1840 {
1841 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1842 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1843 }
1844
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001845 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1846 POLARSSL_PK_ECKEY ) )
1847 {
1848 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1849 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1850 }
1851
1852 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1853
1854 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1855 POLARSSL_ECDH_THEIRS ) ) != 0 )
1856 {
1857 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1858 return( ret );
1859 }
1860
1861 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1862 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001863 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001864 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1865 }
1866
1867 return( ret );
1868}
1869#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1870 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1871
Paul Bakker41c83d32013-03-20 14:39:14 +01001872static int ssl_parse_server_key_exchange( ssl_context *ssl )
1873{
Paul Bakker23986e52011-04-24 08:57:21 +00001874 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001875 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001876 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001877#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001878 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1879 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001880 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001881 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001882 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001883 size_t hashlen;
1884 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001885#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001886
1887 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1888
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001889#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001890 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 {
1892 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1893 ssl->state++;
1894 return( 0 );
1895 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001896 ((void) p);
1897 ((void) end);
1898#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001899
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001900#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1901 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1902 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1903 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1904 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001905 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1906 {
1907 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1908 return( ret );
1909 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001910
1911 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1912 ssl->state++;
1913 return( 0 );
1914 }
1915 ((void) p);
1916 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001917#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1918 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001919
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1921 {
1922 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1923 return( ret );
1924 }
1925
1926 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1927 {
1928 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001929 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 }
1931
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001932 /*
1933 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1934 * doesn't use a psk_identity_hint
1935 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1937 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001938 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1939 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001940 {
1941 ssl->record_read = 1;
1942 goto exit;
1943 }
1944
1945 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1946 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 }
1948
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001949 p = ssl->in_msg + 4;
1950 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001951 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001952
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001953#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1954 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1957 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1958 {
1959 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1960 {
1961 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1962 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1963 }
1964 } /* FALLTROUGH */
1965#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1966
1967#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1968 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1969 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1971 ; /* nothing more to do */
1972 else
1973#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1974 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1975#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1976 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1977 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001980 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001981 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001982 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001983 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1984 }
1985 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001986 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001987#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1988 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001989#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001990 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001991 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1992 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001993 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001994 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001995 {
1996 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
1997 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001998 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1999 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2000 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002001 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002002 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002003#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002004 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002005 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002006 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002007 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002008 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002010
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002011#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002012 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2013 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002014 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002015 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2016 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002017 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002018 params_len = p - ( ssl->in_msg + 4 );
2019
Paul Bakker29e1f122013-04-16 13:07:56 +02002020 /*
2021 * Handle the digitally-signed structure
2022 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002023#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2024 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002025 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002026 if( ssl_parse_signature_algorithm( ssl, &p, end,
2027 &md_alg, &pk_alg ) != 0 )
2028 {
2029 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2030 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2031 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002032
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002033 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002034 {
2035 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2036 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2037 }
Simon Butcher14400c82016-01-02 00:08:13 +00002038
2039#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
2040 if( md_alg == POLARSSL_MD_MD5 )
2041 {
2042 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2043 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2044 }
2045#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002046 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002047 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002048#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002049#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2050 defined(POLARSSL_SSL_PROTO_TLS1_1)
2051 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002052 {
2053 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002054
Paul Bakker9659dae2013-08-28 16:21:34 +02002055 /* Default hash for ECDSA is SHA-1 */
2056 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2057 md_alg = POLARSSL_MD_SHA1;
2058 }
2059 else
2060#endif
2061 {
2062 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002063 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002064 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002065
2066 /*
2067 * Read signature
2068 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002069 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002070 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002071
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002072 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002073 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002074 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002075 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002077
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002078 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002079
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002080 /*
2081 * Compute the hash that has been signed
2082 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002083#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2084 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002085 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002086 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002087 md5_context md5;
2088 sha1_context sha1;
2089
Paul Bakker5b4af392014-06-26 12:09:34 +02002090 md5_init( &md5 );
2091 sha1_init( &sha1 );
2092
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002093 hashlen = 36;
2094
Paul Bakker29e1f122013-04-16 13:07:56 +02002095 /*
2096 * digitally-signed struct {
2097 * opaque md5_hash[16];
2098 * opaque sha_hash[20];
2099 * };
2100 *
2101 * md5_hash
2102 * MD5(ClientHello.random + ServerHello.random
2103 * + ServerParams);
2104 * sha_hash
2105 * SHA(ClientHello.random + ServerHello.random
2106 * + ServerParams);
2107 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002108 md5_starts( &md5 );
2109 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002110 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002111 md5_finish( &md5, hash );
2112
2113 sha1_starts( &sha1 );
2114 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002115 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002116 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002117
2118 md5_free( &md5 );
2119 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002120 }
2121 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002122#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2123 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002124#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2125 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002126 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002127 {
2128 md_context_t ctx;
2129
Paul Bakker84bbeb52014-07-01 14:53:22 +02002130 md_init( &ctx );
2131
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002132 /* Info from md_alg will be used instead */
2133 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002134
2135 /*
2136 * digitally-signed struct {
2137 * opaque client_random[32];
2138 * opaque server_random[32];
2139 * ServerDHParams params;
2140 * };
2141 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002142 if( ( ret = md_init_ctx( &ctx,
2143 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002144 {
2145 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2146 return( ret );
2147 }
2148
2149 md_starts( &ctx );
2150 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002151 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002152 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002153 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002154 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002155 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002156#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2157 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002158 {
Paul Bakker577e0062013-08-28 11:57:20 +02002159 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002160 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002161 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002162
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002163 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2164 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002165
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02002166 if( ssl->session_negotiate->peer_cert == NULL )
2167 {
2168 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2169 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2170 }
2171
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002172 /*
2173 * Verify signature
2174 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002175 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002176 {
2177 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2178 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2179 }
2180
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002181 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2182 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002183 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002184 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002185 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002186 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002187 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002189 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2190 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002191
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002192exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 ssl->state++;
2194
2195 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2196
2197 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002198}
2199
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002200#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2201 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2202 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2203 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2204static int ssl_parse_certificate_request( ssl_context *ssl )
2205{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002206 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2207
2208 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2209
2210 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2211 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2212 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2213 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2214 {
2215 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2216 ssl->state++;
2217 return( 0 );
2218 }
2219
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002220 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2221 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002222}
2223#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002224static int ssl_parse_certificate_request( ssl_context *ssl )
2225{
2226 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002227 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002228 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002229 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002230 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
2232 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2233
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002234 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2235 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2236 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2237 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2238 {
2239 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2240 ssl->state++;
2241 return( 0 );
2242 }
2243
Paul Bakker5121ce52009-01-03 21:22:43 +00002244 /*
2245 * 0 . 0 handshake type
2246 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002247 * 4 . 4 cert type count
2248 * 5 .. m-1 cert types
2249 * m .. m+1 sig alg length (TLS 1.2 only)
2250 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002251 * n .. n+1 length of all DNs
2252 * n+2 .. n+3 length of DN 1
2253 * n+4 .. ... Distinguished Name #1
2254 * ... .. ... length of DN 2, etc.
2255 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002256 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002257 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002258 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2259 {
2260 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2261 return( ret );
2262 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002263
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002264 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2265 {
2266 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2267 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2268 }
2269
2270 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 }
2272
2273 ssl->client_auth = 0;
2274 ssl->state++;
2275
2276 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2277 ssl->client_auth++;
2278
2279 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2280 ssl->client_auth ? "a" : "no" ) );
2281
Paul Bakker926af752012-11-23 13:38:07 +01002282 if( ssl->client_auth == 0 )
2283 goto exit;
2284
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002285 ssl->record_read = 0;
2286
Paul Bakker926af752012-11-23 13:38:07 +01002287 // TODO: handshake_failure alert for an anonymous server to request
2288 // client authentication
2289
2290 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002291
Paul Bakker926af752012-11-23 13:38:07 +01002292 // Retrieve cert types
2293 //
2294 cert_type_len = buf[4];
2295 n = cert_type_len;
2296
2297 if( ssl->in_hslen < 6 + n )
2298 {
2299 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2300 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2301 }
2302
Paul Bakker73d44312013-05-22 13:56:26 +02002303 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002304 while( cert_type_len > 0 )
2305 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002306#if defined(POLARSSL_RSA_C)
2307 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002308 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002309 {
2310 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2311 break;
2312 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002313 else
2314#endif
2315#if defined(POLARSSL_ECDSA_C)
2316 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002317 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002318 {
2319 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2320 break;
2321 }
2322 else
2323#endif
2324 {
2325 ; /* Unsupported cert type, ignore */
2326 }
Paul Bakker926af752012-11-23 13:38:07 +01002327
2328 cert_type_len--;
2329 p++;
2330 }
2331
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002332#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002333 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2334 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002335 /* Ignored, see comments about hash in write_certificate_verify */
2336 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002337 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2338 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002339
2340 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002341 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002342 n += sig_alg_len;
2343
2344 if( ssl->in_hslen < 6 + n )
2345 {
2346 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2347 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2348 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002349 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002350#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002351
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002352 /* Ignore certificate_authorities, we only have one cert anyway */
2353 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002354 dn_len = ( ( buf[5 + m + n] << 8 )
2355 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002356
2357 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002358 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002359 {
2360 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2361 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2362 }
2363
2364exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002365 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2366
2367 return( 0 );
2368}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002369#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2370 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2371 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2372 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002373
2374static int ssl_parse_server_hello_done( ssl_context *ssl )
2375{
2376 int ret;
2377
2378 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2379
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002380 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 {
2382 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2383 {
2384 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2385 return( ret );
2386 }
2387
2388 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2389 {
2390 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002391 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 }
2393 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002394 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395
2396 if( ssl->in_hslen != 4 ||
2397 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2398 {
2399 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002400 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 }
2402
2403 ssl->state++;
2404
2405 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2406
2407 return( 0 );
2408}
2409
2410static int ssl_write_client_key_exchange( ssl_context *ssl )
2411{
Paul Bakker23986e52011-04-24 08:57:21 +00002412 int ret;
2413 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002414 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002415
2416 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2417
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002419 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 /*
2422 * DHM key exchange -- send G^X mod P
2423 */
Paul Bakker48916f92012-09-16 19:57:18 +00002424 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002425
2426 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2427 ssl->out_msg[5] = (unsigned char)( n );
2428 i = 6;
2429
Paul Bakker29b64762012-09-25 09:36:44 +00002430 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002431 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002432 &ssl->out_msg[i], n,
2433 ssl->f_rng, ssl->p_rng );
2434 if( ret != 0 )
2435 {
2436 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2437 return( ret );
2438 }
2439
Paul Bakker48916f92012-09-16 19:57:18 +00002440 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2441 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002443 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
Paul Bakker48916f92012-09-16 19:57:18 +00002445 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2446 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002447 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002448 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002449 {
2450 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2451 return( ret );
2452 }
2453
Paul Bakker48916f92012-09-16 19:57:18 +00002454 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002455 }
2456 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002457#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002458#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002459 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2460 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2461 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002462 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002463 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2464 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2465 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002466 {
2467 /*
2468 * ECDH key exchange -- send client public value
2469 */
2470 i = 4;
2471
2472 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2473 &n,
2474 &ssl->out_msg[i], 1000,
2475 ssl->f_rng, ssl->p_rng );
2476 if( ret != 0 )
2477 {
2478 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2479 return( ret );
2480 }
2481
2482 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2483
2484 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2485 &ssl->handshake->pmslen,
2486 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002487 POLARSSL_MPI_MAX_SIZE,
2488 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002489 {
2490 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2491 return( ret );
2492 }
2493
2494 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2495 }
2496 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002497#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002498 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2499 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2500 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002501#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002502 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002504 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2505 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002506 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002507 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002508 * opaque psk_identity<0..2^16-1>;
2509 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002510 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2512
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 i = 4;
2514 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002515
2516 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2517 {
2518 SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2519 "SSL buffer too short" ) );
2520 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2521 }
2522
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002523 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2524 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002526 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2527 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002529#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002532 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002533 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002534 else
2535#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002536#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2537 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2538 {
2539 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2540 return( ret );
2541 }
2542 else
2543#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002545 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002546 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002547 /*
2548 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2549 */
2550 n = ssl->handshake->dhm_ctx.len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002551
2552 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2553 {
2554 SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2555 " or SSL buffer too short" ) );
2556 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2557 }
2558
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002559 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2560 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002562 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002563 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002564 &ssl->out_msg[i], n,
2565 ssl->f_rng, ssl->p_rng );
2566 if( ret != 0 )
2567 {
2568 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2569 return( ret );
2570 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002572 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002574#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002575 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002576 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002577 /*
2578 * ClientECDiffieHellmanPublic public;
2579 */
2580 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2581 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2582 ssl->f_rng, ssl->p_rng );
2583 if( ret != 0 )
2584 {
2585 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2586 return( ret );
2587 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002588
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002589 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2590 }
2591 else
2592#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2593 {
2594 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002595 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002596 }
2597
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002598 if( ( ret = ssl_psk_derive_premaster( ssl,
2599 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002601 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602 return( ret );
2603 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002604 }
2605 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002606#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002607#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002608 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002610 i = 4;
2611 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002612 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 }
Paul Bakkered27a042013-04-18 22:46:23 +02002614 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002615#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002616 {
2617 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002618 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002619 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002620 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002621
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 ssl->out_msglen = i + n;
2623 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2624 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2625
2626 ssl->state++;
2627
2628 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2629 {
2630 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2631 return( ret );
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2635
2636 return( 0 );
2637}
2638
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2640 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002641 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2642 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002643static int ssl_write_certificate_verify( ssl_context *ssl )
2644{
Paul Bakkered27a042013-04-18 22:46:23 +02002645 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002646 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
2648 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2649
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002650 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2651 {
2652 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2653 return( ret );
2654 }
2655
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002656 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002657 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002658 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002659 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002660 {
2661 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2662 ssl->state++;
2663 return( 0 );
2664 }
2665
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002666 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2667 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002668}
2669#else
2670static int ssl_write_certificate_verify( ssl_context *ssl )
2671{
2672 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2673 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2674 size_t n = 0, offset = 0;
2675 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002676 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002678 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679
2680 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2681
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002682 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2683 {
2684 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2685 return( ret );
2686 }
2687
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002688 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002689 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002690 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002691 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2692 {
2693 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2694 ssl->state++;
2695 return( 0 );
2696 }
2697
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002698 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 {
2700 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2701 ssl->state++;
2702 return( 0 );
2703 }
2704
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002705 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002707 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2708 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 }
2710
2711 /*
2712 * Make an RSA signature of the handshake digests
2713 */
Paul Bakker48916f92012-09-16 19:57:18 +00002714 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2717 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002718 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002719 {
Paul Bakker926af752012-11-23 13:38:07 +01002720 /*
2721 * digitally-signed struct {
2722 * opaque md5_hash[16];
2723 * opaque sha_hash[20];
2724 * };
2725 *
2726 * md5_hash
2727 * MD5(handshake_messages);
2728 *
2729 * sha_hash
2730 * SHA(handshake_messages);
2731 */
2732 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002733 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002734
2735 /*
2736 * For ECDSA, default hash is SHA-1 only
2737 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002738 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002739 {
2740 hash_start += 16;
2741 hashlen -= 16;
2742 md_alg = POLARSSL_MD_SHA1;
2743 }
Paul Bakker926af752012-11-23 13:38:07 +01002744 }
2745 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002746#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2747 POLARSSL_SSL_PROTO_TLS1_1 */
2748#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2749 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002750 {
2751 /*
2752 * digitally-signed struct {
2753 * opaque handshake_messages[handshake_messages_length];
2754 * };
2755 *
2756 * Taking shortcut here. We assume that the server always allows the
2757 * PRF Hash function and has sent it in the allowed signature
2758 * algorithms list received in the Certificate Request message.
2759 *
2760 * Until we encounter a server that does not, we will take this
2761 * shortcut.
2762 *
2763 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2764 * in order to satisfy 'weird' needs from the server side.
2765 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002766 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2767 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002768 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002769 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002770 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002771 }
2772 else
2773 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002774 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002775 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002777 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002778
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002779 /* Info from md_alg will be used instead */
2780 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002781 offset = 2;
2782 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002783 else
2784#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002785 {
2786 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002787 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002788 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002789
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002790 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002791 ssl->out_msg + 6 + offset, &n,
2792 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002793 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002794 SSL_DEBUG_RET( 1, "pk_sign", ret );
2795 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002796 }
Paul Bakker926af752012-11-23 13:38:07 +01002797
Paul Bakker1ef83d62012-04-11 12:09:53 +00002798 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2799 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Paul Bakker1ef83d62012-04-11 12:09:53 +00002801 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002802 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2803 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2804
2805 ssl->state++;
2806
2807 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2808 {
2809 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2810 return( ret );
2811 }
2812
2813 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2814
Paul Bakkered27a042013-04-18 22:46:23 +02002815 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002816}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002817#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2818 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2819 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002820
Paul Bakkera503a632013-08-14 13:48:06 +02002821#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002822static int ssl_parse_new_session_ticket( ssl_context *ssl )
2823{
2824 int ret;
2825 uint32_t lifetime;
2826 size_t ticket_len;
2827 unsigned char *ticket;
2828
2829 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2830
2831 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2832 {
2833 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2834 return( ret );
2835 }
2836
2837 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2838 {
2839 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2840 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2841 }
2842
2843 /*
2844 * struct {
2845 * uint32 ticket_lifetime_hint;
2846 * opaque ticket<0..2^16-1>;
2847 * } NewSessionTicket;
2848 *
2849 * 0 . 0 handshake message type
2850 * 1 . 3 handshake message length
2851 * 4 . 7 ticket_lifetime_hint
2852 * 8 . 9 ticket_len (n)
2853 * 10 . 9+n ticket content
2854 */
2855 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2856 ssl->in_hslen < 10 )
2857 {
2858 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2859 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2860 }
2861
2862 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2863 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2864
2865 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2866
2867 if( ticket_len + 10 != ssl->in_hslen )
2868 {
2869 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2870 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2871 }
2872
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002873 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2874
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002875 /* We're not waiting for a NewSessionTicket message any more */
2876 ssl->handshake->new_session_ticket = 0;
2877
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878 /*
2879 * Zero-length ticket means the server changed his mind and doesn't want
2880 * to send a ticket after all, so just forget it
2881 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002882 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002883 return( 0 );
2884
Paul Bakker34617722014-06-13 17:20:13 +02002885 polarssl_zeroize( ssl->session_negotiate->ticket,
2886 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002887 polarssl_free( ssl->session_negotiate->ticket );
2888 ssl->session_negotiate->ticket = NULL;
2889 ssl->session_negotiate->ticket_len = 0;
2890
2891 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2892 {
2893 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2894 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2895 }
2896
2897 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2898
2899 ssl->session_negotiate->ticket = ticket;
2900 ssl->session_negotiate->ticket_len = ticket_len;
2901 ssl->session_negotiate->ticket_lifetime = lifetime;
2902
2903 /*
2904 * RFC 5077 section 3.4:
2905 * "If the client receives a session ticket from the server, then it
2906 * discards any Session ID that was sent in the ServerHello."
2907 */
2908 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2909 ssl->session_negotiate->length = 0;
2910
2911 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2912
2913 return( 0 );
2914}
Paul Bakkera503a632013-08-14 13:48:06 +02002915#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002916
Paul Bakker5121ce52009-01-03 21:22:43 +00002917/*
Paul Bakker1961b702013-01-25 14:49:24 +01002918 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 */
Paul Bakker1961b702013-01-25 14:49:24 +01002920int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002921{
2922 int ret = 0;
2923
Paul Bakker1961b702013-01-25 14:49:24 +01002924 if( ssl->state == SSL_HANDSHAKE_OVER )
2925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002926
Paul Bakker1961b702013-01-25 14:49:24 +01002927 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2928
2929 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2930 return( ret );
2931
2932 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 {
Paul Bakker1961b702013-01-25 14:49:24 +01002934 case SSL_HELLO_REQUEST:
2935 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 break;
2937
Paul Bakker1961b702013-01-25 14:49:24 +01002938 /*
2939 * ==> ClientHello
2940 */
2941 case SSL_CLIENT_HELLO:
2942 ret = ssl_write_client_hello( ssl );
2943 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Paul Bakker1961b702013-01-25 14:49:24 +01002945 /*
2946 * <== ServerHello
2947 * Certificate
2948 * ( ServerKeyExchange )
2949 * ( CertificateRequest )
2950 * ServerHelloDone
2951 */
2952 case SSL_SERVER_HELLO:
2953 ret = ssl_parse_server_hello( ssl );
2954 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Paul Bakker1961b702013-01-25 14:49:24 +01002956 case SSL_SERVER_CERTIFICATE:
2957 ret = ssl_parse_certificate( ssl );
2958 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Paul Bakker1961b702013-01-25 14:49:24 +01002960 case SSL_SERVER_KEY_EXCHANGE:
2961 ret = ssl_parse_server_key_exchange( ssl );
2962 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakker1961b702013-01-25 14:49:24 +01002964 case SSL_CERTIFICATE_REQUEST:
2965 ret = ssl_parse_certificate_request( ssl );
2966 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Paul Bakker1961b702013-01-25 14:49:24 +01002968 case SSL_SERVER_HELLO_DONE:
2969 ret = ssl_parse_server_hello_done( ssl );
2970 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Paul Bakker1961b702013-01-25 14:49:24 +01002972 /*
2973 * ==> ( Certificate/Alert )
2974 * ClientKeyExchange
2975 * ( CertificateVerify )
2976 * ChangeCipherSpec
2977 * Finished
2978 */
2979 case SSL_CLIENT_CERTIFICATE:
2980 ret = ssl_write_certificate( ssl );
2981 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002982
Paul Bakker1961b702013-01-25 14:49:24 +01002983 case SSL_CLIENT_KEY_EXCHANGE:
2984 ret = ssl_write_client_key_exchange( ssl );
2985 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Paul Bakker1961b702013-01-25 14:49:24 +01002987 case SSL_CERTIFICATE_VERIFY:
2988 ret = ssl_write_certificate_verify( ssl );
2989 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
Paul Bakker1961b702013-01-25 14:49:24 +01002991 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2992 ret = ssl_write_change_cipher_spec( ssl );
2993 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002994
Paul Bakker1961b702013-01-25 14:49:24 +01002995 case SSL_CLIENT_FINISHED:
2996 ret = ssl_write_finished( ssl );
2997 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002998
Paul Bakker1961b702013-01-25 14:49:24 +01002999 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003000 * <== ( NewSessionTicket )
3001 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003002 * Finished
3003 */
3004 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003005#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003006 if( ssl->handshake->new_session_ticket != 0 )
3007 ret = ssl_parse_new_session_ticket( ssl );
3008 else
Paul Bakkera503a632013-08-14 13:48:06 +02003009#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003010 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003011 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003012
Paul Bakker1961b702013-01-25 14:49:24 +01003013 case SSL_SERVER_FINISHED:
3014 ret = ssl_parse_finished( ssl );
3015 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakker1961b702013-01-25 14:49:24 +01003017 case SSL_FLUSH_BUFFERS:
3018 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3019 ssl->state = SSL_HANDSHAKE_WRAPUP;
3020 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Paul Bakker1961b702013-01-25 14:49:24 +01003022 case SSL_HANDSHAKE_WRAPUP:
3023 ssl_handshake_wrapup( ssl );
3024 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003025
Paul Bakker1961b702013-01-25 14:49:24 +01003026 default:
3027 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3028 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3029 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003030
3031 return( ret );
3032}
Paul Bakker9af723c2014-05-01 13:03:14 +02003033#endif /* POLARSSL_SSL_CLI_C */