blob: fbbbf81ba65d597a728f170547af05c6d3df564d [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
Janos Follath4e034392016-04-21 23:37:09 +0100333 if( info == NULL )
334 {
335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid curve in ssl configuration" ) );
336 return;
337 }
338
Simon Butcherb1e325d2015-10-01 00:24:36 +0100339 elliptic_curve_len += 2;
340 }
341
342 if( end < p || (size_t)( end - p ) < 6 + elliptic_curve_len )
343 {
344 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
345 return;
346 }
347
348 elliptic_curve_len = 0;
349
350#if defined(POLARSSL_SSL_SET_CURVES)
351 for( grp_id = ssl->curve_list; *grp_id != POLARSSL_ECP_DP_NONE; grp_id++ )
352 {
353 info = ecp_curve_info_from_grp_id( *grp_id );
354#else
355 for( info = ecp_curve_list(); info->grp_id != POLARSSL_ECP_DP_NONE; info++ )
356 {
357#endif
Manuel Pégourié-Gonnardcd49f762014-02-04 15:14:13 +0100358 elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8;
359 elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200360 }
Paul Bakker5dc6b5f2013-06-29 23:26:34 +0200361
362 if( elliptic_curve_len == 0 )
363 return;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100364
365 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) & 0xFF );
366 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) & 0xFF );
367
368 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF );
369 *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF );
370
371 *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF );
372 *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF );
373
Paul Bakkerd3edc862013-03-20 16:07:17 +0100374 *olen = 6 + elliptic_curve_len;
375}
376
377static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
378 unsigned char *buf,
379 size_t *olen )
380{
381 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100382 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100383
384 *olen = 0;
385
386 SSL_DEBUG_MSG( 3, ( "client hello, adding supported_point_formats extension" ) );
387
Simon Butcherb1e325d2015-10-01 00:24:36 +0100388 if( end < p || (size_t)( end - p ) < 6 )
389 {
390 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
391 return;
392 }
393
Paul Bakkerd3edc862013-03-20 16:07:17 +0100394 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
395 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
396
397 *p++ = 0x00;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100398 *p++ = 2;
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200399
400 *p++ = 1;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100401 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
402
Manuel Pégourié-Gonnard6b8846d2013-08-15 17:42:02 +0200403 *olen = 6;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100404}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200405#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakkerd3edc862013-03-20 16:07:17 +0100406
Paul Bakker05decb22013-08-15 13:33:48 +0200407#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200408static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
409 unsigned char *buf,
410 size_t *olen )
411{
412 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100413 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200414
Simon Butcherb1e325d2015-10-01 00:24:36 +0100415 *olen = 0;
416
417 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE )
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200418 return;
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200419
420 SSL_DEBUG_MSG( 3, ( "client hello, adding max_fragment_length extension" ) );
421
Simon Butcherb1e325d2015-10-01 00:24:36 +0100422 if( end < p || (size_t)( end - p ) < 5 )
423 {
424 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
425 return;
426 }
427
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200428 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
429 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
430
431 *p++ = 0x00;
432 *p++ = 1;
433
434 *p++ = ssl->mfl_code;
435
436 *olen = 5;
437}
Paul Bakker05decb22013-08-15 13:33:48 +0200438#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200439
Paul Bakker1f2bc622013-08-15 13:45:55 +0200440#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200441static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
442 unsigned char *buf, size_t *olen )
443{
444 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100445 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
446
447 *olen = 0;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200448
449 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200450 return;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200451
452 SSL_DEBUG_MSG( 3, ( "client hello, adding truncated_hmac extension" ) );
453
Simon Butcherb1e325d2015-10-01 00:24:36 +0100454 if( end < p || (size_t)( end - p ) < 4 )
455 {
456 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
457 return;
458 }
459
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200460 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
461 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
462
463 *p++ = 0x00;
464 *p++ = 0x00;
465
466 *olen = 4;
467}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200468#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200469
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100470#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
471static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
472 unsigned char *buf, size_t *olen )
473{
474 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100475 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
476
477 *olen = 0;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100478
479 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
480 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
481 {
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100482 return;
483 }
484
485 SSL_DEBUG_MSG( 3, ( "client hello, adding encrypt_then_mac "
486 "extension" ) );
487
Simon Butcherb1e325d2015-10-01 00:24:36 +0100488 if( end < p || (size_t)( end - p ) < 4 )
489 {
490 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
491 return;
492 }
493
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100494 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
495 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
496
497 *p++ = 0x00;
498 *p++ = 0x00;
499
500 *olen = 4;
501}
502#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
503
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200504#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
505static void ssl_write_extended_ms_ext( ssl_context *ssl,
506 unsigned char *buf, size_t *olen )
507{
508 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100509 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
510
511 *olen = 0;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200512
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200513 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
514 ssl->max_minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200515 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200516 return;
517 }
518
519 SSL_DEBUG_MSG( 3, ( "client hello, adding extended_master_secret "
520 "extension" ) );
521
Simon Butcherb1e325d2015-10-01 00:24:36 +0100522 if( end < p || (size_t)( end - p ) < 4 )
523 {
524 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
525 return;
526 }
527
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200528 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
529 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
530
531 *p++ = 0x00;
532 *p++ = 0x00;
533
534 *olen = 4;
535}
536#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
537
Paul Bakkera503a632013-08-14 13:48:06 +0200538#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200539static void ssl_write_session_ticket_ext( ssl_context *ssl,
540 unsigned char *buf, size_t *olen )
541{
542 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100543 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200544 size_t tlen = ssl->session_negotiate->ticket_len;
545
Simon Butcherb1e325d2015-10-01 00:24:36 +0100546 *olen = 0;
547
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200548 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200549 return;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200550
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200551 SSL_DEBUG_MSG( 3, ( "client hello, adding session ticket extension" ) );
552
Simon Butcherb1e325d2015-10-01 00:24:36 +0100553 if( end < p || (size_t)( end - p ) < 4 + tlen )
554 {
555 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
556 return;
557 }
558
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200559 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
560 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
561
562 *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF );
563 *p++ = (unsigned char)( ( tlen ) & 0xFF );
564
565 *olen = 4;
566
567 if( ssl->session_negotiate->ticket == NULL ||
568 ssl->session_negotiate->ticket_len == 0 )
569 {
570 return;
571 }
572
573 SSL_DEBUG_MSG( 3, ( "sending session ticket of length %d", tlen ) );
574
575 memcpy( p, ssl->session_negotiate->ticket, tlen );
576
577 *olen += tlen;
578}
Paul Bakkera503a632013-08-14 13:48:06 +0200579#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200580
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200581#if defined(POLARSSL_SSL_ALPN)
582static void ssl_write_alpn_ext( ssl_context *ssl,
583 unsigned char *buf, size_t *olen )
584{
585 unsigned char *p = buf;
Simon Butcherb1e325d2015-10-01 00:24:36 +0100586 const unsigned char *end = ssl->out_msg + SSL_MAX_CONTENT_LEN;
587 size_t alpnlen = 0;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200588 const char **cur;
589
Simon Butcherb1e325d2015-10-01 00:24:36 +0100590 *olen = 0;
591
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200592 if( ssl->alpn_list == NULL )
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200593 return;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200594
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +0200595 SSL_DEBUG_MSG( 3, ( "client hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200596
Simon Butcherb1e325d2015-10-01 00:24:36 +0100597 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
598 alpnlen += (unsigned char)( strlen( *cur ) & 0xFF ) + 1;
599
600 if( end < p || (size_t)( end - p ) < 6 + alpnlen )
601 {
602 SSL_DEBUG_MSG( 1, ( "buffer too small" ) );
603 return;
604 }
605
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200606 *p++ = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
607 *p++ = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
608
609 /*
610 * opaque ProtocolName<1..2^8-1>;
611 *
612 * struct {
613 * ProtocolName protocol_name_list<2..2^16-1>
614 * } ProtocolNameList;
615 */
616
617 /* Skip writing extension and list length for now */
618 p += 4;
619
620 for( cur = ssl->alpn_list; *cur != NULL; cur++ )
621 {
622 *p = (unsigned char)( strlen( *cur ) & 0xFF );
623 memcpy( p + 1, *cur, *p );
624 p += 1 + *p;
625 }
626
627 *olen = p - buf;
628
629 /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
630 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
631 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
632
633 /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */
634 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
635 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
636}
637#endif /* POLARSSL_SSL_ALPN */
638
Paul Bakker5121ce52009-01-03 21:22:43 +0000639static int ssl_write_client_hello( ssl_context *ssl )
640{
Paul Bakker23986e52011-04-24 08:57:21 +0000641 int ret;
Paul Bakkerd3edc862013-03-20 16:07:17 +0100642 size_t i, n, olen, ext_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000643 unsigned char *buf;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200644 unsigned char *p, *q;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200645#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000646 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200647#endif
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200648 const int *ciphersuites;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200649 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650
651 SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
652
Paul Bakkera9a028e2013-11-21 17:31:06 +0100653 if( ssl->f_rng == NULL )
654 {
655 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
656 return( POLARSSL_ERR_SSL_NO_RNG );
657 }
658
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100659#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +0000660 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100661#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000662 {
Paul Bakker993d11d2012-09-28 15:00:12 +0000663 ssl->major_ver = ssl->min_major_ver;
664 ssl->minor_ver = ssl->min_minor_ver;
Paul Bakker48916f92012-09-16 19:57:18 +0000665 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
Paul Bakker490ecc82011-10-06 13:04:09 +0000667 if( ssl->max_major_ver == 0 && ssl->max_minor_ver == 0 )
668 {
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200669 ssl->max_major_ver = SSL_MAX_MAJOR_VERSION;
670 ssl->max_minor_ver = SSL_MAX_MINOR_VERSION;
Paul Bakker490ecc82011-10-06 13:04:09 +0000671 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 /*
674 * 0 . 0 handshake type
675 * 1 . 3 handshake length
676 * 4 . 5 highest version supported
677 * 6 . 9 current UNIX time
678 * 10 . 37 random bytes
679 */
680 buf = ssl->out_msg;
681 p = buf + 4;
682
683 *p++ = (unsigned char) ssl->max_major_ver;
684 *p++ = (unsigned char) ssl->max_minor_ver;
685
686 SSL_DEBUG_MSG( 3, ( "client hello, max version: [%d:%d]",
687 buf[4], buf[5] ) );
688
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200689#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 t = time( NULL );
691 *p++ = (unsigned char)( t >> 24 );
692 *p++ = (unsigned char)( t >> 16 );
693 *p++ = (unsigned char)( t >> 8 );
694 *p++ = (unsigned char)( t );
695
696 SSL_DEBUG_MSG( 3, ( "client hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200697#else
698 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
699 return( ret );
700
701 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +0200702#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
Paul Bakkera3d195c2011-11-27 21:07:34 +0000704 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
705 return( ret );
706
707 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Paul Bakker48916f92012-09-16 19:57:18 +0000709 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 6, 32 );
712
713 /*
714 * 38 . 38 session id length
715 * 39 . 39+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +0000716 * 40+n . 41+n ciphersuitelist length
717 * 42+n . .. ciphersuitelist
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000718 * .. . .. compression methods length
719 * .. . .. compression methods
720 * .. . .. extensions length
721 * .. . .. extensions
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 */
Paul Bakker48916f92012-09-16 19:57:18 +0000723 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +0000724
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100725 if( n < 16 || n > 32 ||
726#if defined(POLARSSL_SSL_RENEGOTIATION)
727 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
728#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000729 ssl->handshake->resume == 0 )
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200730 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 n = 0;
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200732 }
733
Paul Bakkera503a632013-08-14 13:48:06 +0200734#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200735 /*
736 * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
737 * generate and include a Session ID in the TLS ClientHello."
738 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100739#if defined(POLARSSL_SSL_RENEGOTIATION)
740 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000741#endif
Manuel Pégourié-Gonnard51bccd32015-03-10 16:09:08 +0000742 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000743 if( ssl->session_negotiate->ticket != NULL &&
744 ssl->session_negotiate->ticket_len != 0 )
745 {
746 ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id, 32 );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200747
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000748 if( ret != 0 )
749 return( ret );
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200750
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +0000751 ssl->session_negotiate->length = n = 32;
752 }
Manuel Pégourié-Gonnard6377e412013-07-31 16:31:33 +0200753 }
Paul Bakkera503a632013-08-14 13:48:06 +0200754#endif /* POLARSSL_SSL_SESSION_TICKETS */
Paul Bakker5121ce52009-01-03 21:22:43 +0000755
756 *p++ = (unsigned char) n;
757
758 for( i = 0; i < n; i++ )
Paul Bakker48916f92012-09-16 19:57:18 +0000759 *p++ = ssl->session_negotiate->id[i];
Paul Bakker5121ce52009-01-03 21:22:43 +0000760
761 SSL_DEBUG_MSG( 3, ( "client hello, session id len.: %d", n ) );
762 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 39, n );
763
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200764 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Paul Bakker2fbefde2013-06-29 16:01:15 +0200765 n = 0;
766 q = p;
767
768 // Skip writing ciphersuite length for now
769 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Paul Bakker2fbefde2013-06-29 16:01:15 +0200771 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 {
Paul Bakker2fbefde2013-06-29 16:01:15 +0200773 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
774
775 if( ciphersuite_info == NULL )
776 continue;
777
778 if( ciphersuite_info->min_minor_ver > ssl->max_minor_ver ||
779 ciphersuite_info->max_minor_ver < ssl->min_minor_ver )
780 continue;
781
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100782 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
783 ciphersuite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
784 continue;
785
Paul Bakkere3166ce2011-01-27 17:40:50 +0000786 SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %2d",
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200787 ciphersuites[i] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
Paul Bakker2fbefde2013-06-29 16:01:15 +0200789 n++;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200790 *p++ = (unsigned char)( ciphersuites[i] >> 8 );
791 *p++ = (unsigned char)( ciphersuites[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000792 }
793
Manuel Pégourié-Gonnard5d9cde22015-01-22 10:49:41 +0000794 /*
795 * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
796 */
797#if defined(POLARSSL_SSL_RENEGOTIATION)
798 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
799#endif
800 {
801 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO >> 8 );
802 *p++ = (unsigned char)( SSL_EMPTY_RENEGOTIATION_INFO );
803 n++;
804 }
805
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +0200806 /* Some versions of OpenSSL don't handle it correctly if not at end */
807#if defined(POLARSSL_SSL_FALLBACK_SCSV)
808 if( ssl->fallback == SSL_IS_FALLBACK )
809 {
810 SSL_DEBUG_MSG( 3, ( "adding FALLBACK_SCSV" ) );
811 *p++ = (unsigned char)( SSL_FALLBACK_SCSV >> 8 );
812 *p++ = (unsigned char)( SSL_FALLBACK_SCSV );
813 n++;
814 }
815#endif
816
Paul Bakker2fbefde2013-06-29 16:01:15 +0200817 *q++ = (unsigned char)( n >> 7 );
818 *q++ = (unsigned char)( n << 1 );
819
820 SSL_DEBUG_MSG( 3, ( "client hello, got %d ciphersuites", n ) );
821
822
Paul Bakker2770fbd2012-07-03 13:30:23 +0000823#if defined(POLARSSL_ZLIB_SUPPORT)
824 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 2 ) );
825 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d %d",
Paul Bakker48916f92012-09-16 19:57:18 +0000826 SSL_COMPRESS_DEFLATE, SSL_COMPRESS_NULL ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000827
828 *p++ = 2;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000829 *p++ = SSL_COMPRESS_DEFLATE;
Paul Bakker48916f92012-09-16 19:57:18 +0000830 *p++ = SSL_COMPRESS_NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +0000831#else
Paul Bakker5121ce52009-01-03 21:22:43 +0000832 SSL_DEBUG_MSG( 3, ( "client hello, compress len.: %d", 1 ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +0000833 SSL_DEBUG_MSG( 3, ( "client hello, compress alg.: %d", SSL_COMPRESS_NULL ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000834
835 *p++ = 1;
836 *p++ = SSL_COMPRESS_NULL;
Paul Bakker9af723c2014-05-01 13:03:14 +0200837#endif /* POLARSSL_ZLIB_SUPPORT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000838
Paul Bakkerd3edc862013-03-20 16:07:17 +0100839 // First write extensions, then the total length
840 //
Paul Bakker0be444a2013-08-27 21:55:01 +0200841#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100842 ssl_write_hostname_ext( ssl, p + 2 + ext_len, &olen );
843 ext_len += olen;
Paul Bakker0be444a2013-08-27 21:55:01 +0200844#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000845
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100846#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100847 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
848 ext_len += olen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100849#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000850
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100851#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
852 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100853 ssl_write_signature_algorithms_ext( ssl, p + 2 + ext_len, &olen );
854 ext_len += olen;
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200855#endif
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000856
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200857#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerd3edc862013-03-20 16:07:17 +0100858 ssl_write_supported_elliptic_curves_ext( ssl, p + 2 + ext_len, &olen );
859 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100860
Paul Bakkerd3edc862013-03-20 16:07:17 +0100861 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
862 ext_len += olen;
Paul Bakker41c83d32013-03-20 14:39:14 +0100863#endif
864
Paul Bakker05decb22013-08-15 13:33:48 +0200865#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200866 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
867 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +0200868#endif
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +0200869
Paul Bakker1f2bc622013-08-15 13:45:55 +0200870#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200871 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
872 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +0200873#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200874
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100875#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
876 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
877 ext_len += olen;
878#endif
879
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200880#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
881 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
882 ext_len += olen;
883#endif
884
Simon Butcher643a9222015-10-01 01:17:10 +0100885#if defined(POLARSSL_SSL_ALPN)
886 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200887 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +0200888#endif
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +0200889
Simon Butcher643a9222015-10-01 01:17:10 +0100890#if defined(POLARSSL_SSL_SESSION_TICKETS)
891 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +0200892 ext_len += olen;
893#endif
894
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +0100895 /* olen unused if all extensions are disabled */
896 ((void) olen);
897
Paul Bakkerc3f177a2012-04-11 16:11:49 +0000898 SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %d",
899 ext_len ) );
900
Paul Bakkera7036632014-04-30 10:15:38 +0200901 if( ext_len > 0 )
902 {
903 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
904 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
905 p += ext_len;
906 }
Paul Bakker41c83d32013-03-20 14:39:14 +0100907
Paul Bakker5121ce52009-01-03 21:22:43 +0000908 ssl->out_msglen = p - buf;
909 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
910 ssl->out_msg[0] = SSL_HS_CLIENT_HELLO;
911
912 ssl->state++;
913
914 if( ( ret = ssl_write_record( ssl ) ) != 0 )
915 {
916 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
917 return( ret );
918 }
919
920 SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
921
922 return( 0 );
923}
924
Paul Bakker48916f92012-09-16 19:57:18 +0000925static int ssl_parse_renegotiation_info( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200926 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000927 size_t len )
928{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000929 int ret;
930
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100931#if defined(POLARSSL_SSL_RENEGOTIATION)
932 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +0000933 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100934 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000935 if( len != 1 + ssl->verify_data_len * 2 ||
936 buf[0] != ssl->verify_data_len * 2 ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100937 safer_memcmp( buf + 1,
938 ssl->own_verify_data, ssl->verify_data_len ) != 0 ||
939 safer_memcmp( buf + 1 + ssl->verify_data_len,
940 ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000941 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100942 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000943
944 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
945 return( ret );
946
Paul Bakker48916f92012-09-16 19:57:18 +0000947 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
948 }
949 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100950 else
951#endif /* POLARSSL_SSL_RENEGOTIATION */
952 {
953 if( len != 1 || buf[0] != 0x00 )
954 {
955 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
956
957 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
958 return( ret );
959
960 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
961 }
962
963 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
964 }
Paul Bakker48916f92012-09-16 19:57:18 +0000965
966 return( 0 );
967}
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200968
Paul Bakker05decb22013-08-15 13:33:48 +0200969#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200970static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +0200971 const unsigned char *buf,
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +0200972 size_t len )
973{
974 /*
975 * server should use the extension only if we did,
976 * and if so the server's value should match ours (and len is always 1)
977 */
978 if( ssl->mfl_code == SSL_MAX_FRAG_LEN_NONE ||
979 len != 1 ||
980 buf[0] != ssl->mfl_code )
981 {
982 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
983 }
984
985 return( 0 );
986}
Paul Bakker05decb22013-08-15 13:33:48 +0200987#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Paul Bakker48916f92012-09-16 19:57:18 +0000988
Paul Bakker1f2bc622013-08-15 13:45:55 +0200989#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200990static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
991 const unsigned char *buf,
992 size_t len )
993{
994 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_DISABLED ||
995 len != 0 )
996 {
997 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
998 }
999
1000 ((void) buf);
1001
1002 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
1003
1004 return( 0 );
1005}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001006#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001007
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001008#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1009static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
1010 const unsigned char *buf,
1011 size_t len )
1012{
1013 if( ssl->encrypt_then_mac == SSL_ETM_DISABLED ||
1014 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
1015 len != 0 )
1016 {
1017 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1018 }
1019
1020 ((void) buf);
1021
1022 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
1023
1024 return( 0 );
1025}
1026#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1027
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001028#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1029static int ssl_parse_extended_ms_ext( ssl_context *ssl,
1030 const unsigned char *buf,
1031 size_t len )
1032{
1033 if( ssl->extended_ms == SSL_EXTENDED_MS_DISABLED ||
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001034 ssl->minor_ver == SSL_MINOR_VERSION_0 ||
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001035 len != 0 )
1036 {
1037 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1038 }
1039
1040 ((void) buf);
1041
1042 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
1043
1044 return( 0 );
1045}
1046#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1047
Paul Bakkera503a632013-08-14 13:48:06 +02001048#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001049static int ssl_parse_session_ticket_ext( ssl_context *ssl,
1050 const unsigned char *buf,
1051 size_t len )
1052{
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001053 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED ||
1054 len != 0 )
1055 {
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001056 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02001057 }
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001058
1059 ((void) buf);
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02001060
1061 ssl->handshake->new_session_ticket = 1;
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001062
1063 return( 0 );
1064}
Paul Bakkera503a632013-08-14 13:48:06 +02001065#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001066
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001067#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001068static int ssl_parse_supported_point_formats_ext( ssl_context *ssl,
1069 const unsigned char *buf,
1070 size_t len )
1071{
1072 size_t list_size;
1073 const unsigned char *p;
1074
1075 list_size = buf[0];
1076 if( list_size + 1 != len )
1077 {
1078 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1079 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1080 }
1081
Manuel Pégourié-Gonnardfd35af12014-06-23 14:10:13 +02001082 p = buf + 1;
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001083 while( list_size > 0 )
1084 {
1085 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
1086 p[0] == POLARSSL_ECP_PF_COMPRESSED )
1087 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +02001088 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001089 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
1090 return( 0 );
1091 }
1092
1093 list_size--;
1094 p++;
1095 }
1096
Manuel Pégourié-Gonnard5c1f0322014-06-23 14:24:43 +02001097 SSL_DEBUG_MSG( 1, ( "no point format in common" ) );
1098 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001099}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001100#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001101
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001102#if defined(POLARSSL_SSL_ALPN)
1103static int ssl_parse_alpn_ext( ssl_context *ssl,
1104 const unsigned char *buf, size_t len )
1105{
1106 size_t list_len, name_len;
1107 const char **p;
1108
1109 /* If we didn't send it, the server shouldn't send it */
1110 if( ssl->alpn_list == NULL )
1111 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1112
1113 /*
1114 * opaque ProtocolName<1..2^8-1>;
1115 *
1116 * struct {
1117 * ProtocolName protocol_name_list<2..2^16-1>
1118 * } ProtocolNameList;
1119 *
1120 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
1121 */
1122
1123 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
1124 if( len < 4 )
1125 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1126
1127 list_len = ( buf[0] << 8 ) | buf[1];
1128 if( list_len != len - 2 )
1129 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1130
1131 name_len = buf[2];
1132 if( name_len != list_len - 1 )
1133 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1134
1135 /* Check that the server chosen protocol was in our list and save it */
1136 for( p = ssl->alpn_list; *p != NULL; p++ )
1137 {
1138 if( name_len == strlen( *p ) &&
1139 memcmp( buf + 3, *p, name_len ) == 0 )
1140 {
1141 ssl->alpn_chosen = *p;
1142 return( 0 );
1143 }
1144 }
1145
1146 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1147}
1148#endif /* POLARSSL_SSL_ALPN */
1149
Paul Bakker5121ce52009-01-03 21:22:43 +00001150static int ssl_parse_server_hello( ssl_context *ssl )
1151{
Paul Bakker2770fbd2012-07-03 13:30:23 +00001152 int ret, i, comp;
Paul Bakker23986e52011-04-24 08:57:21 +00001153 size_t n;
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001154 size_t ext_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001155 unsigned char *buf, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001156#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001157 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001158#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001159 int handshake_failure = 0;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001160 const ssl_ciphersuite_t *suite_info;
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001161#if defined(POLARSSL_DEBUG_C)
1162 uint32_t t;
1163#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001164
1165 SSL_DEBUG_MSG( 2, ( "=> parse server hello" ) );
1166
1167 /*
1168 * 0 . 0 handshake type
1169 * 1 . 3 handshake length
1170 * 4 . 5 protocol version
1171 * 6 . 9 UNIX time()
1172 * 10 . 37 random bytes
1173 */
1174 buf = ssl->in_msg;
1175
1176 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1177 {
1178 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1179 return( ret );
1180 }
1181
1182 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1183 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001184#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001185 if( ssl->renegotiation == SSL_RENEGOTIATION )
1186 {
Manuel Pégourié-Gonnard44ade652014-08-19 13:58:40 +02001187 ssl->renego_records_seen++;
1188
1189 if( ssl->renego_max_records >= 0 &&
1190 ssl->renego_records_seen > ssl->renego_max_records )
1191 {
1192 SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
1193 "but not honored by server" ) );
1194 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1195 }
1196
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001197 SSL_DEBUG_MSG( 1, ( "non-handshake message during renego" ) );
1198 return( POLARSSL_ERR_SSL_WAITING_SERVER_HELLO_RENEGO );
1199 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001200#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard65919622014-08-19 12:50:30 +02001201
Paul Bakker5121ce52009-01-03 21:22:43 +00001202 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001203 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 }
1205
1206 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1207 buf[4], buf[5] ) );
1208
1209 if( ssl->in_hslen < 42 ||
1210 buf[0] != SSL_HS_SERVER_HELLO ||
1211 buf[4] != SSL_MAJOR_VERSION_3 )
1212 {
1213 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001214 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 }
1216
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001217 if( buf[5] > ssl->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
1219 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001220 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 }
1222
1223 ssl->minor_ver = buf[5];
1224
Paul Bakker1d29fb52012-09-28 13:28:45 +00001225 if( ssl->minor_ver < ssl->min_minor_ver )
1226 {
1227 SSL_DEBUG_MSG( 1, ( "server only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001228 " [%d:%d] < [%d:%d]", ssl->major_ver,
1229 ssl->minor_ver, buf[4], buf[5] ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001230
1231 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1232 SSL_ALERT_MSG_PROTOCOL_VERSION );
1233
1234 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1235 }
1236
Paul Bakker1504af52012-02-11 16:17:43 +00001237#if defined(POLARSSL_DEBUG_C)
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001238 t = ( (uint32_t) buf[6] << 24 )
1239 | ( (uint32_t) buf[7] << 16 )
1240 | ( (uint32_t) buf[8] << 8 )
1241 | ( (uint32_t) buf[9] );
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001242 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakker87e5cda2012-01-14 18:14:15 +00001243#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Paul Bakker48916f92012-09-16 19:57:18 +00001245 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
1247 n = buf[38];
1248
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1250
Paul Bakker48916f92012-09-16 19:57:18 +00001251 if( n > 32 )
1252 {
1253 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1254 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1255 }
1256
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 /*
1258 * 38 . 38 session id length
1259 * 39 . 38+n session id
Paul Bakkere3166ce2011-01-27 17:40:50 +00001260 * 39+n . 40+n chosen ciphersuite
Paul Bakker5121ce52009-01-03 21:22:43 +00001261 * 41+n . 41+n chosen compression alg.
1262 * 42+n . 43+n extensions length
1263 * 44+n . 44+n+m extensions
1264 */
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001265 if( ssl->in_hslen > 43 + n )
Paul Bakker5121ce52009-01-03 21:22:43 +00001266 {
1267 ext_len = ( ( buf[42 + n] << 8 )
Paul Bakker48916f92012-09-16 19:57:18 +00001268 | ( buf[43 + n] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Paul Bakker48916f92012-09-16 19:57:18 +00001270 if( ( ext_len > 0 && ext_len < 4 ) ||
1271 ssl->in_hslen != 44 + n + ext_len )
1272 {
1273 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1274 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1275 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 }
Manuel Pégourié-Gonnardf7cdbc02014-10-17 17:02:10 +02001277 else if( ssl->in_hslen == 42 + n )
1278 {
1279 ext_len = 0;
1280 }
1281 else
1282 {
1283 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1284 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
1287 i = ( buf[39 + n] << 8 ) | buf[40 + n];
Paul Bakker2770fbd2012-07-03 13:30:23 +00001288 comp = buf[41 + n];
Paul Bakker5121ce52009-01-03 21:22:43 +00001289
Paul Bakker380da532012-04-18 16:10:25 +00001290 /*
1291 * Initialize update checksum functions
1292 */
Paul Bakker68884e32013-01-07 18:20:04 +01001293 ssl->transform_negotiate->ciphersuite_info = ssl_ciphersuite_from_id( i );
1294
1295 if( ssl->transform_negotiate->ciphersuite_info == NULL )
1296 {
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001297 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", i ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001298 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1299 }
Paul Bakker380da532012-04-18 16:10:25 +00001300
Manuel Pégourié-Gonnard3c599f12014-03-10 13:25:07 +01001301 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1304 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1305
1306 /*
1307 * Check if the session can be resumed
1308 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001309 if( ssl->handshake->resume == 0 || n == 0 ||
1310#if defined(POLARSSL_SSL_RENEGOTIATION)
1311 ssl->renegotiation != SSL_INITIAL_HANDSHAKE ||
1312#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001313 ssl->session_negotiate->ciphersuite != i ||
1314 ssl->session_negotiate->compression != comp ||
1315 ssl->session_negotiate->length != n ||
1316 memcmp( ssl->session_negotiate->id, buf + 39, n ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001317 {
1318 ssl->state++;
Paul Bakker0a597072012-09-25 21:55:46 +00001319 ssl->handshake->resume = 0;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001320#if defined(POLARSSL_HAVE_TIME)
Paul Bakker48916f92012-09-16 19:57:18 +00001321 ssl->session_negotiate->start = time( NULL );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001322#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001323 ssl->session_negotiate->ciphersuite = i;
1324 ssl->session_negotiate->compression = comp;
1325 ssl->session_negotiate->length = n;
1326 memcpy( ssl->session_negotiate->id, buf + 39, n );
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
1328 else
1329 {
1330 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001331
1332 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1333 {
1334 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1335 return( ret );
1336 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
1338
1339 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001340 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
Paul Bakkere3166ce2011-01-27 17:40:50 +00001342 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %d", i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: %d", buf[41 + n] ) );
1344
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01001345 suite_info = ssl_ciphersuite_from_id( ssl->session_negotiate->ciphersuite );
1346 if( suite_info == NULL ||
1347 ( ssl->arc4_disabled &&
1348 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 ) )
1349 {
1350 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1351 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1352 }
1353
1354
Paul Bakker5121ce52009-01-03 21:22:43 +00001355 i = 0;
1356 while( 1 )
1357 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001358 if( ssl->ciphersuite_list[ssl->minor_ver][i] == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
1360 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001361 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 }
1363
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001364 if( ssl->ciphersuite_list[ssl->minor_ver][i++] ==
1365 ssl->session_negotiate->ciphersuite )
1366 {
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 break;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
1370
Paul Bakker2770fbd2012-07-03 13:30:23 +00001371 if( comp != SSL_COMPRESS_NULL
1372#if defined(POLARSSL_ZLIB_SUPPORT)
1373 && comp != SSL_COMPRESS_DEFLATE
1374#endif
1375 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 {
1377 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001378 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 }
Paul Bakker48916f92012-09-16 19:57:18 +00001380 ssl->session_negotiate->compression = comp;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Paul Bakker48916f92012-09-16 19:57:18 +00001382 ext = buf + 44 + n;
1383
Manuel Pégourié-Gonnarda0528492013-07-16 17:26:28 +02001384 SSL_DEBUG_MSG( 2, ( "server hello, total extension length: %d", ext_len ) );
1385
Paul Bakker48916f92012-09-16 19:57:18 +00001386 while( ext_len )
1387 {
1388 unsigned int ext_id = ( ( ext[0] << 8 )
1389 | ( ext[1] ) );
1390 unsigned int ext_size = ( ( ext[2] << 8 )
1391 | ( ext[3] ) );
1392
1393 if( ext_size + 4 > ext_len )
1394 {
1395 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1396 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1397 }
1398
1399 switch( ext_id )
1400 {
1401 case TLS_EXT_RENEGOTIATION_INFO:
1402 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001403#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001404 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001405#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001406
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001407 if( ( ret = ssl_parse_renegotiation_info( ssl, ext + 4,
1408 ext_size ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001409 return( ret );
1410
1411 break;
1412
Paul Bakker05decb22013-08-15 13:33:48 +02001413#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001414 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1415 SSL_DEBUG_MSG( 3, ( "found max_fragment_length extension" ) );
1416
1417 if( ( ret = ssl_parse_max_fragment_length_ext( ssl,
1418 ext + 4, ext_size ) ) != 0 )
1419 {
1420 return( ret );
1421 }
1422
1423 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001424#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnardde600e52013-07-17 10:14:38 +02001425
Paul Bakker1f2bc622013-08-15 13:45:55 +02001426#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001427 case TLS_EXT_TRUNCATED_HMAC:
1428 SSL_DEBUG_MSG( 3, ( "found truncated_hmac extension" ) );
1429
1430 if( ( ret = ssl_parse_truncated_hmac_ext( ssl,
1431 ext + 4, ext_size ) ) != 0 )
1432 {
1433 return( ret );
1434 }
1435
1436 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001437#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001438
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001439#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1440 case TLS_EXT_ENCRYPT_THEN_MAC:
1441 SSL_DEBUG_MSG( 3, ( "found encrypt_then_mac extension" ) );
1442
1443 if( ( ret = ssl_parse_encrypt_then_mac_ext( ssl,
1444 ext + 4, ext_size ) ) != 0 )
1445 {
1446 return( ret );
1447 }
1448
1449 break;
1450#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1451
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001452#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1453 case TLS_EXT_EXTENDED_MASTER_SECRET:
1454 SSL_DEBUG_MSG( 3, ( "found extended_master_secret extension" ) );
1455
1456 if( ( ret = ssl_parse_extended_ms_ext( ssl,
1457 ext + 4, ext_size ) ) != 0 )
1458 {
1459 return( ret );
1460 }
1461
1462 break;
1463#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1464
Paul Bakkera503a632013-08-14 13:48:06 +02001465#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001466 case TLS_EXT_SESSION_TICKET:
1467 SSL_DEBUG_MSG( 3, ( "found session_ticket extension" ) );
1468
1469 if( ( ret = ssl_parse_session_ticket_ext( ssl,
1470 ext + 4, ext_size ) ) != 0 )
1471 {
1472 return( ret );
1473 }
1474
1475 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001476#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard60182ef2013-08-02 14:44:54 +02001477
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001478#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001479 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1480 SSL_DEBUG_MSG( 3, ( "found supported_point_formats extension" ) );
1481
1482 if( ( ret = ssl_parse_supported_point_formats_ext( ssl,
1483 ext + 4, ext_size ) ) != 0 )
1484 {
1485 return( ret );
1486 }
1487
1488 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001489#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001490
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02001491#if defined(POLARSSL_SSL_ALPN)
1492 case TLS_EXT_ALPN:
1493 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
1494
1495 if( ( ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size ) ) != 0 )
1496 return( ret );
1497
1498 break;
1499#endif /* POLARSSL_SSL_ALPN */
1500
Paul Bakker48916f92012-09-16 19:57:18 +00001501 default:
1502 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1503 ext_id ) );
1504 }
1505
1506 ext_len -= 4 + ext_size;
1507 ext += 4 + ext_size;
1508
1509 if( ext_len > 0 && ext_len < 4 )
1510 {
1511 SSL_DEBUG_MSG( 1, ( "bad server hello message" ) );
1512 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1513 }
1514 }
1515
1516 /*
1517 * Renegotiation security checks
1518 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001519 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1520 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001521 {
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001522 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1523 handshake_failure = 1;
Paul Bakkerf7abd422013-04-16 13:15:56 +02001524 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001525#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001526 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1527 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1528 renegotiation_info_seen == 0 )
1529 {
1530 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
1531 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001532 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001533 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1534 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1535 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001536 {
1537 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001538 handshake_failure = 1;
1539 }
1540 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1541 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1542 renegotiation_info_seen == 1 )
1543 {
1544 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1545 handshake_failure = 1;
1546 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001547#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001548
1549 if( handshake_failure == 1 )
1550 {
1551 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1552 return( ret );
1553
Paul Bakker48916f92012-09-16 19:57:18 +00001554 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO );
1555 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001556
1557 SSL_DEBUG_MSG( 2, ( "<= parse server hello" ) );
1558
1559 return( 0 );
1560}
1561
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001562#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1563 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001564static int ssl_parse_server_dh_params( ssl_context *ssl, unsigned char **p,
1565 unsigned char *end )
1566{
1567 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1568
Paul Bakker29e1f122013-04-16 13:07:56 +02001569 /*
1570 * Ephemeral DH parameters:
1571 *
1572 * struct {
1573 * opaque dh_p<1..2^16-1>;
1574 * opaque dh_g<1..2^16-1>;
1575 * opaque dh_Ys<1..2^16-1>;
1576 * } ServerDHParams;
1577 */
1578 if( ( ret = dhm_read_params( &ssl->handshake->dhm_ctx, p, end ) ) != 0 )
1579 {
1580 SSL_DEBUG_RET( 2, ( "dhm_read_params" ), ret );
1581 return( ret );
1582 }
1583
Manuel Pégourié-Gonnard9ea1b232015-06-29 15:27:52 +02001584 if( ssl->handshake->dhm_ctx.len < SSL_MIN_DHM_BYTES ||
Paul Bakker29e1f122013-04-16 13:07:56 +02001585 ssl->handshake->dhm_ctx.len > 512 )
1586 {
1587 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (DHM length)" ) );
1588 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1589 }
1590
1591 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1592 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1593 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
Paul Bakker29e1f122013-04-16 13:07:56 +02001594
1595 return( ret );
1596}
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +02001597#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1598 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001599
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001600#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001601 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001602 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
1603 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1604 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1605static int ssl_check_server_ecdh_params( const ssl_context *ssl )
1606{
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001607 const ecp_curve_info *curve_info;
1608
1609 curve_info = ecp_curve_info_from_grp_id( ssl->handshake->ecdh_ctx.grp.id );
1610 if( curve_info == NULL )
1611 {
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001612 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1613 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01001614 }
1615
1616 SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001617
Manuel Pégourié-Gonnard29f777e2015-04-03 17:26:50 +02001618#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001619 if( ! ssl_curve_is_acceptable( ssl, ssl->handshake->ecdh_ctx.grp.id ) )
1620#else
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001621 if( ssl->handshake->ecdh_ctx.grp.nbits < 163 ||
1622 ssl->handshake->ecdh_ctx.grp.nbits > 521 )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001623#endif
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001624 return( -1 );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001625
1626 SSL_DEBUG_ECP( 3, "ECDH: Qp", &ssl->handshake->ecdh_ctx.Qp );
1627
1628 return( 0 );
1629}
Paul Bakker9af723c2014-05-01 13:03:14 +02001630#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1631 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1632 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
1633 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1634 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001635
1636#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1637 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001638 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001639static int ssl_parse_server_ecdh_params( ssl_context *ssl,
1640 unsigned char **p,
1641 unsigned char *end )
1642{
1643 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1644
Paul Bakker29e1f122013-04-16 13:07:56 +02001645 /*
1646 * Ephemeral ECDH parameters:
1647 *
1648 * struct {
1649 * ECParameters curve_params;
1650 * ECPoint public;
1651 * } ServerECDHParams;
1652 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001653 if( ( ret = ecdh_read_params( &ssl->handshake->ecdh_ctx,
1654 (const unsigned char **) p, end ) ) != 0 )
1655 {
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02001656 SSL_DEBUG_RET( 1, ( "ecdh_read_params" ), ret );
Paul Bakker29e1f122013-04-16 13:07:56 +02001657 return( ret );
1658 }
1659
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001660 if( ssl_check_server_ecdh_params( ssl ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02001661 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001662 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (ECDHE curve)" ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001663 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1664 }
1665
Paul Bakker29e1f122013-04-16 13:07:56 +02001666 return( ret );
1667}
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001668#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001669 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
1670 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001671
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001672#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001673static int ssl_parse_server_psk_hint( ssl_context *ssl,
1674 unsigned char **p,
1675 unsigned char *end )
1676{
1677 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001678 size_t len;
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001679 ((void) ssl);
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001680
1681 /*
1682 * PSK parameters:
1683 *
1684 * opaque psk_identity_hint<0..2^16-1>;
1685 */
Manuel Pégourié-Gonnard59b9fe22013-10-15 11:55:33 +02001686 len = (*p)[0] << 8 | (*p)[1];
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001687 *p += 2;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001688
1689 if( (*p) + len > end )
1690 {
1691 SSL_DEBUG_MSG( 1, ( "bad server key exchange message (psk_identity_hint length)" ) );
1692 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1693 }
1694
1695 // TODO: Retrieve PSK identity hint and callback to app
1696 //
1697 *p += len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001698 ret = 0;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001699
1700 return( ret );
1701}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02001702#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001703
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001704#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
1705 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1706/*
1707 * Generate a pre-master secret and encrypt it with the server's RSA key
1708 */
1709static int ssl_write_encrypted_pms( ssl_context *ssl,
1710 size_t offset, size_t *olen,
1711 size_t pms_offset )
1712{
1713 int ret;
1714 size_t len_bytes = ssl->minor_ver == SSL_MINOR_VERSION_0 ? 0 : 2;
1715 unsigned char *p = ssl->handshake->premaster + pms_offset;
1716
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02001717 if( offset + len_bytes > SSL_MAX_CONTENT_LEN )
1718 {
1719 SSL_DEBUG_MSG( 1, ( "buffer too small for encrypted pms" ) );
1720 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1721 }
1722
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001723 /*
1724 * Generate (part of) the pre-master as
1725 * struct {
1726 * ProtocolVersion client_version;
1727 * opaque random[46];
1728 * } PreMasterSecret;
1729 */
1730 p[0] = (unsigned char) ssl->max_major_ver;
1731 p[1] = (unsigned char) ssl->max_minor_ver;
1732
1733 if( ( ret = ssl->f_rng( ssl->p_rng, p + 2, 46 ) ) != 0 )
1734 {
1735 SSL_DEBUG_RET( 1, "f_rng", ret );
1736 return( ret );
1737 }
1738
1739 ssl->handshake->pmslen = 48;
1740
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001741 if( ssl->session_negotiate->peer_cert == NULL )
1742 {
1743 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1744 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1745 }
1746
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001747 /*
1748 * Now write it out, encrypted
1749 */
1750 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1751 POLARSSL_PK_RSA ) )
1752 {
1753 SSL_DEBUG_MSG( 1, ( "certificate key type mismatch" ) );
1754 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1755 }
1756
1757 if( ( ret = pk_encrypt( &ssl->session_negotiate->peer_cert->pk,
1758 p, ssl->handshake->pmslen,
1759 ssl->out_msg + offset + len_bytes, olen,
1760 SSL_MAX_CONTENT_LEN - offset - len_bytes,
1761 ssl->f_rng, ssl->p_rng ) ) != 0 )
1762 {
1763 SSL_DEBUG_RET( 1, "rsa_pkcs1_encrypt", ret );
1764 return( ret );
1765 }
1766
1767#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
1768 defined(POLARSSL_SSL_PROTO_TLS1_2)
1769 if( len_bytes == 2 )
1770 {
1771 ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 );
1772 ssl->out_msg[offset+1] = (unsigned char)( *olen );
1773 *olen += 2;
1774 }
1775#endif
1776
1777 return( 0 );
1778}
1779#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
1780 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker29e1f122013-04-16 13:07:56 +02001781
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001782#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001783#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001784 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1785 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02001786static int ssl_parse_signature_algorithm( ssl_context *ssl,
1787 unsigned char **p,
1788 unsigned char *end,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001789 md_type_t *md_alg,
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001790 pk_type_t *pk_alg )
Paul Bakker29e1f122013-04-16 13:07:56 +02001791{
Paul Bakkerc5a79cc2013-06-26 15:08:35 +02001792 ((void) ssl);
Paul Bakker29e1f122013-04-16 13:07:56 +02001793 *md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001794 *pk_alg = POLARSSL_PK_NONE;
1795
1796 /* Only in TLS 1.2 */
1797 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
1798 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001799 return( 0 );
1800 }
Paul Bakker29e1f122013-04-16 13:07:56 +02001801
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001802 if( (*p) + 2 > end )
Paul Bakker29e1f122013-04-16 13:07:56 +02001803 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1804
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001805 /*
1806 * Get hash algorithm
1807 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001808 if( ( *md_alg = ssl_md_alg_from_hash( (*p)[0] ) ) == POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001809 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001810 SSL_DEBUG_MSG( 2, ( "Server used unsupported "
1811 "HashAlgorithm %d", *(p)[0] ) );
Paul Bakker29e1f122013-04-16 13:07:56 +02001812 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1813 }
1814
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001815 /*
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001816 * Get signature algorithm
1817 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001818 if( ( *pk_alg = ssl_pk_alg_from_sig( (*p)[1] ) ) == POLARSSL_PK_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02001819 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02001820 SSL_DEBUG_MSG( 2, ( "server used unsupported "
1821 "SignatureAlgorithm %d", (*p)[1] ) );
1822 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
Paul Bakker29e1f122013-04-16 13:07:56 +02001823 }
1824
1825 SSL_DEBUG_MSG( 2, ( "Server used SignatureAlgorithm %d", (*p)[1] ) );
1826 SSL_DEBUG_MSG( 2, ( "Server used HashAlgorithm %d", (*p)[0] ) );
1827 *p += 2;
1828
1829 return( 0 );
1830}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001831#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001832 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
1833 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001834#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02001835
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001836
1837#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1838 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1839static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1840{
1841 int ret;
1842 const ecp_keypair *peer_key;
1843
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02001844 if( ssl->session_negotiate->peer_cert == NULL )
1845 {
1846 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
1847 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
1848 }
1849
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001850 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk,
1851 POLARSSL_PK_ECKEY ) )
1852 {
1853 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1854 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1855 }
1856
1857 peer_key = pk_ec( ssl->session_negotiate->peer_cert->pk );
1858
1859 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx, peer_key,
1860 POLARSSL_ECDH_THEIRS ) ) != 0 )
1861 {
1862 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1863 return( ret );
1864 }
1865
1866 if( ssl_check_server_ecdh_params( ssl ) != 0 )
1867 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001868 SSL_DEBUG_MSG( 1, ( "bad server certificate (ECDH curve)" ) );
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001869 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE );
1870 }
1871
1872 return( ret );
1873}
1874#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
1875 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
1876
Paul Bakker41c83d32013-03-20 14:39:14 +01001877static int ssl_parse_server_key_exchange( ssl_context *ssl )
1878{
Paul Bakker23986e52011-04-24 08:57:21 +00001879 int ret;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001880 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001881 unsigned char *p, *end;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001882#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001883 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
1884 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001885 size_t sig_len, params_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00001886 unsigned char hash[64];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001887 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02001888 size_t hashlen;
1889 pk_type_t pk_alg = POLARSSL_PK_NONE;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001890#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
1892 SSL_DEBUG_MSG( 2, ( "=> parse server key exchange" ) );
1893
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001894#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001895 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 {
1897 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1898 ssl->state++;
1899 return( 0 );
1900 }
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02001901 ((void) p);
1902 ((void) end);
1903#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001905#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1906 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1907 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
1908 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
1909 {
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01001910 if( ( ret = ssl_get_ecdh_params_from_cert( ssl ) ) != 0 )
1911 {
1912 SSL_DEBUG_RET( 1, "ssl_get_ecdh_params_from_cert", ret );
1913 return( ret );
1914 }
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001915
1916 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
1917 ssl->state++;
1918 return( 0 );
1919 }
1920 ((void) p);
1921 ((void) end);
Paul Bakker9af723c2014-05-01 13:03:14 +02001922#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
1923 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01001924
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 if( ( ret = ssl_read_record( ssl ) ) != 0 )
1926 {
1927 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
1928 return( ret );
1929 }
1930
1931 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
1932 {
1933 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00001934 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935 }
1936
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001937 /*
1938 * ServerKeyExchange may be skipped with PSK and RSA-PSK when the server
1939 * doesn't use a psk_identity_hint
1940 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001941 if( ssl->in_msg[0] != SSL_HS_SERVER_KEY_EXCHANGE )
1942 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001943 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1944 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker188c8de2013-04-19 09:13:37 +02001945 {
1946 ssl->record_read = 1;
1947 goto exit;
1948 }
1949
1950 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1951 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952 }
1953
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001954 p = ssl->in_msg + 4;
1955 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001956 SSL_DEBUG_BUF( 3, "server key exchange", p, ssl->in_hslen - 4 );
Paul Bakker3b6a07b2013-03-21 11:56:50 +01001957
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001958#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1959 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1960 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
1961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
1963 {
1964 if( ssl_parse_server_psk_hint( ssl, &p, end ) != 0 )
1965 {
1966 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
1967 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1968 }
1969 } /* FALLTROUGH */
1970#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
1971
1972#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
1973 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
1974 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1975 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
1976 ; /* nothing more to do */
1977 else
1978#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED ||
1979 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
1980#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1981 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1982 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1983 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 {
Paul Bakker29e1f122013-04-16 13:07:56 +02001985 if( ssl_parse_server_dh_params( ssl, &p, end ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001986 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001987 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02001988 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
1989 }
1990 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991 else
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001992#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
1993 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001994#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001995 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001996 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
1997 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001998 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02001999 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002000 {
2001 if( ssl_parse_server_ecdh_params( ssl, &p, end ) != 0 )
2002 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002003 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2004 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2005 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002006 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002007 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002008#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002009 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002010 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002011 {
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002012 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002013 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002014 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002015
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002016#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002017 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2018 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker29e1f122013-04-16 13:07:56 +02002019 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002020 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2021 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002022 {
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002023 params_len = p - ( ssl->in_msg + 4 );
2024
Paul Bakker29e1f122013-04-16 13:07:56 +02002025 /*
2026 * Handle the digitally-signed structure
2027 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002028#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2029 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002030 {
Paul Bakker9659dae2013-08-28 16:21:34 +02002031 if( ssl_parse_signature_algorithm( ssl, &p, end,
2032 &md_alg, &pk_alg ) != 0 )
2033 {
2034 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2035 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2036 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002037
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002038 if( pk_alg != ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info ) )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002039 {
2040 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2041 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2042 }
Simon Butcher14400c82016-01-02 00:08:13 +00002043
2044#if !defined(POLARSSL_SSL_ENABLE_MD5_SIGNATURES)
2045 if( md_alg == POLARSSL_MD_MD5 )
2046 {
2047 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2048 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2049 }
2050#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00002051 }
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002052 else
Paul Bakker9af723c2014-05-01 13:03:14 +02002053#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002054#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2055 defined(POLARSSL_SSL_PROTO_TLS1_1)
2056 if( ssl->minor_ver < SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard09edda82013-08-19 13:50:33 +02002057 {
2058 pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002059
Paul Bakker9659dae2013-08-28 16:21:34 +02002060 /* Default hash for ECDSA is SHA-1 */
2061 if( pk_alg == POLARSSL_PK_ECDSA && md_alg == POLARSSL_MD_NONE )
2062 md_alg = POLARSSL_MD_SHA1;
2063 }
2064 else
2065#endif
2066 {
2067 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002068 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker9659dae2013-08-28 16:21:34 +02002069 }
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002070
2071 /*
2072 * Read signature
2073 */
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002074 sig_len = ( p[0] << 8 ) | p[1];
Paul Bakker1ef83d62012-04-11 12:09:53 +00002075 p += 2;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002076
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002077 if( end != p + sig_len )
Paul Bakker41c83d32013-03-20 14:39:14 +01002078 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002079 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01002080 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE );
2081 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002082
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002083 SSL_DEBUG_BUF( 3, "signature", p, sig_len );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002084
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002085 /*
2086 * Compute the hash that has been signed
2087 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002088#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2089 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002090 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002091 {
Paul Bakker29e1f122013-04-16 13:07:56 +02002092 md5_context md5;
2093 sha1_context sha1;
2094
Paul Bakker5b4af392014-06-26 12:09:34 +02002095 md5_init( &md5 );
2096 sha1_init( &sha1 );
2097
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002098 hashlen = 36;
2099
Paul Bakker29e1f122013-04-16 13:07:56 +02002100 /*
2101 * digitally-signed struct {
2102 * opaque md5_hash[16];
2103 * opaque sha_hash[20];
2104 * };
2105 *
2106 * md5_hash
2107 * MD5(ClientHello.random + ServerHello.random
2108 * + ServerParams);
2109 * sha_hash
2110 * SHA(ClientHello.random + ServerHello.random
2111 * + ServerParams);
2112 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002113 md5_starts( &md5 );
2114 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002115 md5_update( &md5, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002116 md5_finish( &md5, hash );
2117
2118 sha1_starts( &sha1 );
2119 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002120 sha1_update( &sha1, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002121 sha1_finish( &sha1, hash + 16 );
Paul Bakker5b4af392014-06-26 12:09:34 +02002122
2123 md5_free( &md5 );
2124 sha1_free( &sha1 );
Paul Bakker29e1f122013-04-16 13:07:56 +02002125 }
2126 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002127#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2128 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002129#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2130 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002131 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker29e1f122013-04-16 13:07:56 +02002132 {
2133 md_context_t ctx;
2134
Paul Bakker84bbeb52014-07-01 14:53:22 +02002135 md_init( &ctx );
2136
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002137 /* Info from md_alg will be used instead */
2138 hashlen = 0;
Paul Bakker29e1f122013-04-16 13:07:56 +02002139
2140 /*
2141 * digitally-signed struct {
2142 * opaque client_random[32];
2143 * opaque server_random[32];
2144 * ServerDHParams params;
2145 * };
2146 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002147 if( ( ret = md_init_ctx( &ctx,
2148 md_info_from_type( md_alg ) ) ) != 0 )
Paul Bakker29e1f122013-04-16 13:07:56 +02002149 {
2150 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2151 return( ret );
2152 }
2153
2154 md_starts( &ctx );
2155 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002156 md_update( &ctx, ssl->in_msg + 4, params_len );
Paul Bakker29e1f122013-04-16 13:07:56 +02002157 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002158 md_free( &ctx );
Paul Bakker29e1f122013-04-16 13:07:56 +02002159 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002160 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002161#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2162 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker29e1f122013-04-16 13:07:56 +02002163 {
Paul Bakker577e0062013-08-28 11:57:20 +02002164 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002165 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002166 }
Paul Bakker29e1f122013-04-16 13:07:56 +02002167
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002168 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2169 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker29e1f122013-04-16 13:07:56 +02002170
Manuel Pégourié-Gonnardbb564e02015-09-03 10:44:32 +02002171 if( ssl->session_negotiate->peer_cert == NULL )
2172 {
2173 SSL_DEBUG_MSG( 2, ( "certificate required" ) );
2174 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2175 }
2176
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002177 /*
2178 * Verify signature
2179 */
Manuel Pégourié-Gonnardf4842822013-08-22 16:03:41 +02002180 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002181 {
2182 SSL_DEBUG_MSG( 1, ( "bad server key exchange message" ) );
2183 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2184 }
2185
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002186 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
2187 md_alg, hash, hashlen, p, sig_len ) ) != 0 )
Manuel Pégourié-Gonnardefebb0a2013-08-19 12:06:38 +02002188 {
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002189 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002190 return( ret );
Paul Bakkerc3f177a2012-04-11 16:11:49 +00002191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002193#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002194 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2195 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002196
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002197exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002198 ssl->state++;
2199
2200 SSL_DEBUG_MSG( 2, ( "<= parse server key exchange" ) );
2201
2202 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002203}
2204
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002205#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2206 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
2207 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2208 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2209static int ssl_parse_certificate_request( ssl_context *ssl )
2210{
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002211 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2212
2213 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2214
2215 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2216 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2217 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2218 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2219 {
2220 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2221 ssl->state++;
2222 return( 0 );
2223 }
2224
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002225 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2226 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002227}
2228#else
Paul Bakker5121ce52009-01-03 21:22:43 +00002229static int ssl_parse_certificate_request( ssl_context *ssl )
2230{
2231 int ret;
Paul Bakker926af752012-11-23 13:38:07 +01002232 unsigned char *buf, *p;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002233 size_t n = 0, m = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002234 size_t cert_type_len = 0, dn_len = 0;
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002235 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002236
2237 SSL_DEBUG_MSG( 2, ( "=> parse certificate request" ) );
2238
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002239 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2240 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
2241 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2242 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2243 {
2244 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate request" ) );
2245 ssl->state++;
2246 return( 0 );
2247 }
2248
Paul Bakker5121ce52009-01-03 21:22:43 +00002249 /*
2250 * 0 . 0 handshake type
2251 * 1 . 3 handshake length
Paul Bakker926af752012-11-23 13:38:07 +01002252 * 4 . 4 cert type count
2253 * 5 .. m-1 cert types
2254 * m .. m+1 sig alg length (TLS 1.2 only)
2255 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002256 * n .. n+1 length of all DNs
2257 * n+2 .. n+3 length of DN 1
2258 * n+4 .. ... Distinguished Name #1
2259 * ... .. ... length of DN 2, etc.
2260 */
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002261 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002263 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2264 {
2265 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2266 return( ret );
2267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002269 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2270 {
2271 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2272 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2273 }
2274
2275 ssl->record_read = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +00002276 }
2277
2278 ssl->client_auth = 0;
2279 ssl->state++;
2280
2281 if( ssl->in_msg[0] == SSL_HS_CERTIFICATE_REQUEST )
2282 ssl->client_auth++;
2283
2284 SSL_DEBUG_MSG( 3, ( "got %s certificate request",
2285 ssl->client_auth ? "a" : "no" ) );
2286
Paul Bakker926af752012-11-23 13:38:07 +01002287 if( ssl->client_auth == 0 )
2288 goto exit;
2289
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002290 ssl->record_read = 0;
2291
Paul Bakker926af752012-11-23 13:38:07 +01002292 // TODO: handshake_failure alert for an anonymous server to request
2293 // client authentication
2294
2295 buf = ssl->in_msg;
Paul Bakkerf7abd422013-04-16 13:15:56 +02002296
Paul Bakker926af752012-11-23 13:38:07 +01002297 // Retrieve cert types
2298 //
2299 cert_type_len = buf[4];
2300 n = cert_type_len;
2301
2302 if( ssl->in_hslen < 6 + n )
2303 {
2304 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2305 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2306 }
2307
Paul Bakker73d44312013-05-22 13:56:26 +02002308 p = buf + 5;
Paul Bakker926af752012-11-23 13:38:07 +01002309 while( cert_type_len > 0 )
2310 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002311#if defined(POLARSSL_RSA_C)
2312 if( *p == SSL_CERT_TYPE_RSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002313 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker926af752012-11-23 13:38:07 +01002314 {
2315 ssl->handshake->cert_type = SSL_CERT_TYPE_RSA_SIGN;
2316 break;
2317 }
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002318 else
2319#endif
2320#if defined(POLARSSL_ECDSA_C)
2321 if( *p == SSL_CERT_TYPE_ECDSA_SIGN &&
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002322 pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002323 {
2324 ssl->handshake->cert_type = SSL_CERT_TYPE_ECDSA_SIGN;
2325 break;
2326 }
2327 else
2328#endif
2329 {
2330 ; /* Unsupported cert type, ignore */
2331 }
Paul Bakker926af752012-11-23 13:38:07 +01002332
2333 cert_type_len--;
2334 p++;
2335 }
2336
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002337#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002338 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2339 {
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002340 /* Ignored, see comments about hash in write_certificate_verify */
2341 // TODO: should check the signature part against our pk_key though
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002342 size_t sig_alg_len = ( ( buf[5 + n] << 8 )
2343 | ( buf[6 + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002344
2345 p = buf + 7 + n;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002346 m += 2;
Paul Bakker926af752012-11-23 13:38:07 +01002347 n += sig_alg_len;
2348
2349 if( ssl->in_hslen < 6 + n )
2350 {
2351 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2352 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2353 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002354 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002355#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker926af752012-11-23 13:38:07 +01002356
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002357 /* Ignore certificate_authorities, we only have one cert anyway */
2358 // TODO: should not send cert if no CA matches
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002359 dn_len = ( ( buf[5 + m + n] << 8 )
2360 | ( buf[6 + m + n] ) );
Paul Bakker926af752012-11-23 13:38:07 +01002361
2362 n += dn_len;
Paul Bakker9c94cdd2013-01-22 13:45:33 +01002363 if( ssl->in_hslen != 7 + m + n )
Paul Bakker926af752012-11-23 13:38:07 +01002364 {
2365 SSL_DEBUG_MSG( 1, ( "bad certificate request message" ) );
2366 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST );
2367 }
2368
2369exit:
Paul Bakker5121ce52009-01-03 21:22:43 +00002370 SSL_DEBUG_MSG( 2, ( "<= parse certificate request" ) );
2371
2372 return( 0 );
2373}
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002374#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2375 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2376 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2377 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
2379static int ssl_parse_server_hello_done( ssl_context *ssl )
2380{
2381 int ret;
2382
2383 SSL_DEBUG_MSG( 2, ( "=> parse server hello done" ) );
2384
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002385 if( ssl->record_read == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 {
2387 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2388 {
2389 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2390 return( ret );
2391 }
2392
2393 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2394 {
2395 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002396 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 }
2398 }
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002399 ssl->record_read = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002400
2401 if( ssl->in_hslen != 4 ||
2402 ssl->in_msg[0] != SSL_HS_SERVER_HELLO_DONE )
2403 {
2404 SSL_DEBUG_MSG( 1, ( "bad server hello done message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002405 return( POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 }
2407
2408 ssl->state++;
2409
2410 SSL_DEBUG_MSG( 2, ( "<= parse server hello done" ) );
2411
2412 return( 0 );
2413}
2414
2415static int ssl_write_client_key_exchange( ssl_context *ssl )
2416{
Paul Bakker23986e52011-04-24 08:57:21 +00002417 int ret;
2418 size_t i, n;
Paul Bakker41c83d32013-03-20 14:39:14 +01002419 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
2421 SSL_DEBUG_MSG( 2, ( "=> write client key exchange" ) );
2422
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002423#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002424 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 {
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 /*
2427 * DHM key exchange -- send G^X mod P
2428 */
Paul Bakker48916f92012-09-16 19:57:18 +00002429 n = ssl->handshake->dhm_ctx.len;
Paul Bakker5121ce52009-01-03 21:22:43 +00002430
2431 ssl->out_msg[4] = (unsigned char)( n >> 8 );
2432 ssl->out_msg[5] = (unsigned char)( n );
2433 i = 6;
2434
Paul Bakker29b64762012-09-25 09:36:44 +00002435 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002436 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 &ssl->out_msg[i], n,
2438 ssl->f_rng, ssl->p_rng );
2439 if( ret != 0 )
2440 {
2441 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2442 return( ret );
2443 }
2444
Paul Bakker48916f92012-09-16 19:57:18 +00002445 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2446 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002448 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Paul Bakker48916f92012-09-16 19:57:18 +00002450 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2451 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002452 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002453 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 {
2455 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2456 return( ret );
2457 }
2458
Paul Bakker48916f92012-09-16 19:57:18 +00002459 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 }
2461 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002462#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002463#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002464 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2465 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2466 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002467 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002468 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2469 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2470 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002471 {
2472 /*
2473 * ECDH key exchange -- send client public value
2474 */
2475 i = 4;
2476
2477 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx,
2478 &n,
2479 &ssl->out_msg[i], 1000,
2480 ssl->f_rng, ssl->p_rng );
2481 if( ret != 0 )
2482 {
2483 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2484 return( ret );
2485 }
2486
2487 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2488
2489 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2490 &ssl->handshake->pmslen,
2491 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002492 POLARSSL_MPI_MAX_SIZE,
2493 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002494 {
2495 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2496 return( ret );
2497 }
2498
2499 SSL_DEBUG_MPI( 3, "ECDH: z", &ssl->handshake->ecdh_ctx.z );
2500 }
2501 else
Manuel Pégourié-Gonnard20846b12013-08-19 12:32:12 +02002502#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnardd18cc572013-12-11 17:45:46 +01002503 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2504 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2505 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002506#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002507 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002508 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002509 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2510 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002511 {
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002512 /*
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002513 * opaque psk_identity<0..2^16-1>;
2514 */
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002515 if( ssl->psk == NULL || ssl->psk_identity == NULL )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002516 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2517
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002518 i = 4;
2519 n = ssl->psk_identity_len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002520
2521 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2522 {
2523 SSL_DEBUG_MSG( 1, ( "psk identity too long or "
2524 "SSL buffer too short" ) );
2525 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2526 }
2527
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002528 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2529 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002531 memcpy( ssl->out_msg + i, ssl->psk_identity, ssl->psk_identity_len );
2532 i += ssl->psk_identity_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002534#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002535 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002537 n = 0;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002538 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002539 else
2540#endif
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002541#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2542 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2543 {
2544 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 2 ) ) != 0 )
2545 return( ret );
2546 }
2547 else
2548#endif
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002550 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002552 /*
2553 * ClientDiffieHellmanPublic public (DHM send G^X mod P)
2554 */
2555 n = ssl->handshake->dhm_ctx.len;
Manuel Pégourié-Gonnard65125542015-08-27 16:37:35 +02002556
2557 if( i + 2 + n > SSL_MAX_CONTENT_LEN )
2558 {
2559 SSL_DEBUG_MSG( 1, ( "psk identity or DHM size too long"
2560 " or SSL buffer too short" ) );
2561 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
2562 }
2563
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002564 ssl->out_msg[i++] = (unsigned char)( n >> 8 );
2565 ssl->out_msg[i++] = (unsigned char)( n );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002567 ret = dhm_make_public( &ssl->handshake->dhm_ctx,
Paul Bakker68881672013-10-15 13:24:01 +02002568 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002569 &ssl->out_msg[i], n,
2570 ssl->f_rng, ssl->p_rng );
2571 if( ret != 0 )
2572 {
2573 SSL_DEBUG_RET( 1, "dhm_make_public", ret );
2574 return( ret );
2575 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002576 }
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002577 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002579#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002580 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002581 {
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002582 /*
2583 * ClientECDiffieHellmanPublic public;
2584 */
2585 ret = ecdh_make_public( &ssl->handshake->ecdh_ctx, &n,
2586 &ssl->out_msg[i], SSL_MAX_CONTENT_LEN - i,
2587 ssl->f_rng, ssl->p_rng );
2588 if( ret != 0 )
2589 {
2590 SSL_DEBUG_RET( 1, "ecdh_make_public", ret );
2591 return( ret );
2592 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002593
Manuel Pégourié-Gonnard72fb62d2013-10-14 14:01:58 +02002594 SSL_DEBUG_ECP( 3, "ECDH: Q", &ssl->handshake->ecdh_ctx.Q );
2595 }
2596 else
2597#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
2598 {
2599 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002600 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601 }
2602
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002603 if( ( ret = ssl_psk_derive_premaster( ssl,
2604 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002605 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002606 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002607 return( ret );
2608 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002609 }
2610 else
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002611#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002612#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
Paul Bakkered27a042013-04-18 22:46:23 +02002613 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002614 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002615 i = 4;
2616 if( ( ret = ssl_write_encrypted_pms( ssl, i, &n, 0 ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +00002617 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
Paul Bakkered27a042013-04-18 22:46:23 +02002619 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002620#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
Paul Bakkered27a042013-04-18 22:46:23 +02002621 {
2622 ((void) ciphersuite_info);
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002623 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002624 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakkered27a042013-04-18 22:46:23 +02002625 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002626
Paul Bakker5121ce52009-01-03 21:22:43 +00002627 ssl->out_msglen = i + n;
2628 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2629 ssl->out_msg[0] = SSL_HS_CLIENT_KEY_EXCHANGE;
2630
2631 ssl->state++;
2632
2633 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2634 {
2635 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2636 return( ret );
2637 }
2638
2639 SSL_DEBUG_MSG( 2, ( "<= write client key exchange" ) );
2640
2641 return( 0 );
2642}
2643
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2645 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002646 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2647 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002648static int ssl_write_certificate_verify( ssl_context *ssl )
2649{
Paul Bakkered27a042013-04-18 22:46:23 +02002650 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002651 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
2653 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2654
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002655 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2656 {
2657 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2658 return( ret );
2659 }
2660
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002661 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002662 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002663 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002664 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002665 {
2666 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2667 ssl->state++;
2668 return( 0 );
2669 }
2670
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002671 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2672 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673}
2674#else
2675static int ssl_write_certificate_verify( ssl_context *ssl )
2676{
2677 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2678 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2679 size_t n = 0, offset = 0;
2680 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002681 unsigned char *hash_start = hash;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002682 md_type_t md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002683 unsigned int hashlen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684
2685 SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
2686
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02002687 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2688 {
2689 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2690 return( ret );
2691 }
2692
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002693 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002694 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002695 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002696 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2697 {
2698 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2699 ssl->state++;
2700 return( 0 );
2701 }
2702
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002703 if( ssl->client_auth == 0 || ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
2705 SSL_DEBUG_MSG( 2, ( "<= skip write certificate verify" ) );
2706 ssl->state++;
2707 return( 0 );
2708 }
2709
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002710 if( ssl_own_key( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 {
Paul Bakkereb2c6582012-09-27 19:15:01 +00002712 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2713 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00002714 }
2715
2716 /*
2717 * Make an RSA signature of the handshake digests
2718 */
Paul Bakker48916f92012-09-16 19:57:18 +00002719 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002720
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002721#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2722 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker926af752012-11-23 13:38:07 +01002723 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002724 {
Paul Bakker926af752012-11-23 13:38:07 +01002725 /*
2726 * digitally-signed struct {
2727 * opaque md5_hash[16];
2728 * opaque sha_hash[20];
2729 * };
2730 *
2731 * md5_hash
2732 * MD5(handshake_messages);
2733 *
2734 * sha_hash
2735 * SHA(handshake_messages);
2736 */
2737 hashlen = 36;
Paul Bakkerc70b9822013-04-07 22:00:46 +02002738 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002739
2740 /*
2741 * For ECDSA, default hash is SHA-1 only
2742 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002743 if( pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECDSA ) )
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002744 {
2745 hash_start += 16;
2746 hashlen -= 16;
2747 md_alg = POLARSSL_MD_SHA1;
2748 }
Paul Bakker926af752012-11-23 13:38:07 +01002749 }
2750 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2752 POLARSSL_SSL_PROTO_TLS1_1 */
2753#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2754 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002755 {
2756 /*
2757 * digitally-signed struct {
2758 * opaque handshake_messages[handshake_messages_length];
2759 * };
2760 *
2761 * Taking shortcut here. We assume that the server always allows the
2762 * PRF Hash function and has sent it in the allowed signature
2763 * algorithms list received in the Certificate Request message.
2764 *
2765 * Until we encounter a server that does not, we will take this
2766 * shortcut.
2767 *
2768 * Reason: Otherwise we should have running hashes for SHA512 and SHA224
2769 * in order to satisfy 'weird' needs from the server side.
2770 */
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002771 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2772 POLARSSL_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002773 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002774 md_alg = POLARSSL_MD_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002775 ssl->out_msg[4] = SSL_HASH_SHA384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002776 }
2777 else
2778 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02002779 md_alg = POLARSSL_MD_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002780 ssl->out_msg[4] = SSL_HASH_SHA256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002781 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002782 ssl->out_msg[5] = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00002783
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002784 /* Info from md_alg will be used instead */
2785 hashlen = 0;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002786 offset = 2;
2787 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002788 else
2789#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002790 {
2791 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002792 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002793 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00002794
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002795 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002796 ssl->out_msg + 6 + offset, &n,
2797 ssl->f_rng, ssl->p_rng ) ) != 0 )
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002798 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002799 SSL_DEBUG_RET( 1, "pk_sign", ret );
2800 return( ret );
Manuel Pégourié-Gonnard76c18a12013-08-20 16:50:40 +02002801 }
Paul Bakker926af752012-11-23 13:38:07 +01002802
Paul Bakker1ef83d62012-04-11 12:09:53 +00002803 ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 );
2804 ssl->out_msg[5 + offset] = (unsigned char)( n );
Paul Bakker5121ce52009-01-03 21:22:43 +00002805
Paul Bakker1ef83d62012-04-11 12:09:53 +00002806 ssl->out_msglen = 6 + n + offset;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2808 ssl->out_msg[0] = SSL_HS_CERTIFICATE_VERIFY;
2809
2810 ssl->state++;
2811
2812 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2813 {
2814 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2815 return( ret );
2816 }
2817
2818 SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
2819
Paul Bakkered27a042013-04-18 22:46:23 +02002820 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002822#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2823 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2824 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002825
Paul Bakkera503a632013-08-14 13:48:06 +02002826#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002827static int ssl_parse_new_session_ticket( ssl_context *ssl )
2828{
2829 int ret;
2830 uint32_t lifetime;
2831 size_t ticket_len;
2832 unsigned char *ticket;
2833
2834 SSL_DEBUG_MSG( 2, ( "=> parse new session ticket" ) );
2835
2836 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2837 {
2838 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2839 return( ret );
2840 }
2841
2842 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2843 {
2844 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2845 return( POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE );
2846 }
2847
2848 /*
2849 * struct {
2850 * uint32 ticket_lifetime_hint;
2851 * opaque ticket<0..2^16-1>;
2852 * } NewSessionTicket;
2853 *
2854 * 0 . 0 handshake message type
2855 * 1 . 3 handshake message length
2856 * 4 . 7 ticket_lifetime_hint
2857 * 8 . 9 ticket_len (n)
2858 * 10 . 9+n ticket content
2859 */
2860 if( ssl->in_msg[0] != SSL_HS_NEW_SESSION_TICKET ||
2861 ssl->in_hslen < 10 )
2862 {
2863 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2864 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2865 }
2866
2867 lifetime = ( ssl->in_msg[4] << 24 ) | ( ssl->in_msg[5] << 16 ) |
2868 ( ssl->in_msg[6] << 8 ) | ( ssl->in_msg[7] );
2869
2870 ticket_len = ( ssl->in_msg[8] << 8 ) | ( ssl->in_msg[9] );
2871
2872 if( ticket_len + 10 != ssl->in_hslen )
2873 {
2874 SSL_DEBUG_MSG( 1, ( "bad new session ticket message" ) );
2875 return( POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET );
2876 }
2877
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002878 SSL_DEBUG_MSG( 3, ( "ticket length: %d", ticket_len ) );
2879
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002880 /* We're not waiting for a NewSessionTicket message any more */
2881 ssl->handshake->new_session_ticket = 0;
2882
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002883 /*
2884 * Zero-length ticket means the server changed his mind and doesn't want
2885 * to send a ticket after all, so just forget it
2886 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002887 if( ticket_len == 0 )
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002888 return( 0 );
2889
Paul Bakker34617722014-06-13 17:20:13 +02002890 polarssl_zeroize( ssl->session_negotiate->ticket,
2891 ssl->session_negotiate->ticket_len );
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002892 polarssl_free( ssl->session_negotiate->ticket );
2893 ssl->session_negotiate->ticket = NULL;
2894 ssl->session_negotiate->ticket_len = 0;
2895
2896 if( ( ticket = polarssl_malloc( ticket_len ) ) == NULL )
2897 {
2898 SSL_DEBUG_MSG( 1, ( "ticket malloc failed" ) );
2899 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
2900 }
2901
2902 memcpy( ticket, ssl->in_msg + 10, ticket_len );
2903
2904 ssl->session_negotiate->ticket = ticket;
2905 ssl->session_negotiate->ticket_len = ticket_len;
2906 ssl->session_negotiate->ticket_lifetime = lifetime;
2907
2908 /*
2909 * RFC 5077 section 3.4:
2910 * "If the client receives a session ticket from the server, then it
2911 * discards any Session ID that was sent in the ServerHello."
2912 */
2913 SSL_DEBUG_MSG( 3, ( "ticket in use, discarding session id" ) );
2914 ssl->session_negotiate->length = 0;
2915
2916 SSL_DEBUG_MSG( 2, ( "<= parse new session ticket" ) );
2917
2918 return( 0 );
2919}
Paul Bakkera503a632013-08-14 13:48:06 +02002920#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02002921
Paul Bakker5121ce52009-01-03 21:22:43 +00002922/*
Paul Bakker1961b702013-01-25 14:49:24 +01002923 * SSL handshake -- client side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002924 */
Paul Bakker1961b702013-01-25 14:49:24 +01002925int ssl_handshake_client_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002926{
2927 int ret = 0;
2928
Paul Bakker1961b702013-01-25 14:49:24 +01002929 if( ssl->state == SSL_HANDSHAKE_OVER )
2930 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
Paul Bakker1961b702013-01-25 14:49:24 +01002932 SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) );
2933
2934 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2935 return( ret );
2936
2937 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002938 {
Paul Bakker1961b702013-01-25 14:49:24 +01002939 case SSL_HELLO_REQUEST:
2940 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 break;
2942
Paul Bakker1961b702013-01-25 14:49:24 +01002943 /*
2944 * ==> ClientHello
2945 */
2946 case SSL_CLIENT_HELLO:
2947 ret = ssl_write_client_hello( ssl );
2948 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Paul Bakker1961b702013-01-25 14:49:24 +01002950 /*
2951 * <== ServerHello
2952 * Certificate
2953 * ( ServerKeyExchange )
2954 * ( CertificateRequest )
2955 * ServerHelloDone
2956 */
2957 case SSL_SERVER_HELLO:
2958 ret = ssl_parse_server_hello( ssl );
2959 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002960
Paul Bakker1961b702013-01-25 14:49:24 +01002961 case SSL_SERVER_CERTIFICATE:
2962 ret = ssl_parse_certificate( ssl );
2963 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002964
Paul Bakker1961b702013-01-25 14:49:24 +01002965 case SSL_SERVER_KEY_EXCHANGE:
2966 ret = ssl_parse_server_key_exchange( ssl );
2967 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002968
Paul Bakker1961b702013-01-25 14:49:24 +01002969 case SSL_CERTIFICATE_REQUEST:
2970 ret = ssl_parse_certificate_request( ssl );
2971 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Paul Bakker1961b702013-01-25 14:49:24 +01002973 case SSL_SERVER_HELLO_DONE:
2974 ret = ssl_parse_server_hello_done( ssl );
2975 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Paul Bakker1961b702013-01-25 14:49:24 +01002977 /*
2978 * ==> ( Certificate/Alert )
2979 * ClientKeyExchange
2980 * ( CertificateVerify )
2981 * ChangeCipherSpec
2982 * Finished
2983 */
2984 case SSL_CLIENT_CERTIFICATE:
2985 ret = ssl_write_certificate( ssl );
2986 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002987
Paul Bakker1961b702013-01-25 14:49:24 +01002988 case SSL_CLIENT_KEY_EXCHANGE:
2989 ret = ssl_write_client_key_exchange( ssl );
2990 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002991
Paul Bakker1961b702013-01-25 14:49:24 +01002992 case SSL_CERTIFICATE_VERIFY:
2993 ret = ssl_write_certificate_verify( ssl );
2994 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002995
Paul Bakker1961b702013-01-25 14:49:24 +01002996 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2997 ret = ssl_write_change_cipher_spec( ssl );
2998 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00002999
Paul Bakker1961b702013-01-25 14:49:24 +01003000 case SSL_CLIENT_FINISHED:
3001 ret = ssl_write_finished( ssl );
3002 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003003
Paul Bakker1961b702013-01-25 14:49:24 +01003004 /*
Manuel Pégourié-Gonnarda5cc6022013-07-31 12:58:16 +02003005 * <== ( NewSessionTicket )
3006 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003007 * Finished
3008 */
3009 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003010#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003011 if( ssl->handshake->new_session_ticket != 0 )
3012 ret = ssl_parse_new_session_ticket( ssl );
3013 else
Paul Bakkera503a632013-08-14 13:48:06 +02003014#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003015 ret = ssl_parse_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003016 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003017
Paul Bakker1961b702013-01-25 14:49:24 +01003018 case SSL_SERVER_FINISHED:
3019 ret = ssl_parse_finished( ssl );
3020 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003021
Paul Bakker1961b702013-01-25 14:49:24 +01003022 case SSL_FLUSH_BUFFERS:
3023 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3024 ssl->state = SSL_HANDSHAKE_WRAPUP;
3025 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Paul Bakker1961b702013-01-25 14:49:24 +01003027 case SSL_HANDSHAKE_WRAPUP:
3028 ssl_handshake_wrapup( ssl );
3029 break;
Paul Bakker48916f92012-09-16 19:57:18 +00003030
Paul Bakker1961b702013-01-25 14:49:24 +01003031 default:
3032 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3033 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
3034 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003035
3036 return( ret );
3037}
Paul Bakker9af723c2014-05-01 13:03:14 +02003038#endif /* POLARSSL_SSL_CLI_C */