blob: 16fb264dcc20311843c2f8c07651f9037e773733 [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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010032#if defined(POLARSSL_ECP_C)
33#include "polarssl/ecp.h"
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020038#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
44#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045
46#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkera503a632013-08-14 13:48:06 +020050#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020051/*
52 * Serialize a session in the following format:
53 * 0 . n-1 session structure, n = sizeof(ssl_session)
54 * n . n+2 peer_cert length = m (0 if no certificate)
55 * n+3 . n+2+m peer cert ASN.1
56 *
57 * Assumes ticket is NULL (always true on server side).
58 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020059static int ssl_save_session( const ssl_session *session,
60 unsigned char *buf, size_t buf_len,
61 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020062{
63 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020064 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020066 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020068
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020069 if( left < sizeof( ssl_session ) )
70 return( -1 );
71
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072 memcpy( p, session, sizeof( ssl_session ) );
73 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020074 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077 if( session->peer_cert == NULL )
78 cert_len = 0;
79 else
80 cert_len = session->peer_cert->raw.len;
81
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020082 if( left < 3 + cert_len )
83 return( -1 );
84
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020085 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
86 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
87 *p++ = (unsigned char)( cert_len & 0xFF );
88
89 if( session->peer_cert != NULL )
90 memcpy( p, session->peer_cert->raw.p, cert_len );
91
92 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094
95 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020096
97 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020098}
99
100/*
101 * Unserialise session, see ssl_save_session()
102 */
103static int ssl_load_session( ssl_session *session,
104 const unsigned char *buf, size_t len )
105{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200106 const unsigned char *p = buf;
107 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200109 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200111
112 if( p + sizeof( ssl_session ) > end )
113 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
114
115 memcpy( session, p, sizeof( ssl_session ) );
116 p += sizeof( ssl_session );
117
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200119 if( p + 3 > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
123 p += 3;
124
125 if( cert_len == 0 )
126 {
127 session->peer_cert = NULL;
128 }
129 else
130 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200131 int ret;
132
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200133 if( p + cert_len > end )
134 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
135
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200136 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200137
138 if( session->peer_cert == NULL )
139 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
140
Paul Bakkerb6b09562013-09-18 14:17:41 +0200141 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142
Paul Bakkerddf26b42013-09-18 13:46:23 +0200143 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200147 session->peer_cert = NULL;
148 return( ret );
149 }
150
151 p += cert_len;
152 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154
155 if( p != end )
156 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
157
158 return( 0 );
159}
160
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200161/*
162 * Create session ticket, secured as recommended in RFC 5077 section 4:
163 *
164 * struct {
165 * opaque key_name[16];
166 * opaque iv[16];
167 * opaque encrypted_state<0..2^16-1>;
168 * opaque mac[32];
169 * } ticket;
170 *
171 * (the internal state structure differs, however).
172 */
173static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
174{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200175 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200176 unsigned char * const start = ssl->out_msg + 10;
177 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200178 unsigned char *state;
179 unsigned char iv[16];
180 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200181
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200182 *tlen = 0;
183
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200184 if( ssl->ticket_keys == NULL )
185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
186
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200187 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200188 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189 p += 16;
190
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200191 /* Generate and write IV (with a copy for aes_crypt) */
192 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
193 return( ret );
194 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200195 p += 16;
196
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200197 /*
198 * Dump session state
199 *
200 * After the session state itself, we still need room for 16 bytes of
201 * padding and 32 bytes of MAC, so there's only so much room left
202 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200203 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200204 if( ssl_save_session( ssl->session_negotiate, state,
205 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
206 &clear_len ) != 0 )
207 {
208 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
209 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200210 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200211
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 /* Apply PKCS padding */
213 pad_len = 16 - clear_len % 16;
214 enc_len = clear_len + pad_len;
215 for( i = clear_len; i < enc_len; i++ )
216 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200217
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 /* Encrypt */
219 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
220 enc_len, iv, state, state ) ) != 0 )
221 {
222 return( ret );
223 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200224
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200225 /* Write length */
226 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
227 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
228 p = state + enc_len;
229
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200230 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
231 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232 p += 32;
233
234 *tlen = p - start;
235
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200236 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200237
238 return( 0 );
239}
240
241/*
242 * Load session ticket (see ssl_write_ticket for structure)
243 */
244static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200245 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200246 size_t len )
247{
248 int ret;
249 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200250 unsigned char *key_name = buf;
251 unsigned char *iv = buf + 16;
252 unsigned char *enc_len_p = iv + 16;
253 unsigned char *ticket = enc_len_p + 2;
254 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200255 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200256 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100257 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258
259 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200260
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200261 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
263
264 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
265 mac = ticket + enc_len;
266
267 if( len != enc_len + 66 )
268 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
269
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100270 /* Check name, in constant time though it's not a big secret */
271 diff = 0;
272 for( i = 0; i < 16; i++ )
273 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
274 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100276 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200277 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
278 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100279
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200280 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100281 diff |= mac[i] ^ computed_mac[i];
282
283 /* Now return if ticket is not authentic, since we want to avoid
284 * decrypting arbitrary attacker-chosen data */
285 if( diff != 0 )
286 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200287
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200288 /* Decrypt */
289 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
290 enc_len, iv, ticket, ticket ) ) != 0 )
291 {
292 return( ret );
293 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200294
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200295 /* Check PKCS padding */
296 pad_len = ticket[enc_len - 1];
297
298 ret = 0;
299 for( i = 2; i < pad_len; i++ )
300 if( ticket[enc_len - i] != pad_len )
301 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
302 if( ret != 0 )
303 return( ret );
304
305 clear_len = enc_len - pad_len;
306
307 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
308
309 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200310 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
311 {
312 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100313 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200314 return( ret );
315 }
316
Paul Bakker606b4ba2013-08-14 16:52:14 +0200317#if defined(POLARSSL_HAVE_TIME)
318 /* Check if still valid */
319 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
320 {
321 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100322 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200323 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
324 }
325#endif
326
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200327 /*
328 * Keep the session ID sent by the client, since we MUST send it back to
329 * inform him we're accepting the ticket (RFC 5077 section 3.4)
330 */
331 session.length = ssl->session_negotiate->length;
332 memcpy( &session.id, ssl->session_negotiate->id, session.length );
333
334 ssl_session_free( ssl->session_negotiate );
335 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
336 memset( &session, 0, sizeof( ssl_session ) );
337
338 return( 0 );
339}
Paul Bakkera503a632013-08-14 13:48:06 +0200340#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200341
Paul Bakker0be444a2013-08-27 21:55:01 +0200342#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200343/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200344 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
345 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200346 */
347static int ssl_sni_wrapper( ssl_context *ssl,
348 const unsigned char* name, size_t len )
349{
350 int ret;
351 ssl_key_cert *key_cert_ori = ssl->key_cert;
352
353 ssl->key_cert = NULL;
354 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200355 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200356
357 ssl->key_cert = key_cert_ori;
358
359 return( ret );
360}
361
Paul Bakker5701cdc2012-09-27 21:49:42 +0000362static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000363 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000364 size_t len )
365{
366 int ret;
367 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000368 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000369
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100370 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
371
Paul Bakker5701cdc2012-09-27 21:49:42 +0000372 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
373 if( servername_list_size + 2 != len )
374 {
375 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
376 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
377 }
378
379 p = buf + 2;
380 while( servername_list_size > 0 )
381 {
382 hostname_len = ( ( p[1] << 8 ) | p[2] );
383 if( hostname_len + 3 > servername_list_size )
384 {
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
387 }
388
389 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
390 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200391 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000392 if( ret != 0 )
393 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100394 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000395 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
396 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
397 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
398 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000399 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000400 }
401
402 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000403 p += hostname_len + 3;
404 }
405
406 if( servername_list_size != 0 )
407 {
408 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 }
411
412 return( 0 );
413}
Paul Bakker0be444a2013-08-27 21:55:01 +0200414#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000415
Paul Bakker48916f92012-09-16 19:57:18 +0000416static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000417 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000418 size_t len )
419{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000420 int ret;
421
Paul Bakker48916f92012-09-16 19:57:18 +0000422 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
423 {
424 if( len != 1 || buf[0] != 0x0 )
425 {
426 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000427
428 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
429 return( ret );
430
Paul Bakker48916f92012-09-16 19:57:18 +0000431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
432 }
433
434 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
435 }
436 else
437 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100438 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000439 if( len != 1 + ssl->verify_data_len ||
440 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100441 safer_memcmp( buf + 1, ssl->peer_verify_data,
442 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000443 {
444 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000445
446 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
447 return( ret );
448
Paul Bakker48916f92012-09-16 19:57:18 +0000449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
450 }
451 }
452
453 return( 0 );
454}
455
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200456#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000457static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
458 const unsigned char *buf,
459 size_t len )
460{
461 size_t sig_alg_list_size;
462 const unsigned char *p;
463
464 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
465 if( sig_alg_list_size + 2 != len ||
466 sig_alg_list_size %2 != 0 )
467 {
468 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
469 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
470 }
471
472 p = buf + 2;
473 while( sig_alg_list_size > 0 )
474 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200475 /*
476 * For now, just ignore signature algorithm and rely on offered
477 * ciphersuites only. To be fixed later.
478 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200479#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000480 if( p[0] == SSL_HASH_SHA512 )
481 {
482 ssl->handshake->sig_alg = SSL_HASH_SHA512;
483 break;
484 }
485 if( p[0] == SSL_HASH_SHA384 )
486 {
487 ssl->handshake->sig_alg = SSL_HASH_SHA384;
488 break;
489 }
490#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200491#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000492 if( p[0] == SSL_HASH_SHA256 )
493 {
494 ssl->handshake->sig_alg = SSL_HASH_SHA256;
495 break;
496 }
497 if( p[0] == SSL_HASH_SHA224 )
498 {
499 ssl->handshake->sig_alg = SSL_HASH_SHA224;
500 break;
501 }
502#endif
503 if( p[0] == SSL_HASH_SHA1 )
504 {
505 ssl->handshake->sig_alg = SSL_HASH_SHA1;
506 break;
507 }
508 if( p[0] == SSL_HASH_MD5 )
509 {
510 ssl->handshake->sig_alg = SSL_HASH_MD5;
511 break;
512 }
513
514 sig_alg_list_size -= 2;
515 p += 2;
516 }
517
518 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
519 ssl->handshake->sig_alg ) );
520
521 return( 0 );
522}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200523#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000524
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200525#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200526static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
527 const unsigned char *buf,
528 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100529{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200530 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100531 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200532 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100533
534 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
535 if( list_size + 2 != len ||
536 list_size % 2 != 0 )
537 {
538 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
539 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
540 }
541
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100542 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200543 * and leave room for a final 0 */
544 our_size = list_size / 2 + 1;
545 if( our_size > POLARSSL_ECP_DP_MAX )
546 our_size = POLARSSL_ECP_DP_MAX;
547
548 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
549 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
550
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200551 /* explicit void pointer cast for buggy MS compiler */
552 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 ssl->handshake->curves = curves;
554
Paul Bakker41c83d32013-03-20 14:39:14 +0100555 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200556 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200558 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200559
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200560 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200562 *curves++ = curve_info;
563 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100564 }
565
566 list_size -= 2;
567 p += 2;
568 }
569
570 return( 0 );
571}
572
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200573static int ssl_parse_supported_point_formats( ssl_context *ssl,
574 const unsigned char *buf,
575 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100576{
577 size_t list_size;
578 const unsigned char *p;
579
580 list_size = buf[0];
581 if( list_size + 1 != len )
582 {
583 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
584 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
585 }
586
587 p = buf + 2;
588 while( list_size > 0 )
589 {
590 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
591 p[0] == POLARSSL_ECP_PF_COMPRESSED )
592 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200593 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200594 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100595 return( 0 );
596 }
597
598 list_size--;
599 p++;
600 }
601
602 return( 0 );
603}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200604#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100605
Paul Bakker05decb22013-08-15 13:33:48 +0200606#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200607static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
608 const unsigned char *buf,
609 size_t len )
610{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200611 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200617 ssl->session_negotiate->mfl_code = buf[0];
618
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200619 return( 0 );
620}
Paul Bakker05decb22013-08-15 13:33:48 +0200621#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200622
Paul Bakker1f2bc622013-08-15 13:45:55 +0200623#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200624static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
625 const unsigned char *buf,
626 size_t len )
627{
628 if( len != 0 )
629 {
630 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
631 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
632 }
633
634 ((void) buf);
635
636 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
637
638 return( 0 );
639}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200640#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200641
Paul Bakkera503a632013-08-14 13:48:06 +0200642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200643static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200644 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200645 size_t len )
646{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200647 int ret;
648
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200649 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
650 return( 0 );
651
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200652 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653 ssl->handshake->new_session_ticket = 1;
654
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200655 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
656
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200657 if( len == 0 )
658 return( 0 );
659
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200660 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
661 {
662 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
663 return( 0 );
664 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665
666 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667 * Failures are ok: just ignore the ticket and proceed.
668 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200669 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
670 {
671 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200672 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200673 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200674
675 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
676
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200677 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200679 /* Don't send a new ticket after all, this one is OK */
680 ssl->handshake->new_session_ticket = 0;
681
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200682 return( 0 );
683}
Paul Bakkera503a632013-08-14 13:48:06 +0200684#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200685
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100686/*
687 * Auxiliary functions for ServerHello parsing and related actions
688 */
689
690#if defined(POLARSSL_X509_CRT_PARSE_C)
691/*
692 * Return 1 if the given EC key uses the given curve, 0 otherwise
693 */
694#if defined(POLARSSL_ECDSA_C)
695static int ssl_key_matches_curves( pk_context *pk,
696 const ecp_curve_info **curves )
697{
698 const ecp_curve_info **crv = curves;
699 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
700
701 while( *crv != NULL )
702 {
703 if( (*crv)->grp_id == grp_id )
704 return( 1 );
705 crv++;
706 }
707
708 return( 0 );
709}
710#endif /* POLARSSL_ECDSA_C */
711
712/*
713 * Try picking a certificate for this ciphersuite,
714 * return 0 on success and -1 on failure.
715 */
716static int ssl_pick_cert( ssl_context *ssl,
717 const ssl_ciphersuite_t * ciphersuite_info )
718{
719 ssl_key_cert *cur, *list;
720 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
721
722#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
723 if( ssl->handshake->sni_key_cert != NULL )
724 list = ssl->handshake->sni_key_cert;
725 else
726#endif
727 list = ssl->handshake->key_cert;
728
729 if( pk_alg == POLARSSL_PK_NONE )
730 return( 0 );
731
732 for( cur = list; cur != NULL; cur = cur->next )
733 {
734 if( ! pk_can_do( cur->key, pk_alg ) )
735 continue;
736
737#if defined(POLARSSL_ECDSA_C)
738 if( pk_alg == POLARSSL_PK_ECDSA )
739 {
740 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
741 break;
742 }
743 else
744#endif
745 break;
746 }
747
748 if( cur == NULL )
749 return( -1 );
750
751 ssl->handshake->key_cert = cur;
752 return( 0 );
753}
754#endif /* POLARSSL_X509_CRT_PARSE_C */
755
756/*
757 * Check if a given ciphersuite is suitable for use with our config/keys/etc
758 * Sets ciphersuite_info only if the suite matches.
759 */
760static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
761 const ssl_ciphersuite_t **ciphersuite_info )
762{
763 const ssl_ciphersuite_t *suite_info;
764
765 suite_info = ssl_ciphersuite_from_id( suite_id );
766 if( suite_info == NULL )
767 {
768 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
769 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
770 }
771
772 if( suite_info->min_minor_ver > ssl->minor_ver ||
773 suite_info->max_minor_ver < ssl->minor_ver )
774 return( 0 );
775
776#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
777 if( ssl_ciphersuite_uses_ec( suite_info ) &&
778 ( ssl->handshake->curves == NULL ||
779 ssl->handshake->curves[0] == NULL ) )
780 return( 0 );
781#endif
782
783#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
784 /* If the ciphersuite requires a pre-shared key and we don't
785 * have one, skip it now rather than failing later */
786 if( ssl_ciphersuite_uses_psk( suite_info ) &&
787 ssl->f_psk == NULL &&
788 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
789 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
790 return( 0 );
791#endif
792
793#if defined(POLARSSL_X509_CRT_PARSE_C)
794 /*
795 * Final check: if ciphersuite requires us to have a
796 * certificate/key of a particular type:
797 * - select the appropriate certificate if we have one, or
798 * - try the next ciphersuite if we don't
799 * This must be done last since we modify the key_cert list.
800 */
801 if( ssl_pick_cert( ssl, suite_info ) != 0 )
802 return( 0 );
803#endif
804
805 *ciphersuite_info = suite_info;
806 return( 0 );
807}
808
Paul Bakker78a8c712013-03-06 17:01:52 +0100809#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
810static int ssl_parse_client_hello_v2( ssl_context *ssl )
811{
812 int ret;
813 unsigned int i, j;
814 size_t n;
815 unsigned int ciph_len, sess_len, chal_len;
816 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200817 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200818 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100819
820 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
821
822 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
823 {
824 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
825
826 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
827 return( ret );
828
829 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
830 }
831
832 buf = ssl->in_hdr;
833
834 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
835
836 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
837 buf[2] ) );
838 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
839 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
840 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
841 buf[3], buf[4] ) );
842
843 /*
844 * SSLv2 Client Hello
845 *
846 * Record layer:
847 * 0 . 1 message length
848 *
849 * SSL layer:
850 * 2 . 2 message type
851 * 3 . 4 protocol version
852 */
853 if( buf[2] != SSL_HS_CLIENT_HELLO ||
854 buf[3] != SSL_MAJOR_VERSION_3 )
855 {
856 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
857 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
858 }
859
860 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
861
862 if( n < 17 || n > 512 )
863 {
864 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
865 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
866 }
867
868 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200869 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
870 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100871
872 if( ssl->minor_ver < ssl->min_minor_ver )
873 {
874 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
875 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
876 ssl->min_major_ver, ssl->min_minor_ver ) );
877
878 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
879 SSL_ALERT_MSG_PROTOCOL_VERSION );
880 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
881 }
882
Paul Bakker2fbefde2013-06-29 16:01:15 +0200883 ssl->handshake->max_major_ver = buf[3];
884 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100885
886 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
887 {
888 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
889 return( ret );
890 }
891
892 ssl->handshake->update_checksum( ssl, buf + 2, n );
893
894 buf = ssl->in_msg;
895 n = ssl->in_left - 5;
896
897 /*
898 * 0 . 1 ciphersuitelist length
899 * 2 . 3 session id length
900 * 4 . 5 challenge length
901 * 6 . .. ciphersuitelist
902 * .. . .. session id
903 * .. . .. challenge
904 */
905 SSL_DEBUG_BUF( 4, "record contents", buf, n );
906
907 ciph_len = ( buf[0] << 8 ) | buf[1];
908 sess_len = ( buf[2] << 8 ) | buf[3];
909 chal_len = ( buf[4] << 8 ) | buf[5];
910
911 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
912 ciph_len, sess_len, chal_len ) );
913
914 /*
915 * Make sure each parameter length is valid
916 */
917 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
918 {
919 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
920 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
921 }
922
923 if( sess_len > 32 )
924 {
925 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
926 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
927 }
928
929 if( chal_len < 8 || chal_len > 32 )
930 {
931 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
932 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
933 }
934
935 if( n != 6 + ciph_len + sess_len + chal_len )
936 {
937 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
939 }
940
941 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
942 buf + 6, ciph_len );
943 SSL_DEBUG_BUF( 3, "client hello, session id",
944 buf + 6 + ciph_len, sess_len );
945 SSL_DEBUG_BUF( 3, "client hello, challenge",
946 buf + 6 + ciph_len + sess_len, chal_len );
947
948 p = buf + 6 + ciph_len;
949 ssl->session_negotiate->length = sess_len;
950 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
951 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
952
953 p += sess_len;
954 memset( ssl->handshake->randbytes, 0, 64 );
955 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
956
957 /*
958 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
959 */
960 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
961 {
962 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
963 {
964 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
965 if( ssl->renegotiation == SSL_RENEGOTIATION )
966 {
967 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
968
969 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
970 return( ret );
971
972 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
973 }
974 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
975 break;
976 }
977 }
978
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200979 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100980 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +0100981#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
982 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
983 {
984 for( i = 0; ciphersuites[i] != 0; i++ )
985#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200986 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100987 {
988 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +0100989#endif
Paul Bakker78a8c712013-03-06 17:01:52 +0100990 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100991 if( p[0] != 0 ||
992 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
993 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
994 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200995
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100996 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
997 &ciphersuite_info ) ) != 0 )
998 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +0200999
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001000 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001001 goto have_ciphersuite_v2;
1002 }
1003 }
1004
1005 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1006
1007 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1008
1009have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001010 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001011 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001012 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001013
1014 /*
1015 * SSLv2 Client Hello relevant renegotiation security checks
1016 */
1017 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1018 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1021
1022 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1023 return( ret );
1024
1025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1026 }
1027
1028 ssl->in_left = 0;
1029 ssl->state++;
1030
1031 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1032
1033 return( 0 );
1034}
1035#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1036
Paul Bakker5121ce52009-01-03 21:22:43 +00001037static int ssl_parse_client_hello( ssl_context *ssl )
1038{
Paul Bakker23986e52011-04-24 08:57:21 +00001039 int ret;
1040 unsigned int i, j;
1041 size_t n;
1042 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001043 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001044 unsigned int ext_len = 0;
1045 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001046 int renegotiation_info_seen = 0;
1047 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001048 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001049 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
1051 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1052
Paul Bakker48916f92012-09-16 19:57:18 +00001053 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1054 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
1056 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1057 return( ret );
1058 }
1059
1060 buf = ssl->in_hdr;
1061
Paul Bakker78a8c712013-03-06 17:01:52 +01001062#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1063 if( ( buf[0] & 0x80 ) != 0 )
1064 return ssl_parse_client_hello_v2( ssl );
1065#endif
1066
Paul Bakkerec636f32012-09-09 19:17:02 +00001067 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1068
1069 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1070 buf[0] ) );
1071 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1072 ( buf[3] << 8 ) | buf[4] ) );
1073 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1074 buf[1], buf[2] ) );
1075
1076 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001077 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001078 *
1079 * Record layer:
1080 * 0 . 0 message type
1081 * 1 . 2 protocol version
1082 * 3 . 4 message length
1083 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001084
1085 /* According to RFC 5246 Appendix E.1, the version here is typically
1086 * "{03,00}, the lowest version number supported by the client, [or] the
1087 * value of ClientHello.client_version", so the only meaningful check here
1088 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001089 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001090 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001091 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001092 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1093 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001095
Paul Bakkerec636f32012-09-09 19:17:02 +00001096 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001098 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001099 {
1100 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1101 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1102 }
1103
Paul Bakker48916f92012-09-16 19:57:18 +00001104 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1105 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001106 {
1107 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1108 return( ret );
1109 }
1110
1111 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001112 if( !ssl->renegotiation )
1113 n = ssl->in_left - 5;
1114 else
1115 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001116
Paul Bakker48916f92012-09-16 19:57:18 +00001117 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001118
1119 /*
1120 * SSL layer:
1121 * 0 . 0 handshake type
1122 * 1 . 3 handshake length
1123 * 4 . 5 protocol version
1124 * 6 . 9 UNIX time()
1125 * 10 . 37 random bytes
1126 * 38 . 38 session id length
1127 * 39 . 38+x session id
1128 * 39+x . 40+x ciphersuitelist length
1129 * 41+x . .. ciphersuitelist
1130 * .. . .. compression alg.
1131 * .. . .. extensions
1132 */
1133 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1134
1135 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1136 buf[0] ) );
1137 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1138 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1139 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1140 buf[4], buf[5] ) );
1141
1142 /*
1143 * Check the handshake type and protocol version
1144 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001145 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001146 {
1147 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1148 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1149 }
1150
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001151 ssl->major_ver = buf[4];
1152 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001153
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001154 ssl->handshake->max_major_ver = ssl->major_ver;
1155 ssl->handshake->max_minor_ver = ssl->minor_ver;
1156
1157 if( ssl->major_ver < ssl->min_major_ver ||
1158 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001159 {
1160 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001161 " [%d:%d] < [%d:%d]",
1162 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001163 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001164
1165 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1166 SSL_ALERT_MSG_PROTOCOL_VERSION );
1167
1168 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1169 }
1170
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001171 if( ssl->major_ver > ssl->max_major_ver )
1172 {
1173 ssl->major_ver = ssl->max_major_ver;
1174 ssl->minor_ver = ssl->max_minor_ver;
1175 }
1176 else if( ssl->minor_ver > ssl->max_minor_ver )
1177 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001178
Paul Bakker48916f92012-09-16 19:57:18 +00001179 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001180
1181 /*
1182 * Check the handshake message length
1183 */
1184 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1185 {
1186 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1187 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1188 }
1189
1190 /*
1191 * Check the session length
1192 */
1193 sess_len = buf[38];
1194
1195 if( sess_len > 32 )
1196 {
1197 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1199 }
1200
Paul Bakker48916f92012-09-16 19:57:18 +00001201 ssl->session_negotiate->length = sess_len;
1202 memset( ssl->session_negotiate->id, 0,
1203 sizeof( ssl->session_negotiate->id ) );
1204 memcpy( ssl->session_negotiate->id, buf + 39,
1205 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001206
1207 /*
1208 * Check the ciphersuitelist length
1209 */
1210 ciph_len = ( buf[39 + sess_len] << 8 )
1211 | ( buf[40 + sess_len] );
1212
1213 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1214 {
1215 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1216 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1217 }
1218
1219 /*
1220 * Check the compression algorithms length
1221 */
1222 comp_len = buf[41 + sess_len + ciph_len];
1223
1224 if( comp_len < 1 || comp_len > 16 )
1225 {
1226 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1227 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1228 }
1229
Paul Bakker48916f92012-09-16 19:57:18 +00001230 /*
1231 * Check the extension length
1232 */
1233 if( n > 42 + sess_len + ciph_len + comp_len )
1234 {
1235 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1236 | ( buf[43 + sess_len + ciph_len + comp_len] );
1237
1238 if( ( ext_len > 0 && ext_len < 4 ) ||
1239 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1240 {
1241 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1242 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1243 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1244 }
1245 }
1246
1247 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001248#if defined(POLARSSL_ZLIB_SUPPORT)
1249 for( i = 0; i < comp_len; ++i )
1250 {
Paul Bakker48916f92012-09-16 19:57:18 +00001251 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 {
Paul Bakker48916f92012-09-16 19:57:18 +00001253 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001254 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 }
1256 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001257#endif
1258
Paul Bakkerec636f32012-09-09 19:17:02 +00001259 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1260 buf + 6, 32 );
1261 SSL_DEBUG_BUF( 3, "client hello, session id",
1262 buf + 38, sess_len );
1263 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1264 buf + 41 + sess_len, ciph_len );
1265 SSL_DEBUG_BUF( 3, "client hello, compression",
1266 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Paul Bakkerec636f32012-09-09 19:17:02 +00001268 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001269 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1270 */
1271 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1272 {
1273 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1274 {
1275 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1276 if( ssl->renegotiation == SSL_RENEGOTIATION )
1277 {
1278 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001279
1280 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1281 return( ret );
1282
Paul Bakker48916f92012-09-16 19:57:18 +00001283 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1284 }
1285 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1286 break;
1287 }
1288 }
1289
Paul Bakker48916f92012-09-16 19:57:18 +00001290 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001291
1292 while( ext_len )
1293 {
1294 unsigned int ext_id = ( ( ext[0] << 8 )
1295 | ( ext[1] ) );
1296 unsigned int ext_size = ( ( ext[2] << 8 )
1297 | ( ext[3] ) );
1298
1299 if( ext_size + 4 > ext_len )
1300 {
1301 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1302 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1303 }
1304 switch( ext_id )
1305 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001306#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001307 case TLS_EXT_SERVERNAME:
1308 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1309 if( ssl->f_sni == NULL )
1310 break;
1311
1312 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1313 if( ret != 0 )
1314 return( ret );
1315 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001316#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001317
Paul Bakker48916f92012-09-16 19:57:18 +00001318 case TLS_EXT_RENEGOTIATION_INFO:
1319 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1320 renegotiation_info_seen = 1;
1321
Paul Bakker23f36802012-09-28 14:15:14 +00001322 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1323 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001324 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001325 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001326
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001327#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001328 case TLS_EXT_SIG_ALG:
1329 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1330 if( ssl->renegotiation == SSL_RENEGOTIATION )
1331 break;
1332
1333 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1334 if( ret != 0 )
1335 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001336 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001337#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001338
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001339#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001340 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1341 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1342
1343 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1344 if( ret != 0 )
1345 return( ret );
1346 break;
1347
1348 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1349 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001350 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001351
1352 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1353 if( ret != 0 )
1354 return( ret );
1355 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001356#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001357
Paul Bakker05decb22013-08-15 13:33:48 +02001358#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001359 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1360 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1361
1362 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1363 if( ret != 0 )
1364 return( ret );
1365 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001366#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001367
Paul Bakker1f2bc622013-08-15 13:45:55 +02001368#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001369 case TLS_EXT_TRUNCATED_HMAC:
1370 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1371
1372 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1373 if( ret != 0 )
1374 return( ret );
1375 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001376#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001377
Paul Bakkera503a632013-08-14 13:48:06 +02001378#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001379 case TLS_EXT_SESSION_TICKET:
1380 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1381
1382 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1383 if( ret != 0 )
1384 return( ret );
1385 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001386#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001387
Paul Bakker48916f92012-09-16 19:57:18 +00001388 default:
1389 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1390 ext_id ) );
1391 }
1392
1393 ext_len -= 4 + ext_size;
1394 ext += 4 + ext_size;
1395
1396 if( ext_len > 0 && ext_len < 4 )
1397 {
1398 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1399 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1400 }
1401 }
1402
1403 /*
1404 * Renegotiation security checks
1405 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001406 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1407 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1408 {
1409 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1410 handshake_failure = 1;
1411 }
1412 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1413 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1414 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001415 {
1416 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001417 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001418 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001419 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1420 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1421 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001422 {
1423 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001424 handshake_failure = 1;
1425 }
1426 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1427 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1428 renegotiation_info_seen == 1 )
1429 {
1430 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1431 handshake_failure = 1;
1432 }
1433
1434 if( handshake_failure == 1 )
1435 {
1436 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1437 return( ret );
1438
Paul Bakker48916f92012-09-16 19:57:18 +00001439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1440 }
Paul Bakker380da532012-04-18 16:10:25 +00001441
Paul Bakker41c83d32013-03-20 14:39:14 +01001442 /*
1443 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001444 * (At the end because we need information from the EC-based extensions
1445 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001446 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001447 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001448 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001449#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1450 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1451 {
1452 for( i = 0; ciphersuites[i] != 0; i++ )
1453#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001454 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001455 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001456 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001457#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001458 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001459 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1460 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1461 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001462
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001463 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1464 &ciphersuite_info ) ) != 0 )
1465 return( ret );
1466
1467 if( ciphersuite_info != NULL )
1468 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001469 }
1470 }
1471
1472 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1473
1474 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1475 return( ret );
1476
1477 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1478
1479have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001480 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001481 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1482 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1483
Paul Bakker5121ce52009-01-03 21:22:43 +00001484 ssl->in_left = 0;
1485 ssl->state++;
1486
1487 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1488
1489 return( 0 );
1490}
1491
Paul Bakker1f2bc622013-08-15 13:45:55 +02001492#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001493static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1494 unsigned char *buf,
1495 size_t *olen )
1496{
1497 unsigned char *p = buf;
1498
1499 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1500 {
1501 *olen = 0;
1502 return;
1503 }
1504
1505 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1506
1507 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1508 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1509
1510 *p++ = 0x00;
1511 *p++ = 0x00;
1512
1513 *olen = 4;
1514}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001515#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001516
Paul Bakkera503a632013-08-14 13:48:06 +02001517#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001518static void ssl_write_session_ticket_ext( ssl_context *ssl,
1519 unsigned char *buf,
1520 size_t *olen )
1521{
1522 unsigned char *p = buf;
1523
1524 if( ssl->handshake->new_session_ticket == 0 )
1525 {
1526 *olen = 0;
1527 return;
1528 }
1529
1530 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1531
1532 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1533 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1534
1535 *p++ = 0x00;
1536 *p++ = 0x00;
1537
1538 *olen = 4;
1539}
Paul Bakkera503a632013-08-14 13:48:06 +02001540#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001541
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001542static void ssl_write_renegotiation_ext( ssl_context *ssl,
1543 unsigned char *buf,
1544 size_t *olen )
1545{
1546 unsigned char *p = buf;
1547
1548 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1549 {
1550 *olen = 0;
1551 return;
1552 }
1553
1554 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1555
1556 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1557 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1558
1559 *p++ = 0x00;
1560 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1561 *p++ = ssl->verify_data_len * 2 & 0xFF;
1562
1563 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1564 p += ssl->verify_data_len;
1565 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1566 p += ssl->verify_data_len;
1567
1568 *olen = 5 + ssl->verify_data_len * 2;
1569}
1570
Paul Bakker05decb22013-08-15 13:33:48 +02001571#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001572static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1573 unsigned char *buf,
1574 size_t *olen )
1575{
1576 unsigned char *p = buf;
1577
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001578 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1579 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001580 *olen = 0;
1581 return;
1582 }
1583
1584 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1585
1586 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1587 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1588
1589 *p++ = 0x00;
1590 *p++ = 1;
1591
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001592 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001593
1594 *olen = 5;
1595}
Paul Bakker05decb22013-08-15 13:33:48 +02001596#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001597
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001598#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001599static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1600 unsigned char *buf,
1601 size_t *olen )
1602{
1603 unsigned char *p = buf;
1604 ((void) ssl);
1605
Paul Bakker677377f2013-10-28 12:54:26 +01001606 if( ( ssl->handshake->cli_exts &
1607 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1608 {
1609 *olen = 0;
1610 return;
1611 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001612
1613 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1614
1615 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1616 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1617
1618 *p++ = 0x00;
1619 *p++ = 2;
1620
1621 *p++ = 1;
1622 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1623
1624 *olen = 6;
1625}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001626#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001627
Paul Bakker5121ce52009-01-03 21:22:43 +00001628static int ssl_write_server_hello( ssl_context *ssl )
1629{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001630#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001631 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001632#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001633 int ret;
1634 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 unsigned char *buf, *p;
1636
1637 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1638
Paul Bakkera9a028e2013-11-21 17:31:06 +01001639 if( ssl->f_rng == NULL )
1640 {
1641 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1642 return( POLARSSL_ERR_SSL_NO_RNG );
1643 }
1644
Paul Bakker5121ce52009-01-03 21:22:43 +00001645 /*
1646 * 0 . 0 handshake type
1647 * 1 . 3 handshake length
1648 * 4 . 5 protocol version
1649 * 6 . 9 UNIX time()
1650 * 10 . 37 random bytes
1651 */
1652 buf = ssl->out_msg;
1653 p = buf + 4;
1654
1655 *p++ = (unsigned char) ssl->major_ver;
1656 *p++ = (unsigned char) ssl->minor_ver;
1657
1658 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1659 buf[4], buf[5] ) );
1660
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001661#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001662 t = time( NULL );
1663 *p++ = (unsigned char)( t >> 24 );
1664 *p++ = (unsigned char)( t >> 16 );
1665 *p++ = (unsigned char)( t >> 8 );
1666 *p++ = (unsigned char)( t );
1667
1668 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001669#else
1670 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1671 return( ret );
1672
1673 p += 4;
1674#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Paul Bakkera3d195c2011-11-27 21:07:34 +00001676 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1677 return( ret );
1678
1679 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Paul Bakker48916f92012-09-16 19:57:18 +00001681 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
1683 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1684
1685 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001686 * Resume is 0 by default, see ssl_handshake_init().
1687 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1688 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001689 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001690 if( ssl->handshake->resume == 0 &&
1691 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001692 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001693 ssl->f_get_cache != NULL &&
1694 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1695 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001696 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001697 ssl->handshake->resume = 1;
1698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001699
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001700 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 {
1702 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001703 * New session, create a new session id,
1704 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001706 ssl->state++;
1707
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001708#if defined(POLARSSL_HAVE_TIME)
1709 ssl->session_negotiate->start = time( NULL );
1710#endif
1711
Paul Bakkera503a632013-08-14 13:48:06 +02001712#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001713 if( ssl->handshake->new_session_ticket != 0 )
1714 {
1715 ssl->session_negotiate->length = n = 0;
1716 memset( ssl->session_negotiate->id, 0, 32 );
1717 }
1718 else
1719#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001720 {
1721 ssl->session_negotiate->length = n = 32;
1722 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001723 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001724 return( ret );
1725 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001726 }
1727 else
1728 {
1729 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001730 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001731 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001732 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001734
1735 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1736 {
1737 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1738 return( ret );
1739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001740 }
1741
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001742 /*
1743 * 38 . 38 session id length
1744 * 39 . 38+n session id
1745 * 39+n . 40+n chosen ciphersuite
1746 * 41+n . 41+n chosen compression alg.
1747 * 42+n . 43+n extensions length
1748 * 44+n . 43+n+m extensions
1749 */
1750 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001751 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1752 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001753
1754 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1755 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1756 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001757 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Paul Bakker48916f92012-09-16 19:57:18 +00001759 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1760 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1761 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001763 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1764 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001765 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001766 ssl->session_negotiate->compression ) );
1767
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001768 /*
1769 * First write extensions, then the total length
1770 */
1771 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1772 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001773
Paul Bakker05decb22013-08-15 13:33:48 +02001774#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001775 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1776 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001777#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001778
Paul Bakker1f2bc622013-08-15 13:45:55 +02001779#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001780 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1781 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001782#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001783
Paul Bakkera503a632013-08-14 13:48:06 +02001784#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001785 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1786 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001787#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001788
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001789#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001790 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1791 ext_len += olen;
1792#endif
1793
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001794 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001795
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001796 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1797 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1798 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
1800 ssl->out_msglen = p - buf;
1801 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1802 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1803
1804 ret = ssl_write_record( ssl );
1805
1806 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1807
1808 return( ret );
1809}
1810
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001811#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1812 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001813 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1814 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001815static int ssl_write_certificate_request( ssl_context *ssl )
1816{
Paul Bakkered27a042013-04-18 22:46:23 +02001817 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1818 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001819
1820 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1821
1822 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001823 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001824 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1825 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001826 {
1827 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1828 ssl->state++;
1829 return( 0 );
1830 }
1831
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001832 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001833 return( ret );
1834}
1835#else
1836static int ssl_write_certificate_request( ssl_context *ssl )
1837{
1838 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1839 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001840 size_t dn_size, total_dn_size; /* excluding length bytes */
1841 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001843 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
1845 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1846
1847 ssl->state++;
1848
Paul Bakkerfbb17802013-04-17 19:10:21 +02001849 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001850 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001851 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001852 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001853 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001855 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 return( 0 );
1857 }
1858
1859 /*
1860 * 0 . 0 handshake type
1861 * 1 . 3 handshake length
1862 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001863 * 5 .. m-1 cert types
1864 * m .. m+1 sig alg length (TLS 1.2 only)
1865 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 * n .. n+1 length of all DNs
1867 * n+2 .. n+3 length of DN 1
1868 * n+4 .. ... Distinguished Name #1
1869 * ... .. ... length of DN 2, etc.
1870 */
1871 buf = ssl->out_msg;
1872 p = buf + 4;
1873
1874 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001875 * Supported certificate types
1876 *
1877 * ClientCertificateType certificate_types<1..2^8-1>;
1878 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001880 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001881
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001882#if defined(POLARSSL_RSA_C)
1883 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1884#endif
1885#if defined(POLARSSL_ECDSA_C)
1886 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1887#endif
1888
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001889 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001890 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001891
Paul Bakker577e0062013-08-28 11:57:20 +02001892 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001893#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001894 /*
1895 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001896 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001897 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1898 *
1899 * struct {
1900 * HashAlgorithm hash;
1901 * SignatureAlgorithm signature;
1902 * } SignatureAndHashAlgorithm;
1903 *
1904 * enum { (255) } HashAlgorithm;
1905 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001906 */
Paul Bakker21dca692013-01-03 11:41:08 +01001907 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001908 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001909 /*
1910 * Only use current running hash algorithm that is already required
1911 * for requested ciphersuite.
1912 */
Paul Bakker926af752012-11-23 13:38:07 +01001913 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1914
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001915 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1916 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001917 {
1918 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1919 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001920
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001921 /*
1922 * Supported signature algorithms
1923 */
1924#if defined(POLARSSL_RSA_C)
1925 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1926 p[2 + sa_len++] = SSL_SIG_RSA;
1927#endif
1928#if defined(POLARSSL_ECDSA_C)
1929 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1930 p[2 + sa_len++] = SSL_SIG_ECDSA;
1931#endif
Paul Bakker926af752012-11-23 13:38:07 +01001932
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001933 p[0] = (unsigned char)( sa_len >> 8 );
1934 p[1] = (unsigned char)( sa_len );
1935 sa_len += 2;
1936 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001937 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001938#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001940 /*
1941 * DistinguishedName certificate_authorities<0..2^16-1>;
1942 * opaque DistinguishedName<1..2^16-1>;
1943 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 p += 2;
1945 crt = ssl->ca_chain;
1946
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001947 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001948 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001949 {
1950 if( p - buf > 4096 )
1951 break;
1952
Paul Bakker926af752012-11-23 13:38:07 +01001953 dn_size = crt->subject_raw.len;
1954 *p++ = (unsigned char)( dn_size >> 8 );
1955 *p++ = (unsigned char)( dn_size );
1956 memcpy( p, crt->subject_raw.p, dn_size );
1957 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Paul Bakker926af752012-11-23 13:38:07 +01001959 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1960
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001961 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001962 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 }
1964
Paul Bakker926af752012-11-23 13:38:07 +01001965 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001966 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1967 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001968 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1969 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001970
1971 ret = ssl_write_record( ssl );
1972
1973 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1974
1975 return( ret );
1976}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001977#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1978 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001979 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
1980 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001981
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01001982#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1983 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1984static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1985{
1986 int ret;
1987
1988 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
1989 {
1990 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1991 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1992 }
1993
1994 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
1995 pk_ec( *ssl_own_key( ssl ) ),
1996 POLARSSL_ECDH_OURS ) ) != 0 )
1997 {
1998 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1999 return( ret );
2000 }
2001
2002 return( 0 );
2003}
2004#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2005 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2006
Paul Bakker41c83d32013-03-20 14:39:14 +01002007static int ssl_write_server_key_exchange( ssl_context *ssl )
2008{
Paul Bakker23986e52011-04-24 08:57:21 +00002009 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002010 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002011 const ssl_ciphersuite_t *ciphersuite_info =
2012 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002013
2014#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2015 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2016 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002017 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002018 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002019 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002020 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002021 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002022 ((void) dig_signed);
2023 ((void) dig_signed_len);
2024#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002025
Paul Bakker5121ce52009-01-03 21:22:43 +00002026 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2027
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002028#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2029 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2030 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002031 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2032 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2033 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 {
2035 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2036 ssl->state++;
2037 return( 0 );
2038 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002039#endif
2040
2041#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2042 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2043 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2044 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2045 {
2046 ssl_get_ecdh_params_from_cert( ssl );
2047
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002048 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002049 ssl->state++;
2050 return( 0 );
2051 }
2052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002053
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002054#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2055 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2056 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2057 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002058 {
2059 /* TODO: Support identity hints */
2060 *(p++) = 0x00;
2061 *(p++) = 0x00;
2062
2063 n += 2;
2064 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002065#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2066 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002067
2068#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2069 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2070 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2071 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002072 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002073 /*
2074 * Ephemeral DH parameters:
2075 *
2076 * struct {
2077 * opaque dh_p<1..2^16-1>;
2078 * opaque dh_g<1..2^16-1>;
2079 * opaque dh_Ys<1..2^16-1>;
2080 * } ServerDHParams;
2081 */
2082 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2083 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2084 {
2085 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2086 return( ret );
2087 }
Paul Bakker48916f92012-09-16 19:57:18 +00002088
Paul Bakker41c83d32013-03-20 14:39:14 +01002089 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002090 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002091 p,
2092 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002093 {
2094 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2095 return( ret );
2096 }
2097
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002098 dig_signed = p;
2099 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002100
2101 p += len;
2102 n += len;
2103
Paul Bakker41c83d32013-03-20 14:39:14 +01002104 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2105 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2106 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2107 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2108 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002109#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2110 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002111
Gergely Budai987bfb52014-01-19 21:48:42 +01002112#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002113 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002114 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2115 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002116 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002117 /*
2118 * Ephemeral ECDH parameters:
2119 *
2120 * struct {
2121 * ECParameters curve_params;
2122 * ECPoint public;
2123 * } ServerECDHParams;
2124 */
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002125 const ecp_curve_info **curve;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002126#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002127 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002128
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002129 /* Match our preference list against the offered curves */
2130 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2131 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2132 if( (*curve)->grp_id == *gid )
2133 goto curve_matching_done;
2134
2135curve_matching_done:
2136#else
2137 curve = ssl->handshake->curves;
2138#endif
2139
2140 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002141 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002142 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2143 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002144 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002145
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002146 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002147
Paul Bakker41c83d32013-03-20 14:39:14 +01002148 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002149 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002150 {
2151 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2152 return( ret );
2153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002154
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002155 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2156 p, SSL_MAX_CONTENT_LEN - n,
2157 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002158 {
2159 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2160 return( ret );
2161 }
2162
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002163 dig_signed = p;
2164 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002165
2166 p += len;
2167 n += len;
2168
Paul Bakker41c83d32013-03-20 14:39:14 +01002169 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2170 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002171#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002172
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002173#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002174 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2175 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002176 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002177 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2178 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002179 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002180 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002181 unsigned int hashlen = 0;
2182 unsigned char hash[64];
2183 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002184
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002185 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002186 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2187 */
Paul Bakker577e0062013-08-28 11:57:20 +02002188#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002189 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2190 {
2191 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2192
2193 if( md_alg == POLARSSL_MD_NONE )
2194 {
2195 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2196 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2197 }
2198 }
Paul Bakker577e0062013-08-28 11:57:20 +02002199 else
2200#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002201#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2202 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002203 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002204 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2205 {
2206 md_alg = POLARSSL_MD_SHA1;
2207 }
2208 else
Paul Bakker577e0062013-08-28 11:57:20 +02002209#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002210 {
2211 md_alg = POLARSSL_MD_NONE;
2212 }
2213
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002214 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002215 * Compute the hash to be signed
2216 */
Paul Bakker577e0062013-08-28 11:57:20 +02002217#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2218 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002219 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002220 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002221 md5_context md5;
2222 sha1_context sha1;
2223
2224 /*
2225 * digitally-signed struct {
2226 * opaque md5_hash[16];
2227 * opaque sha_hash[20];
2228 * };
2229 *
2230 * md5_hash
2231 * MD5(ClientHello.random + ServerHello.random
2232 * + ServerParams);
2233 * sha_hash
2234 * SHA(ClientHello.random + ServerHello.random
2235 * + ServerParams);
2236 */
2237 md5_starts( &md5 );
2238 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002239 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240 md5_finish( &md5, hash );
2241
2242 sha1_starts( &sha1 );
2243 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002244 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002245 sha1_finish( &sha1, hash + 16 );
2246
2247 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002248 }
2249 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002250#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2251 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002252#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2253 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002254 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002255 {
2256 md_context_t ctx;
2257
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002258 /* Info from md_alg will be used instead */
2259 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002260
2261 /*
2262 * digitally-signed struct {
2263 * opaque client_random[32];
2264 * opaque server_random[32];
2265 * ServerDHParams params;
2266 * };
2267 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002268 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269 {
2270 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2271 return( ret );
2272 }
2273
2274 md_starts( &ctx );
2275 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002276 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002278
2279 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2280 {
2281 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2282 return( ret );
2283 }
2284
Paul Bakker23f36802012-09-28 14:15:14 +00002285 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002286 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002287#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2288 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002289 {
2290 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002291 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002292 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002293
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002294 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2295 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002297 /*
2298 * Make the signature
2299 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002300 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002301 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002302 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2303 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304 }
Paul Bakker23f36802012-09-28 14:15:14 +00002305
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002306#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002307 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2308 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002309 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002310 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311
2312 n += 2;
2313 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002314#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002316 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002317 p + 2 , &signature_len,
2318 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002319 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002320 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002321 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002322 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002323
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002324 *(p++) = (unsigned char)( signature_len >> 8 );
2325 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002326 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002327
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002328 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002329
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002330 p += signature_len;
2331 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002332 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002334 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2335 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002336
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002337 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2339 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2340
2341 ssl->state++;
2342
2343 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2344 {
2345 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2346 return( ret );
2347 }
2348
2349 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2350
2351 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002352}
2353
2354static int ssl_write_server_hello_done( ssl_context *ssl )
2355{
2356 int ret;
2357
2358 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2359
2360 ssl->out_msglen = 4;
2361 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2362 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2363
2364 ssl->state++;
2365
2366 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2367 {
2368 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2369 return( ret );
2370 }
2371
2372 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2373
2374 return( 0 );
2375}
2376
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2378 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2379static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2380 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002381{
2382 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002383 size_t n;
2384
2385 /*
2386 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2387 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 if( *p + 2 > end )
2389 {
2390 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2392 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002393
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 n = ( (*p)[0] << 8 ) | (*p)[1];
2395 *p += 2;
2396
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002397 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002398 {
2399 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2400 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2401 }
2402
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002403 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002404 {
2405 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2407 }
2408
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002409 *p += n;
2410
Paul Bakker70df2fb2013-04-17 17:19:09 +02002411 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2412
Paul Bakker70df2fb2013-04-17 17:19:09 +02002413 return( ret );
2414}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2416 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002417
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002418#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2419 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002420static int ssl_parse_encrypted_pms( ssl_context *ssl,
2421 const unsigned char *p,
2422 const unsigned char *end,
2423 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002424{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002425 int ret;
2426 size_t len = pk_get_len( ssl_own_key( ssl ) );
2427 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002428
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002429 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002430 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002431 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002432 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2433 }
2434
2435 /*
2436 * Decrypt the premaster using own private RSA key
2437 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2439 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002440 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2441 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002442 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2443 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002444 {
2445 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2447 }
2448 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002449#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002450
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002451 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002452 {
2453 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2454 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2455 }
2456
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002457 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2458 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002459 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002460 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002461
2462 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002463 pms[0] != ssl->handshake->max_major_ver ||
2464 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002465 {
2466 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2467
2468 /*
2469 * Protection against Bleichenbacher's attack:
2470 * invalid PKCS#1 v1.5 padding must not cause
2471 * the connection to end immediately; instead,
2472 * send a bad_record_mac later in the handshake.
2473 */
2474 ssl->handshake->pmslen = 48;
2475
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002476 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002477 if( ret != 0 )
2478 return( ret );
2479 }
2480
2481 return( ret );
2482}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002483#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2484 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002485
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002486#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002487static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2488 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002489{
Paul Bakker6db455e2013-09-18 17:29:31 +02002490 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002491 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002492
Paul Bakker6db455e2013-09-18 17:29:31 +02002493 if( ssl->f_psk == NULL &&
2494 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2495 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002496 {
2497 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2498 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2499 }
2500
2501 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002502 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002503 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002504 if( *p + 2 > end )
2505 {
2506 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2507 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2508 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002509
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 n = ( (*p)[0] << 8 ) | (*p)[1];
2511 *p += 2;
2512
2513 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002514 {
2515 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2516 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2517 }
2518
Paul Bakker6db455e2013-09-18 17:29:31 +02002519 if( ssl->f_psk != NULL )
2520 {
2521 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2522 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2523 }
2524
2525 if( ret == 0 )
2526 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002527 /* Identity is not a big secret since clients send it in the clear,
2528 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002529 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002530 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002531 {
2532 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2533 }
2534 }
2535
2536 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002537 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002538 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002539 if( ( ret = ssl_send_alert_message( ssl,
2540 SSL_ALERT_LEVEL_FATAL,
2541 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2542 {
2543 return( ret );
2544 }
2545
2546 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002547 }
2548
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002550 ret = 0;
2551
Paul Bakkerfbb17802013-04-17 19:10:21 +02002552 return( ret );
2553}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002554#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002555
Paul Bakker5121ce52009-01-03 21:22:43 +00002556static int ssl_parse_client_key_exchange( ssl_context *ssl )
2557{
Paul Bakker23986e52011-04-24 08:57:21 +00002558 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002559 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002560
Paul Bakker41c83d32013-03-20 14:39:14 +01002561 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002562
2563 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2564
2565 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2566 {
2567 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2568 return( ret );
2569 }
2570
2571 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2572 {
2573 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002574 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 }
2576
2577 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2578 {
2579 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002581 }
2582
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002584 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002586 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002587 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002588
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002591 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2592 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002593 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002594
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002595 if( p != end )
2596 {
2597 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2598 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2599 }
2600
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2602
2603 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2604 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002605 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002606 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002607 {
2608 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2609 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2610 }
2611
2612 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002613 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002614 else
2615#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002616#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002617 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2618 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2619 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002620 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002621 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2622 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002624 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002625 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002626 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002627 {
2628 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2629 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2630 }
2631
2632 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2633
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002634 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2635 &ssl->handshake->pmslen,
2636 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002637 POLARSSL_MPI_MAX_SIZE,
2638 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002639 {
2640 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2641 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2642 }
2643
2644 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002647#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002648 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2649 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2650 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002651#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2652 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002653 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002654 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002655 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002656
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002657 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002658 {
2659 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2660 return( ret );
2661 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002662
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002663 if( p != end )
2664 {
2665 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2666 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2667 }
2668
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002669 if( ( ret = ssl_psk_derive_premaster( ssl,
2670 ciphersuite_info->key_exchange ) ) != 0 )
2671 {
2672 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2673 return( ret );
2674 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002675 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002676 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002678#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2679 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2680 {
2681 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002682 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002683
2684 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2685 {
2686 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2687 return( ret );
2688 }
2689
2690 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2691 {
2692 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2693 return( ret );
2694 }
2695
2696 if( ( ret = ssl_psk_derive_premaster( ssl,
2697 ciphersuite_info->key_exchange ) ) != 0 )
2698 {
2699 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2700 return( ret );
2701 }
2702 }
2703 else
2704#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002705#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2706 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2707 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002708 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002709 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002710
2711 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2712 {
2713 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2714 return( ret );
2715 }
2716 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2717 {
2718 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2719 return( ret );
2720 }
2721
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002722 if( p != end )
2723 {
2724 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2726 }
2727
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002728 if( ( ret = ssl_psk_derive_premaster( ssl,
2729 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002730 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002731 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2732 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002733 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002734 }
2735 else
2736#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002737#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2738 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2739 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002740 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002741 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002742
2743 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2744 {
2745 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2746 return( ret );
2747 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002748
2749 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2750 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002751 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002752 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2753 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002754 }
2755
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002756 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2757
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002758 if( ( ret = ssl_psk_derive_premaster( ssl,
2759 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002760 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002761 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002762 return( ret );
2763 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002764 }
2765 else
2766#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002767#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2768 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002769 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002770 if( ( ret = ssl_parse_encrypted_pms( ssl,
2771 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002772 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002773 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002774 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002775 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002776 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779 else
2780#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2781 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002782 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002783 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2784 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Paul Bakkerff60ee62010-03-16 21:09:09 +00002786 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2787 {
2788 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2789 return( ret );
2790 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Paul Bakker5121ce52009-01-03 21:22:43 +00002792 ssl->state++;
2793
2794 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2795
2796 return( 0 );
2797}
2798
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2800 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002801 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2802 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002803static int ssl_parse_certificate_verify( ssl_context *ssl )
2804{
Paul Bakkered27a042013-04-18 22:46:23 +02002805 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002806 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002807
2808 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2809
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002810 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002811 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002812 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002813 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002814 {
2815 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2816 ssl->state++;
2817 return( 0 );
2818 }
2819
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002820 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821 return( ret );
2822}
2823#else
2824static int ssl_parse_certificate_verify( ssl_context *ssl )
2825{
2826 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002827 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002828 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002829 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002830 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002831#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002832 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002833#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002834 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2836
2837 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2838
2839 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002840 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002841 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002842 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2843 {
2844 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2845 ssl->state++;
2846 return( 0 );
2847 }
2848
Paul Bakkered27a042013-04-18 22:46:23 +02002849 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002850 {
2851 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2852 ssl->state++;
2853 return( 0 );
2854 }
2855
Paul Bakker48916f92012-09-16 19:57:18 +00002856 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002857
2858 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2859 {
2860 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2861 return( ret );
2862 }
2863
2864 ssl->state++;
2865
2866 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2867 {
2868 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002869 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 }
2871
2872 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2873 {
2874 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002875 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 }
2877
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002878 /*
2879 * 0 . 0 handshake type
2880 * 1 . 3 handshake length
2881 * 4 . 5 sig alg (TLS 1.2 only)
2882 * 4+n . 5+n signature length (n = sa_len)
2883 * 6+n . 6+n+m signature (m = sig_len)
2884 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002885
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002886#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2887 defined(POLARSSL_SSL_PROTO_TLS1_1)
2888 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002889 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002890 sa_len = 0;
2891
Paul Bakkerc70b9822013-04-07 22:00:46 +02002892 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002893 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002894
2895 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2896 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2897 POLARSSL_PK_ECDSA ) )
2898 {
2899 hash_start += 16;
2900 hashlen -= 16;
2901 md_alg = POLARSSL_MD_SHA1;
2902 }
Paul Bakker926af752012-11-23 13:38:07 +01002903 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002904 else
2905#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002906#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2907 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002908 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002909 sa_len = 2;
2910
Paul Bakker5121ce52009-01-03 21:22:43 +00002911 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002912 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002913 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002914 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002916 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2917 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002918 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2919 }
2920
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002921 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002922
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002923 /* Info from md_alg will be used instead */
2924 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002925
2926 /*
2927 * Signature
2928 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002929 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2930 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002931 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002932 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2933 " for verify message" ) );
2934 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002935 }
2936
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002937 /*
2938 * Check the certificate's key type matches the signature alg
2939 */
2940 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2941 {
2942 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2943 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2944 }
Paul Bakker577e0062013-08-28 11:57:20 +02002945 }
2946 else
2947#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2948 {
2949 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002950 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002951 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002952
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002953 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002954
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002955 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002956 {
2957 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002958 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 }
2960
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002961 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002962 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002963 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002964 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002965 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 return( ret );
2967 }
2968
2969 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2970
Paul Bakkered27a042013-04-18 22:46:23 +02002971 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2974 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2975 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Paul Bakkera503a632013-08-14 13:48:06 +02002977#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002978static int ssl_write_new_session_ticket( ssl_context *ssl )
2979{
2980 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002981 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002982 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002983
2984 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2985
2986 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2987 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2988
2989 /*
2990 * struct {
2991 * uint32 ticket_lifetime_hint;
2992 * opaque ticket<0..2^16-1>;
2993 * } NewSessionTicket;
2994 *
2995 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2996 * 8 . 9 ticket_len (n)
2997 * 10 . 9+n ticket content
2998 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002999
3000 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3001 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3002 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3003 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003004
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003005 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3006 {
3007 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3008 tlen = 0;
3009 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003010
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003011 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3012 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003013
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003014 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003015
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003016 /*
3017 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3018 * ChangeCipherSpec share the same state.
3019 */
3020 ssl->handshake->new_session_ticket = 0;
3021
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003022 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3023 {
3024 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3025 return( ret );
3026 }
3027
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003028 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3029
3030 return( 0 );
3031}
Paul Bakkera503a632013-08-14 13:48:06 +02003032#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003033
Paul Bakker5121ce52009-01-03 21:22:43 +00003034/*
Paul Bakker1961b702013-01-25 14:49:24 +01003035 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 */
Paul Bakker1961b702013-01-25 14:49:24 +01003037int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003038{
3039 int ret = 0;
3040
Paul Bakker1961b702013-01-25 14:49:24 +01003041 if( ssl->state == SSL_HANDSHAKE_OVER )
3042 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043
Paul Bakker1961b702013-01-25 14:49:24 +01003044 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3045
3046 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3047 return( ret );
3048
3049 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003050 {
Paul Bakker1961b702013-01-25 14:49:24 +01003051 case SSL_HELLO_REQUEST:
3052 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 break;
3054
Paul Bakker1961b702013-01-25 14:49:24 +01003055 /*
3056 * <== ClientHello
3057 */
3058 case SSL_CLIENT_HELLO:
3059 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003060 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003061
3062 /*
3063 * ==> ServerHello
3064 * Certificate
3065 * ( ServerKeyExchange )
3066 * ( CertificateRequest )
3067 * ServerHelloDone
3068 */
3069 case SSL_SERVER_HELLO:
3070 ret = ssl_write_server_hello( ssl );
3071 break;
3072
3073 case SSL_SERVER_CERTIFICATE:
3074 ret = ssl_write_certificate( ssl );
3075 break;
3076
3077 case SSL_SERVER_KEY_EXCHANGE:
3078 ret = ssl_write_server_key_exchange( ssl );
3079 break;
3080
3081 case SSL_CERTIFICATE_REQUEST:
3082 ret = ssl_write_certificate_request( ssl );
3083 break;
3084
3085 case SSL_SERVER_HELLO_DONE:
3086 ret = ssl_write_server_hello_done( ssl );
3087 break;
3088
3089 /*
3090 * <== ( Certificate/Alert )
3091 * ClientKeyExchange
3092 * ( CertificateVerify )
3093 * ChangeCipherSpec
3094 * Finished
3095 */
3096 case SSL_CLIENT_CERTIFICATE:
3097 ret = ssl_parse_certificate( ssl );
3098 break;
3099
3100 case SSL_CLIENT_KEY_EXCHANGE:
3101 ret = ssl_parse_client_key_exchange( ssl );
3102 break;
3103
3104 case SSL_CERTIFICATE_VERIFY:
3105 ret = ssl_parse_certificate_verify( ssl );
3106 break;
3107
3108 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3109 ret = ssl_parse_change_cipher_spec( ssl );
3110 break;
3111
3112 case SSL_CLIENT_FINISHED:
3113 ret = ssl_parse_finished( ssl );
3114 break;
3115
3116 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003117 * ==> ( NewSessionTicket )
3118 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003119 * Finished
3120 */
3121 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003122#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003123 if( ssl->handshake->new_session_ticket != 0 )
3124 ret = ssl_write_new_session_ticket( ssl );
3125 else
Paul Bakkera503a632013-08-14 13:48:06 +02003126#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003127 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003128 break;
3129
3130 case SSL_SERVER_FINISHED:
3131 ret = ssl_write_finished( ssl );
3132 break;
3133
3134 case SSL_FLUSH_BUFFERS:
3135 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3136 ssl->state = SSL_HANDSHAKE_WRAPUP;
3137 break;
3138
3139 case SSL_HANDSHAKE_WRAPUP:
3140 ssl_handshake_wrapup( ssl );
3141 break;
3142
3143 default:
3144 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3145 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 }
3147
Paul Bakker5121ce52009-01-03 21:22:43 +00003148 return( ret );
3149}
Paul Bakker5121ce52009-01-03 21:22:43 +00003150#endif