blob: 54c931aee86a61dff04e1ad889df1045c8a3e5d8 [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" ) );
313 memset( &session, 0, sizeof( ssl_session ) );
314 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" ) );
322 memset( &session, 0, sizeof( ssl_session ) );
323 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
370 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
371 if( servername_list_size + 2 != len )
372 {
373 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
374 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
375 }
376
377 p = buf + 2;
378 while( servername_list_size > 0 )
379 {
380 hostname_len = ( ( p[1] << 8 ) | p[2] );
381 if( hostname_len + 3 > servername_list_size )
382 {
383 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
384 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
385 }
386
387 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
388 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200389 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000390 if( ret != 0 )
391 {
392 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
393 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
395 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000396 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000397 }
398
399 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000400 p += hostname_len + 3;
401 }
402
403 if( servername_list_size != 0 )
404 {
405 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
406 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 }
408
409 return( 0 );
410}
Paul Bakker0be444a2013-08-27 21:55:01 +0200411#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412
Paul Bakker48916f92012-09-16 19:57:18 +0000413static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000414 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000415 size_t len )
416{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000417 int ret;
418
Paul Bakker48916f92012-09-16 19:57:18 +0000419 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
420 {
421 if( len != 1 || buf[0] != 0x0 )
422 {
423 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000424
425 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
426 return( ret );
427
Paul Bakker48916f92012-09-16 19:57:18 +0000428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
429 }
430
431 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
432 }
433 else
434 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100435 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000436 if( len != 1 + ssl->verify_data_len ||
437 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100438 safer_memcmp( buf + 1, ssl->peer_verify_data,
439 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000440 {
441 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000442
443 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
444 return( ret );
445
Paul Bakker48916f92012-09-16 19:57:18 +0000446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
447 }
448 }
449
450 return( 0 );
451}
452
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200453#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000454static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
455 const unsigned char *buf,
456 size_t len )
457{
458 size_t sig_alg_list_size;
459 const unsigned char *p;
460
461 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
462 if( sig_alg_list_size + 2 != len ||
463 sig_alg_list_size %2 != 0 )
464 {
465 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
466 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
467 }
468
469 p = buf + 2;
470 while( sig_alg_list_size > 0 )
471 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200472 /*
473 * For now, just ignore signature algorithm and rely on offered
474 * ciphersuites only. To be fixed later.
475 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200476#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000477 if( p[0] == SSL_HASH_SHA512 )
478 {
479 ssl->handshake->sig_alg = SSL_HASH_SHA512;
480 break;
481 }
482 if( p[0] == SSL_HASH_SHA384 )
483 {
484 ssl->handshake->sig_alg = SSL_HASH_SHA384;
485 break;
486 }
487#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200488#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000489 if( p[0] == SSL_HASH_SHA256 )
490 {
491 ssl->handshake->sig_alg = SSL_HASH_SHA256;
492 break;
493 }
494 if( p[0] == SSL_HASH_SHA224 )
495 {
496 ssl->handshake->sig_alg = SSL_HASH_SHA224;
497 break;
498 }
499#endif
500 if( p[0] == SSL_HASH_SHA1 )
501 {
502 ssl->handshake->sig_alg = SSL_HASH_SHA1;
503 break;
504 }
505 if( p[0] == SSL_HASH_MD5 )
506 {
507 ssl->handshake->sig_alg = SSL_HASH_MD5;
508 break;
509 }
510
511 sig_alg_list_size -= 2;
512 p += 2;
513 }
514
515 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
516 ssl->handshake->sig_alg ) );
517
518 return( 0 );
519}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200520#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000521
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200522#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200523static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
524 const unsigned char *buf,
525 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100526{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200527 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100528 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200529 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100530
531 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
532 if( list_size + 2 != len ||
533 list_size % 2 != 0 )
534 {
535 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
536 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
537 }
538
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100539 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200540 * and leave room for a final 0 */
541 our_size = list_size / 2 + 1;
542 if( our_size > POLARSSL_ECP_DP_MAX )
543 our_size = POLARSSL_ECP_DP_MAX;
544
545 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
546 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
547
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200548 /* explicit void pointer cast for buggy MS compiler */
549 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200550 ssl->handshake->curves = curves;
551
Paul Bakker41c83d32013-03-20 14:39:14 +0100552 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200555 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200556
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200557 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100558 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200559 *curves++ = curve_info;
560 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 }
562
563 list_size -= 2;
564 p += 2;
565 }
566
567 return( 0 );
568}
569
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200570static int ssl_parse_supported_point_formats( ssl_context *ssl,
571 const unsigned char *buf,
572 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100573{
574 size_t list_size;
575 const unsigned char *p;
576
577 list_size = buf[0];
578 if( list_size + 1 != len )
579 {
580 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
581 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
582 }
583
584 p = buf + 2;
585 while( list_size > 0 )
586 {
587 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
588 p[0] == POLARSSL_ECP_PF_COMPRESSED )
589 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200590 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200591 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 return( 0 );
593 }
594
595 list_size--;
596 p++;
597 }
598
599 return( 0 );
600}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200601#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100602
Paul Bakker05decb22013-08-15 13:33:48 +0200603#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200604static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
605 const unsigned char *buf,
606 size_t len )
607{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200608 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200609 {
610 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
611 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
612 }
613
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200614 ssl->session_negotiate->mfl_code = buf[0];
615
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200616 return( 0 );
617}
Paul Bakker05decb22013-08-15 13:33:48 +0200618#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200619
Paul Bakker1f2bc622013-08-15 13:45:55 +0200620#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200621static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
622 const unsigned char *buf,
623 size_t len )
624{
625 if( len != 0 )
626 {
627 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
628 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
629 }
630
631 ((void) buf);
632
633 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
634
635 return( 0 );
636}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200637#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200638
Paul Bakkera503a632013-08-14 13:48:06 +0200639#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200640static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200641 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200642 size_t len )
643{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200644 int ret;
645
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200646 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
647 return( 0 );
648
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200649 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200650 ssl->handshake->new_session_ticket = 1;
651
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200652 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
653
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200654 if( len == 0 )
655 return( 0 );
656
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200657 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
658 {
659 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
660 return( 0 );
661 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200662
663 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200664 * Failures are ok: just ignore the ticket and proceed.
665 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200666 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
667 {
668 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200669 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200670 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200671
672 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
673
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200674 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200675
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200676 /* Don't send a new ticket after all, this one is OK */
677 ssl->handshake->new_session_ticket = 0;
678
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200679 return( 0 );
680}
Paul Bakkera503a632013-08-14 13:48:06 +0200681#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200682
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100683/*
684 * Auxiliary functions for ServerHello parsing and related actions
685 */
686
687#if defined(POLARSSL_X509_CRT_PARSE_C)
688/*
689 * Return 1 if the given EC key uses the given curve, 0 otherwise
690 */
691#if defined(POLARSSL_ECDSA_C)
692static int ssl_key_matches_curves( pk_context *pk,
693 const ecp_curve_info **curves )
694{
695 const ecp_curve_info **crv = curves;
696 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
697
698 while( *crv != NULL )
699 {
700 if( (*crv)->grp_id == grp_id )
701 return( 1 );
702 crv++;
703 }
704
705 return( 0 );
706}
707#endif /* POLARSSL_ECDSA_C */
708
709/*
710 * Try picking a certificate for this ciphersuite,
711 * return 0 on success and -1 on failure.
712 */
713static int ssl_pick_cert( ssl_context *ssl,
714 const ssl_ciphersuite_t * ciphersuite_info )
715{
716 ssl_key_cert *cur, *list;
717 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
718
719#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
720 if( ssl->handshake->sni_key_cert != NULL )
721 list = ssl->handshake->sni_key_cert;
722 else
723#endif
724 list = ssl->handshake->key_cert;
725
726 if( pk_alg == POLARSSL_PK_NONE )
727 return( 0 );
728
729 for( cur = list; cur != NULL; cur = cur->next )
730 {
731 if( ! pk_can_do( cur->key, pk_alg ) )
732 continue;
733
734#if defined(POLARSSL_ECDSA_C)
735 if( pk_alg == POLARSSL_PK_ECDSA )
736 {
737 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
738 break;
739 }
740 else
741#endif
742 break;
743 }
744
745 if( cur == NULL )
746 return( -1 );
747
748 ssl->handshake->key_cert = cur;
749 return( 0 );
750}
751#endif /* POLARSSL_X509_CRT_PARSE_C */
752
753/*
754 * Check if a given ciphersuite is suitable for use with our config/keys/etc
755 * Sets ciphersuite_info only if the suite matches.
756 */
757static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
758 const ssl_ciphersuite_t **ciphersuite_info )
759{
760 const ssl_ciphersuite_t *suite_info;
761
762 suite_info = ssl_ciphersuite_from_id( suite_id );
763 if( suite_info == NULL )
764 {
765 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
766 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
767 }
768
769 if( suite_info->min_minor_ver > ssl->minor_ver ||
770 suite_info->max_minor_ver < ssl->minor_ver )
771 return( 0 );
772
773#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
774 if( ssl_ciphersuite_uses_ec( suite_info ) &&
775 ( ssl->handshake->curves == NULL ||
776 ssl->handshake->curves[0] == NULL ) )
777 return( 0 );
778#endif
779
780#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
781 /* If the ciphersuite requires a pre-shared key and we don't
782 * have one, skip it now rather than failing later */
783 if( ssl_ciphersuite_uses_psk( suite_info ) &&
784 ssl->f_psk == NULL &&
785 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
786 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
787 return( 0 );
788#endif
789
790#if defined(POLARSSL_X509_CRT_PARSE_C)
791 /*
792 * Final check: if ciphersuite requires us to have a
793 * certificate/key of a particular type:
794 * - select the appropriate certificate if we have one, or
795 * - try the next ciphersuite if we don't
796 * This must be done last since we modify the key_cert list.
797 */
798 if( ssl_pick_cert( ssl, suite_info ) != 0 )
799 return( 0 );
800#endif
801
802 *ciphersuite_info = suite_info;
803 return( 0 );
804}
805
Paul Bakker78a8c712013-03-06 17:01:52 +0100806#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
807static int ssl_parse_client_hello_v2( ssl_context *ssl )
808{
809 int ret;
810 unsigned int i, j;
811 size_t n;
812 unsigned int ciph_len, sess_len, chal_len;
813 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200814 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200815 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100816
817 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
818
819 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
820 {
821 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
822
823 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
824 return( ret );
825
826 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
827 }
828
829 buf = ssl->in_hdr;
830
831 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
832
833 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
834 buf[2] ) );
835 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
836 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
837 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
838 buf[3], buf[4] ) );
839
840 /*
841 * SSLv2 Client Hello
842 *
843 * Record layer:
844 * 0 . 1 message length
845 *
846 * SSL layer:
847 * 2 . 2 message type
848 * 3 . 4 protocol version
849 */
850 if( buf[2] != SSL_HS_CLIENT_HELLO ||
851 buf[3] != SSL_MAJOR_VERSION_3 )
852 {
853 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
854 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
855 }
856
857 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
858
859 if( n < 17 || n > 512 )
860 {
861 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
862 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
863 }
864
865 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200866 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
867 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100868
869 if( ssl->minor_ver < ssl->min_minor_ver )
870 {
871 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
872 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
873 ssl->min_major_ver, ssl->min_minor_ver ) );
874
875 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
876 SSL_ALERT_MSG_PROTOCOL_VERSION );
877 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
878 }
879
Paul Bakker2fbefde2013-06-29 16:01:15 +0200880 ssl->handshake->max_major_ver = buf[3];
881 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100882
883 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
884 {
885 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
886 return( ret );
887 }
888
889 ssl->handshake->update_checksum( ssl, buf + 2, n );
890
891 buf = ssl->in_msg;
892 n = ssl->in_left - 5;
893
894 /*
895 * 0 . 1 ciphersuitelist length
896 * 2 . 3 session id length
897 * 4 . 5 challenge length
898 * 6 . .. ciphersuitelist
899 * .. . .. session id
900 * .. . .. challenge
901 */
902 SSL_DEBUG_BUF( 4, "record contents", buf, n );
903
904 ciph_len = ( buf[0] << 8 ) | buf[1];
905 sess_len = ( buf[2] << 8 ) | buf[3];
906 chal_len = ( buf[4] << 8 ) | buf[5];
907
908 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
909 ciph_len, sess_len, chal_len ) );
910
911 /*
912 * Make sure each parameter length is valid
913 */
914 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
915 {
916 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
918 }
919
920 if( sess_len > 32 )
921 {
922 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
923 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
924 }
925
926 if( chal_len < 8 || chal_len > 32 )
927 {
928 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
929 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
930 }
931
932 if( n != 6 + ciph_len + sess_len + chal_len )
933 {
934 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
935 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
936 }
937
938 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
939 buf + 6, ciph_len );
940 SSL_DEBUG_BUF( 3, "client hello, session id",
941 buf + 6 + ciph_len, sess_len );
942 SSL_DEBUG_BUF( 3, "client hello, challenge",
943 buf + 6 + ciph_len + sess_len, chal_len );
944
945 p = buf + 6 + ciph_len;
946 ssl->session_negotiate->length = sess_len;
947 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
948 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
949
950 p += sess_len;
951 memset( ssl->handshake->randbytes, 0, 64 );
952 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
953
954 /*
955 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
956 */
957 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
958 {
959 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
960 {
961 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
962 if( ssl->renegotiation == SSL_RENEGOTIATION )
963 {
964 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
965
966 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
967 return( ret );
968
969 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
970 }
971 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
972 break;
973 }
974 }
975
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200976 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100977 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +0100978#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
979 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
980 {
981 for( i = 0; ciphersuites[i] != 0; i++ )
982#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200983 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100984 {
985 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +0100986#endif
Paul Bakker78a8c712013-03-06 17:01:52 +0100987 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100988 if( p[0] != 0 ||
989 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
990 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
991 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200992
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100993 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
994 &ciphersuite_info ) ) != 0 )
995 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +0200996
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +0100997 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +0100998 goto have_ciphersuite_v2;
999 }
1000 }
1001
1002 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1003
1004 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1005
1006have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001007 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001008 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001009 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001010
1011 /*
1012 * SSLv2 Client Hello relevant renegotiation security checks
1013 */
1014 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1015 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1016 {
1017 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1018
1019 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1020 return( ret );
1021
1022 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1023 }
1024
1025 ssl->in_left = 0;
1026 ssl->state++;
1027
1028 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1029
1030 return( 0 );
1031}
1032#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1033
Paul Bakker5121ce52009-01-03 21:22:43 +00001034static int ssl_parse_client_hello( ssl_context *ssl )
1035{
Paul Bakker23986e52011-04-24 08:57:21 +00001036 int ret;
1037 unsigned int i, j;
1038 size_t n;
1039 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001040 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001041 unsigned int ext_len = 0;
1042 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001043 int renegotiation_info_seen = 0;
1044 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001045 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001046 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001047
1048 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1049
Paul Bakker48916f92012-09-16 19:57:18 +00001050 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1051 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001052 {
1053 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1054 return( ret );
1055 }
1056
1057 buf = ssl->in_hdr;
1058
Paul Bakker78a8c712013-03-06 17:01:52 +01001059#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1060 if( ( buf[0] & 0x80 ) != 0 )
1061 return ssl_parse_client_hello_v2( ssl );
1062#endif
1063
Paul Bakkerec636f32012-09-09 19:17:02 +00001064 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1065
1066 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1067 buf[0] ) );
1068 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1069 ( buf[3] << 8 ) | buf[4] ) );
1070 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1071 buf[1], buf[2] ) );
1072
1073 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001074 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001075 *
1076 * Record layer:
1077 * 0 . 0 message type
1078 * 1 . 2 protocol version
1079 * 3 . 4 message length
1080 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001081
1082 /* According to RFC 5246 Appendix E.1, the version here is typically
1083 * "{03,00}, the lowest version number supported by the client, [or] the
1084 * value of ClientHello.client_version", so the only meaningful check here
1085 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001086 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001087 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001089 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1090 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
Paul Bakkerec636f32012-09-09 19:17:02 +00001093 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001094
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001095 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001096 {
1097 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1098 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1099 }
1100
Paul Bakker48916f92012-09-16 19:57:18 +00001101 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1102 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001103 {
1104 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1105 return( ret );
1106 }
1107
1108 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001109 if( !ssl->renegotiation )
1110 n = ssl->in_left - 5;
1111 else
1112 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001113
Paul Bakker48916f92012-09-16 19:57:18 +00001114 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001115
1116 /*
1117 * SSL layer:
1118 * 0 . 0 handshake type
1119 * 1 . 3 handshake length
1120 * 4 . 5 protocol version
1121 * 6 . 9 UNIX time()
1122 * 10 . 37 random bytes
1123 * 38 . 38 session id length
1124 * 39 . 38+x session id
1125 * 39+x . 40+x ciphersuitelist length
1126 * 41+x . .. ciphersuitelist
1127 * .. . .. compression alg.
1128 * .. . .. extensions
1129 */
1130 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1131
1132 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1133 buf[0] ) );
1134 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1135 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1136 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1137 buf[4], buf[5] ) );
1138
1139 /*
1140 * Check the handshake type and protocol version
1141 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001142 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001143 {
1144 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1145 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1146 }
1147
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001148 ssl->major_ver = buf[4];
1149 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001150
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001151 ssl->handshake->max_major_ver = ssl->major_ver;
1152 ssl->handshake->max_minor_ver = ssl->minor_ver;
1153
1154 if( ssl->major_ver < ssl->min_major_ver ||
1155 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001156 {
1157 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001158 " [%d:%d] < [%d:%d]",
1159 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001160 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001161
1162 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1163 SSL_ALERT_MSG_PROTOCOL_VERSION );
1164
1165 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1166 }
1167
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001168 if( ssl->major_ver > ssl->max_major_ver )
1169 {
1170 ssl->major_ver = ssl->max_major_ver;
1171 ssl->minor_ver = ssl->max_minor_ver;
1172 }
1173 else if( ssl->minor_ver > ssl->max_minor_ver )
1174 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001175
Paul Bakker48916f92012-09-16 19:57:18 +00001176 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001177
1178 /*
1179 * Check the handshake message length
1180 */
1181 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1182 {
1183 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1185 }
1186
1187 /*
1188 * Check the session length
1189 */
1190 sess_len = buf[38];
1191
1192 if( sess_len > 32 )
1193 {
1194 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1195 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1196 }
1197
Paul Bakker48916f92012-09-16 19:57:18 +00001198 ssl->session_negotiate->length = sess_len;
1199 memset( ssl->session_negotiate->id, 0,
1200 sizeof( ssl->session_negotiate->id ) );
1201 memcpy( ssl->session_negotiate->id, buf + 39,
1202 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001203
1204 /*
1205 * Check the ciphersuitelist length
1206 */
1207 ciph_len = ( buf[39 + sess_len] << 8 )
1208 | ( buf[40 + sess_len] );
1209
1210 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1211 {
1212 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1213 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1214 }
1215
1216 /*
1217 * Check the compression algorithms length
1218 */
1219 comp_len = buf[41 + sess_len + ciph_len];
1220
1221 if( comp_len < 1 || comp_len > 16 )
1222 {
1223 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1225 }
1226
Paul Bakker48916f92012-09-16 19:57:18 +00001227 /*
1228 * Check the extension length
1229 */
1230 if( n > 42 + sess_len + ciph_len + comp_len )
1231 {
1232 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1233 | ( buf[43 + sess_len + ciph_len + comp_len] );
1234
1235 if( ( ext_len > 0 && ext_len < 4 ) ||
1236 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1237 {
1238 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1239 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1240 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1241 }
1242 }
1243
1244 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001245#if defined(POLARSSL_ZLIB_SUPPORT)
1246 for( i = 0; i < comp_len; ++i )
1247 {
Paul Bakker48916f92012-09-16 19:57:18 +00001248 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
Paul Bakker48916f92012-09-16 19:57:18 +00001250 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001251 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 }
1253 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001254#endif
1255
Paul Bakkerec636f32012-09-09 19:17:02 +00001256 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1257 buf + 6, 32 );
1258 SSL_DEBUG_BUF( 3, "client hello, session id",
1259 buf + 38, sess_len );
1260 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1261 buf + 41 + sess_len, ciph_len );
1262 SSL_DEBUG_BUF( 3, "client hello, compression",
1263 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001264
Paul Bakkerec636f32012-09-09 19:17:02 +00001265 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001266 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1267 */
1268 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1269 {
1270 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1271 {
1272 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1273 if( ssl->renegotiation == SSL_RENEGOTIATION )
1274 {
1275 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001276
1277 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1278 return( ret );
1279
Paul Bakker48916f92012-09-16 19:57:18 +00001280 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1281 }
1282 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1283 break;
1284 }
1285 }
1286
Paul Bakker48916f92012-09-16 19:57:18 +00001287 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001288
1289 while( ext_len )
1290 {
1291 unsigned int ext_id = ( ( ext[0] << 8 )
1292 | ( ext[1] ) );
1293 unsigned int ext_size = ( ( ext[2] << 8 )
1294 | ( ext[3] ) );
1295
1296 if( ext_size + 4 > ext_len )
1297 {
1298 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1299 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1300 }
1301 switch( ext_id )
1302 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001303#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001304 case TLS_EXT_SERVERNAME:
1305 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1306 if( ssl->f_sni == NULL )
1307 break;
1308
1309 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1310 if( ret != 0 )
1311 return( ret );
1312 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001313#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001314
Paul Bakker48916f92012-09-16 19:57:18 +00001315 case TLS_EXT_RENEGOTIATION_INFO:
1316 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1317 renegotiation_info_seen = 1;
1318
Paul Bakker23f36802012-09-28 14:15:14 +00001319 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1320 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001321 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001322 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001323
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001324#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001325 case TLS_EXT_SIG_ALG:
1326 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1327 if( ssl->renegotiation == SSL_RENEGOTIATION )
1328 break;
1329
1330 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1331 if( ret != 0 )
1332 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001333 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001334#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001335
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001336#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001337 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1338 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1339
1340 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1341 if( ret != 0 )
1342 return( ret );
1343 break;
1344
1345 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1346 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001347 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001348
1349 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1350 if( ret != 0 )
1351 return( ret );
1352 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001353#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001354
Paul Bakker05decb22013-08-15 13:33:48 +02001355#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001356 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1357 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1358
1359 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1360 if( ret != 0 )
1361 return( ret );
1362 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001363#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001364
Paul Bakker1f2bc622013-08-15 13:45:55 +02001365#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001366 case TLS_EXT_TRUNCATED_HMAC:
1367 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1368
1369 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1370 if( ret != 0 )
1371 return( ret );
1372 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001373#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001374
Paul Bakkera503a632013-08-14 13:48:06 +02001375#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001376 case TLS_EXT_SESSION_TICKET:
1377 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1378
1379 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1380 if( ret != 0 )
1381 return( ret );
1382 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001383#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001384
Paul Bakker48916f92012-09-16 19:57:18 +00001385 default:
1386 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1387 ext_id ) );
1388 }
1389
1390 ext_len -= 4 + ext_size;
1391 ext += 4 + ext_size;
1392
1393 if( ext_len > 0 && ext_len < 4 )
1394 {
1395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1397 }
1398 }
1399
1400 /*
1401 * Renegotiation security checks
1402 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001403 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1404 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1405 {
1406 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1407 handshake_failure = 1;
1408 }
1409 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1410 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1411 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001412 {
1413 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001414 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001415 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001416 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1417 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1418 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001419 {
1420 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001421 handshake_failure = 1;
1422 }
1423 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1424 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1425 renegotiation_info_seen == 1 )
1426 {
1427 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1428 handshake_failure = 1;
1429 }
1430
1431 if( handshake_failure == 1 )
1432 {
1433 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1434 return( ret );
1435
Paul Bakker48916f92012-09-16 19:57:18 +00001436 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1437 }
Paul Bakker380da532012-04-18 16:10:25 +00001438
Paul Bakker41c83d32013-03-20 14:39:14 +01001439 /*
1440 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001441 * (At the end because we need information from the EC-based extensions
1442 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001443 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001444 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001445 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001446#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1447 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1448 {
1449 for( i = 0; ciphersuites[i] != 0; i++ )
1450#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001451 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001452 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001453 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001454#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001455 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001456 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1457 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1458 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001459
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001460 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1461 &ciphersuite_info ) ) != 0 )
1462 return( ret );
1463
1464 if( ciphersuite_info != NULL )
1465 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001466 }
1467 }
1468
1469 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1470
1471 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1472 return( ret );
1473
1474 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1475
1476have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001477 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001478 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1479 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1480
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 ssl->in_left = 0;
1482 ssl->state++;
1483
1484 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1485
1486 return( 0 );
1487}
1488
Paul Bakker1f2bc622013-08-15 13:45:55 +02001489#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001490static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1491 unsigned char *buf,
1492 size_t *olen )
1493{
1494 unsigned char *p = buf;
1495
1496 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1497 {
1498 *olen = 0;
1499 return;
1500 }
1501
1502 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1503
1504 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1505 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1506
1507 *p++ = 0x00;
1508 *p++ = 0x00;
1509
1510 *olen = 4;
1511}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001512#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001513
Paul Bakkera503a632013-08-14 13:48:06 +02001514#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001515static void ssl_write_session_ticket_ext( ssl_context *ssl,
1516 unsigned char *buf,
1517 size_t *olen )
1518{
1519 unsigned char *p = buf;
1520
1521 if( ssl->handshake->new_session_ticket == 0 )
1522 {
1523 *olen = 0;
1524 return;
1525 }
1526
1527 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1528
1529 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1530 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1531
1532 *p++ = 0x00;
1533 *p++ = 0x00;
1534
1535 *olen = 4;
1536}
Paul Bakkera503a632013-08-14 13:48:06 +02001537#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001538
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001539static void ssl_write_renegotiation_ext( ssl_context *ssl,
1540 unsigned char *buf,
1541 size_t *olen )
1542{
1543 unsigned char *p = buf;
1544
1545 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1546 {
1547 *olen = 0;
1548 return;
1549 }
1550
1551 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1552
1553 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1554 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1555
1556 *p++ = 0x00;
1557 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1558 *p++ = ssl->verify_data_len * 2 & 0xFF;
1559
1560 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1561 p += ssl->verify_data_len;
1562 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1563 p += ssl->verify_data_len;
1564
1565 *olen = 5 + ssl->verify_data_len * 2;
1566}
1567
Paul Bakker05decb22013-08-15 13:33:48 +02001568#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001569static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1570 unsigned char *buf,
1571 size_t *olen )
1572{
1573 unsigned char *p = buf;
1574
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001575 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1576 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001577 *olen = 0;
1578 return;
1579 }
1580
1581 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1582
1583 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1584 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1585
1586 *p++ = 0x00;
1587 *p++ = 1;
1588
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001589 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001590
1591 *olen = 5;
1592}
Paul Bakker05decb22013-08-15 13:33:48 +02001593#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001594
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001595#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001596static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1597 unsigned char *buf,
1598 size_t *olen )
1599{
1600 unsigned char *p = buf;
1601 ((void) ssl);
1602
Paul Bakker677377f2013-10-28 12:54:26 +01001603 if( ( ssl->handshake->cli_exts &
1604 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1605 {
1606 *olen = 0;
1607 return;
1608 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001609
1610 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1611
1612 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1613 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1614
1615 *p++ = 0x00;
1616 *p++ = 2;
1617
1618 *p++ = 1;
1619 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1620
1621 *olen = 6;
1622}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001623#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001624
Paul Bakker5121ce52009-01-03 21:22:43 +00001625static int ssl_write_server_hello( ssl_context *ssl )
1626{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001627#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001629#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001630 int ret;
1631 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 unsigned char *buf, *p;
1633
1634 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1635
Paul Bakkera9a028e2013-11-21 17:31:06 +01001636 if( ssl->f_rng == NULL )
1637 {
1638 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1639 return( POLARSSL_ERR_SSL_NO_RNG );
1640 }
1641
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 /*
1643 * 0 . 0 handshake type
1644 * 1 . 3 handshake length
1645 * 4 . 5 protocol version
1646 * 6 . 9 UNIX time()
1647 * 10 . 37 random bytes
1648 */
1649 buf = ssl->out_msg;
1650 p = buf + 4;
1651
1652 *p++ = (unsigned char) ssl->major_ver;
1653 *p++ = (unsigned char) ssl->minor_ver;
1654
1655 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1656 buf[4], buf[5] ) );
1657
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001658#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001659 t = time( NULL );
1660 *p++ = (unsigned char)( t >> 24 );
1661 *p++ = (unsigned char)( t >> 16 );
1662 *p++ = (unsigned char)( t >> 8 );
1663 *p++ = (unsigned char)( t );
1664
1665 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001666#else
1667 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1668 return( ret );
1669
1670 p += 4;
1671#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001672
Paul Bakkera3d195c2011-11-27 21:07:34 +00001673 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1674 return( ret );
1675
1676 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Paul Bakker48916f92012-09-16 19:57:18 +00001678 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1681
1682 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001683 * Resume is 0 by default, see ssl_handshake_init().
1684 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1685 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001687 if( ssl->handshake->resume == 0 &&
1688 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001689 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001690 ssl->f_get_cache != NULL &&
1691 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1692 {
1693 ssl->handshake->resume = 1;
1694 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001696 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001697 {
1698 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001699 * New session, create a new session id,
1700 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001701 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001702 ssl->state++;
1703
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001704#if defined(POLARSSL_HAVE_TIME)
1705 ssl->session_negotiate->start = time( NULL );
1706#endif
1707
Paul Bakkera503a632013-08-14 13:48:06 +02001708#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001709 if( ssl->handshake->new_session_ticket != 0 )
1710 {
1711 ssl->session_negotiate->length = n = 0;
1712 memset( ssl->session_negotiate->id, 0, 32 );
1713 }
1714 else
1715#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001716 {
1717 ssl->session_negotiate->length = n = 32;
1718 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001719 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001720 return( ret );
1721 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001722 }
1723 else
1724 {
1725 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001726 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001728 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001729 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001730
1731 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1732 {
1733 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1734 return( ret );
1735 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 }
1737
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001738 /*
1739 * 38 . 38 session id length
1740 * 39 . 38+n session id
1741 * 39+n . 40+n chosen ciphersuite
1742 * 41+n . 41+n chosen compression alg.
1743 * 42+n . 43+n extensions length
1744 * 44+n . 43+n+m extensions
1745 */
1746 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001747 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1748 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001749
1750 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1751 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1752 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001753 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1756 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1757 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001759 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1760 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001761 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001762 ssl->session_negotiate->compression ) );
1763
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001764 /*
1765 * First write extensions, then the total length
1766 */
1767 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1768 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001769
Paul Bakker05decb22013-08-15 13:33:48 +02001770#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001771 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1772 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001773#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001774
Paul Bakker1f2bc622013-08-15 13:45:55 +02001775#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001776 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1777 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001778#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001779
Paul Bakkera503a632013-08-14 13:48:06 +02001780#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001781 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1782 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001783#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001784
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001785#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001786 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1787 ext_len += olen;
1788#endif
1789
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001790 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001791
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001792 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1793 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1794 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
1796 ssl->out_msglen = p - buf;
1797 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1798 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1799
1800 ret = ssl_write_record( ssl );
1801
1802 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1803
1804 return( ret );
1805}
1806
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001807#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1808 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001809 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1810 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001811static int ssl_write_certificate_request( ssl_context *ssl )
1812{
Paul Bakkered27a042013-04-18 22:46:23 +02001813 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1814 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001815
1816 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1817
1818 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001819 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001820 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1821 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001822 {
1823 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1824 ssl->state++;
1825 return( 0 );
1826 }
1827
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001828 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001829 return( ret );
1830}
1831#else
1832static int ssl_write_certificate_request( ssl_context *ssl )
1833{
1834 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1835 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001836 size_t dn_size, total_dn_size; /* excluding length bytes */
1837 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001838 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001839 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001840
1841 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1842
1843 ssl->state++;
1844
Paul Bakkerfbb17802013-04-17 19:10:21 +02001845 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001846 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001847 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001848 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001849 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001851 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 return( 0 );
1853 }
1854
1855 /*
1856 * 0 . 0 handshake type
1857 * 1 . 3 handshake length
1858 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001859 * 5 .. m-1 cert types
1860 * m .. m+1 sig alg length (TLS 1.2 only)
1861 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001862 * n .. n+1 length of all DNs
1863 * n+2 .. n+3 length of DN 1
1864 * n+4 .. ... Distinguished Name #1
1865 * ... .. ... length of DN 2, etc.
1866 */
1867 buf = ssl->out_msg;
1868 p = buf + 4;
1869
1870 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001871 * Supported certificate types
1872 *
1873 * ClientCertificateType certificate_types<1..2^8-1>;
1874 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001876 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001877
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001878#if defined(POLARSSL_RSA_C)
1879 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1880#endif
1881#if defined(POLARSSL_ECDSA_C)
1882 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1883#endif
1884
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001885 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001886 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001887
Paul Bakker577e0062013-08-28 11:57:20 +02001888 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001889#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001890 /*
1891 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001892 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001893 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1894 *
1895 * struct {
1896 * HashAlgorithm hash;
1897 * SignatureAlgorithm signature;
1898 * } SignatureAndHashAlgorithm;
1899 *
1900 * enum { (255) } HashAlgorithm;
1901 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001902 */
Paul Bakker21dca692013-01-03 11:41:08 +01001903 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001904 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001905 /*
1906 * Only use current running hash algorithm that is already required
1907 * for requested ciphersuite.
1908 */
Paul Bakker926af752012-11-23 13:38:07 +01001909 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1910
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001911 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1912 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001913 {
1914 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1915 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001916
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001917 /*
1918 * Supported signature algorithms
1919 */
1920#if defined(POLARSSL_RSA_C)
1921 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1922 p[2 + sa_len++] = SSL_SIG_RSA;
1923#endif
1924#if defined(POLARSSL_ECDSA_C)
1925 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1926 p[2 + sa_len++] = SSL_SIG_ECDSA;
1927#endif
Paul Bakker926af752012-11-23 13:38:07 +01001928
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001929 p[0] = (unsigned char)( sa_len >> 8 );
1930 p[1] = (unsigned char)( sa_len );
1931 sa_len += 2;
1932 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001933 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001934#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001936 /*
1937 * DistinguishedName certificate_authorities<0..2^16-1>;
1938 * opaque DistinguishedName<1..2^16-1>;
1939 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 p += 2;
1941 crt = ssl->ca_chain;
1942
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001943 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001944 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001945 {
1946 if( p - buf > 4096 )
1947 break;
1948
Paul Bakker926af752012-11-23 13:38:07 +01001949 dn_size = crt->subject_raw.len;
1950 *p++ = (unsigned char)( dn_size >> 8 );
1951 *p++ = (unsigned char)( dn_size );
1952 memcpy( p, crt->subject_raw.p, dn_size );
1953 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
Paul Bakker926af752012-11-23 13:38:07 +01001955 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1956
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001957 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001958 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001959 }
1960
Paul Bakker926af752012-11-23 13:38:07 +01001961 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001962 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1963 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001964 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1965 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966
1967 ret = ssl_write_record( ssl );
1968
1969 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1970
1971 return( ret );
1972}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001973#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1974 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001975 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
1976 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001977
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01001978#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
1979 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
1980static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
1981{
1982 int ret;
1983
1984 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
1985 {
1986 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
1987 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
1988 }
1989
1990 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
1991 pk_ec( *ssl_own_key( ssl ) ),
1992 POLARSSL_ECDH_OURS ) ) != 0 )
1993 {
1994 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
1995 return( ret );
1996 }
1997
1998 return( 0 );
1999}
2000#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2001 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2002
Paul Bakker41c83d32013-03-20 14:39:14 +01002003static int ssl_write_server_key_exchange( ssl_context *ssl )
2004{
Paul Bakker23986e52011-04-24 08:57:21 +00002005 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002006 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002007 const ssl_ciphersuite_t *ciphersuite_info =
2008 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002009
2010#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2011 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2012 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002013 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002014 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002015 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002016 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002017 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002018 ((void) dig_signed);
2019 ((void) dig_signed_len);
2020#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002021
Paul Bakker5121ce52009-01-03 21:22:43 +00002022 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2023
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002024#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2025 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2026 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002027 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2028 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2029 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002030 {
2031 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2032 ssl->state++;
2033 return( 0 );
2034 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002035#endif
2036
2037#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2038 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2039 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2040 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2041 {
2042 ssl_get_ecdh_params_from_cert( ssl );
2043
2044 SSL_DEBUG_MSG( 2, ( "<= skip parse server key exchange" ) );
2045 ssl->state++;
2046 return( 0 );
2047 }
2048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002049
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002050#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2051 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2052 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2053 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002054 {
2055 /* TODO: Support identity hints */
2056 *(p++) = 0x00;
2057 *(p++) = 0x00;
2058
2059 n += 2;
2060 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002061#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2062 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002063
2064#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2065 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2066 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2067 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002068 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002069 /*
2070 * Ephemeral DH parameters:
2071 *
2072 * struct {
2073 * opaque dh_p<1..2^16-1>;
2074 * opaque dh_g<1..2^16-1>;
2075 * opaque dh_Ys<1..2^16-1>;
2076 * } ServerDHParams;
2077 */
2078 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2079 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2080 {
2081 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2082 return( ret );
2083 }
Paul Bakker48916f92012-09-16 19:57:18 +00002084
Paul Bakker41c83d32013-03-20 14:39:14 +01002085 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002086 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002087 p,
2088 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002089 {
2090 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2091 return( ret );
2092 }
2093
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002094 dig_signed = p;
2095 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096
2097 p += len;
2098 n += len;
2099
Paul Bakker41c83d32013-03-20 14:39:14 +01002100 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2101 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2102 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2103 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2104 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002105#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2106 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002107
Gergely Budai987bfb52014-01-19 21:48:42 +01002108#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002109 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002110 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2111 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002112 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002113 /*
2114 * Ephemeral ECDH parameters:
2115 *
2116 * struct {
2117 * ECParameters curve_params;
2118 * ECPoint public;
2119 * } ServerECDHParams;
2120 */
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002121 const ecp_curve_info **curve;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002122#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002123 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002124
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002125 /* Match our preference list against the offered curves */
2126 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2127 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2128 if( (*curve)->grp_id == *gid )
2129 goto curve_matching_done;
2130
2131curve_matching_done:
2132#else
2133 curve = ssl->handshake->curves;
2134#endif
2135
2136 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002137 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002138 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2139 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002140 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002141
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002142 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002143
Paul Bakker41c83d32013-03-20 14:39:14 +01002144 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002145 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002146 {
2147 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2148 return( ret );
2149 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002151 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2152 p, SSL_MAX_CONTENT_LEN - n,
2153 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002154 {
2155 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2156 return( ret );
2157 }
2158
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002159 dig_signed = p;
2160 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002161
2162 p += len;
2163 n += len;
2164
Paul Bakker41c83d32013-03-20 14:39:14 +01002165 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2166 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002167#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002170 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2171 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002173 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002175 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002176 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002177 unsigned int hashlen = 0;
2178 unsigned char hash[64];
2179 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002180
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002181 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002182 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2183 */
Paul Bakker577e0062013-08-28 11:57:20 +02002184#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002185 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2186 {
2187 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2188
2189 if( md_alg == POLARSSL_MD_NONE )
2190 {
2191 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2192 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2193 }
2194 }
Paul Bakker577e0062013-08-28 11:57:20 +02002195 else
2196#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002197#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2198 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002199 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002200 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2201 {
2202 md_alg = POLARSSL_MD_SHA1;
2203 }
2204 else
Paul Bakker577e0062013-08-28 11:57:20 +02002205#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002206 {
2207 md_alg = POLARSSL_MD_NONE;
2208 }
2209
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002210 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002211 * Compute the hash to be signed
2212 */
Paul Bakker577e0062013-08-28 11:57:20 +02002213#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2214 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002215 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002216 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002217 md5_context md5;
2218 sha1_context sha1;
2219
2220 /*
2221 * digitally-signed struct {
2222 * opaque md5_hash[16];
2223 * opaque sha_hash[20];
2224 * };
2225 *
2226 * md5_hash
2227 * MD5(ClientHello.random + ServerHello.random
2228 * + ServerParams);
2229 * sha_hash
2230 * SHA(ClientHello.random + ServerHello.random
2231 * + ServerParams);
2232 */
2233 md5_starts( &md5 );
2234 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002235 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236 md5_finish( &md5, hash );
2237
2238 sha1_starts( &sha1 );
2239 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002240 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002241 sha1_finish( &sha1, hash + 16 );
2242
2243 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244 }
2245 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002246#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2247 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002248#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2249 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002250 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251 {
2252 md_context_t ctx;
2253
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002254 /* Info from md_alg will be used instead */
2255 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002256
2257 /*
2258 * digitally-signed struct {
2259 * opaque client_random[32];
2260 * opaque server_random[32];
2261 * ServerDHParams params;
2262 * };
2263 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002264 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002265 {
2266 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2267 return( ret );
2268 }
2269
2270 md_starts( &ctx );
2271 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002272 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002273 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002274
2275 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2276 {
2277 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2278 return( ret );
2279 }
2280
Paul Bakker23f36802012-09-28 14:15:14 +00002281 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002282 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002283#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2284 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002285 {
2286 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002287 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002288 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002289
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002290 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2291 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002292
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002293 /*
2294 * Make the signature
2295 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002296 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002297 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002298 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2299 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002300 }
Paul Bakker23f36802012-09-28 14:15:14 +00002301
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002302#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002303 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2304 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002306 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307
2308 n += 2;
2309 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002310#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002312 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002313 p + 2 , &signature_len,
2314 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002315 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002316 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002317 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002318 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002319
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002320 *(p++) = (unsigned char)( signature_len >> 8 );
2321 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002322 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002323
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002324 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002325
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002326 p += signature_len;
2327 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002328 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002329#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002330 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2331 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002332
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002333 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2335 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2336
2337 ssl->state++;
2338
2339 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2340 {
2341 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2342 return( ret );
2343 }
2344
2345 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2346
2347 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002348}
2349
2350static int ssl_write_server_hello_done( ssl_context *ssl )
2351{
2352 int ret;
2353
2354 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2355
2356 ssl->out_msglen = 4;
2357 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2358 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2359
2360 ssl->state++;
2361
2362 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2363 {
2364 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2365 return( ret );
2366 }
2367
2368 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2369
2370 return( 0 );
2371}
2372
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002373#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2374 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2375static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2376 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002377{
2378 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002379 size_t n;
2380
2381 /*
2382 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2383 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384 if( *p + 2 > end )
2385 {
2386 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2387 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2388 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002389
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 n = ( (*p)[0] << 8 ) | (*p)[1];
2391 *p += 2;
2392
2393 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002394 {
2395 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2397 }
2398
2399 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002401 {
2402 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2404 }
2405
2406 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2407
Paul Bakker70df2fb2013-04-17 17:19:09 +02002408 return( ret );
2409}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2411 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002412
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002413#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2414 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002415static int ssl_parse_encrypted_pms( ssl_context *ssl,
2416 const unsigned char *p,
2417 const unsigned char *end,
2418 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002419{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002420 int ret;
2421 size_t len = pk_get_len( ssl_own_key( ssl ) );
2422 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002423
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002424 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002425 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002426 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002427 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2428 }
2429
2430 /*
2431 * Decrypt the premaster using own private RSA key
2432 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2434 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002435 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2436 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002437 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2438 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002439 {
2440 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2442 }
2443 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002444#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002445
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002446 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002447 {
2448 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2450 }
2451
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002452 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2453 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002454 sizeof(ssl->handshake->premaster),
2455 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002456
2457 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002458 pms[0] != ssl->handshake->max_major_ver ||
2459 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002460 {
2461 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2462
2463 /*
2464 * Protection against Bleichenbacher's attack:
2465 * invalid PKCS#1 v1.5 padding must not cause
2466 * the connection to end immediately; instead,
2467 * send a bad_record_mac later in the handshake.
2468 */
2469 ssl->handshake->pmslen = 48;
2470
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002471 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002472 if( ret != 0 )
2473 return( ret );
2474 }
2475
2476 return( ret );
2477}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002478#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2479 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002480
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002481#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002482static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2483 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002484{
Paul Bakker6db455e2013-09-18 17:29:31 +02002485 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002486 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002487
Paul Bakker6db455e2013-09-18 17:29:31 +02002488 if( ssl->f_psk == NULL &&
2489 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2490 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002491 {
2492 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2493 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2494 }
2495
2496 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002497 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002498 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499 if( *p + 2 > end )
2500 {
2501 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2502 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2503 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002504
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 n = ( (*p)[0] << 8 ) | (*p)[1];
2506 *p += 2;
2507
2508 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002509 {
2510 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2511 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2512 }
2513
Paul Bakker6db455e2013-09-18 17:29:31 +02002514 if( ssl->f_psk != NULL )
2515 {
2516 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2517 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2518 }
2519
2520 if( ret == 0 )
2521 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002522 /* Identity is not a big secret since clients send it in the clear,
2523 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002524 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002525 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002526 {
2527 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2528 }
2529 }
2530
2531 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002532 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002534 if( ( ret = ssl_send_alert_message( ssl,
2535 SSL_ALERT_LEVEL_FATAL,
2536 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2537 {
2538 return( ret );
2539 }
2540
2541 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002542 }
2543
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002545 ret = 0;
2546
Paul Bakkerfbb17802013-04-17 19:10:21 +02002547 return( ret );
2548}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002549#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002550
Paul Bakker5121ce52009-01-03 21:22:43 +00002551static int ssl_parse_client_key_exchange( ssl_context *ssl )
2552{
Paul Bakker23986e52011-04-24 08:57:21 +00002553 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002554 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002555
Paul Bakker41c83d32013-03-20 14:39:14 +01002556 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
2558 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2559
2560 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2561 {
2562 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2563 return( ret );
2564 }
2565
2566 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2567 {
2568 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002570 }
2571
2572 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2573 {
2574 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002575 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002576 }
2577
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002579 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002581 unsigned char *p = ssl->in_msg + 4;
2582 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2583
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002585 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002586 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2587 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002588 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589
2590 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2591
2592 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2593 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002594 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002595 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002596 {
2597 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2598 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2599 }
2600
2601 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002602 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002603 else
2604#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002605#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002606 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2607 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2608 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002609 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002610 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2611 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2612 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002613 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002614 size_t n = ssl->in_msg[3];
2615
2616 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2617 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002619 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2620 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002623 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2624 ssl->in_msg + 4, n ) ) != 0 )
2625 {
2626 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2628 }
2629
2630 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2631
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002632 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2633 &ssl->handshake->pmslen,
2634 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002635 POLARSSL_MPI_MAX_SIZE,
2636 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637 {
2638 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2639 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2640 }
2641
2642 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002643 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002644 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002645#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002646 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2647 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2648 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002649#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2650 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002651 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002652 unsigned char *p = ssl->in_msg + 4;
2653 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2654
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002655 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002656 {
2657 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2658 return( ret );
2659 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002660
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002661 if( ( ret = ssl_psk_derive_premaster( ssl,
2662 ciphersuite_info->key_exchange ) ) != 0 )
2663 {
2664 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2665 return( ret );
2666 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002667 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002668 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002670#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2671 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2672 {
2673 unsigned char *p = ssl->in_msg + 4;
2674 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2675
2676 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2677 {
2678 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2679 return( ret );
2680 }
2681
2682 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2683 {
2684 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2685 return( ret );
2686 }
2687
2688 if( ( ret = ssl_psk_derive_premaster( ssl,
2689 ciphersuite_info->key_exchange ) ) != 0 )
2690 {
2691 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2692 return( ret );
2693 }
2694 }
2695 else
2696#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002697#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2698 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2699 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002700 unsigned char *p = ssl->in_msg + 4;
2701 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002702
2703 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2704 {
2705 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2706 return( ret );
2707 }
2708 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2709 {
2710 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2711 return( ret );
2712 }
2713
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002714 if( ( ret = ssl_psk_derive_premaster( ssl,
2715 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002716 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002717 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2718 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002719 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720 }
2721 else
2722#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002723#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2724 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2725 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002726 unsigned char *p = ssl->in_msg + 4;
2727 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2728
2729 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2730 {
2731 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2732 return( ret );
2733 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002734
2735 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2736 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002737 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002738 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2739 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002740 }
2741
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002742 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2743
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002744 if( ( ret = ssl_psk_derive_premaster( ssl,
2745 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002746 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002747 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002748 return( ret );
2749 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002750 }
2751 else
2752#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002753#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2754 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002755 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002756 if( ( ret = ssl_parse_encrypted_pms( ssl,
2757 ssl->in_msg + 4,
2758 ssl->in_msg + ssl->in_msglen,
2759 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002760 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002761 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002762 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 }
2764 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002765 else
2766#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2767 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002768 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002769 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2770 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002771
Paul Bakkerff60ee62010-03-16 21:09:09 +00002772 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2773 {
2774 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2775 return( ret );
2776 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 ssl->state++;
2779
2780 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2781
2782 return( 0 );
2783}
2784
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002785#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2786 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002787 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2788 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002789static int ssl_parse_certificate_verify( ssl_context *ssl )
2790{
Paul Bakkered27a042013-04-18 22:46:23 +02002791 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002792 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
2794 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2795
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002796 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002798 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002799 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002800 {
2801 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2802 ssl->state++;
2803 return( 0 );
2804 }
2805
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002806 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807 return( ret );
2808}
2809#else
2810static int ssl_parse_certificate_verify( ssl_context *ssl )
2811{
2812 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002813 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002814 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002815 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002816 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002817#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002818 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002819#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002820 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002821 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2822
2823 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2824
2825 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002826 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002827 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002828 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2829 {
2830 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2831 ssl->state++;
2832 return( 0 );
2833 }
2834
Paul Bakkered27a042013-04-18 22:46:23 +02002835 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002836 {
2837 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2838 ssl->state++;
2839 return( 0 );
2840 }
2841
Paul Bakker48916f92012-09-16 19:57:18 +00002842 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
2844 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2845 {
2846 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2847 return( ret );
2848 }
2849
2850 ssl->state++;
2851
2852 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2853 {
2854 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002855 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002856 }
2857
2858 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2859 {
2860 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002861 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 }
2863
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002864 /*
2865 * 0 . 0 handshake type
2866 * 1 . 3 handshake length
2867 * 4 . 5 sig alg (TLS 1.2 only)
2868 * 4+n . 5+n signature length (n = sa_len)
2869 * 6+n . 6+n+m signature (m = sig_len)
2870 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002871
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002872#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2873 defined(POLARSSL_SSL_PROTO_TLS1_1)
2874 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002875 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002876 sa_len = 0;
2877
Paul Bakkerc70b9822013-04-07 22:00:46 +02002878 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002879 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002880
2881 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2882 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2883 POLARSSL_PK_ECDSA ) )
2884 {
2885 hash_start += 16;
2886 hashlen -= 16;
2887 md_alg = POLARSSL_MD_SHA1;
2888 }
Paul Bakker926af752012-11-23 13:38:07 +01002889 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002890 else
2891#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002892#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2893 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002894 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002895 sa_len = 2;
2896
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002898 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002899 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002900 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002902 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2903 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002904 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2905 }
2906
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002907 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002908
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002909 /* Info from md_alg will be used instead */
2910 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002911
2912 /*
2913 * Signature
2914 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002915 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2916 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002917 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002918 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2919 " for verify message" ) );
2920 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002921 }
2922
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002923 /*
2924 * Check the certificate's key type matches the signature alg
2925 */
2926 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2927 {
2928 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2929 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2930 }
Paul Bakker577e0062013-08-28 11:57:20 +02002931 }
2932 else
2933#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2934 {
2935 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002936 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002937 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002938
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002939 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002940
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002941 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 {
2943 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002944 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002945 }
2946
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002947 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002948 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002949 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002951 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 return( ret );
2953 }
2954
2955 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2956
Paul Bakkered27a042013-04-18 22:46:23 +02002957 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002959#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2960 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2961 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Paul Bakkera503a632013-08-14 13:48:06 +02002963#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002964static int ssl_write_new_session_ticket( ssl_context *ssl )
2965{
2966 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002967 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002968 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002969
2970 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2971
2972 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2973 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2974
2975 /*
2976 * struct {
2977 * uint32 ticket_lifetime_hint;
2978 * opaque ticket<0..2^16-1>;
2979 * } NewSessionTicket;
2980 *
2981 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2982 * 8 . 9 ticket_len (n)
2983 * 10 . 9+n ticket content
2984 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002985
2986 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2987 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2988 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2989 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002990
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002991 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2992 {
2993 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2994 tlen = 0;
2995 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002996
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002997 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2998 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002999
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003000 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003001
3002 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3003 {
3004 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3005 return( ret );
3006 }
3007
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003008 /* No need to remember writing a NewSessionTicket any more */
3009 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003010
3011 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3012
3013 return( 0 );
3014}
Paul Bakkera503a632013-08-14 13:48:06 +02003015#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003016
Paul Bakker5121ce52009-01-03 21:22:43 +00003017/*
Paul Bakker1961b702013-01-25 14:49:24 +01003018 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003019 */
Paul Bakker1961b702013-01-25 14:49:24 +01003020int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003021{
3022 int ret = 0;
3023
Paul Bakker1961b702013-01-25 14:49:24 +01003024 if( ssl->state == SSL_HANDSHAKE_OVER )
3025 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003026
Paul Bakker1961b702013-01-25 14:49:24 +01003027 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3028
3029 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3030 return( ret );
3031
3032 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003033 {
Paul Bakker1961b702013-01-25 14:49:24 +01003034 case SSL_HELLO_REQUEST:
3035 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 break;
3037
Paul Bakker1961b702013-01-25 14:49:24 +01003038 /*
3039 * <== ClientHello
3040 */
3041 case SSL_CLIENT_HELLO:
3042 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003044
3045 /*
3046 * ==> ServerHello
3047 * Certificate
3048 * ( ServerKeyExchange )
3049 * ( CertificateRequest )
3050 * ServerHelloDone
3051 */
3052 case SSL_SERVER_HELLO:
3053 ret = ssl_write_server_hello( ssl );
3054 break;
3055
3056 case SSL_SERVER_CERTIFICATE:
3057 ret = ssl_write_certificate( ssl );
3058 break;
3059
3060 case SSL_SERVER_KEY_EXCHANGE:
3061 ret = ssl_write_server_key_exchange( ssl );
3062 break;
3063
3064 case SSL_CERTIFICATE_REQUEST:
3065 ret = ssl_write_certificate_request( ssl );
3066 break;
3067
3068 case SSL_SERVER_HELLO_DONE:
3069 ret = ssl_write_server_hello_done( ssl );
3070 break;
3071
3072 /*
3073 * <== ( Certificate/Alert )
3074 * ClientKeyExchange
3075 * ( CertificateVerify )
3076 * ChangeCipherSpec
3077 * Finished
3078 */
3079 case SSL_CLIENT_CERTIFICATE:
3080 ret = ssl_parse_certificate( ssl );
3081 break;
3082
3083 case SSL_CLIENT_KEY_EXCHANGE:
3084 ret = ssl_parse_client_key_exchange( ssl );
3085 break;
3086
3087 case SSL_CERTIFICATE_VERIFY:
3088 ret = ssl_parse_certificate_verify( ssl );
3089 break;
3090
3091 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3092 ret = ssl_parse_change_cipher_spec( ssl );
3093 break;
3094
3095 case SSL_CLIENT_FINISHED:
3096 ret = ssl_parse_finished( ssl );
3097 break;
3098
3099 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003100 * ==> ( NewSessionTicket )
3101 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003102 * Finished
3103 */
3104 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003105#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003106 if( ssl->handshake->new_session_ticket != 0 )
3107 ret = ssl_write_new_session_ticket( ssl );
3108 else
Paul Bakkera503a632013-08-14 13:48:06 +02003109#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003110 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003111 break;
3112
3113 case SSL_SERVER_FINISHED:
3114 ret = ssl_write_finished( ssl );
3115 break;
3116
3117 case SSL_FLUSH_BUFFERS:
3118 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3119 ssl->state = SSL_HANDSHAKE_WRAPUP;
3120 break;
3121
3122 case SSL_HANDSHAKE_WRAPUP:
3123 ssl_handshake_wrapup( ssl );
3124 break;
3125
3126 default:
3127 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3128 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003129 }
3130
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 return( ret );
3132}
Paul Bakker5121ce52009-01-03 21:22:43 +00003133#endif