blob: ffd73e2fc9396e347c7647776c48072435f6c307 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker68884e32013-01-07 18:20:04 +01004 * Copyright (C) 2006-2013, 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
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020036#if defined(POLARSSL_MEMORY_C)
37#include "polarssl/memory.h"
38#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é-Gonnardd09453c2013-09-23 19:11:32 +0200539 /* Don't allow our peer to make use allocated too much memory,
540 * 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
Paul Bakker78a8c712013-03-06 17:01:52 +0100683#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
684static int ssl_parse_client_hello_v2( ssl_context *ssl )
685{
686 int ret;
687 unsigned int i, j;
688 size_t n;
689 unsigned int ciph_len, sess_len, chal_len;
690 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200691 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200692 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100693
694 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
695
696 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
697 {
698 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
699
700 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
701 return( ret );
702
703 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
704 }
705
706 buf = ssl->in_hdr;
707
708 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
709
710 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
711 buf[2] ) );
712 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
713 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
714 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
715 buf[3], buf[4] ) );
716
717 /*
718 * SSLv2 Client Hello
719 *
720 * Record layer:
721 * 0 . 1 message length
722 *
723 * SSL layer:
724 * 2 . 2 message type
725 * 3 . 4 protocol version
726 */
727 if( buf[2] != SSL_HS_CLIENT_HELLO ||
728 buf[3] != SSL_MAJOR_VERSION_3 )
729 {
730 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
731 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
732 }
733
734 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
735
736 if( n < 17 || n > 512 )
737 {
738 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
739 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
740 }
741
742 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200743 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
744 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100745
746 if( ssl->minor_ver < ssl->min_minor_ver )
747 {
748 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
749 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
750 ssl->min_major_ver, ssl->min_minor_ver ) );
751
752 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
753 SSL_ALERT_MSG_PROTOCOL_VERSION );
754 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
755 }
756
Paul Bakker2fbefde2013-06-29 16:01:15 +0200757 ssl->handshake->max_major_ver = buf[3];
758 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100759
760 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
761 {
762 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
763 return( ret );
764 }
765
766 ssl->handshake->update_checksum( ssl, buf + 2, n );
767
768 buf = ssl->in_msg;
769 n = ssl->in_left - 5;
770
771 /*
772 * 0 . 1 ciphersuitelist length
773 * 2 . 3 session id length
774 * 4 . 5 challenge length
775 * 6 . .. ciphersuitelist
776 * .. . .. session id
777 * .. . .. challenge
778 */
779 SSL_DEBUG_BUF( 4, "record contents", buf, n );
780
781 ciph_len = ( buf[0] << 8 ) | buf[1];
782 sess_len = ( buf[2] << 8 ) | buf[3];
783 chal_len = ( buf[4] << 8 ) | buf[5];
784
785 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
786 ciph_len, sess_len, chal_len ) );
787
788 /*
789 * Make sure each parameter length is valid
790 */
791 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
792 {
793 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
794 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
795 }
796
797 if( sess_len > 32 )
798 {
799 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
800 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
801 }
802
803 if( chal_len < 8 || chal_len > 32 )
804 {
805 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
806 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
807 }
808
809 if( n != 6 + ciph_len + sess_len + chal_len )
810 {
811 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
812 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
813 }
814
815 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
816 buf + 6, ciph_len );
817 SSL_DEBUG_BUF( 3, "client hello, session id",
818 buf + 6 + ciph_len, sess_len );
819 SSL_DEBUG_BUF( 3, "client hello, challenge",
820 buf + 6 + ciph_len + sess_len, chal_len );
821
822 p = buf + 6 + ciph_len;
823 ssl->session_negotiate->length = sess_len;
824 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
825 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
826
827 p += sess_len;
828 memset( ssl->handshake->randbytes, 0, 64 );
829 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
830
831 /*
832 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
833 */
834 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
835 {
836 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
837 {
838 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
839 if( ssl->renegotiation == SSL_RENEGOTIATION )
840 {
841 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
842
843 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
844 return( ret );
845
846 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
847 }
848 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
849 break;
850 }
851 }
852
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200853 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
854 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100855 {
856 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
857 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100858 // Only allow non-ECC ciphersuites as we do not have extensions
859 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200860 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200861 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
862 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200863 {
864 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
865
866 if( ciphersuite_info == NULL )
867 {
868 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
869 ciphersuites[i] ) );
870 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
871 }
872
Paul Bakker2fbefde2013-06-29 16:01:15 +0200873 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
874 ciphersuite_info->max_minor_ver < ssl->minor_ver )
875 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200876
Paul Bakker78a8c712013-03-06 17:01:52 +0100877 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200878 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100879 }
880 }
881
882 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
883
884 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
885
886have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200887 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200888 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100889 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100890
891 /*
892 * SSLv2 Client Hello relevant renegotiation security checks
893 */
894 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
895 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
896 {
897 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
898
899 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
900 return( ret );
901
902 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
903 }
904
905 ssl->in_left = 0;
906 ssl->state++;
907
908 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
909
910 return( 0 );
911}
912#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
913
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200914#if defined(POLARSSL_X509_CRT_PARSE_C)
915#if defined(POLARSSL_ECDSA_C)
916static int ssl_key_matches_curves( pk_context *pk,
917 const ecp_curve_info **curves )
918{
919 const ecp_curve_info **crv = curves;
920 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
921
922 while( *crv != NULL )
923 {
924 if( (*crv)->grp_id == grp_id )
925 return( 1 );
926 crv++;
927 }
928
929 return( 0 );
930}
931#endif /* POLARSSL_ECDSA_C */
932
933/*
934 * Try picking a certificate for this ciphersuite,
935 * return 0 on success and -1 on failure.
936 */
937static int ssl_pick_cert( ssl_context *ssl,
938 const ssl_ciphersuite_t * ciphersuite_info )
939{
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200940 ssl_key_cert *cur, *list;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200941 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
942
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200943#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
944 if( ssl->handshake->sni_key_cert != NULL )
945 list = ssl->handshake->sni_key_cert;
946 else
947#endif
948 list = ssl->handshake->key_cert;
949
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200950 if( pk_alg == POLARSSL_PK_NONE )
951 return( 0 );
952
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200953 for( cur = list; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200954 {
955 if( ! pk_can_do( cur->key, pk_alg ) )
956 continue;
957
958#if defined(POLARSSL_ECDSA_C)
959 if( pk_alg == POLARSSL_PK_ECDSA )
960 {
961 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
962 break;
963 }
964 else
965#endif
966 break;
967 }
968
969 if( cur == NULL )
970 return( -1 );
971
972 ssl->handshake->key_cert = cur;
973 return( 0 );
974}
975#endif /* POLARSSL_X509_CRT_PARSE_C */
976
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +0100977/*
978 * Check if a given ciphersuite is suitable for use with our config/keys/etc
979 * Sets ciphersuite_info only if the suite matches.
980 */
981static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
982 const ssl_ciphersuite_t **ciphersuite_info )
983{
984 const ssl_ciphersuite_t *suite_info;
985
986 suite_info = ssl_ciphersuite_from_id( suite_id );
987 if( suite_info == NULL )
988 {
989 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
990 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
991 }
992
993 if( suite_info->min_minor_ver > ssl->minor_ver ||
994 suite_info->max_minor_ver < ssl->minor_ver )
995 return( 0 );
996
997#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
998 if( ssl_ciphersuite_uses_ec( suite_info ) &&
999 ( ssl->handshake->curves == NULL ||
1000 ssl->handshake->curves[0] == NULL ) )
1001 return( 0 );
1002#endif
1003
1004#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1005 /* If the ciphersuite requires a pre-shared key and we don't
1006 * have one, skip it now rather than failing later */
1007 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1008 ssl->f_psk == NULL &&
1009 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1010 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
1011 return( 0 );
1012#endif
1013
1014#if defined(POLARSSL_X509_CRT_PARSE_C)
1015 /*
1016 * Final check: if ciphersuite requires us to have a
1017 * certificate/key of a particular type:
1018 * - select the appropriate certificate if we have one, or
1019 * - try the next ciphersuite if we don't
1020 * This must be done last since we modify the key_cert list.
1021 */
1022 if( ssl_pick_cert( ssl, suite_info ) != 0 )
1023 return( 0 );
1024#endif
1025
1026 *ciphersuite_info = suite_info;
1027 return( 0 );
1028}
1029
Paul Bakker5121ce52009-01-03 21:22:43 +00001030static int ssl_parse_client_hello( ssl_context *ssl )
1031{
Paul Bakker23986e52011-04-24 08:57:21 +00001032 int ret;
1033 unsigned int i, j;
1034 size_t n;
1035 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001036 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001037 unsigned int ext_len = 0;
1038 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001039 int renegotiation_info_seen = 0;
1040 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001041 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001042 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001043
1044 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1045
Paul Bakker48916f92012-09-16 19:57:18 +00001046 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1047 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
1049 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1050 return( ret );
1051 }
1052
1053 buf = ssl->in_hdr;
1054
Paul Bakker78a8c712013-03-06 17:01:52 +01001055#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1056 if( ( buf[0] & 0x80 ) != 0 )
1057 return ssl_parse_client_hello_v2( ssl );
1058#endif
1059
Paul Bakkerec636f32012-09-09 19:17:02 +00001060 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1061
1062 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1063 buf[0] ) );
1064 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1065 ( buf[3] << 8 ) | buf[4] ) );
1066 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1067 buf[1], buf[2] ) );
1068
1069 /*
1070 * SSLv3 Client Hello
1071 *
1072 * Record layer:
1073 * 0 . 0 message type
1074 * 1 . 2 protocol version
1075 * 3 . 4 message length
1076 */
1077 if( buf[0] != SSL_MSG_HANDSHAKE ||
1078 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001080 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
Paul Bakkerec636f32012-09-09 19:17:02 +00001084 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001085
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001086 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001087 {
1088 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1090 }
1091
Paul Bakker48916f92012-09-16 19:57:18 +00001092 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1093 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001094 {
1095 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1096 return( ret );
1097 }
1098
1099 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001100 if( !ssl->renegotiation )
1101 n = ssl->in_left - 5;
1102 else
1103 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001104
Paul Bakker48916f92012-09-16 19:57:18 +00001105 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001106
1107 /*
1108 * SSL layer:
1109 * 0 . 0 handshake type
1110 * 1 . 3 handshake length
1111 * 4 . 5 protocol version
1112 * 6 . 9 UNIX time()
1113 * 10 . 37 random bytes
1114 * 38 . 38 session id length
1115 * 39 . 38+x session id
1116 * 39+x . 40+x ciphersuitelist length
1117 * 41+x . .. ciphersuitelist
1118 * .. . .. compression alg.
1119 * .. . .. extensions
1120 */
1121 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1122
1123 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1124 buf[0] ) );
1125 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1126 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1127 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1128 buf[4], buf[5] ) );
1129
1130 /*
1131 * Check the handshake type and protocol version
1132 */
1133 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1134 buf[4] != SSL_MAJOR_VERSION_3 )
1135 {
1136 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1137 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1138 }
1139
1140 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001141 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1142 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001143
Paul Bakker1d29fb52012-09-28 13:28:45 +00001144 if( ssl->minor_ver < ssl->min_minor_ver )
1145 {
1146 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1147 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001148 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001149
1150 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1151 SSL_ALERT_MSG_PROTOCOL_VERSION );
1152
1153 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1154 }
1155
Paul Bakker2fbefde2013-06-29 16:01:15 +02001156 ssl->handshake->max_major_ver = buf[4];
1157 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001158
Paul Bakker48916f92012-09-16 19:57:18 +00001159 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001160
1161 /*
1162 * Check the handshake message length
1163 */
1164 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1165 {
1166 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1167 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1168 }
1169
1170 /*
1171 * Check the session length
1172 */
1173 sess_len = buf[38];
1174
1175 if( sess_len > 32 )
1176 {
1177 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1178 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1179 }
1180
Paul Bakker48916f92012-09-16 19:57:18 +00001181 ssl->session_negotiate->length = sess_len;
1182 memset( ssl->session_negotiate->id, 0,
1183 sizeof( ssl->session_negotiate->id ) );
1184 memcpy( ssl->session_negotiate->id, buf + 39,
1185 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001186
1187 /*
1188 * Check the ciphersuitelist length
1189 */
1190 ciph_len = ( buf[39 + sess_len] << 8 )
1191 | ( buf[40 + sess_len] );
1192
1193 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1194 {
1195 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1196 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1197 }
1198
1199 /*
1200 * Check the compression algorithms length
1201 */
1202 comp_len = buf[41 + sess_len + ciph_len];
1203
1204 if( comp_len < 1 || comp_len > 16 )
1205 {
1206 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1207 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1208 }
1209
Paul Bakker48916f92012-09-16 19:57:18 +00001210 /*
1211 * Check the extension length
1212 */
1213 if( n > 42 + sess_len + ciph_len + comp_len )
1214 {
1215 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1216 | ( buf[43 + sess_len + ciph_len + comp_len] );
1217
1218 if( ( ext_len > 0 && ext_len < 4 ) ||
1219 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1220 {
1221 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1222 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1223 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1224 }
1225 }
1226
1227 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001228#if defined(POLARSSL_ZLIB_SUPPORT)
1229 for( i = 0; i < comp_len; ++i )
1230 {
Paul Bakker48916f92012-09-16 19:57:18 +00001231 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Paul Bakker48916f92012-09-16 19:57:18 +00001233 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001234 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 }
1236 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001237#endif
1238
Paul Bakkerec636f32012-09-09 19:17:02 +00001239 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1240 buf + 6, 32 );
1241 SSL_DEBUG_BUF( 3, "client hello, session id",
1242 buf + 38, sess_len );
1243 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1244 buf + 41 + sess_len, ciph_len );
1245 SSL_DEBUG_BUF( 3, "client hello, compression",
1246 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
Paul Bakkerec636f32012-09-09 19:17:02 +00001248 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001249 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1250 */
1251 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1252 {
1253 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1254 {
1255 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1256 if( ssl->renegotiation == SSL_RENEGOTIATION )
1257 {
1258 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001259
1260 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1261 return( ret );
1262
Paul Bakker48916f92012-09-16 19:57:18 +00001263 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1264 }
1265 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1266 break;
1267 }
1268 }
1269
Paul Bakker48916f92012-09-16 19:57:18 +00001270 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001271
1272 while( ext_len )
1273 {
1274 unsigned int ext_id = ( ( ext[0] << 8 )
1275 | ( ext[1] ) );
1276 unsigned int ext_size = ( ( ext[2] << 8 )
1277 | ( ext[3] ) );
1278
1279 if( ext_size + 4 > ext_len )
1280 {
1281 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1282 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1283 }
1284 switch( ext_id )
1285 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001286#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001287 case TLS_EXT_SERVERNAME:
1288 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1289 if( ssl->f_sni == NULL )
1290 break;
1291
1292 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1293 if( ret != 0 )
1294 return( ret );
1295 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001296#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001297
Paul Bakker48916f92012-09-16 19:57:18 +00001298 case TLS_EXT_RENEGOTIATION_INFO:
1299 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1300 renegotiation_info_seen = 1;
1301
Paul Bakker23f36802012-09-28 14:15:14 +00001302 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1303 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001304 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001305 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001306
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001307#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001308 case TLS_EXT_SIG_ALG:
1309 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1310 if( ssl->renegotiation == SSL_RENEGOTIATION )
1311 break;
1312
1313 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1314 if( ret != 0 )
1315 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001316 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001317#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001318
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001319#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001320 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1321 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1322
1323 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1324 if( ret != 0 )
1325 return( ret );
1326 break;
1327
1328 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1329 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001330 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001331
1332 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1333 if( ret != 0 )
1334 return( ret );
1335 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001336#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001337
Paul Bakker05decb22013-08-15 13:33:48 +02001338#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001339 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1340 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1341
1342 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1343 if( ret != 0 )
1344 return( ret );
1345 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001346#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001347
Paul Bakker1f2bc622013-08-15 13:45:55 +02001348#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001349 case TLS_EXT_TRUNCATED_HMAC:
1350 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1351
1352 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1353 if( ret != 0 )
1354 return( ret );
1355 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001356#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001357
Paul Bakkera503a632013-08-14 13:48:06 +02001358#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001359 case TLS_EXT_SESSION_TICKET:
1360 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1361
1362 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1363 if( ret != 0 )
1364 return( ret );
1365 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001366#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001367
Paul Bakker48916f92012-09-16 19:57:18 +00001368 default:
1369 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1370 ext_id ) );
1371 }
1372
1373 ext_len -= 4 + ext_size;
1374 ext += 4 + ext_size;
1375
1376 if( ext_len > 0 && ext_len < 4 )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1379 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1380 }
1381 }
1382
1383 /*
1384 * Renegotiation security checks
1385 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001386 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1387 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1388 {
1389 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1390 handshake_failure = 1;
1391 }
1392 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1393 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1394 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001395 {
1396 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001397 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001398 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001399 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1400 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1401 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001402 {
1403 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001404 handshake_failure = 1;
1405 }
1406 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1407 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1408 renegotiation_info_seen == 1 )
1409 {
1410 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1411 handshake_failure = 1;
1412 }
1413
1414 if( handshake_failure == 1 )
1415 {
1416 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1417 return( ret );
1418
Paul Bakker48916f92012-09-16 19:57:18 +00001419 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1420 }
Paul Bakker380da532012-04-18 16:10:25 +00001421
Paul Bakker41c83d32013-03-20 14:39:14 +01001422 /*
1423 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001424 * (At the end because we need information from the EC-based extensions
1425 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001426 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001427 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001428 ciphersuite_info = NULL;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001429 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001430 {
1431 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1432 j += 2, p += 2 )
1433 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001434 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1435 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001436 {
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001437 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1438 &ciphersuite_info ) ) != 0 )
1439 return( ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01001440
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001441 if( ciphersuite_info != NULL )
1442 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001443 }
1444 }
1445 }
1446
1447 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1448
1449 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1450 return( ret );
1451
1452 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1453
1454have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001455 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001456 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1457 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1458
Paul Bakker5121ce52009-01-03 21:22:43 +00001459 ssl->in_left = 0;
1460 ssl->state++;
1461
1462 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1463
1464 return( 0 );
1465}
1466
Paul Bakker1f2bc622013-08-15 13:45:55 +02001467#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001468static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1469 unsigned char *buf,
1470 size_t *olen )
1471{
1472 unsigned char *p = buf;
1473
1474 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1475 {
1476 *olen = 0;
1477 return;
1478 }
1479
1480 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1481
1482 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1483 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1484
1485 *p++ = 0x00;
1486 *p++ = 0x00;
1487
1488 *olen = 4;
1489}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001490#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001491
Paul Bakkera503a632013-08-14 13:48:06 +02001492#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001493static void ssl_write_session_ticket_ext( ssl_context *ssl,
1494 unsigned char *buf,
1495 size_t *olen )
1496{
1497 unsigned char *p = buf;
1498
1499 if( ssl->handshake->new_session_ticket == 0 )
1500 {
1501 *olen = 0;
1502 return;
1503 }
1504
1505 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1506
1507 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1508 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1509
1510 *p++ = 0x00;
1511 *p++ = 0x00;
1512
1513 *olen = 4;
1514}
Paul Bakkera503a632013-08-14 13:48:06 +02001515#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001516
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001517static void ssl_write_renegotiation_ext( ssl_context *ssl,
1518 unsigned char *buf,
1519 size_t *olen )
1520{
1521 unsigned char *p = buf;
1522
1523 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1524 {
1525 *olen = 0;
1526 return;
1527 }
1528
1529 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1530
1531 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1532 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1533
1534 *p++ = 0x00;
1535 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1536 *p++ = ssl->verify_data_len * 2 & 0xFF;
1537
1538 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1539 p += ssl->verify_data_len;
1540 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1541 p += ssl->verify_data_len;
1542
1543 *olen = 5 + ssl->verify_data_len * 2;
1544}
1545
Paul Bakker05decb22013-08-15 13:33:48 +02001546#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001547static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1548 unsigned char *buf,
1549 size_t *olen )
1550{
1551 unsigned char *p = buf;
1552
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001553 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1554 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001555 *olen = 0;
1556 return;
1557 }
1558
1559 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1560
1561 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1562 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1563
1564 *p++ = 0x00;
1565 *p++ = 1;
1566
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001567 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001568
1569 *olen = 5;
1570}
Paul Bakker05decb22013-08-15 13:33:48 +02001571#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001572
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001573#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001574static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1575 unsigned char *buf,
1576 size_t *olen )
1577{
1578 unsigned char *p = buf;
1579 ((void) ssl);
1580
Paul Bakker677377f2013-10-28 12:54:26 +01001581 if( ( ssl->handshake->cli_exts &
1582 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1583 {
1584 *olen = 0;
1585 return;
1586 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001587
1588 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1589
1590 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1591 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1592
1593 *p++ = 0x00;
1594 *p++ = 2;
1595
1596 *p++ = 1;
1597 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1598
1599 *olen = 6;
1600}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001601#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001602
Paul Bakker5121ce52009-01-03 21:22:43 +00001603static int ssl_write_server_hello( ssl_context *ssl )
1604{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001605#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001606 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001607#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001608 int ret;
1609 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001610 unsigned char *buf, *p;
1611
1612 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1613
Paul Bakkera9a028e2013-11-21 17:31:06 +01001614 if( ssl->f_rng == NULL )
1615 {
1616 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1617 return( POLARSSL_ERR_SSL_NO_RNG );
1618 }
1619
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 /*
1621 * 0 . 0 handshake type
1622 * 1 . 3 handshake length
1623 * 4 . 5 protocol version
1624 * 6 . 9 UNIX time()
1625 * 10 . 37 random bytes
1626 */
1627 buf = ssl->out_msg;
1628 p = buf + 4;
1629
1630 *p++ = (unsigned char) ssl->major_ver;
1631 *p++ = (unsigned char) ssl->minor_ver;
1632
1633 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1634 buf[4], buf[5] ) );
1635
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001636#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001637 t = time( NULL );
1638 *p++ = (unsigned char)( t >> 24 );
1639 *p++ = (unsigned char)( t >> 16 );
1640 *p++ = (unsigned char)( t >> 8 );
1641 *p++ = (unsigned char)( t );
1642
1643 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001644#else
1645 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1646 return( ret );
1647
1648 p += 4;
1649#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Paul Bakkera3d195c2011-11-27 21:07:34 +00001651 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1652 return( ret );
1653
1654 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001655
Paul Bakker48916f92012-09-16 19:57:18 +00001656 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1659
1660 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001661 * Resume is 0 by default, see ssl_handshake_init().
1662 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1663 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001664 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001665 if( ssl->handshake->resume == 0 &&
1666 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001667 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001668 ssl->f_get_cache != NULL &&
1669 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1670 {
1671 ssl->handshake->resume = 1;
1672 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001674 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 {
1676 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001677 * New session, create a new session id,
1678 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001680 ssl->state++;
1681
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001682#if defined(POLARSSL_HAVE_TIME)
1683 ssl->session_negotiate->start = time( NULL );
1684#endif
1685
Paul Bakkera503a632013-08-14 13:48:06 +02001686#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001687 if( ssl->handshake->new_session_ticket != 0 )
1688 {
1689 ssl->session_negotiate->length = n = 0;
1690 memset( ssl->session_negotiate->id, 0, 32 );
1691 }
1692 else
1693#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001694 {
1695 ssl->session_negotiate->length = n = 32;
1696 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001697 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001698 return( ret );
1699 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 }
1701 else
1702 {
1703 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001704 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001706 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001708
1709 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1710 {
1711 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1712 return( ret );
1713 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 }
1715
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001716 /*
1717 * 38 . 38 session id length
1718 * 39 . 38+n session id
1719 * 39+n . 40+n chosen ciphersuite
1720 * 41+n . 41+n chosen compression alg.
1721 * 42+n . 43+n extensions length
1722 * 44+n . 43+n+m extensions
1723 */
1724 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001725 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1726 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727
1728 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1729 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1730 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001731 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Paul Bakker48916f92012-09-16 19:57:18 +00001733 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1734 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1735 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001737 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1738 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001739 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001740 ssl->session_negotiate->compression ) );
1741
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001742 /*
1743 * First write extensions, then the total length
1744 */
1745 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1746 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001747
Paul Bakker05decb22013-08-15 13:33:48 +02001748#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001749 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1750 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001751#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001752
Paul Bakker1f2bc622013-08-15 13:45:55 +02001753#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001754 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1755 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001756#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001757
Paul Bakkera503a632013-08-14 13:48:06 +02001758#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001759 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1760 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001761#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001762
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001763#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001764 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1765 ext_len += olen;
1766#endif
1767
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001768 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001769
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001770 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1771 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1772 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773
1774 ssl->out_msglen = p - buf;
1775 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1776 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1777
1778 ret = ssl_write_record( ssl );
1779
1780 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1781
1782 return( ret );
1783}
1784
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001785#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1786 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001787 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1788 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001789static int ssl_write_certificate_request( ssl_context *ssl )
1790{
Paul Bakkered27a042013-04-18 22:46:23 +02001791 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1792 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001793
1794 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1795
1796 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001797 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001798 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1799 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001800 {
1801 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1802 ssl->state++;
1803 return( 0 );
1804 }
1805
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001806 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001807 return( ret );
1808}
1809#else
1810static int ssl_write_certificate_request( ssl_context *ssl )
1811{
1812 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1813 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001814 size_t dn_size, total_dn_size; /* excluding length bytes */
1815 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001817 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
1819 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1820
1821 ssl->state++;
1822
Paul Bakkerfbb17802013-04-17 19:10:21 +02001823 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001824 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001825 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001826 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001827 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001829 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001830 return( 0 );
1831 }
1832
1833 /*
1834 * 0 . 0 handshake type
1835 * 1 . 3 handshake length
1836 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001837 * 5 .. m-1 cert types
1838 * m .. m+1 sig alg length (TLS 1.2 only)
1839 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 * n .. n+1 length of all DNs
1841 * n+2 .. n+3 length of DN 1
1842 * n+4 .. ... Distinguished Name #1
1843 * ... .. ... length of DN 2, etc.
1844 */
1845 buf = ssl->out_msg;
1846 p = buf + 4;
1847
1848 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001849 * Supported certificate types
1850 *
1851 * ClientCertificateType certificate_types<1..2^8-1>;
1852 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001853 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001854 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001855
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001856#if defined(POLARSSL_RSA_C)
1857 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1858#endif
1859#if defined(POLARSSL_ECDSA_C)
1860 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1861#endif
1862
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001863 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001864 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001865
Paul Bakker577e0062013-08-28 11:57:20 +02001866 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001867#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001868 /*
1869 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001870 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001871 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1872 *
1873 * struct {
1874 * HashAlgorithm hash;
1875 * SignatureAlgorithm signature;
1876 * } SignatureAndHashAlgorithm;
1877 *
1878 * enum { (255) } HashAlgorithm;
1879 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001880 */
Paul Bakker21dca692013-01-03 11:41:08 +01001881 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001882 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001883 /*
1884 * Only use current running hash algorithm that is already required
1885 * for requested ciphersuite.
1886 */
Paul Bakker926af752012-11-23 13:38:07 +01001887 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1888
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001889 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1890 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001891 {
1892 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1893 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001894
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001895 /*
1896 * Supported signature algorithms
1897 */
1898#if defined(POLARSSL_RSA_C)
1899 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1900 p[2 + sa_len++] = SSL_SIG_RSA;
1901#endif
1902#if defined(POLARSSL_ECDSA_C)
1903 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1904 p[2 + sa_len++] = SSL_SIG_ECDSA;
1905#endif
Paul Bakker926af752012-11-23 13:38:07 +01001906
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001907 p[0] = (unsigned char)( sa_len >> 8 );
1908 p[1] = (unsigned char)( sa_len );
1909 sa_len += 2;
1910 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001911 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001912#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001914 /*
1915 * DistinguishedName certificate_authorities<0..2^16-1>;
1916 * opaque DistinguishedName<1..2^16-1>;
1917 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 p += 2;
1919 crt = ssl->ca_chain;
1920
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001921 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001922 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 {
1924 if( p - buf > 4096 )
1925 break;
1926
Paul Bakker926af752012-11-23 13:38:07 +01001927 dn_size = crt->subject_raw.len;
1928 *p++ = (unsigned char)( dn_size >> 8 );
1929 *p++ = (unsigned char)( dn_size );
1930 memcpy( p, crt->subject_raw.p, dn_size );
1931 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Paul Bakker926af752012-11-23 13:38:07 +01001933 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1934
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001935 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001936 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001937 }
1938
Paul Bakker926af752012-11-23 13:38:07 +01001939 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001940 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1941 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001942 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1943 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 ret = ssl_write_record( ssl );
1946
1947 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1948
1949 return( ret );
1950}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001951#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1952 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001953 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
1954 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Paul Bakker41c83d32013-03-20 14:39:14 +01001956static int ssl_write_server_key_exchange( ssl_context *ssl )
1957{
Paul Bakker23986e52011-04-24 08:57:21 +00001958 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001959 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001960 const ssl_ciphersuite_t *ciphersuite_info =
1961 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001962
1963#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1964 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1965 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001966 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001967 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001968 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001969 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001970 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001971 ((void) dig_signed);
1972 ((void) dig_signed_len);
1973#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001974
Paul Bakker5121ce52009-01-03 21:22:43 +00001975 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1976
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001977 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1978 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1979 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001980 {
1981 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1982 ssl->state++;
1983 return( 0 );
1984 }
1985
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001986#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1987 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1988 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990 {
1991 /* TODO: Support identity hints */
1992 *(p++) = 0x00;
1993 *(p++) = 0x00;
1994
1995 n += 2;
1996 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001997#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1998 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001999
2000#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2001 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2002 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2003 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002004 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002005 /*
2006 * Ephemeral DH parameters:
2007 *
2008 * struct {
2009 * opaque dh_p<1..2^16-1>;
2010 * opaque dh_g<1..2^16-1>;
2011 * opaque dh_Ys<1..2^16-1>;
2012 * } ServerDHParams;
2013 */
2014 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2015 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2016 {
2017 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2018 return( ret );
2019 }
Paul Bakker48916f92012-09-16 19:57:18 +00002020
Paul Bakker41c83d32013-03-20 14:39:14 +01002021 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002022 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002023 p,
2024 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002025 {
2026 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2027 return( ret );
2028 }
2029
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002030 dig_signed = p;
2031 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002032
2033 p += len;
2034 n += len;
2035
Paul Bakker41c83d32013-03-20 14:39:14 +01002036 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2037 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2038 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2039 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2040 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002041#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2042 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002043
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002044#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002045 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2046 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2047
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002048 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002049 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2050 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002051 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002052 /*
2053 * Ephemeral ECDH parameters:
2054 *
2055 * struct {
2056 * ECParameters curve_params;
2057 * ECPoint public;
2058 * } ServerECDHParams;
2059 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002060 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002061 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002062 {
2063 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2064 return( ret );
2065 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002066
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002067 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2068 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2069
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002070 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2071 p, SSL_MAX_CONTENT_LEN - n,
2072 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002073 {
2074 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2075 return( ret );
2076 }
2077
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002078 dig_signed = p;
2079 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002080
2081 p += len;
2082 n += len;
2083
Paul Bakker41c83d32013-03-20 14:39:14 +01002084 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2085 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002086#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002087 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2088 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002090#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002091 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2092 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002093 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002094 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2095 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002096 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002097 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002098 unsigned int hashlen = 0;
2099 unsigned char hash[64];
2100 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002101
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002102 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002103 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2104 */
Paul Bakker577e0062013-08-28 11:57:20 +02002105#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002106 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2107 {
2108 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2109
2110 if( md_alg == POLARSSL_MD_NONE )
2111 {
2112 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2113 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2114 }
2115 }
Paul Bakker577e0062013-08-28 11:57:20 +02002116 else
2117#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002118#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2119 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002120 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002121 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2122 {
2123 md_alg = POLARSSL_MD_SHA1;
2124 }
2125 else
Paul Bakker577e0062013-08-28 11:57:20 +02002126#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002127 {
2128 md_alg = POLARSSL_MD_NONE;
2129 }
2130
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002131 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002132 * Compute the hash to be signed
2133 */
Paul Bakker577e0062013-08-28 11:57:20 +02002134#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2135 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002136 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002137 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002138 md5_context md5;
2139 sha1_context sha1;
2140
2141 /*
2142 * digitally-signed struct {
2143 * opaque md5_hash[16];
2144 * opaque sha_hash[20];
2145 * };
2146 *
2147 * md5_hash
2148 * MD5(ClientHello.random + ServerHello.random
2149 * + ServerParams);
2150 * sha_hash
2151 * SHA(ClientHello.random + ServerHello.random
2152 * + ServerParams);
2153 */
2154 md5_starts( &md5 );
2155 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002156 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002157 md5_finish( &md5, hash );
2158
2159 sha1_starts( &sha1 );
2160 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002161 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002162 sha1_finish( &sha1, hash + 16 );
2163
2164 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002165 }
2166 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002167#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2168 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002169#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2170 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002171 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 {
2173 md_context_t ctx;
2174
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002175 /* Info from md_alg will be used instead */
2176 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002177
2178 /*
2179 * digitally-signed struct {
2180 * opaque client_random[32];
2181 * opaque server_random[32];
2182 * ServerDHParams params;
2183 * };
2184 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002185 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186 {
2187 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2188 return( ret );
2189 }
2190
2191 md_starts( &ctx );
2192 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002193 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002195
2196 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2197 {
2198 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2199 return( ret );
2200 }
2201
Paul Bakker23f36802012-09-28 14:15:14 +00002202 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002203 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002204#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2205 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002206 {
2207 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002208 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002209 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002210
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002211 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2212 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002213
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002214 /*
2215 * Make the signature
2216 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002217 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002218 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002219 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2220 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002221 }
Paul Bakker23f36802012-09-28 14:15:14 +00002222
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002223#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002224 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2225 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002226 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002227 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228
2229 n += 2;
2230 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002231#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002232
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002233 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002234 p + 2 , &signature_len,
2235 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002237 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002238 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002239 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002240
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002241 *(p++) = (unsigned char)( signature_len >> 8 );
2242 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002243 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002244
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002245 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002246
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002247 p += signature_len;
2248 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002249 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002250#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002251 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2252 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002253
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002254 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002255 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2256 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2257
2258 ssl->state++;
2259
2260 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2261 {
2262 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2263 return( ret );
2264 }
2265
2266 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2267
2268 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002269}
2270
2271static int ssl_write_server_hello_done( ssl_context *ssl )
2272{
2273 int ret;
2274
2275 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2276
2277 ssl->out_msglen = 4;
2278 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2279 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2280
2281 ssl->state++;
2282
2283 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2284 {
2285 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2286 return( ret );
2287 }
2288
2289 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2290
2291 return( 0 );
2292}
2293
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2295 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2296static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2297 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002298{
2299 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002300 size_t n;
2301
2302 /*
2303 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2304 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 if( *p + 2 > end )
2306 {
2307 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2308 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2309 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002310
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002311 n = ( (*p)[0] << 8 ) | (*p)[1];
2312 *p += 2;
2313
2314 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002315 {
2316 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2317 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2318 }
2319
2320 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002321 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002322 {
2323 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2324 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2325 }
2326
2327 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2328
Paul Bakker70df2fb2013-04-17 17:19:09 +02002329 return( ret );
2330}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002331#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2332 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002333
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002334#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2335 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002336static int ssl_parse_encrypted_pms( ssl_context *ssl,
2337 const unsigned char *p,
2338 const unsigned char *end,
2339 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002340{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002341 int ret;
2342 size_t len = pk_get_len( ssl_own_key( ssl ) );
2343 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002344
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002345 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002346 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002347 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002348 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2349 }
2350
2351 /*
2352 * Decrypt the premaster using own private RSA key
2353 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002354#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2355 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002356 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2357 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002358 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2359 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002360 {
2361 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2363 }
2364 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002365#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002366
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002367 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002368 {
2369 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2370 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2371 }
2372
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002373 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2374 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002375 sizeof(ssl->handshake->premaster),
2376 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002377
2378 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002379 pms[0] != ssl->handshake->max_major_ver ||
2380 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002381 {
2382 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2383
2384 /*
2385 * Protection against Bleichenbacher's attack:
2386 * invalid PKCS#1 v1.5 padding must not cause
2387 * the connection to end immediately; instead,
2388 * send a bad_record_mac later in the handshake.
2389 */
2390 ssl->handshake->pmslen = 48;
2391
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002392 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002393 if( ret != 0 )
2394 return( ret );
2395 }
2396
2397 return( ret );
2398}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002399#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2400 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002401
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002402#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002403static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2404 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002405{
Paul Bakker6db455e2013-09-18 17:29:31 +02002406 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002407 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002408
Paul Bakker6db455e2013-09-18 17:29:31 +02002409 if( ssl->f_psk == NULL &&
2410 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2411 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002412 {
2413 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2414 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2415 }
2416
2417 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002418 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002419 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 if( *p + 2 > end )
2421 {
2422 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2424 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002425
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002426 n = ( (*p)[0] << 8 ) | (*p)[1];
2427 *p += 2;
2428
2429 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002430 {
2431 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2432 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2433 }
2434
Paul Bakker6db455e2013-09-18 17:29:31 +02002435 if( ssl->f_psk != NULL )
2436 {
2437 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2438 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2439 }
2440
2441 if( ret == 0 )
2442 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002443 /* Identity is not a big secret since clients send it in the clear,
2444 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002445 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002446 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002447 {
2448 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2449 }
2450 }
2451
2452 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002453 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002454 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002455 if( ( ret = ssl_send_alert_message( ssl,
2456 SSL_ALERT_LEVEL_FATAL,
2457 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2458 {
2459 return( ret );
2460 }
2461
2462 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002463 }
2464
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002466 ret = 0;
2467
Paul Bakkerfbb17802013-04-17 19:10:21 +02002468 return( ret );
2469}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002470#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002471
Paul Bakker5121ce52009-01-03 21:22:43 +00002472static int ssl_parse_client_key_exchange( ssl_context *ssl )
2473{
Paul Bakker23986e52011-04-24 08:57:21 +00002474 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002475 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002476
Paul Bakker41c83d32013-03-20 14:39:14 +01002477 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
2479 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2480
2481 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2482 {
2483 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2484 return( ret );
2485 }
2486
2487 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2488 {
2489 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002490 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 }
2492
2493 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2494 {
2495 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002496 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002497 }
2498
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002500 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002502 unsigned char *p = ssl->in_msg + 4;
2503 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2504
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002507 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2508 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510
2511 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2512
2513 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2514 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002515 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002516 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517 {
2518 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2519 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2520 }
2521
2522 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002523 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002524 else
2525#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002526#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2527 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2528 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2529 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002531 size_t n = ssl->in_msg[3];
2532
2533 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2534 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002536 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002539
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002540 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2541 ssl->in_msg + 4, n ) ) != 0 )
2542 {
2543 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2544 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2545 }
2546
2547 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2548
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002549 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2550 &ssl->handshake->pmslen,
2551 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002552 POLARSSL_MPI_MAX_SIZE,
2553 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554 {
2555 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2556 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2557 }
2558
2559 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002560 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002562#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2563 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2565 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002566 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002567 unsigned char *p = ssl->in_msg + 4;
2568 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2569
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002570 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002571 {
2572 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2573 return( ret );
2574 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002575
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002576 if( ( ret = ssl_psk_derive_premaster( ssl,
2577 ciphersuite_info->key_exchange ) ) != 0 )
2578 {
2579 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2580 return( ret );
2581 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002583 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002584#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002585#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2586 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2587 {
2588 unsigned char *p = ssl->in_msg + 4;
2589 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2590
2591 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2592 {
2593 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2594 return( ret );
2595 }
2596
2597 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2598 {
2599 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2600 return( ret );
2601 }
2602
2603 if( ( ret = ssl_psk_derive_premaster( ssl,
2604 ciphersuite_info->key_exchange ) ) != 0 )
2605 {
2606 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2607 return( ret );
2608 }
2609 }
2610 else
2611#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002612#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2613 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2614 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002615 unsigned char *p = ssl->in_msg + 4;
2616 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002617
2618 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2619 {
2620 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2621 return( ret );
2622 }
2623 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2624 {
2625 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2626 return( ret );
2627 }
2628
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002629 if( ( ret = ssl_psk_derive_premaster( ssl,
2630 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002631 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002632 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2633 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002634 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002635 }
2636 else
2637#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002638#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2639 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2640 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002641 unsigned char *p = ssl->in_msg + 4;
2642 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2643
2644 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2645 {
2646 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2647 return( ret );
2648 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002649
2650 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2651 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002652 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002653 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2654 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002655 }
2656
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002657 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2658
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002659 if( ( ret = ssl_psk_derive_premaster( ssl,
2660 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002661 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002662 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002663 return( ret );
2664 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002665 }
2666 else
2667#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002668#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002670 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002671 if( ( ret = ssl_parse_encrypted_pms( ssl,
2672 ssl->in_msg + 4,
2673 ssl->in_msg + ssl->in_msglen,
2674 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002675 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002676 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002677 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002678 }
2679 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 else
2681#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2682 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002683 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2685 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Paul Bakkerff60ee62010-03-16 21:09:09 +00002687 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2688 {
2689 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2690 return( ret );
2691 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
Paul Bakker5121ce52009-01-03 21:22:43 +00002693 ssl->state++;
2694
2695 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2696
2697 return( 0 );
2698}
2699
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002700#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2701 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002702 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2703 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002704static int ssl_parse_certificate_verify( ssl_context *ssl )
2705{
Paul Bakkered27a042013-04-18 22:46:23 +02002706 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002707 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2710
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002711 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002712 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002713 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002715 {
2716 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2717 ssl->state++;
2718 return( 0 );
2719 }
2720
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002721 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002722 return( ret );
2723}
2724#else
2725static int ssl_parse_certificate_verify( ssl_context *ssl )
2726{
2727 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002728 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002730 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002731 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002732#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002733 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002734#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002735 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002736 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2737
2738 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2739
2740 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002741 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002742 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002743 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2744 {
2745 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2746 ssl->state++;
2747 return( 0 );
2748 }
2749
Paul Bakkered27a042013-04-18 22:46:23 +02002750 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002751 {
2752 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2753 ssl->state++;
2754 return( 0 );
2755 }
2756
Paul Bakker48916f92012-09-16 19:57:18 +00002757 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
2759 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2760 {
2761 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2762 return( ret );
2763 }
2764
2765 ssl->state++;
2766
2767 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2768 {
2769 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002770 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 }
2772
2773 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002776 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002777 }
2778
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002779 /*
2780 * 0 . 0 handshake type
2781 * 1 . 3 handshake length
2782 * 4 . 5 sig alg (TLS 1.2 only)
2783 * 4+n . 5+n signature length (n = sa_len)
2784 * 6+n . 6+n+m signature (m = sig_len)
2785 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002787#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2788 defined(POLARSSL_SSL_PROTO_TLS1_1)
2789 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002790 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002791 sa_len = 0;
2792
Paul Bakkerc70b9822013-04-07 22:00:46 +02002793 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002794 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002795
2796 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2797 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2798 POLARSSL_PK_ECDSA ) )
2799 {
2800 hash_start += 16;
2801 hashlen -= 16;
2802 md_alg = POLARSSL_MD_SHA1;
2803 }
Paul Bakker926af752012-11-23 13:38:07 +01002804 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002805 else
2806#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002807#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2808 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002809 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002810 sa_len = 2;
2811
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002813 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002815 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002817 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2818 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002819 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2820 }
2821
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002822 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002823
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002824 /* Info from md_alg will be used instead */
2825 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002826
2827 /*
2828 * Signature
2829 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002830 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2831 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002832 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002833 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2834 " for verify message" ) );
2835 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002836 }
2837
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002838 /*
2839 * Check the certificate's key type matches the signature alg
2840 */
2841 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2844 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2845 }
Paul Bakker577e0062013-08-28 11:57:20 +02002846 }
2847 else
2848#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2849 {
2850 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002851 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002852 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002853
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002854 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002855
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002856 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 {
2858 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002859 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002860 }
2861
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002862 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002863 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002864 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002865 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002866 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 return( ret );
2868 }
2869
2870 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2871
Paul Bakkered27a042013-04-18 22:46:23 +02002872 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002873}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002874#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2875 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2876 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Paul Bakkera503a632013-08-14 13:48:06 +02002878#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002879static int ssl_write_new_session_ticket( ssl_context *ssl )
2880{
2881 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002882 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002883 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002884
2885 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2886
2887 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2888 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2889
2890 /*
2891 * struct {
2892 * uint32 ticket_lifetime_hint;
2893 * opaque ticket<0..2^16-1>;
2894 * } NewSessionTicket;
2895 *
2896 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2897 * 8 . 9 ticket_len (n)
2898 * 10 . 9+n ticket content
2899 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002900
2901 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2902 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2903 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2904 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002905
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002906 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2907 {
2908 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2909 tlen = 0;
2910 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002911
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002912 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2913 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002914
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002915 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002916
2917 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2918 {
2919 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2920 return( ret );
2921 }
2922
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002923 /* No need to remember writing a NewSessionTicket any more */
2924 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002925
2926 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2927
2928 return( 0 );
2929}
Paul Bakkera503a632013-08-14 13:48:06 +02002930#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002931
Paul Bakker5121ce52009-01-03 21:22:43 +00002932/*
Paul Bakker1961b702013-01-25 14:49:24 +01002933 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002934 */
Paul Bakker1961b702013-01-25 14:49:24 +01002935int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002936{
2937 int ret = 0;
2938
Paul Bakker1961b702013-01-25 14:49:24 +01002939 if( ssl->state == SSL_HANDSHAKE_OVER )
2940 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Paul Bakker1961b702013-01-25 14:49:24 +01002942 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2943
2944 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2945 return( ret );
2946
2947 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002948 {
Paul Bakker1961b702013-01-25 14:49:24 +01002949 case SSL_HELLO_REQUEST:
2950 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002951 break;
2952
Paul Bakker1961b702013-01-25 14:49:24 +01002953 /*
2954 * <== ClientHello
2955 */
2956 case SSL_CLIENT_HELLO:
2957 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002959
2960 /*
2961 * ==> ServerHello
2962 * Certificate
2963 * ( ServerKeyExchange )
2964 * ( CertificateRequest )
2965 * ServerHelloDone
2966 */
2967 case SSL_SERVER_HELLO:
2968 ret = ssl_write_server_hello( ssl );
2969 break;
2970
2971 case SSL_SERVER_CERTIFICATE:
2972 ret = ssl_write_certificate( ssl );
2973 break;
2974
2975 case SSL_SERVER_KEY_EXCHANGE:
2976 ret = ssl_write_server_key_exchange( ssl );
2977 break;
2978
2979 case SSL_CERTIFICATE_REQUEST:
2980 ret = ssl_write_certificate_request( ssl );
2981 break;
2982
2983 case SSL_SERVER_HELLO_DONE:
2984 ret = ssl_write_server_hello_done( ssl );
2985 break;
2986
2987 /*
2988 * <== ( Certificate/Alert )
2989 * ClientKeyExchange
2990 * ( CertificateVerify )
2991 * ChangeCipherSpec
2992 * Finished
2993 */
2994 case SSL_CLIENT_CERTIFICATE:
2995 ret = ssl_parse_certificate( ssl );
2996 break;
2997
2998 case SSL_CLIENT_KEY_EXCHANGE:
2999 ret = ssl_parse_client_key_exchange( ssl );
3000 break;
3001
3002 case SSL_CERTIFICATE_VERIFY:
3003 ret = ssl_parse_certificate_verify( ssl );
3004 break;
3005
3006 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3007 ret = ssl_parse_change_cipher_spec( ssl );
3008 break;
3009
3010 case SSL_CLIENT_FINISHED:
3011 ret = ssl_parse_finished( ssl );
3012 break;
3013
3014 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003015 * ==> ( NewSessionTicket )
3016 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003017 * Finished
3018 */
3019 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003020#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003021 if( ssl->handshake->new_session_ticket != 0 )
3022 ret = ssl_write_new_session_ticket( ssl );
3023 else
Paul Bakkera503a632013-08-14 13:48:06 +02003024#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003025 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003026 break;
3027
3028 case SSL_SERVER_FINISHED:
3029 ret = ssl_write_finished( ssl );
3030 break;
3031
3032 case SSL_FLUSH_BUFFERS:
3033 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3034 ssl->state = SSL_HANDSHAKE_WRAPUP;
3035 break;
3036
3037 case SSL_HANDSHAKE_WRAPUP:
3038 ssl_handshake_wrapup( ssl );
3039 break;
3040
3041 default:
3042 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3043 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 }
3045
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 return( ret );
3047}
Paul Bakker5121ce52009-01-03 21:22:43 +00003048#endif