blob: 59ff3ac2e67f16894fa5c6bb01a9e43750dcd821 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Paul Bakker0be444a2013-08-27 21:55:01 +0200354#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200356 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100357 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200358 */
359static int ssl_sni_wrapper( ssl_context *ssl,
360 const unsigned char* name, size_t len )
361{
362 int ret;
363 ssl_key_cert *key_cert_ori = ssl->key_cert;
364
365 ssl->key_cert = NULL;
366 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200367 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200368
369 ssl->key_cert = key_cert_ori;
370
371 return( ret );
372}
373
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000375 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 size_t len )
377{
378 int ret;
379 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000380 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100382 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
383
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
385 if( servername_list_size + 2 != len )
386 {
387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
389 }
390
391 p = buf + 2;
392 while( servername_list_size > 0 )
393 {
394 hostname_len = ( ( p[1] << 8 ) | p[2] );
395 if( hostname_len + 3 > servername_list_size )
396 {
397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
399 }
400
401 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
402 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200403 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 if( ret != 0 )
405 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100406 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
408 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
410 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000411 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 }
413
414 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000415 p += hostname_len + 3;
416 }
417
418 if( servername_list_size != 0 )
419 {
420 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000422 }
423
424 return( 0 );
425}
Paul Bakker0be444a2013-08-27 21:55:01 +0200426#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427
Paul Bakker48916f92012-09-16 19:57:18 +0000428static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000429 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000430 size_t len )
431{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000432 int ret;
433
Paul Bakker48916f92012-09-16 19:57:18 +0000434 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
435 {
436 if( len != 1 || buf[0] != 0x0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000439
440 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
441 return( ret );
442
Paul Bakker48916f92012-09-16 19:57:18 +0000443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
447 }
448 else
449 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100450 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 if( len != 1 + ssl->verify_data_len ||
452 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100453 safer_memcmp( buf + 1, ssl->peer_verify_data,
454 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000455 {
456 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000457
458 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
459 return( ret );
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
462 }
463 }
464
465 return( 0 );
466}
467
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000469static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
472{
473 size_t sig_alg_list_size;
474 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200475 const unsigned char *end = buf + len;
476 const int *md_cur;
477
Paul Bakker23f36802012-09-28 14:15:14 +0000478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200481 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200487 /*
488 * For now, ignore the SignatureAlgorithm part and rely on offered
489 * ciphersuites only for that part. To be fixed later.
490 *
491 * So, just look at the HashAlgorithm part.
492 */
493 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
494 for( p = buf + 2; p < end; p += 2 ) {
495 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
496 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200497 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200498 }
Paul Bakker23f36802012-09-28 14:15:14 +0000499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
501
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200502 /* Some key echanges do not need signatures at all */
503 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
504 return( 0 );
505
506have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000507 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
508 ssl->handshake->sig_alg ) );
509
510 return( 0 );
511}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000513
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200514#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100518{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200519 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100520 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522
523 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
524 if( list_size + 2 != len ||
525 list_size % 2 != 0 )
526 {
527 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
529 }
530
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200531 /* Should never happen unless client duplicates the extension */
532 if( ssl->handshake->curves != NULL )
533 {
534 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
536 }
537
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100538 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 * and leave room for a final 0 */
540 our_size = list_size / 2 + 1;
541 if( our_size > POLARSSL_ECP_DP_MAX )
542 our_size = POLARSSL_ECP_DP_MAX;
543
544 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
545 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
546
Paul Bakker9af723c2014-05-01 13:03:14 +0200547 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200548 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 ssl->handshake->curves = curves;
550
Paul Bakker41c83d32013-03-20 14:39:14 +0100551 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200554 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200555
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200558 *curves++ = curve_info;
559 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100560 }
561
562 list_size -= 2;
563 p += 2;
564 }
565
566 return( 0 );
567}
568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200569static int ssl_parse_supported_point_formats( ssl_context *ssl,
570 const unsigned char *buf,
571 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100572{
573 size_t list_size;
574 const unsigned char *p;
575
576 list_size = buf[0];
577 if( list_size + 1 != len )
578 {
579 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
581 }
582
583 p = buf + 2;
584 while( list_size > 0 )
585 {
586 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
587 p[0] == POLARSSL_ECP_PF_COMPRESSED )
588 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200589 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200590 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 return( 0 );
592 }
593
594 list_size--;
595 p++;
596 }
597
598 return( 0 );
599}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200600#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100601
Paul Bakker05decb22013-08-15 13:33:48 +0200602#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200603static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
606{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200607 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200613 ssl->session_negotiate->mfl_code = buf[0];
614
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615 return( 0 );
616}
Paul Bakker05decb22013-08-15 13:33:48 +0200617#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200618
Paul Bakker1f2bc622013-08-15 13:45:55 +0200619#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200620static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
621 const unsigned char *buf,
622 size_t len )
623{
624 if( len != 0 )
625 {
626 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
628 }
629
630 ((void) buf);
631
632 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
633
634 return( 0 );
635}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200636#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200637
Paul Bakkera503a632013-08-14 13:48:06 +0200638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200640 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200641 size_t len )
642{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200643 int ret;
644
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200645 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
646 return( 0 );
647
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200648 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200649 ssl->handshake->new_session_ticket = 1;
650
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200651 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
652
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653 if( len == 0 )
654 return( 0 );
655
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200656 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
657 {
658 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
659 return( 0 );
660 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661
662 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663 * Failures are ok: just ignore the ticket and proceed.
664 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
666 {
667 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200668 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200669 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200670
671 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
672
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200674
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200675 /* Don't send a new ticket after all, this one is OK */
676 ssl->handshake->new_session_ticket = 0;
677
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678 return( 0 );
679}
Paul Bakkera503a632013-08-14 13:48:06 +0200680#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200681
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200682#if defined(POLARSSL_SSL_ALPN)
683static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200684 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200685{
Paul Bakker14b16c62014-05-28 11:33:54 +0200686 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200687 const unsigned char *theirs, *start, *end;
688 const char **ours;
689
690 /* If ALPN not configured, just ignore the extension */
691 if( ssl->alpn_list == NULL )
692 return( 0 );
693
694 /*
695 * opaque ProtocolName<1..2^8-1>;
696 *
697 * struct {
698 * ProtocolName protocol_name_list<2..2^16-1>
699 * } ProtocolNameList;
700 */
701
702 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
703 if( len < 4 )
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705
706 list_len = ( buf[0] << 8 ) | buf[1];
707 if( list_len != len - 2 )
708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
709
710 /*
711 * Use our order of preference
712 */
713 start = buf + 2;
714 end = buf + len;
715 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
716 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200717 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718 for( theirs = start; theirs != end; theirs += cur_len )
719 {
720 /* If the list is well formed, we should get equality first */
721 if( theirs > end )
722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
723
724 cur_len = *theirs++;
725
726 /* Empty strings MUST NOT be included */
727 if( cur_len == 0 )
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729
Paul Bakker14b16c62014-05-28 11:33:54 +0200730 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200731 memcmp( theirs, *ours, cur_len ) == 0 )
732 {
733 ssl->alpn_chosen = *ours;
734 return( 0 );
735 }
736 }
737 }
738
739 /* If we get there, no match was found */
740 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
741 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743}
744#endif /* POLARSSL_SSL_ALPN */
745
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100746/*
747 * Auxiliary functions for ServerHello parsing and related actions
748 */
749
750#if defined(POLARSSL_X509_CRT_PARSE_C)
751/*
752 * Return 1 if the given EC key uses the given curve, 0 otherwise
753 */
754#if defined(POLARSSL_ECDSA_C)
755static int ssl_key_matches_curves( pk_context *pk,
756 const ecp_curve_info **curves )
757{
758 const ecp_curve_info **crv = curves;
759 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
760
761 while( *crv != NULL )
762 {
763 if( (*crv)->grp_id == grp_id )
764 return( 1 );
765 crv++;
766 }
767
768 return( 0 );
769}
770#endif /* POLARSSL_ECDSA_C */
771
772/*
773 * Try picking a certificate for this ciphersuite,
774 * return 0 on success and -1 on failure.
775 */
776static int ssl_pick_cert( ssl_context *ssl,
777 const ssl_ciphersuite_t * ciphersuite_info )
778{
779 ssl_key_cert *cur, *list;
780 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
781
782#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
783 if( ssl->handshake->sni_key_cert != NULL )
784 list = ssl->handshake->sni_key_cert;
785 else
786#endif
787 list = ssl->handshake->key_cert;
788
789 if( pk_alg == POLARSSL_PK_NONE )
790 return( 0 );
791
792 for( cur = list; cur != NULL; cur = cur->next )
793 {
794 if( ! pk_can_do( cur->key, pk_alg ) )
795 continue;
796
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200797 /*
798 * This avoids sending the client a cert it'll reject based on
799 * keyUsage or other extensions.
800 *
801 * It also allows the user to provision different certificates for
802 * different uses based on keyUsage, eg if they want to avoid signing
803 * and decrypting with the same RSA key.
804 */
805 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
806 SSL_IS_SERVER ) != 0 )
807 {
808 continue;
809 }
810
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100811#if defined(POLARSSL_ECDSA_C)
812 if( pk_alg == POLARSSL_PK_ECDSA )
813 {
814 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
815 break;
816 }
817 else
818#endif
819 break;
820 }
821
822 if( cur == NULL )
823 return( -1 );
824
825 ssl->handshake->key_cert = cur;
826 return( 0 );
827}
828#endif /* POLARSSL_X509_CRT_PARSE_C */
829
830/*
831 * Check if a given ciphersuite is suitable for use with our config/keys/etc
832 * Sets ciphersuite_info only if the suite matches.
833 */
834static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
835 const ssl_ciphersuite_t **ciphersuite_info )
836{
837 const ssl_ciphersuite_t *suite_info;
838
839 suite_info = ssl_ciphersuite_from_id( suite_id );
840 if( suite_info == NULL )
841 {
842 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
844 }
845
846 if( suite_info->min_minor_ver > ssl->minor_ver ||
847 suite_info->max_minor_ver < ssl->minor_ver )
848 return( 0 );
849
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100850#if defined(POLARSSL_SSL_PROTO_DTLS)
851 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
852 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
853 return( 0 );
854#endif
855
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100856#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
857 if( ssl_ciphersuite_uses_ec( suite_info ) &&
858 ( ssl->handshake->curves == NULL ||
859 ssl->handshake->curves[0] == NULL ) )
860 return( 0 );
861#endif
862
863#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
864 /* If the ciphersuite requires a pre-shared key and we don't
865 * have one, skip it now rather than failing later */
866 if( ssl_ciphersuite_uses_psk( suite_info ) &&
867 ssl->f_psk == NULL &&
868 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
869 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
870 return( 0 );
871#endif
872
873#if defined(POLARSSL_X509_CRT_PARSE_C)
874 /*
875 * Final check: if ciphersuite requires us to have a
876 * certificate/key of a particular type:
877 * - select the appropriate certificate if we have one, or
878 * - try the next ciphersuite if we don't
879 * This must be done last since we modify the key_cert list.
880 */
881 if( ssl_pick_cert( ssl, suite_info ) != 0 )
882 return( 0 );
883#endif
884
885 *ciphersuite_info = suite_info;
886 return( 0 );
887}
888
Paul Bakker78a8c712013-03-06 17:01:52 +0100889#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
890static int ssl_parse_client_hello_v2( ssl_context *ssl )
891{
892 int ret;
893 unsigned int i, j;
894 size_t n;
895 unsigned int ciph_len, sess_len, chal_len;
896 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200897 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200898 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100899
900 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
901
902 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
903 {
904 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
905
906 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
907 return( ret );
908
909 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
910 }
911
912 buf = ssl->in_hdr;
913
914 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
915
916 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
917 buf[2] ) );
918 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
919 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
920 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
921 buf[3], buf[4] ) );
922
923 /*
924 * SSLv2 Client Hello
925 *
926 * Record layer:
927 * 0 . 1 message length
928 *
929 * SSL layer:
930 * 2 . 2 message type
931 * 3 . 4 protocol version
932 */
933 if( buf[2] != SSL_HS_CLIENT_HELLO ||
934 buf[3] != SSL_MAJOR_VERSION_3 )
935 {
936 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
937 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
938 }
939
940 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
941
942 if( n < 17 || n > 512 )
943 {
944 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
945 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
946 }
947
948 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200949 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
950 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100951
952 if( ssl->minor_ver < ssl->min_minor_ver )
953 {
954 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200955 " [%d:%d] < [%d:%d]",
956 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100957 ssl->min_major_ver, ssl->min_minor_ver ) );
958
959 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
960 SSL_ALERT_MSG_PROTOCOL_VERSION );
961 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
962 }
963
Paul Bakker2fbefde2013-06-29 16:01:15 +0200964 ssl->handshake->max_major_ver = buf[3];
965 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100966
967 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
968 {
969 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
970 return( ret );
971 }
972
973 ssl->handshake->update_checksum( ssl, buf + 2, n );
974
975 buf = ssl->in_msg;
976 n = ssl->in_left - 5;
977
978 /*
979 * 0 . 1 ciphersuitelist length
980 * 2 . 3 session id length
981 * 4 . 5 challenge length
982 * 6 . .. ciphersuitelist
983 * .. . .. session id
984 * .. . .. challenge
985 */
986 SSL_DEBUG_BUF( 4, "record contents", buf, n );
987
988 ciph_len = ( buf[0] << 8 ) | buf[1];
989 sess_len = ( buf[2] << 8 ) | buf[3];
990 chal_len = ( buf[4] << 8 ) | buf[5];
991
992 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
993 ciph_len, sess_len, chal_len ) );
994
995 /*
996 * Make sure each parameter length is valid
997 */
998 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
999 {
1000 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1001 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1002 }
1003
1004 if( sess_len > 32 )
1005 {
1006 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1007 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1008 }
1009
1010 if( chal_len < 8 || chal_len > 32 )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1013 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1014 }
1015
1016 if( n != 6 + ciph_len + sess_len + chal_len )
1017 {
1018 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1019 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1020 }
1021
1022 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1023 buf + 6, ciph_len );
1024 SSL_DEBUG_BUF( 3, "client hello, session id",
1025 buf + 6 + ciph_len, sess_len );
1026 SSL_DEBUG_BUF( 3, "client hello, challenge",
1027 buf + 6 + ciph_len + sess_len, chal_len );
1028
1029 p = buf + 6 + ciph_len;
1030 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001031 memset( ssl->session_negotiate->id, 0,
1032 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001033 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1034
1035 p += sess_len;
1036 memset( ssl->handshake->randbytes, 0, 64 );
1037 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1038
1039 /*
1040 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1041 */
1042 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1043 {
1044 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1045 {
1046 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1047 if( ssl->renegotiation == SSL_RENEGOTIATION )
1048 {
1049 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1050
1051 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1052 return( ret );
1053
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1055 }
1056 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1057 break;
1058 }
1059 }
1060
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001061 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001062 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001063#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1064 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1065 {
1066 for( i = 0; ciphersuites[i] != 0; i++ )
1067#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001068 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001069 {
1070 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001071#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001072 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001073 if( p[0] != 0 ||
1074 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1075 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1076 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001077
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001078 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1079 &ciphersuite_info ) ) != 0 )
1080 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001081
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001082 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001083 goto have_ciphersuite_v2;
1084 }
1085 }
1086
1087 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1088
1089 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1090
1091have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001092 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001093 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001094 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001095
1096 /*
1097 * SSLv2 Client Hello relevant renegotiation security checks
1098 */
1099 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1100 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1103
1104 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1105 return( ret );
1106
1107 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1108 }
1109
1110 ssl->in_left = 0;
1111 ssl->state++;
1112
1113 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1114
1115 return( 0 );
1116}
1117#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119static int ssl_parse_client_hello( ssl_context *ssl )
1120{
Paul Bakker23986e52011-04-24 08:57:21 +00001121 int ret;
1122 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001123 unsigned int ciph_offset, comp_offset, ext_offset;
1124 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001125#if defined(POLARSSL_SSL_PROTO_DTLS)
1126 unsigned int cookie_offset, cookie_len;
1127#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001128 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001129 int renegotiation_info_seen = 0;
1130 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001131 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001132 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001133 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001134
1135 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1136
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001137 /*
1138 * If renegotiating, then the input was read with ssl_read_record(),
1139 * otherwise read it ourselves manually in order to support SSLv2
1140 * ClientHello, which doesn't use the same record layer format.
1141 */
Paul Bakker48916f92012-09-16 19:57:18 +00001142 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001143 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 {
1145 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1146 return( ret );
1147 }
1148
1149 buf = ssl->in_hdr;
1150
Paul Bakker78a8c712013-03-06 17:01:52 +01001151#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001152 if( ssl->transport == SSL_TRANSPORT_STREAM && ( buf[0] & 0x80 ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001153 return ssl_parse_client_hello_v2( ssl );
1154#endif
1155
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001156 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001157
Paul Bakkerec636f32012-09-09 19:17:02 +00001158 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001159 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001160 *
1161 * Record layer:
1162 * 0 . 0 message type
1163 * 1 . 2 protocol version
1164 * 3 . 4 message length
1165 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001166 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1167 buf[0] ) );
1168
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001169 if( buf[0] != SSL_MSG_HANDSHAKE )
1170 {
1171 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1172 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1173 }
1174
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001175 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1176 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1177
1178 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1179 buf[1], buf[2] ) );
1180
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001181 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001182
1183 /* According to RFC 5246 Appendix E.1, the version here is typically
1184 * "{03,00}, the lowest version number supported by the client, [or] the
1185 * value of ClientHello.client_version", so the only meaningful check here
1186 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001187 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1190 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1191 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001193 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001195 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001196 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001197 if( msg_len > SSL_MAX_CONTENT_LEN )
1198 {
1199 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1200 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1201 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001202
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001203 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1204 {
1205 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1206 return( ret );
1207 }
1208 }
1209 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001210 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001211 /* Set by ssl_read_record() */
1212 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001213 }
1214
1215 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001216
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001217 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001218
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001219 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001220
1221 /*
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001222 * The DTLS handshake layer contains additional fields. Use them, then
1223 * remove them so that it looks like TLS format to the rest of the code.
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001224 */
1225#if defined(POLARSSL_SSL_PROTO_DTLS)
1226 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1227 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001228 /* 1 (type) + 3 (len) + 2 (seq) + 3 (frag_offset) + 3 (frag_len) */
1229 if( msg_len < 12 )
1230 {
1231 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1232 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1233 }
1234
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001235 // TODO: DTLS: check message_seq
1236
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001237 /*
1238 * For now we don't support fragmentation, so make sure
1239 * fragment_offset == 0 and fragment_length == length
1240 *
1241 * TODO: DTLS: support fragmentation??
1242 * Well, ClientHello is rarely much longer than 512 bytes
1243 * so it will probably never be fragmented anyway...
1244 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001245 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1246 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1247 {
1248 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
1249 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1250 }
1251
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001252 /* Remove the additional fields */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001253 memmove( buf + 4, buf + 12, msg_len - 12 );
1254 msg_len -= 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001255 }
1256#endif /* POLARSSL_SSL_PROTO_DTLS */
1257
1258 /*
Paul Bakkerec636f32012-09-09 19:17:02 +00001259 * SSL layer:
1260 * 0 . 0 handshake type
1261 * 1 . 3 handshake length
1262 * 4 . 5 protocol version
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001263 * 6 . 37 random bytes (starting with 4 bytes of Unix time)
1264 * 38 . 38 session id length (1 byte)
Paul Bakkerec636f32012-09-09 19:17:02 +00001265 * 39 . 38+x session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001266 * 39+x . 39+x DTLS only: cookie length (1 byte)
1267 * 40+x . .. DTSL only: cookie
1268 * .. . .. ciphersuite list length (2 bytes)
1269 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001270 * .. . .. compression alg. list length (1 byte)
1271 * .. . .. compression alg. list
1272 * .. . .. extensions length (2 bytes, optional)
1273 * .. . .. extensions (optional)
1274 *
1275 * Minimal length (with everything empty and extensions ommitted) is
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001276 * 4 + 2 + 32 + 1 + 2 + 1 = 42 bytes. Check that first, so that we can
1277 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001278 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001279
1280 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001281 * Check the handshake type and message length
Paul Bakkerec636f32012-09-09 19:17:02 +00001282 */
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001283 if( msg_len < 42 )
1284 {
1285 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1286 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1287 }
1288
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001289 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1290
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001291 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001292 {
1293 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1294 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1295 }
1296
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001297 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1298 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1299
1300 if( buf[1] != 0 ||
1301 msg_len != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1302 {
1303 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1304 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1305 }
1306
1307 /*
1308 * Check and save the protocol version
1309 */
1310 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1311 buf[4], buf[5] ) );
1312
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001313 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1314 ssl->transport, buf + 4 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001315
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001316 ssl->handshake->max_major_ver = ssl->major_ver;
1317 ssl->handshake->max_minor_ver = ssl->minor_ver;
1318
1319 if( ssl->major_ver < ssl->min_major_ver ||
1320 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001321 {
1322 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001323 " [%d:%d] < [%d:%d]",
1324 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001325 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001326
1327 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1328 SSL_ALERT_MSG_PROTOCOL_VERSION );
1329
1330 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1331 }
1332
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001333 if( ssl->major_ver > ssl->max_major_ver )
1334 {
1335 ssl->major_ver = ssl->max_major_ver;
1336 ssl->minor_ver = ssl->max_minor_ver;
1337 }
1338 else if( ssl->minor_ver > ssl->max_minor_ver )
1339 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001340
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001341 /*
1342 * Save client random (inc. Unix time)
1343 */
1344 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1345 buf + 6, 32 );
1346
Paul Bakker48916f92012-09-16 19:57:18 +00001347 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001348
1349 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001350 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001351 */
1352 sess_len = buf[38];
1353
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001354 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001355 sess_len + 38 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001356 {
1357 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1358 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1359 }
1360
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001361 SSL_DEBUG_BUF( 3, "client hello, session id",
1362 buf + 39, sess_len );
1363
Paul Bakker48916f92012-09-16 19:57:18 +00001364 ssl->session_negotiate->length = sess_len;
1365 memset( ssl->session_negotiate->id, 0,
1366 sizeof( ssl->session_negotiate->id ) );
1367 memcpy( ssl->session_negotiate->id, buf + 39,
1368 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001369
1370 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001371 * Check the cookie length and content
1372 */
1373#if defined(POLARSSL_SSL_PROTO_DTLS)
1374 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1375 {
1376 cookie_offset = 39 + sess_len;
1377 cookie_len = buf[cookie_offset];
1378
1379 if( // cookie_len > <MAX> || // TODO-DTLS
1380 cookie_offset + 1 + cookie_len + 2 > msg_len )
1381 {
1382 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1383 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1384 }
1385
1386 SSL_DEBUG_BUF( 3, "client hello, cookie",
1387 buf + cookie_offset + 1, cookie_len );
1388
1389 // TODO-DTLS: check cookie, reject if invalid!
1390 }
1391#endif
1392
1393 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001394 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001395 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001396#if defined(POLARSSL_SSL_PROTO_DTLS)
1397 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1398 ciph_offset = cookie_offset + 1 + cookie_len;
1399 else
1400#endif
1401 ciph_offset = 39 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001402
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001403 ciph_len = ( buf[ciph_offset + 0] << 8 )
1404 | ( buf[ciph_offset + 1] );
1405
1406 if( ciph_len < 2 ||
1407 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1408 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001409 {
1410 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1411 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1412 }
1413
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001414 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1415 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001416
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001417 /*
1418 * Check the compression algorithms length and pick one
1419 */
1420 comp_offset = ciph_offset + 2 + ciph_len;
1421
1422 comp_len = buf[comp_offset];
1423
1424 if( comp_len < 1 ||
1425 comp_len > 16 ||
1426 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001427 {
1428 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1429 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1430 }
1431
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001432 SSL_DEBUG_BUF( 3, "client hello, compression",
1433 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001434
1435 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001436#if defined(POLARSSL_ZLIB_SUPPORT)
1437 for( i = 0; i < comp_len; ++i )
1438 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001439 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001440 {
Paul Bakker48916f92012-09-16 19:57:18 +00001441 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001442 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001443 }
1444 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001445#endif
1446
Paul Bakkerec636f32012-09-09 19:17:02 +00001447 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001448 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001449 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001450 ext_offset = comp_offset + 1 + comp_len;
1451 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001452 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001453 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001454 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001455 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1456 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1457 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001458
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001459 ext_len = ( buf[ext_offset + 0] << 8 )
1460 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001461
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001462 if( ( ext_len > 0 && ext_len < 4 ) ||
1463 msg_len != ext_offset + 2 + ext_len )
1464 {
1465 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1466 SSL_DEBUG_BUF( 3, "client hello extensions",
1467 buf + ext_offset + 2, ext_len );
1468 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001469 }
1470 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001471 else
1472 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001473
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001474 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001475
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001476 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001477 {
1478 unsigned int ext_id = ( ( ext[0] << 8 )
1479 | ( ext[1] ) );
1480 unsigned int ext_size = ( ( ext[2] << 8 )
1481 | ( ext[3] ) );
1482
1483 if( ext_size + 4 > ext_len )
1484 {
1485 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1486 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1487 }
1488 switch( ext_id )
1489 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001490#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001491 case TLS_EXT_SERVERNAME:
1492 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1493 if( ssl->f_sni == NULL )
1494 break;
1495
1496 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1497 if( ret != 0 )
1498 return( ret );
1499 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001500#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001501
Paul Bakker48916f92012-09-16 19:57:18 +00001502 case TLS_EXT_RENEGOTIATION_INFO:
1503 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1504 renegotiation_info_seen = 1;
1505
Paul Bakker23f36802012-09-28 14:15:14 +00001506 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1507 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001508 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001509 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001510
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001511#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001512 case TLS_EXT_SIG_ALG:
1513 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1514 if( ssl->renegotiation == SSL_RENEGOTIATION )
1515 break;
1516
1517 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1518 if( ret != 0 )
1519 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001520 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001521#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001522
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001523#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001524 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1525 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1526
1527 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1528 if( ret != 0 )
1529 return( ret );
1530 break;
1531
1532 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1533 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001534 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001535
1536 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1537 if( ret != 0 )
1538 return( ret );
1539 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001540#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001541
Paul Bakker05decb22013-08-15 13:33:48 +02001542#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001543 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1544 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1545
1546 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1547 if( ret != 0 )
1548 return( ret );
1549 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001550#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001551
Paul Bakker1f2bc622013-08-15 13:45:55 +02001552#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001553 case TLS_EXT_TRUNCATED_HMAC:
1554 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1555
1556 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1557 if( ret != 0 )
1558 return( ret );
1559 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001560#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001561
Paul Bakkera503a632013-08-14 13:48:06 +02001562#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001563 case TLS_EXT_SESSION_TICKET:
1564 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1565
1566 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1567 if( ret != 0 )
1568 return( ret );
1569 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001570#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001571
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001572#if defined(POLARSSL_SSL_ALPN)
1573 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001574 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001575
1576 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1577 if( ret != 0 )
1578 return( ret );
1579 break;
1580#endif /* POLARSSL_SSL_SESSION_TICKETS */
1581
Paul Bakker48916f92012-09-16 19:57:18 +00001582 default:
1583 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1584 ext_id ) );
1585 }
1586
1587 ext_len -= 4 + ext_size;
1588 ext += 4 + ext_size;
1589
1590 if( ext_len > 0 && ext_len < 4 )
1591 {
1592 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1593 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1594 }
1595 }
1596
1597 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001598 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1599 */
1600 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1601 {
1602 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1603 {
1604 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1605 if( ssl->renegotiation == SSL_RENEGOTIATION )
1606 {
1607 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1608
1609 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1610 return( ret );
1611
1612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1613 }
1614 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1615 break;
1616 }
1617 }
1618
1619 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001620 * Renegotiation security checks
1621 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001622 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1623 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1624 {
1625 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1626 handshake_failure = 1;
1627 }
1628 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1629 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1630 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001631 {
1632 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001633 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001634 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001635 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1636 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1637 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001638 {
1639 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001640 handshake_failure = 1;
1641 }
1642 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1643 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1644 renegotiation_info_seen == 1 )
1645 {
1646 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1647 handshake_failure = 1;
1648 }
1649
1650 if( handshake_failure == 1 )
1651 {
1652 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1653 return( ret );
1654
Paul Bakker48916f92012-09-16 19:57:18 +00001655 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1656 }
Paul Bakker380da532012-04-18 16:10:25 +00001657
Paul Bakker41c83d32013-03-20 14:39:14 +01001658 /*
1659 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001660 * (At the end because we need information from the EC-based extensions
1661 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001662 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001663 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001664 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001665#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001666 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001667 {
1668 for( i = 0; ciphersuites[i] != 0; i++ )
1669#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001670 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001671 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001672 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001673#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001674 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001675 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1676 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1677 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001678
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001679 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1680 &ciphersuite_info ) ) != 0 )
1681 return( ret );
1682
1683 if( ciphersuite_info != NULL )
1684 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001685 }
1686 }
1687
1688 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1689
1690 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1691 return( ret );
1692
1693 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1694
1695have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001696 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001697 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1698 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1699
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 ssl->in_left = 0;
1701 ssl->state++;
1702
1703 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1704
1705 return( 0 );
1706}
1707
Paul Bakker1f2bc622013-08-15 13:45:55 +02001708#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001709static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1710 unsigned char *buf,
1711 size_t *olen )
1712{
1713 unsigned char *p = buf;
1714
1715 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1716 {
1717 *olen = 0;
1718 return;
1719 }
1720
1721 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1722
1723 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1724 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1725
1726 *p++ = 0x00;
1727 *p++ = 0x00;
1728
1729 *olen = 4;
1730}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001731#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001732
Paul Bakkera503a632013-08-14 13:48:06 +02001733#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001734static void ssl_write_session_ticket_ext( ssl_context *ssl,
1735 unsigned char *buf,
1736 size_t *olen )
1737{
1738 unsigned char *p = buf;
1739
1740 if( ssl->handshake->new_session_ticket == 0 )
1741 {
1742 *olen = 0;
1743 return;
1744 }
1745
1746 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1747
1748 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1749 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1750
1751 *p++ = 0x00;
1752 *p++ = 0x00;
1753
1754 *olen = 4;
1755}
Paul Bakkera503a632013-08-14 13:48:06 +02001756#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001757
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001758static void ssl_write_renegotiation_ext( ssl_context *ssl,
1759 unsigned char *buf,
1760 size_t *olen )
1761{
1762 unsigned char *p = buf;
1763
1764 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1765 {
1766 *olen = 0;
1767 return;
1768 }
1769
1770 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1771
1772 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1773 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1774
1775 *p++ = 0x00;
1776 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1777 *p++ = ssl->verify_data_len * 2 & 0xFF;
1778
1779 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1780 p += ssl->verify_data_len;
1781 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1782 p += ssl->verify_data_len;
1783
1784 *olen = 5 + ssl->verify_data_len * 2;
1785}
1786
Paul Bakker05decb22013-08-15 13:33:48 +02001787#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001788static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1789 unsigned char *buf,
1790 size_t *olen )
1791{
1792 unsigned char *p = buf;
1793
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001794 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1795 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001796 *olen = 0;
1797 return;
1798 }
1799
1800 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1801
1802 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1803 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1804
1805 *p++ = 0x00;
1806 *p++ = 1;
1807
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001808 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001809
1810 *olen = 5;
1811}
Paul Bakker05decb22013-08-15 13:33:48 +02001812#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001813
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001814#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001815static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1816 unsigned char *buf,
1817 size_t *olen )
1818{
1819 unsigned char *p = buf;
1820 ((void) ssl);
1821
Paul Bakker677377f2013-10-28 12:54:26 +01001822 if( ( ssl->handshake->cli_exts &
1823 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1824 {
1825 *olen = 0;
1826 return;
1827 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001828
1829 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1830
1831 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1832 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1833
1834 *p++ = 0x00;
1835 *p++ = 2;
1836
1837 *p++ = 1;
1838 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1839
1840 *olen = 6;
1841}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001842#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001843
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001844#if defined(POLARSSL_SSL_ALPN )
1845static void ssl_write_alpn_ext( ssl_context *ssl,
1846 unsigned char *buf, size_t *olen )
1847{
1848 if( ssl->alpn_chosen == NULL )
1849 {
1850 *olen = 0;
1851 return;
1852 }
1853
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001854 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001855
1856 /*
1857 * 0 . 1 ext identifier
1858 * 2 . 3 ext length
1859 * 4 . 5 protocol list length
1860 * 6 . 6 protocol name length
1861 * 7 . 7+n protocol name
1862 */
1863 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1864 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1865
1866 *olen = 7 + strlen( ssl->alpn_chosen );
1867
1868 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1869 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1870
1871 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1872 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1873
1874 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1875
1876 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1877}
1878#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1879
Paul Bakker5121ce52009-01-03 21:22:43 +00001880static int ssl_write_server_hello( ssl_context *ssl )
1881{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001882#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001884#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001885 int ret;
1886 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 unsigned char *buf, *p;
1888
1889 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1890
Paul Bakkera9a028e2013-11-21 17:31:06 +01001891 if( ssl->f_rng == NULL )
1892 {
1893 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1894 return( POLARSSL_ERR_SSL_NO_RNG );
1895 }
1896
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 /*
1898 * 0 . 0 handshake type
1899 * 1 . 3 handshake length
1900 * 4 . 5 protocol version
1901 * 6 . 9 UNIX time()
1902 * 10 . 37 random bytes
1903 */
1904 buf = ssl->out_msg;
1905 p = buf + 4;
1906
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001907 ssl_write_version( ssl->major_ver, ssl->minor_ver,
1908 ssl->transport, p );
1909 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001910
1911 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001912 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001914#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001915 t = time( NULL );
1916 *p++ = (unsigned char)( t >> 24 );
1917 *p++ = (unsigned char)( t >> 16 );
1918 *p++ = (unsigned char)( t >> 8 );
1919 *p++ = (unsigned char)( t );
1920
1921 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001922#else
1923 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1924 return( ret );
1925
1926 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001927#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Paul Bakkera3d195c2011-11-27 21:07:34 +00001929 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1930 return( ret );
1931
1932 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Paul Bakker48916f92012-09-16 19:57:18 +00001934 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
1936 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1937
1938 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001939 * Resume is 0 by default, see ssl_handshake_init().
1940 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1941 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001942 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001943 if( ssl->handshake->resume == 0 &&
1944 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001945 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001946 ssl->f_get_cache != NULL &&
1947 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1948 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001949 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001950 ssl->handshake->resume = 1;
1951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001953 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001954 {
1955 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001956 * New session, create a new session id,
1957 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001958 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 ssl->state++;
1960
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001961#if defined(POLARSSL_HAVE_TIME)
1962 ssl->session_negotiate->start = time( NULL );
1963#endif
1964
Paul Bakkera503a632013-08-14 13:48:06 +02001965#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001966 if( ssl->handshake->new_session_ticket != 0 )
1967 {
1968 ssl->session_negotiate->length = n = 0;
1969 memset( ssl->session_negotiate->id, 0, 32 );
1970 }
1971 else
1972#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001973 {
1974 ssl->session_negotiate->length = n = 32;
1975 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001976 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001977 return( ret );
1978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001979 }
1980 else
1981 {
1982 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001983 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001985 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001987
1988 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1989 {
1990 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1991 return( ret );
1992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001993 }
1994
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001995 /*
1996 * 38 . 38 session id length
1997 * 39 . 38+n session id
1998 * 39+n . 40+n chosen ciphersuite
1999 * 41+n . 41+n chosen compression alg.
2000 * 42+n . 43+n extensions length
2001 * 44+n . 43+n+m extensions
2002 */
2003 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002004 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2005 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002006
2007 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2008 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2009 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002010 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002011
Paul Bakker48916f92012-09-16 19:57:18 +00002012 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2013 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2014 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002015
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002016 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2017 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002018 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002019 ssl->session_negotiate->compression ) );
2020
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002021 /*
2022 * First write extensions, then the total length
2023 */
2024 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2025 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002026
Paul Bakker05decb22013-08-15 13:33:48 +02002027#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002028 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2029 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002030#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002031
Paul Bakker1f2bc622013-08-15 13:45:55 +02002032#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002033 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2034 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002035#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002036
Paul Bakkera503a632013-08-14 13:48:06 +02002037#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002038 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2039 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002040#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002041
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002042#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002043 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2044 ext_len += olen;
2045#endif
2046
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002047#if defined(POLARSSL_SSL_ALPN)
2048 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2049 ext_len += olen;
2050#endif
2051
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002052 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002053
Paul Bakkera7036632014-04-30 10:15:38 +02002054 if( ext_len > 0 )
2055 {
2056 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2057 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2058 p += ext_len;
2059 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002060
2061 ssl->out_msglen = p - buf;
2062 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2063 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2064
2065 ret = ssl_write_record( ssl );
2066
2067 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2068
2069 return( ret );
2070}
2071
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002072#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2073 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002074 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2075 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002076static int ssl_write_certificate_request( ssl_context *ssl )
2077{
Paul Bakkered27a042013-04-18 22:46:23 +02002078 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002079
2080 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2081
2082 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002083 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002084 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2085 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002086 {
2087 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2088 ssl->state++;
2089 return( 0 );
2090 }
2091
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002092 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2093 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002094}
2095#else
2096static int ssl_write_certificate_request( ssl_context *ssl )
2097{
2098 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2099 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002100 size_t dn_size, total_dn_size; /* excluding length bytes */
2101 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002102 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002103 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104
2105 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2106
2107 ssl->state++;
2108
Paul Bakkerfbb17802013-04-17 19:10:21 +02002109 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002110 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002111 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002112 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002113 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002114 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 return( 0 );
2117 }
2118
2119 /*
2120 * 0 . 0 handshake type
2121 * 1 . 3 handshake length
2122 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002123 * 5 .. m-1 cert types
2124 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002125 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002126 * n .. n+1 length of all DNs
2127 * n+2 .. n+3 length of DN 1
2128 * n+4 .. ... Distinguished Name #1
2129 * ... .. ... length of DN 2, etc.
2130 */
2131 buf = ssl->out_msg;
2132 p = buf + 4;
2133
2134 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002135 * Supported certificate types
2136 *
2137 * ClientCertificateType certificate_types<1..2^8-1>;
2138 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002139 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002140 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002141
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002142#if defined(POLARSSL_RSA_C)
2143 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2144#endif
2145#if defined(POLARSSL_ECDSA_C)
2146 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2147#endif
2148
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002149 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002150 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002151
Paul Bakker577e0062013-08-28 11:57:20 +02002152 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002153#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002154 /*
2155 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002156 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002157 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2158 *
2159 * struct {
2160 * HashAlgorithm hash;
2161 * SignatureAlgorithm signature;
2162 * } SignatureAndHashAlgorithm;
2163 *
2164 * enum { (255) } HashAlgorithm;
2165 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002166 */
Paul Bakker21dca692013-01-03 11:41:08 +01002167 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002168 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002169 /*
2170 * Only use current running hash algorithm that is already required
2171 * for requested ciphersuite.
2172 */
Paul Bakker926af752012-11-23 13:38:07 +01002173 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2174
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002175 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2176 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002177 {
2178 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2179 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002180
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002181 /*
2182 * Supported signature algorithms
2183 */
2184#if defined(POLARSSL_RSA_C)
2185 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2186 p[2 + sa_len++] = SSL_SIG_RSA;
2187#endif
2188#if defined(POLARSSL_ECDSA_C)
2189 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2190 p[2 + sa_len++] = SSL_SIG_ECDSA;
2191#endif
Paul Bakker926af752012-11-23 13:38:07 +01002192
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002193 p[0] = (unsigned char)( sa_len >> 8 );
2194 p[1] = (unsigned char)( sa_len );
2195 sa_len += 2;
2196 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002197 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002198#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002199
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002200 /*
2201 * DistinguishedName certificate_authorities<0..2^16-1>;
2202 * opaque DistinguishedName<1..2^16-1>;
2203 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 p += 2;
2205 crt = ssl->ca_chain;
2206
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002207 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002208 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 {
2210 if( p - buf > 4096 )
2211 break;
2212
Paul Bakker926af752012-11-23 13:38:07 +01002213 dn_size = crt->subject_raw.len;
2214 *p++ = (unsigned char)( dn_size >> 8 );
2215 *p++ = (unsigned char)( dn_size );
2216 memcpy( p, crt->subject_raw.p, dn_size );
2217 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002218
Paul Bakker926af752012-11-23 13:38:07 +01002219 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2220
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002221 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002222 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 }
2224
Paul Bakker926af752012-11-23 13:38:07 +01002225 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002226 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2227 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002228 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2229 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002230
2231 ret = ssl_write_record( ssl );
2232
2233 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2234
2235 return( ret );
2236}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002237#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2238 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002239 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2240 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002241
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002242#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2243 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2244static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2245{
2246 int ret;
2247
2248 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2249 {
2250 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2251 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2252 }
2253
2254 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2255 pk_ec( *ssl_own_key( ssl ) ),
2256 POLARSSL_ECDH_OURS ) ) != 0 )
2257 {
2258 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2259 return( ret );
2260 }
2261
2262 return( 0 );
2263}
2264#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2265 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2266
Paul Bakker41c83d32013-03-20 14:39:14 +01002267static int ssl_write_server_key_exchange( ssl_context *ssl )
2268{
Paul Bakker23986e52011-04-24 08:57:21 +00002269 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002270 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002271 const ssl_ciphersuite_t *ciphersuite_info =
2272 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002273
2274#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2275 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2276 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002277 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002278 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002279 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002280 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002281 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002282 ((void) dig_signed);
2283 ((void) dig_signed_len);
2284#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002285
Paul Bakker5121ce52009-01-03 21:22:43 +00002286 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2287
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002288#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2289 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2290 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002291 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2292 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2293 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 {
2295 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2296 ssl->state++;
2297 return( 0 );
2298 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002299#endif
2300
2301#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2302 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2303 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2304 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2305 {
2306 ssl_get_ecdh_params_from_cert( ssl );
2307
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002308 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002309 ssl->state++;
2310 return( 0 );
2311 }
2312#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002313
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002314#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2315 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2316 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318 {
2319 /* TODO: Support identity hints */
2320 *(p++) = 0x00;
2321 *(p++) = 0x00;
2322
2323 n += 2;
2324 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002325#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2326 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327
2328#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2329 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2330 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2331 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002332 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002333 /*
2334 * Ephemeral DH parameters:
2335 *
2336 * struct {
2337 * opaque dh_p<1..2^16-1>;
2338 * opaque dh_g<1..2^16-1>;
2339 * opaque dh_Ys<1..2^16-1>;
2340 * } ServerDHParams;
2341 */
2342 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2343 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2344 {
2345 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2346 return( ret );
2347 }
Paul Bakker48916f92012-09-16 19:57:18 +00002348
Paul Bakker41c83d32013-03-20 14:39:14 +01002349 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002350 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2351 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002352 {
2353 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2354 return( ret );
2355 }
2356
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002357 dig_signed = p;
2358 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002359
2360 p += len;
2361 n += len;
2362
Paul Bakker41c83d32013-03-20 14:39:14 +01002363 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2364 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2365 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2366 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2367 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002368#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2369 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002370
Gergely Budai987bfb52014-01-19 21:48:42 +01002371#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002372 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002373 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002375 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002376 /*
2377 * Ephemeral ECDH parameters:
2378 *
2379 * struct {
2380 * ECParameters curve_params;
2381 * ECPoint public;
2382 * } ServerECDHParams;
2383 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002384 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002385#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002386 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002387
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002388 /* Match our preference list against the offered curves */
2389 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2390 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2391 if( (*curve)->grp_id == *gid )
2392 goto curve_matching_done;
2393
2394curve_matching_done:
2395#else
2396 curve = ssl->handshake->curves;
2397#endif
2398
2399 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002400 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002401 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2402 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002403 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002404
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002405 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002406
Paul Bakker41c83d32013-03-20 14:39:14 +01002407 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002408 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002409 {
2410 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2411 return( ret );
2412 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002413
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002414 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2415 p, SSL_MAX_CONTENT_LEN - n,
2416 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002417 {
2418 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2419 return( ret );
2420 }
2421
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002422 dig_signed = p;
2423 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002424
2425 p += len;
2426 n += len;
2427
Paul Bakker41c83d32013-03-20 14:39:14 +01002428 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2429 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002430#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002431
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002432#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002433 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2434 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002436 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2437 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002438 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002439 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002440 unsigned int hashlen = 0;
2441 unsigned char hash[64];
2442 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002443
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002444 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002445 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2446 */
Paul Bakker577e0062013-08-28 11:57:20 +02002447#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002448 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2449 {
2450 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2451
2452 if( md_alg == POLARSSL_MD_NONE )
2453 {
2454 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002455 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002456 }
2457 }
Paul Bakker577e0062013-08-28 11:57:20 +02002458 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002459#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002460#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2461 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002462 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002463 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2464 {
2465 md_alg = POLARSSL_MD_SHA1;
2466 }
2467 else
Paul Bakker577e0062013-08-28 11:57:20 +02002468#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002469 {
2470 md_alg = POLARSSL_MD_NONE;
2471 }
2472
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002473 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002474 * Compute the hash to be signed
2475 */
Paul Bakker577e0062013-08-28 11:57:20 +02002476#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2477 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002478 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002479 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 md5_context md5;
2481 sha1_context sha1;
2482
Paul Bakker5b4af392014-06-26 12:09:34 +02002483 md5_init( &md5 );
2484 sha1_init( &sha1 );
2485
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002486 /*
2487 * digitally-signed struct {
2488 * opaque md5_hash[16];
2489 * opaque sha_hash[20];
2490 * };
2491 *
2492 * md5_hash
2493 * MD5(ClientHello.random + ServerHello.random
2494 * + ServerParams);
2495 * sha_hash
2496 * SHA(ClientHello.random + ServerHello.random
2497 * + ServerParams);
2498 */
2499 md5_starts( &md5 );
2500 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002501 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002502 md5_finish( &md5, hash );
2503
2504 sha1_starts( &sha1 );
2505 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002506 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507 sha1_finish( &sha1, hash + 16 );
2508
2509 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002510
2511 md5_free( &md5 );
2512 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513 }
2514 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002515#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2516 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002517#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2518 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002519 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 {
2521 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002522 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002523
Paul Bakker84bbeb52014-07-01 14:53:22 +02002524 md_init( &ctx );
2525
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002526 /* Info from md_alg will be used instead */
2527 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002528
2529 /*
2530 * digitally-signed struct {
2531 * opaque client_random[32];
2532 * opaque server_random[32];
2533 * ServerDHParams params;
2534 * };
2535 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002536 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002537 {
2538 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2539 return( ret );
2540 }
2541
2542 md_starts( &ctx );
2543 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002544 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002545 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002546 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002547 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002548 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002549#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2550 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002551 {
2552 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002553 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002554 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002555
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002556 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2557 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002558
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002559 /*
2560 * Make the signature
2561 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002562 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002563 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002564 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2565 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566 }
Paul Bakker23f36802012-09-28 14:15:14 +00002567
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002568#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002569 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2570 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002572 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573
2574 n += 2;
2575 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002576#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002578 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002579 p + 2 , &signature_len,
2580 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002582 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002583 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002584 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002585
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002586 *(p++) = (unsigned char)( signature_len >> 8 );
2587 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002588 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002590 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002591
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002592 p += signature_len;
2593 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002594 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002596 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2597 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002598
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002599 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2601 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2602
2603 ssl->state++;
2604
2605 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2606 {
2607 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2608 return( ret );
2609 }
2610
2611 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2612
2613 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002614}
2615
2616static int ssl_write_server_hello_done( ssl_context *ssl )
2617{
2618 int ret;
2619
2620 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2621
2622 ssl->out_msglen = 4;
2623 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2624 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2625
2626 ssl->state++;
2627
2628 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2629 {
2630 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2631 return( ret );
2632 }
2633
2634 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2635
2636 return( 0 );
2637}
2638
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2640 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2641static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2642 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002643{
2644 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002645 size_t n;
2646
2647 /*
2648 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2649 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650 if( *p + 2 > end )
2651 {
2652 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2653 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2654 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002655
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002656 n = ( (*p)[0] << 8 ) | (*p)[1];
2657 *p += 2;
2658
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002659 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002660 {
2661 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2662 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2663 }
2664
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002665 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002666 {
2667 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2668 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2669 }
2670
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002671 *p += n;
2672
Paul Bakker70df2fb2013-04-17 17:19:09 +02002673 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2674
Paul Bakker70df2fb2013-04-17 17:19:09 +02002675 return( ret );
2676}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2678 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002679
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002680#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2681 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002682static int ssl_parse_encrypted_pms( ssl_context *ssl,
2683 const unsigned char *p,
2684 const unsigned char *end,
2685 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002686{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002687 int ret;
2688 size_t len = pk_get_len( ssl_own_key( ssl ) );
2689 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002690 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02002691
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002692 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002693 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002694 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002695 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2696 }
2697
2698 /*
2699 * Decrypt the premaster using own private RSA key
2700 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002701#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2702 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002703 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2704 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002705 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2706 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002707 {
2708 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2709 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2710 }
2711 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002712#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002713
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002714 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002715 {
2716 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2717 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2718 }
2719
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002720 ssl_write_version( ssl->handshake->max_major_ver,
2721 ssl->handshake->max_minor_ver,
2722 ssl->transport, ver );
2723
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002724 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2725 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002726 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002727 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002728
2729 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002730 pms[0] != ver[0] ||
2731 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002732 {
2733 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2734
2735 /*
2736 * Protection against Bleichenbacher's attack:
2737 * invalid PKCS#1 v1.5 padding must not cause
2738 * the connection to end immediately; instead,
2739 * send a bad_record_mac later in the handshake.
2740 */
2741 ssl->handshake->pmslen = 48;
2742
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002743 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002744 if( ret != 0 )
2745 return( ret );
2746 }
2747
2748 return( ret );
2749}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002750#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2751 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002752
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002753#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002754static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2755 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002756{
Paul Bakker6db455e2013-09-18 17:29:31 +02002757 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002758 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002759
Paul Bakker6db455e2013-09-18 17:29:31 +02002760 if( ssl->f_psk == NULL &&
2761 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2762 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002763 {
2764 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2765 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2766 }
2767
2768 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002769 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002770 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771 if( *p + 2 > end )
2772 {
2773 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2774 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2775 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002776
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777 n = ( (*p)[0] << 8 ) | (*p)[1];
2778 *p += 2;
2779
2780 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002781 {
2782 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2783 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2784 }
2785
Paul Bakker6db455e2013-09-18 17:29:31 +02002786 if( ssl->f_psk != NULL )
2787 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002788 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002789 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2790 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002791 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002792 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002793 /* Identity is not a big secret since clients send it in the clear,
2794 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002795 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002796 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002797 {
2798 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2799 }
2800 }
2801
2802 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002803 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002804 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002805 if( ( ret = ssl_send_alert_message( ssl,
2806 SSL_ALERT_LEVEL_FATAL,
2807 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2808 {
2809 return( ret );
2810 }
2811
2812 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002813 }
2814
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002815 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002816
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002817 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002818}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002819#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002820
Paul Bakker5121ce52009-01-03 21:22:43 +00002821static int ssl_parse_client_key_exchange( ssl_context *ssl )
2822{
Paul Bakker23986e52011-04-24 08:57:21 +00002823 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002824 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002825
Paul Bakker41c83d32013-03-20 14:39:14 +01002826 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002827
2828 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2829
2830 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2831 {
2832 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2833 return( ret );
2834 }
2835
2836 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2837 {
2838 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002839 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 }
2841
2842 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2843 {
2844 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002845 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 }
2847
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002848#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002849 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002851 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002852 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002853
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002854 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002855 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002856 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2857 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002858 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002859
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002860 if( p != end )
2861 {
2862 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2863 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2864 }
2865
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002866 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002867
2868 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2869 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002870 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002871 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002872 {
2873 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2874 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2875 }
2876
2877 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002878 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002879 else
2880#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002881#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002882 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2883 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2884 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002885 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002886 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2887 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2888 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002889 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002890 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002891 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002892 {
2893 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2894 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2895 }
2896
2897 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2898
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2900 &ssl->handshake->pmslen,
2901 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002902 POLARSSL_MPI_MAX_SIZE,
2903 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002904 {
2905 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2906 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2907 }
2908
2909 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002912#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002913 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2914 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2915 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002916#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2917 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002918 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002919 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002920 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002921
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002923 {
2924 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2925 return( ret );
2926 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002927
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002928 if( p != end )
2929 {
2930 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2931 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2932 }
2933
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002934 if( ( ret = ssl_psk_derive_premaster( ssl,
2935 ciphersuite_info->key_exchange ) ) != 0 )
2936 {
2937 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2938 return( ret );
2939 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002943#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2944 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2945 {
2946 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002947 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002948
2949 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2950 {
2951 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2952 return( ret );
2953 }
2954
2955 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2956 {
2957 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2958 return( ret );
2959 }
2960
2961 if( ( ret = ssl_psk_derive_premaster( ssl,
2962 ciphersuite_info->key_exchange ) ) != 0 )
2963 {
2964 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2965 return( ret );
2966 }
2967 }
2968 else
2969#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002970#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2971 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2972 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002973 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002974 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002975
2976 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2977 {
2978 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2979 return( ret );
2980 }
2981 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2982 {
2983 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2984 return( ret );
2985 }
2986
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002987 if( p != end )
2988 {
2989 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2990 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2991 }
2992
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002993 if( ( ret = ssl_psk_derive_premaster( ssl,
2994 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002996 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2997 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002998 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999 }
3000 else
3001#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003002#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3003 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3004 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003005 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003006 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003007
3008 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3009 {
3010 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3011 return( ret );
3012 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003013
3014 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3015 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003016 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003017 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3018 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003019 }
3020
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003021 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3022
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003023 if( ( ret = ssl_psk_derive_premaster( ssl,
3024 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003025 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003026 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003027 return( ret );
3028 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003029 }
3030 else
3031#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003032#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3033 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003034 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003035 if( ( ret = ssl_parse_encrypted_pms( ssl,
3036 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003037 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003038 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003039 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003040 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003041 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 }
3043 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003044 else
3045#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3046 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003047 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003048 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003049 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003050
Paul Bakkerff60ee62010-03-16 21:09:09 +00003051 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3052 {
3053 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3054 return( ret );
3055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003056
Paul Bakker5121ce52009-01-03 21:22:43 +00003057 ssl->state++;
3058
3059 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3060
3061 return( 0 );
3062}
3063
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003064#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3065 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003066 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3067 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003068static int ssl_parse_certificate_verify( ssl_context *ssl )
3069{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003070 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003071
3072 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3073
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003074 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003075 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003076 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003078 {
3079 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3080 ssl->state++;
3081 return( 0 );
3082 }
3083
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003084 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3085 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003086}
3087#else
3088static int ssl_parse_certificate_verify( ssl_context *ssl )
3089{
3090 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003091 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003092 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003093 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003094 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003095#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003097#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003098 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003099 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3100
3101 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3102
3103 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003104 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003105 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003106 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3107 {
3108 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3109 ssl->state++;
3110 return( 0 );
3111 }
3112
Paul Bakkered27a042013-04-18 22:46:23 +02003113 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003114 {
3115 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3116 ssl->state++;
3117 return( 0 );
3118 }
3119
Paul Bakker48916f92012-09-16 19:57:18 +00003120 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003121
3122 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3123 {
3124 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3125 return( ret );
3126 }
3127
3128 ssl->state++;
3129
3130 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3131 {
3132 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003133 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 }
3135
3136 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3137 {
3138 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003139 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 }
3141
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003142 /*
3143 * 0 . 0 handshake type
3144 * 1 . 3 handshake length
3145 * 4 . 5 sig alg (TLS 1.2 only)
3146 * 4+n . 5+n signature length (n = sa_len)
3147 * 6+n . 6+n+m signature (m = sig_len)
3148 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003149
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003150#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3151 defined(POLARSSL_SSL_PROTO_TLS1_1)
3152 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003153 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003154 sa_len = 0;
3155
Paul Bakkerc70b9822013-04-07 22:00:46 +02003156 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003157 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003158
3159 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3160 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3161 POLARSSL_PK_ECDSA ) )
3162 {
3163 hash_start += 16;
3164 hashlen -= 16;
3165 md_alg = POLARSSL_MD_SHA1;
3166 }
Paul Bakker926af752012-11-23 13:38:07 +01003167 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003168 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003169#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3170 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003171#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3172 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003173 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003174 sa_len = 2;
3175
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003177 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003178 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003179 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003181 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3182 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003183 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3184 }
3185
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003186 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003187
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003188 /* Info from md_alg will be used instead */
3189 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003190
3191 /*
3192 * Signature
3193 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003194 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3195 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003196 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003197 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3198 " for verify message" ) );
3199 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003200 }
3201
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003202 /*
3203 * Check the certificate's key type matches the signature alg
3204 */
3205 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3206 {
3207 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3208 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3209 }
Paul Bakker577e0062013-08-28 11:57:20 +02003210 }
3211 else
3212#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3213 {
3214 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003215 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003216 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003217
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003218 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003219
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003220 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003221 {
3222 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003223 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003224 }
3225
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003226 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003227 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003228 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003229 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003230 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 return( ret );
3232 }
3233
3234 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3235
Paul Bakkered27a042013-04-18 22:46:23 +02003236 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003237}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003238#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3239 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3240 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003241
Paul Bakkera503a632013-08-14 13:48:06 +02003242#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003243static int ssl_write_new_session_ticket( ssl_context *ssl )
3244{
3245 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003246 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003247 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003248
3249 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3250
3251 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3252 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3253
3254 /*
3255 * struct {
3256 * uint32 ticket_lifetime_hint;
3257 * opaque ticket<0..2^16-1>;
3258 * } NewSessionTicket;
3259 *
3260 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3261 * 8 . 9 ticket_len (n)
3262 * 10 . 9+n ticket content
3263 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003264
3265 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3266 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3267 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3268 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003269
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003270 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3271 {
3272 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3273 tlen = 0;
3274 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003275
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003276 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3277 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003278
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003279 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003280
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003281 /*
3282 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3283 * ChangeCipherSpec share the same state.
3284 */
3285 ssl->handshake->new_session_ticket = 0;
3286
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003287 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3288 {
3289 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3290 return( ret );
3291 }
3292
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003293 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3294
3295 return( 0 );
3296}
Paul Bakkera503a632013-08-14 13:48:06 +02003297#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003298
Paul Bakker5121ce52009-01-03 21:22:43 +00003299/*
Paul Bakker1961b702013-01-25 14:49:24 +01003300 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003301 */
Paul Bakker1961b702013-01-25 14:49:24 +01003302int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003303{
3304 int ret = 0;
3305
Paul Bakker1961b702013-01-25 14:49:24 +01003306 if( ssl->state == SSL_HANDSHAKE_OVER )
3307 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Paul Bakker1961b702013-01-25 14:49:24 +01003309 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3310
3311 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3312 return( ret );
3313
3314 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003315 {
Paul Bakker1961b702013-01-25 14:49:24 +01003316 case SSL_HELLO_REQUEST:
3317 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 break;
3319
Paul Bakker1961b702013-01-25 14:49:24 +01003320 /*
3321 * <== ClientHello
3322 */
3323 case SSL_CLIENT_HELLO:
3324 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003326
3327 /*
3328 * ==> ServerHello
3329 * Certificate
3330 * ( ServerKeyExchange )
3331 * ( CertificateRequest )
3332 * ServerHelloDone
3333 */
3334 case SSL_SERVER_HELLO:
3335 ret = ssl_write_server_hello( ssl );
3336 break;
3337
3338 case SSL_SERVER_CERTIFICATE:
3339 ret = ssl_write_certificate( ssl );
3340 break;
3341
3342 case SSL_SERVER_KEY_EXCHANGE:
3343 ret = ssl_write_server_key_exchange( ssl );
3344 break;
3345
3346 case SSL_CERTIFICATE_REQUEST:
3347 ret = ssl_write_certificate_request( ssl );
3348 break;
3349
3350 case SSL_SERVER_HELLO_DONE:
3351 ret = ssl_write_server_hello_done( ssl );
3352 break;
3353
3354 /*
3355 * <== ( Certificate/Alert )
3356 * ClientKeyExchange
3357 * ( CertificateVerify )
3358 * ChangeCipherSpec
3359 * Finished
3360 */
3361 case SSL_CLIENT_CERTIFICATE:
3362 ret = ssl_parse_certificate( ssl );
3363 break;
3364
3365 case SSL_CLIENT_KEY_EXCHANGE:
3366 ret = ssl_parse_client_key_exchange( ssl );
3367 break;
3368
3369 case SSL_CERTIFICATE_VERIFY:
3370 ret = ssl_parse_certificate_verify( ssl );
3371 break;
3372
3373 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3374 ret = ssl_parse_change_cipher_spec( ssl );
3375 break;
3376
3377 case SSL_CLIENT_FINISHED:
3378 ret = ssl_parse_finished( ssl );
3379 break;
3380
3381 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003382 * ==> ( NewSessionTicket )
3383 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003384 * Finished
3385 */
3386 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003387#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003388 if( ssl->handshake->new_session_ticket != 0 )
3389 ret = ssl_write_new_session_ticket( ssl );
3390 else
Paul Bakkera503a632013-08-14 13:48:06 +02003391#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003392 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003393 break;
3394
3395 case SSL_SERVER_FINISHED:
3396 ret = ssl_write_finished( ssl );
3397 break;
3398
3399 case SSL_FLUSH_BUFFERS:
3400 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3401 ssl->state = SSL_HANDSHAKE_WRAPUP;
3402 break;
3403
3404 case SSL_HANDSHAKE_WRAPUP:
3405 ssl_handshake_wrapup( ssl );
3406 break;
3407
3408 default:
3409 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3410 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003411 }
3412
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 return( ret );
3414}
Paul Bakker9af723c2014-05-01 13:03:14 +02003415#endif /* POLARSSL_SSL_SRV_C */