blob: 3a3ec75a7367341713b443f7638c4d310398dca9 [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
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100683/*
684 * Auxiliary functions for ServerHello parsing and related actions
685 */
686
687#if defined(POLARSSL_X509_CRT_PARSE_C)
688/*
689 * Return 1 if the given EC key uses the given curve, 0 otherwise
690 */
691#if defined(POLARSSL_ECDSA_C)
692static int ssl_key_matches_curves( pk_context *pk,
693 const ecp_curve_info **curves )
694{
695 const ecp_curve_info **crv = curves;
696 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
697
698 while( *crv != NULL )
699 {
700 if( (*crv)->grp_id == grp_id )
701 return( 1 );
702 crv++;
703 }
704
705 return( 0 );
706}
707#endif /* POLARSSL_ECDSA_C */
708
709/*
710 * Try picking a certificate for this ciphersuite,
711 * return 0 on success and -1 on failure.
712 */
713static int ssl_pick_cert( ssl_context *ssl,
714 const ssl_ciphersuite_t * ciphersuite_info )
715{
716 ssl_key_cert *cur, *list;
717 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
718
719#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
720 if( ssl->handshake->sni_key_cert != NULL )
721 list = ssl->handshake->sni_key_cert;
722 else
723#endif
724 list = ssl->handshake->key_cert;
725
726 if( pk_alg == POLARSSL_PK_NONE )
727 return( 0 );
728
729 for( cur = list; cur != NULL; cur = cur->next )
730 {
731 if( ! pk_can_do( cur->key, pk_alg ) )
732 continue;
733
734#if defined(POLARSSL_ECDSA_C)
735 if( pk_alg == POLARSSL_PK_ECDSA )
736 {
737 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
738 break;
739 }
740 else
741#endif
742 break;
743 }
744
745 if( cur == NULL )
746 return( -1 );
747
748 ssl->handshake->key_cert = cur;
749 return( 0 );
750}
751#endif /* POLARSSL_X509_CRT_PARSE_C */
752
753/*
754 * Check if a given ciphersuite is suitable for use with our config/keys/etc
755 * Sets ciphersuite_info only if the suite matches.
756 */
757static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
758 const ssl_ciphersuite_t **ciphersuite_info )
759{
760 const ssl_ciphersuite_t *suite_info;
761
762 suite_info = ssl_ciphersuite_from_id( suite_id );
763 if( suite_info == NULL )
764 {
765 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
766 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
767 }
768
769 if( suite_info->min_minor_ver > ssl->minor_ver ||
770 suite_info->max_minor_ver < ssl->minor_ver )
771 return( 0 );
772
773#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
774 if( ssl_ciphersuite_uses_ec( suite_info ) &&
775 ( ssl->handshake->curves == NULL ||
776 ssl->handshake->curves[0] == NULL ) )
777 return( 0 );
778#endif
779
780#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
781 /* If the ciphersuite requires a pre-shared key and we don't
782 * have one, skip it now rather than failing later */
783 if( ssl_ciphersuite_uses_psk( suite_info ) &&
784 ssl->f_psk == NULL &&
785 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
786 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
787 return( 0 );
788#endif
789
790#if defined(POLARSSL_X509_CRT_PARSE_C)
791 /*
792 * Final check: if ciphersuite requires us to have a
793 * certificate/key of a particular type:
794 * - select the appropriate certificate if we have one, or
795 * - try the next ciphersuite if we don't
796 * This must be done last since we modify the key_cert list.
797 */
798 if( ssl_pick_cert( ssl, suite_info ) != 0 )
799 return( 0 );
800#endif
801
802 *ciphersuite_info = suite_info;
803 return( 0 );
804}
805
Paul Bakker78a8c712013-03-06 17:01:52 +0100806#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
807static int ssl_parse_client_hello_v2( ssl_context *ssl )
808{
809 int ret;
810 unsigned int i, j;
811 size_t n;
812 unsigned int ciph_len, sess_len, chal_len;
813 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200814 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200815 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100816
817 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
818
819 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
820 {
821 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
822
823 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
824 return( ret );
825
826 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
827 }
828
829 buf = ssl->in_hdr;
830
831 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
832
833 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
834 buf[2] ) );
835 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
836 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
837 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
838 buf[3], buf[4] ) );
839
840 /*
841 * SSLv2 Client Hello
842 *
843 * Record layer:
844 * 0 . 1 message length
845 *
846 * SSL layer:
847 * 2 . 2 message type
848 * 3 . 4 protocol version
849 */
850 if( buf[2] != SSL_HS_CLIENT_HELLO ||
851 buf[3] != SSL_MAJOR_VERSION_3 )
852 {
853 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
854 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
855 }
856
857 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
858
859 if( n < 17 || n > 512 )
860 {
861 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
862 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
863 }
864
865 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200866 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
867 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100868
869 if( ssl->minor_ver < ssl->min_minor_ver )
870 {
871 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
872 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
873 ssl->min_major_ver, ssl->min_minor_ver ) );
874
875 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
876 SSL_ALERT_MSG_PROTOCOL_VERSION );
877 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
878 }
879
Paul Bakker2fbefde2013-06-29 16:01:15 +0200880 ssl->handshake->max_major_ver = buf[3];
881 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100882
883 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
884 {
885 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
886 return( ret );
887 }
888
889 ssl->handshake->update_checksum( ssl, buf + 2, n );
890
891 buf = ssl->in_msg;
892 n = ssl->in_left - 5;
893
894 /*
895 * 0 . 1 ciphersuitelist length
896 * 2 . 3 session id length
897 * 4 . 5 challenge length
898 * 6 . .. ciphersuitelist
899 * .. . .. session id
900 * .. . .. challenge
901 */
902 SSL_DEBUG_BUF( 4, "record contents", buf, n );
903
904 ciph_len = ( buf[0] << 8 ) | buf[1];
905 sess_len = ( buf[2] << 8 ) | buf[3];
906 chal_len = ( buf[4] << 8 ) | buf[5];
907
908 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
909 ciph_len, sess_len, chal_len ) );
910
911 /*
912 * Make sure each parameter length is valid
913 */
914 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
915 {
916 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
917 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
918 }
919
920 if( sess_len > 32 )
921 {
922 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
923 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
924 }
925
926 if( chal_len < 8 || chal_len > 32 )
927 {
928 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
929 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
930 }
931
932 if( n != 6 + ciph_len + sess_len + chal_len )
933 {
934 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
935 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
936 }
937
938 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
939 buf + 6, ciph_len );
940 SSL_DEBUG_BUF( 3, "client hello, session id",
941 buf + 6 + ciph_len, sess_len );
942 SSL_DEBUG_BUF( 3, "client hello, challenge",
943 buf + 6 + ciph_len + sess_len, chal_len );
944
945 p = buf + 6 + ciph_len;
946 ssl->session_negotiate->length = sess_len;
947 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
948 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
949
950 p += sess_len;
951 memset( ssl->handshake->randbytes, 0, 64 );
952 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
953
954 /*
955 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
956 */
957 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
958 {
959 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
960 {
961 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
962 if( ssl->renegotiation == SSL_RENEGOTIATION )
963 {
964 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
965
966 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
967 return( ret );
968
969 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
970 }
971 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
972 break;
973 }
974 }
975
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200976 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
977 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100978 {
979 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
980 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100981 // Only allow non-ECC ciphersuites as we do not have extensions
982 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200983 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200984 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
985 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200986 {
987 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
988
989 if( ciphersuite_info == NULL )
990 {
991 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
992 ciphersuites[i] ) );
993 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
994 }
995
Paul Bakker2fbefde2013-06-29 16:01:15 +0200996 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
997 ciphersuite_info->max_minor_ver < ssl->minor_ver )
998 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200999
Paul Bakker78a8c712013-03-06 17:01:52 +01001000 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +02001001 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001002 }
1003 }
1004
1005 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1006
1007 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1008
1009have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001010 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001011 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001012 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001013
1014 /*
1015 * SSLv2 Client Hello relevant renegotiation security checks
1016 */
1017 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1018 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1021
1022 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1023 return( ret );
1024
1025 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1026 }
1027
1028 ssl->in_left = 0;
1029 ssl->state++;
1030
1031 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1032
1033 return( 0 );
1034}
1035#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1036
Paul Bakker5121ce52009-01-03 21:22:43 +00001037static int ssl_parse_client_hello( ssl_context *ssl )
1038{
Paul Bakker23986e52011-04-24 08:57:21 +00001039 int ret;
1040 unsigned int i, j;
1041 size_t n;
1042 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001043 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001044 unsigned int ext_len = 0;
1045 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001046 int renegotiation_info_seen = 0;
1047 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001048 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001049 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001050
1051 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1052
Paul Bakker48916f92012-09-16 19:57:18 +00001053 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1054 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001055 {
1056 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1057 return( ret );
1058 }
1059
1060 buf = ssl->in_hdr;
1061
Paul Bakker78a8c712013-03-06 17:01:52 +01001062#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1063 if( ( buf[0] & 0x80 ) != 0 )
1064 return ssl_parse_client_hello_v2( ssl );
1065#endif
1066
Paul Bakkerec636f32012-09-09 19:17:02 +00001067 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1068
1069 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1070 buf[0] ) );
1071 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1072 ( buf[3] << 8 ) | buf[4] ) );
1073 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1074 buf[1], buf[2] ) );
1075
1076 /*
1077 * SSLv3 Client Hello
1078 *
1079 * Record layer:
1080 * 0 . 0 message type
1081 * 1 . 2 protocol version
1082 * 3 . 4 message length
1083 */
1084 if( buf[0] != SSL_MSG_HANDSHAKE ||
1085 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001086 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001087 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1088 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1089 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Paul Bakkerec636f32012-09-09 19:17:02 +00001091 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001092
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001093 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001094 {
1095 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1096 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1097 }
1098
Paul Bakker48916f92012-09-16 19:57:18 +00001099 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1100 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001101 {
1102 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1103 return( ret );
1104 }
1105
1106 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001107 if( !ssl->renegotiation )
1108 n = ssl->in_left - 5;
1109 else
1110 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001111
Paul Bakker48916f92012-09-16 19:57:18 +00001112 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001113
1114 /*
1115 * SSL layer:
1116 * 0 . 0 handshake type
1117 * 1 . 3 handshake length
1118 * 4 . 5 protocol version
1119 * 6 . 9 UNIX time()
1120 * 10 . 37 random bytes
1121 * 38 . 38 session id length
1122 * 39 . 38+x session id
1123 * 39+x . 40+x ciphersuitelist length
1124 * 41+x . .. ciphersuitelist
1125 * .. . .. compression alg.
1126 * .. . .. extensions
1127 */
1128 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1129
1130 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1131 buf[0] ) );
1132 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1133 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1134 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1135 buf[4], buf[5] ) );
1136
1137 /*
1138 * Check the handshake type and protocol version
1139 */
1140 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1141 buf[4] != SSL_MAJOR_VERSION_3 )
1142 {
1143 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1144 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1145 }
1146
1147 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001148 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1149 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001150
Paul Bakker1d29fb52012-09-28 13:28:45 +00001151 if( ssl->minor_ver < ssl->min_minor_ver )
1152 {
1153 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1154 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001155 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001156
1157 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1158 SSL_ALERT_MSG_PROTOCOL_VERSION );
1159
1160 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1161 }
1162
Paul Bakker2fbefde2013-06-29 16:01:15 +02001163 ssl->handshake->max_major_ver = buf[4];
1164 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001165
Paul Bakker48916f92012-09-16 19:57:18 +00001166 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001167
1168 /*
1169 * Check the handshake message length
1170 */
1171 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1172 {
1173 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1174 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1175 }
1176
1177 /*
1178 * Check the session length
1179 */
1180 sess_len = buf[38];
1181
1182 if( sess_len > 32 )
1183 {
1184 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1185 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1186 }
1187
Paul Bakker48916f92012-09-16 19:57:18 +00001188 ssl->session_negotiate->length = sess_len;
1189 memset( ssl->session_negotiate->id, 0,
1190 sizeof( ssl->session_negotiate->id ) );
1191 memcpy( ssl->session_negotiate->id, buf + 39,
1192 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001193
1194 /*
1195 * Check the ciphersuitelist length
1196 */
1197 ciph_len = ( buf[39 + sess_len] << 8 )
1198 | ( buf[40 + sess_len] );
1199
1200 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1201 {
1202 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1203 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1204 }
1205
1206 /*
1207 * Check the compression algorithms length
1208 */
1209 comp_len = buf[41 + sess_len + ciph_len];
1210
1211 if( comp_len < 1 || comp_len > 16 )
1212 {
1213 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1214 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1215 }
1216
Paul Bakker48916f92012-09-16 19:57:18 +00001217 /*
1218 * Check the extension length
1219 */
1220 if( n > 42 + sess_len + ciph_len + comp_len )
1221 {
1222 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1223 | ( buf[43 + sess_len + ciph_len + comp_len] );
1224
1225 if( ( ext_len > 0 && ext_len < 4 ) ||
1226 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1229 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1230 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1231 }
1232 }
1233
1234 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001235#if defined(POLARSSL_ZLIB_SUPPORT)
1236 for( i = 0; i < comp_len; ++i )
1237 {
Paul Bakker48916f92012-09-16 19:57:18 +00001238 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001239 {
Paul Bakker48916f92012-09-16 19:57:18 +00001240 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001241 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001242 }
1243 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001244#endif
1245
Paul Bakkerec636f32012-09-09 19:17:02 +00001246 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1247 buf + 6, 32 );
1248 SSL_DEBUG_BUF( 3, "client hello, session id",
1249 buf + 38, sess_len );
1250 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1251 buf + 41 + sess_len, ciph_len );
1252 SSL_DEBUG_BUF( 3, "client hello, compression",
1253 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Paul Bakkerec636f32012-09-09 19:17:02 +00001255 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001256 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1257 */
1258 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1259 {
1260 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1261 {
1262 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1263 if( ssl->renegotiation == SSL_RENEGOTIATION )
1264 {
1265 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001266
1267 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1268 return( ret );
1269
Paul Bakker48916f92012-09-16 19:57:18 +00001270 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1271 }
1272 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1273 break;
1274 }
1275 }
1276
Paul Bakker48916f92012-09-16 19:57:18 +00001277 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001278
1279 while( ext_len )
1280 {
1281 unsigned int ext_id = ( ( ext[0] << 8 )
1282 | ( ext[1] ) );
1283 unsigned int ext_size = ( ( ext[2] << 8 )
1284 | ( ext[3] ) );
1285
1286 if( ext_size + 4 > ext_len )
1287 {
1288 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1289 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1290 }
1291 switch( ext_id )
1292 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001293#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001294 case TLS_EXT_SERVERNAME:
1295 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1296 if( ssl->f_sni == NULL )
1297 break;
1298
1299 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1300 if( ret != 0 )
1301 return( ret );
1302 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001303#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001304
Paul Bakker48916f92012-09-16 19:57:18 +00001305 case TLS_EXT_RENEGOTIATION_INFO:
1306 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1307 renegotiation_info_seen = 1;
1308
Paul Bakker23f36802012-09-28 14:15:14 +00001309 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1310 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001311 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001312 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001313
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001314#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001315 case TLS_EXT_SIG_ALG:
1316 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1317 if( ssl->renegotiation == SSL_RENEGOTIATION )
1318 break;
1319
1320 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1321 if( ret != 0 )
1322 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001323 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001324#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001325
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001326#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001327 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1328 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1329
1330 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1331 if( ret != 0 )
1332 return( ret );
1333 break;
1334
1335 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1336 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001337 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001338
1339 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1340 if( ret != 0 )
1341 return( ret );
1342 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001343#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001344
Paul Bakker05decb22013-08-15 13:33:48 +02001345#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001346 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1347 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1348
1349 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1350 if( ret != 0 )
1351 return( ret );
1352 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001353#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001354
Paul Bakker1f2bc622013-08-15 13:45:55 +02001355#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001356 case TLS_EXT_TRUNCATED_HMAC:
1357 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1358
1359 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1360 if( ret != 0 )
1361 return( ret );
1362 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001363#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001364
Paul Bakkera503a632013-08-14 13:48:06 +02001365#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001366 case TLS_EXT_SESSION_TICKET:
1367 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1368
1369 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1370 if( ret != 0 )
1371 return( ret );
1372 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001373#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001374
Paul Bakker48916f92012-09-16 19:57:18 +00001375 default:
1376 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1377 ext_id ) );
1378 }
1379
1380 ext_len -= 4 + ext_size;
1381 ext += 4 + ext_size;
1382
1383 if( ext_len > 0 && ext_len < 4 )
1384 {
1385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1387 }
1388 }
1389
1390 /*
1391 * Renegotiation security checks
1392 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001393 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1394 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1395 {
1396 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1397 handshake_failure = 1;
1398 }
1399 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1400 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1401 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001402 {
1403 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001404 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001405 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001406 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1407 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1408 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001409 {
1410 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001411 handshake_failure = 1;
1412 }
1413 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1414 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1415 renegotiation_info_seen == 1 )
1416 {
1417 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1418 handshake_failure = 1;
1419 }
1420
1421 if( handshake_failure == 1 )
1422 {
1423 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1424 return( ret );
1425
Paul Bakker48916f92012-09-16 19:57:18 +00001426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1427 }
Paul Bakker380da532012-04-18 16:10:25 +00001428
Paul Bakker41c83d32013-03-20 14:39:14 +01001429 /*
1430 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001431 * (At the end because we need information from the EC-based extensions
1432 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001433 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001434 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001435 ciphersuite_info = NULL;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001436 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001437 {
1438 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1439 j += 2, p += 2 )
1440 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001441 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1442 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001443 {
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001444 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1445 &ciphersuite_info ) ) != 0 )
1446 return( ret );
Paul Bakker41c83d32013-03-20 14:39:14 +01001447
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001448 if( ciphersuite_info != NULL )
1449 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001450 }
1451 }
1452 }
1453
1454 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1455
1456 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1457 return( ret );
1458
1459 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1460
1461have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001462 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001463 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1464 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1465
Paul Bakker5121ce52009-01-03 21:22:43 +00001466 ssl->in_left = 0;
1467 ssl->state++;
1468
1469 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1470
1471 return( 0 );
1472}
1473
Paul Bakker1f2bc622013-08-15 13:45:55 +02001474#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001475static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1476 unsigned char *buf,
1477 size_t *olen )
1478{
1479 unsigned char *p = buf;
1480
1481 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1482 {
1483 *olen = 0;
1484 return;
1485 }
1486
1487 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1488
1489 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1490 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1491
1492 *p++ = 0x00;
1493 *p++ = 0x00;
1494
1495 *olen = 4;
1496}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001497#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001498
Paul Bakkera503a632013-08-14 13:48:06 +02001499#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001500static void ssl_write_session_ticket_ext( ssl_context *ssl,
1501 unsigned char *buf,
1502 size_t *olen )
1503{
1504 unsigned char *p = buf;
1505
1506 if( ssl->handshake->new_session_ticket == 0 )
1507 {
1508 *olen = 0;
1509 return;
1510 }
1511
1512 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1513
1514 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1515 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1516
1517 *p++ = 0x00;
1518 *p++ = 0x00;
1519
1520 *olen = 4;
1521}
Paul Bakkera503a632013-08-14 13:48:06 +02001522#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001523
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001524static void ssl_write_renegotiation_ext( ssl_context *ssl,
1525 unsigned char *buf,
1526 size_t *olen )
1527{
1528 unsigned char *p = buf;
1529
1530 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1531 {
1532 *olen = 0;
1533 return;
1534 }
1535
1536 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1537
1538 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1539 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1540
1541 *p++ = 0x00;
1542 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1543 *p++ = ssl->verify_data_len * 2 & 0xFF;
1544
1545 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1546 p += ssl->verify_data_len;
1547 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1548 p += ssl->verify_data_len;
1549
1550 *olen = 5 + ssl->verify_data_len * 2;
1551}
1552
Paul Bakker05decb22013-08-15 13:33:48 +02001553#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001554static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1555 unsigned char *buf,
1556 size_t *olen )
1557{
1558 unsigned char *p = buf;
1559
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001560 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1561 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001562 *olen = 0;
1563 return;
1564 }
1565
1566 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1567
1568 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1569 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1570
1571 *p++ = 0x00;
1572 *p++ = 1;
1573
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001574 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001575
1576 *olen = 5;
1577}
Paul Bakker05decb22013-08-15 13:33:48 +02001578#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001579
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001580#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001581static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1582 unsigned char *buf,
1583 size_t *olen )
1584{
1585 unsigned char *p = buf;
1586 ((void) ssl);
1587
Paul Bakker677377f2013-10-28 12:54:26 +01001588 if( ( ssl->handshake->cli_exts &
1589 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1590 {
1591 *olen = 0;
1592 return;
1593 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001594
1595 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1596
1597 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1598 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1599
1600 *p++ = 0x00;
1601 *p++ = 2;
1602
1603 *p++ = 1;
1604 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1605
1606 *olen = 6;
1607}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001608#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001609
Paul Bakker5121ce52009-01-03 21:22:43 +00001610static int ssl_write_server_hello( ssl_context *ssl )
1611{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001612#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001614#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001615 int ret;
1616 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 unsigned char *buf, *p;
1618
1619 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1620
Paul Bakkera9a028e2013-11-21 17:31:06 +01001621 if( ssl->f_rng == NULL )
1622 {
1623 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1624 return( POLARSSL_ERR_SSL_NO_RNG );
1625 }
1626
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 /*
1628 * 0 . 0 handshake type
1629 * 1 . 3 handshake length
1630 * 4 . 5 protocol version
1631 * 6 . 9 UNIX time()
1632 * 10 . 37 random bytes
1633 */
1634 buf = ssl->out_msg;
1635 p = buf + 4;
1636
1637 *p++ = (unsigned char) ssl->major_ver;
1638 *p++ = (unsigned char) ssl->minor_ver;
1639
1640 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1641 buf[4], buf[5] ) );
1642
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001643#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 t = time( NULL );
1645 *p++ = (unsigned char)( t >> 24 );
1646 *p++ = (unsigned char)( t >> 16 );
1647 *p++ = (unsigned char)( t >> 8 );
1648 *p++ = (unsigned char)( t );
1649
1650 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001651#else
1652 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1653 return( ret );
1654
1655 p += 4;
1656#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Paul Bakkera3d195c2011-11-27 21:07:34 +00001658 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1659 return( ret );
1660
1661 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Paul Bakker48916f92012-09-16 19:57:18 +00001663 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
1665 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1666
1667 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001668 * Resume is 0 by default, see ssl_handshake_init().
1669 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1670 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001672 if( ssl->handshake->resume == 0 &&
1673 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001674 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001675 ssl->f_get_cache != NULL &&
1676 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1677 {
1678 ssl->handshake->resume = 1;
1679 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001680
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001681 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 {
1683 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001684 * New session, create a new session id,
1685 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001686 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 ssl->state++;
1688
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001689#if defined(POLARSSL_HAVE_TIME)
1690 ssl->session_negotiate->start = time( NULL );
1691#endif
1692
Paul Bakkera503a632013-08-14 13:48:06 +02001693#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001694 if( ssl->handshake->new_session_ticket != 0 )
1695 {
1696 ssl->session_negotiate->length = n = 0;
1697 memset( ssl->session_negotiate->id, 0, 32 );
1698 }
1699 else
1700#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001701 {
1702 ssl->session_negotiate->length = n = 32;
1703 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001704 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001705 return( ret );
1706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001707 }
1708 else
1709 {
1710 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001711 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001712 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001713 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001714 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001715
1716 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1717 {
1718 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1719 return( ret );
1720 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001721 }
1722
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001723 /*
1724 * 38 . 38 session id length
1725 * 39 . 38+n session id
1726 * 39+n . 40+n chosen ciphersuite
1727 * 41+n . 41+n chosen compression alg.
1728 * 42+n . 43+n extensions length
1729 * 44+n . 43+n+m extensions
1730 */
1731 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001732 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1733 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
1735 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1736 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1737 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001738 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Paul Bakker48916f92012-09-16 19:57:18 +00001740 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1741 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1742 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001744 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1745 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001746 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001747 ssl->session_negotiate->compression ) );
1748
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001749 /*
1750 * First write extensions, then the total length
1751 */
1752 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1753 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001754
Paul Bakker05decb22013-08-15 13:33:48 +02001755#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001756 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1757 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001758#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001759
Paul Bakker1f2bc622013-08-15 13:45:55 +02001760#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001761 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1762 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001763#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001764
Paul Bakkera503a632013-08-14 13:48:06 +02001765#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001766 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1767 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001768#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001769
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001770#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001771 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1772 ext_len += olen;
1773#endif
1774
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001775 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001776
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001777 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1778 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1779 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
1781 ssl->out_msglen = p - buf;
1782 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1783 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1784
1785 ret = ssl_write_record( ssl );
1786
1787 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1788
1789 return( ret );
1790}
1791
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001792#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1793 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001794 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1795 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001796static int ssl_write_certificate_request( ssl_context *ssl )
1797{
Paul Bakkered27a042013-04-18 22:46:23 +02001798 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1799 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001800
1801 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1802
1803 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001804 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001805 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1806 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001807 {
1808 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1809 ssl->state++;
1810 return( 0 );
1811 }
1812
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001813 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001814 return( ret );
1815}
1816#else
1817static int ssl_write_certificate_request( ssl_context *ssl )
1818{
1819 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1820 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001821 size_t dn_size, total_dn_size; /* excluding length bytes */
1822 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001824 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
1826 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1827
1828 ssl->state++;
1829
Paul Bakkerfbb17802013-04-17 19:10:21 +02001830 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001831 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001832 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001833 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001834 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001836 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 return( 0 );
1838 }
1839
1840 /*
1841 * 0 . 0 handshake type
1842 * 1 . 3 handshake length
1843 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001844 * 5 .. m-1 cert types
1845 * m .. m+1 sig alg length (TLS 1.2 only)
1846 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 * n .. n+1 length of all DNs
1848 * n+2 .. n+3 length of DN 1
1849 * n+4 .. ... Distinguished Name #1
1850 * ... .. ... length of DN 2, etc.
1851 */
1852 buf = ssl->out_msg;
1853 p = buf + 4;
1854
1855 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001856 * Supported certificate types
1857 *
1858 * ClientCertificateType certificate_types<1..2^8-1>;
1859 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001861 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001862
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001863#if defined(POLARSSL_RSA_C)
1864 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1865#endif
1866#if defined(POLARSSL_ECDSA_C)
1867 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1868#endif
1869
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001870 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001871 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001872
Paul Bakker577e0062013-08-28 11:57:20 +02001873 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001874#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001875 /*
1876 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001877 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001878 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1879 *
1880 * struct {
1881 * HashAlgorithm hash;
1882 * SignatureAlgorithm signature;
1883 * } SignatureAndHashAlgorithm;
1884 *
1885 * enum { (255) } HashAlgorithm;
1886 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001887 */
Paul Bakker21dca692013-01-03 11:41:08 +01001888 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001889 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001890 /*
1891 * Only use current running hash algorithm that is already required
1892 * for requested ciphersuite.
1893 */
Paul Bakker926af752012-11-23 13:38:07 +01001894 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1895
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001896 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1897 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001898 {
1899 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1900 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001901
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001902 /*
1903 * Supported signature algorithms
1904 */
1905#if defined(POLARSSL_RSA_C)
1906 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1907 p[2 + sa_len++] = SSL_SIG_RSA;
1908#endif
1909#if defined(POLARSSL_ECDSA_C)
1910 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1911 p[2 + sa_len++] = SSL_SIG_ECDSA;
1912#endif
Paul Bakker926af752012-11-23 13:38:07 +01001913
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001914 p[0] = (unsigned char)( sa_len >> 8 );
1915 p[1] = (unsigned char)( sa_len );
1916 sa_len += 2;
1917 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001918 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001919#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001921 /*
1922 * DistinguishedName certificate_authorities<0..2^16-1>;
1923 * opaque DistinguishedName<1..2^16-1>;
1924 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 p += 2;
1926 crt = ssl->ca_chain;
1927
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001928 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001929 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001930 {
1931 if( p - buf > 4096 )
1932 break;
1933
Paul Bakker926af752012-11-23 13:38:07 +01001934 dn_size = crt->subject_raw.len;
1935 *p++ = (unsigned char)( dn_size >> 8 );
1936 *p++ = (unsigned char)( dn_size );
1937 memcpy( p, crt->subject_raw.p, dn_size );
1938 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
Paul Bakker926af752012-11-23 13:38:07 +01001940 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1941
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001942 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001943 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 }
1945
Paul Bakker926af752012-11-23 13:38:07 +01001946 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001947 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1948 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001949 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1950 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
1952 ret = ssl_write_record( ssl );
1953
1954 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1955
1956 return( ret );
1957}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001958#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1959 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01001960 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
1961 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001962
Paul Bakker41c83d32013-03-20 14:39:14 +01001963static int ssl_write_server_key_exchange( ssl_context *ssl )
1964{
Paul Bakker23986e52011-04-24 08:57:21 +00001965 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001966 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001967 const ssl_ciphersuite_t *ciphersuite_info =
1968 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001969
1970#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1971 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1972 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001973 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001974 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001975 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001976 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001977 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001978 ((void) dig_signed);
1979 ((void) dig_signed_len);
1980#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001981
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1983
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001984 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1985 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1986 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 {
1988 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1989 ssl->state++;
1990 return( 0 );
1991 }
1992
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001993#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1994 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1995 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1996 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001997 {
1998 /* TODO: Support identity hints */
1999 *(p++) = 0x00;
2000 *(p++) = 0x00;
2001
2002 n += 2;
2003 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002004#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2005 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002006
2007#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2008 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2009 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2010 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002011 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002012 /*
2013 * Ephemeral DH parameters:
2014 *
2015 * struct {
2016 * opaque dh_p<1..2^16-1>;
2017 * opaque dh_g<1..2^16-1>;
2018 * opaque dh_Ys<1..2^16-1>;
2019 * } ServerDHParams;
2020 */
2021 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2022 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2023 {
2024 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2025 return( ret );
2026 }
Paul Bakker48916f92012-09-16 19:57:18 +00002027
Paul Bakker41c83d32013-03-20 14:39:14 +01002028 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002029 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002030 p,
2031 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002032 {
2033 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2034 return( ret );
2035 }
2036
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002037 dig_signed = p;
2038 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002039
2040 p += len;
2041 n += len;
2042
Paul Bakker41c83d32013-03-20 14:39:14 +01002043 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2044 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2045 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2046 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2047 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002048#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2049 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002050
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002051#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002052 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2053 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2054
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002055 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002056 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2057 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002059 /*
2060 * Ephemeral ECDH parameters:
2061 *
2062 * struct {
2063 * ECParameters curve_params;
2064 * ECPoint public;
2065 * } ServerECDHParams;
2066 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002067 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002068 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002069 {
2070 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2071 return( ret );
2072 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002073
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002074 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2075 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2076
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002077 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2078 p, SSL_MAX_CONTENT_LEN - n,
2079 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002080 {
2081 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2082 return( ret );
2083 }
2084
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002085 dig_signed = p;
2086 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002087
2088 p += len;
2089 n += len;
2090
Paul Bakker41c83d32013-03-20 14:39:14 +01002091 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2092 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002093#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002094 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2095 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002096
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002097#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002098 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2099 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002100 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002101 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2102 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002103 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002104 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002105 unsigned int hashlen = 0;
2106 unsigned char hash[64];
2107 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002108
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002109 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002110 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2111 */
Paul Bakker577e0062013-08-28 11:57:20 +02002112#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002113 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2114 {
2115 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2116
2117 if( md_alg == POLARSSL_MD_NONE )
2118 {
2119 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2120 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2121 }
2122 }
Paul Bakker577e0062013-08-28 11:57:20 +02002123 else
2124#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002125#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2126 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002127 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002128 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2129 {
2130 md_alg = POLARSSL_MD_SHA1;
2131 }
2132 else
Paul Bakker577e0062013-08-28 11:57:20 +02002133#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002134 {
2135 md_alg = POLARSSL_MD_NONE;
2136 }
2137
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002138 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002139 * Compute the hash to be signed
2140 */
Paul Bakker577e0062013-08-28 11:57:20 +02002141#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2142 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002143 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002144 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145 md5_context md5;
2146 sha1_context sha1;
2147
2148 /*
2149 * digitally-signed struct {
2150 * opaque md5_hash[16];
2151 * opaque sha_hash[20];
2152 * };
2153 *
2154 * md5_hash
2155 * MD5(ClientHello.random + ServerHello.random
2156 * + ServerParams);
2157 * sha_hash
2158 * SHA(ClientHello.random + ServerHello.random
2159 * + ServerParams);
2160 */
2161 md5_starts( &md5 );
2162 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002163 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002164 md5_finish( &md5, hash );
2165
2166 sha1_starts( &sha1 );
2167 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002168 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002169 sha1_finish( &sha1, hash + 16 );
2170
2171 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002172 }
2173 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002174#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2175 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002176#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2177 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002178 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179 {
2180 md_context_t ctx;
2181
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002182 /* Info from md_alg will be used instead */
2183 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184
2185 /*
2186 * digitally-signed struct {
2187 * opaque client_random[32];
2188 * opaque server_random[32];
2189 * ServerDHParams params;
2190 * };
2191 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002192 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002193 {
2194 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2195 return( ret );
2196 }
2197
2198 md_starts( &ctx );
2199 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002200 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002201 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002202
2203 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2204 {
2205 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2206 return( ret );
2207 }
2208
Paul Bakker23f36802012-09-28 14:15:14 +00002209 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002210 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002211#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2212 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002213 {
2214 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002215 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002216 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002217
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002218 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2219 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002220
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002221 /*
2222 * Make the signature
2223 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002224 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002225 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002226 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2227 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002228 }
Paul Bakker23f36802012-09-28 14:15:14 +00002229
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002230#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002231 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2232 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002233 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002234 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002235
2236 n += 2;
2237 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002238#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002239
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002240 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002241 p + 2 , &signature_len,
2242 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002243 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002244 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002245 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002246 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002247
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002248 *(p++) = (unsigned char)( signature_len >> 8 );
2249 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002250 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002251
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002252 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002253
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002254 p += signature_len;
2255 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002256 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002257#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002258 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2259 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002260
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002261 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002262 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2263 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2264
2265 ssl->state++;
2266
2267 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2268 {
2269 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2270 return( ret );
2271 }
2272
2273 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2274
2275 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002276}
2277
2278static int ssl_write_server_hello_done( ssl_context *ssl )
2279{
2280 int ret;
2281
2282 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2283
2284 ssl->out_msglen = 4;
2285 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2286 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2287
2288 ssl->state++;
2289
2290 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2291 {
2292 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2293 return( ret );
2294 }
2295
2296 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2297
2298 return( 0 );
2299}
2300
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002301#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2302 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2303static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2304 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002305{
2306 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002307 size_t n;
2308
2309 /*
2310 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2311 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002312 if( *p + 2 > end )
2313 {
2314 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2315 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2316 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002317
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002318 n = ( (*p)[0] << 8 ) | (*p)[1];
2319 *p += 2;
2320
2321 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002322 {
2323 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2324 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2325 }
2326
2327 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002328 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002329 {
2330 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2331 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2332 }
2333
2334 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2335
Paul Bakker70df2fb2013-04-17 17:19:09 +02002336 return( ret );
2337}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002338#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2339 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002340
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002341#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2342 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002343static int ssl_parse_encrypted_pms( ssl_context *ssl,
2344 const unsigned char *p,
2345 const unsigned char *end,
2346 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002347{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002348 int ret;
2349 size_t len = pk_get_len( ssl_own_key( ssl ) );
2350 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002351
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002352 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002353 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002354 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002355 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2356 }
2357
2358 /*
2359 * Decrypt the premaster using own private RSA key
2360 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002361#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2362 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002363 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2364 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002365 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2366 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002367 {
2368 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2369 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2370 }
2371 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002372#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002373
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002374 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002375 {
2376 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2377 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2378 }
2379
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002380 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2381 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002382 sizeof(ssl->handshake->premaster),
2383 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002384
2385 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002386 pms[0] != ssl->handshake->max_major_ver ||
2387 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002388 {
2389 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2390
2391 /*
2392 * Protection against Bleichenbacher's attack:
2393 * invalid PKCS#1 v1.5 padding must not cause
2394 * the connection to end immediately; instead,
2395 * send a bad_record_mac later in the handshake.
2396 */
2397 ssl->handshake->pmslen = 48;
2398
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002399 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002400 if( ret != 0 )
2401 return( ret );
2402 }
2403
2404 return( ret );
2405}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002406#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2407 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002408
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002409#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2411 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002412{
Paul Bakker6db455e2013-09-18 17:29:31 +02002413 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002414 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002415
Paul Bakker6db455e2013-09-18 17:29:31 +02002416 if( ssl->f_psk == NULL &&
2417 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2418 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002419 {
2420 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2421 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2422 }
2423
2424 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002426 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427 if( *p + 2 > end )
2428 {
2429 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2430 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2431 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002432
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002433 n = ( (*p)[0] << 8 ) | (*p)[1];
2434 *p += 2;
2435
2436 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002437 {
2438 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2440 }
2441
Paul Bakker6db455e2013-09-18 17:29:31 +02002442 if( ssl->f_psk != NULL )
2443 {
2444 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2445 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2446 }
2447
2448 if( ret == 0 )
2449 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002450 /* Identity is not a big secret since clients send it in the clear,
2451 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002452 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002453 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002454 {
2455 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2456 }
2457 }
2458
2459 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002460 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002462 if( ( ret = ssl_send_alert_message( ssl,
2463 SSL_ALERT_LEVEL_FATAL,
2464 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2465 {
2466 return( ret );
2467 }
2468
2469 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002470 }
2471
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002472 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002473 ret = 0;
2474
Paul Bakkerfbb17802013-04-17 19:10:21 +02002475 return( ret );
2476}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002477#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002478
Paul Bakker5121ce52009-01-03 21:22:43 +00002479static int ssl_parse_client_key_exchange( ssl_context *ssl )
2480{
Paul Bakker23986e52011-04-24 08:57:21 +00002481 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002482 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002483
Paul Bakker41c83d32013-03-20 14:39:14 +01002484 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002485
2486 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2487
2488 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2489 {
2490 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2491 return( ret );
2492 }
2493
2494 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2495 {
2496 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002497 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002498 }
2499
2500 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2501 {
2502 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002503 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
2505
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002506#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002507 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002509 unsigned char *p = ssl->in_msg + 4;
2510 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2511
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002512 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002513 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002514 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2515 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517
2518 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2519
2520 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2521 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002522 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002523 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002524 {
2525 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2527 }
2528
2529 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531 else
2532#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002533#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2534 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2535 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2536 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002537 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002538 size_t n = ssl->in_msg[3];
2539
2540 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2541 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002542 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002543 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2544 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002546
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002547 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2548 ssl->in_msg + 4, n ) ) != 0 )
2549 {
2550 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2551 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2552 }
2553
2554 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2555
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002556 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2557 &ssl->handshake->pmslen,
2558 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002559 POLARSSL_MPI_MAX_SIZE,
2560 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561 {
2562 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2563 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2564 }
2565
2566 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002567 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002569#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2570 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002571#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2572 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002573 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002574 unsigned char *p = ssl->in_msg + 4;
2575 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2576
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002577 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002578 {
2579 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2580 return( ret );
2581 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002582
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002583 if( ( ret = ssl_psk_derive_premaster( ssl,
2584 ciphersuite_info->key_exchange ) ) != 0 )
2585 {
2586 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2587 return( ret );
2588 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002590 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002591#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002592#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2593 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2594 {
2595 unsigned char *p = ssl->in_msg + 4;
2596 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2597
2598 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2599 {
2600 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2601 return( ret );
2602 }
2603
2604 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2605 {
2606 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2607 return( ret );
2608 }
2609
2610 if( ( ret = ssl_psk_derive_premaster( ssl,
2611 ciphersuite_info->key_exchange ) ) != 0 )
2612 {
2613 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2614 return( ret );
2615 }
2616 }
2617 else
2618#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2620 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2621 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002622 unsigned char *p = ssl->in_msg + 4;
2623 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002624
2625 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2626 {
2627 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2628 return( ret );
2629 }
2630 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2631 {
2632 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2633 return( ret );
2634 }
2635
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002636 if( ( ret = ssl_psk_derive_premaster( ssl,
2637 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002638 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002639 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2640 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 }
2643 else
2644#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002645#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2646 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2647 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002648 unsigned char *p = ssl->in_msg + 4;
2649 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2650
2651 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2652 {
2653 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2654 return( ret );
2655 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002656
2657 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2658 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002659 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002660 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2661 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002662 }
2663
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002664 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2665
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002666 if( ( ret = ssl_psk_derive_premaster( ssl,
2667 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002668 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002669 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002670 return( ret );
2671 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002672 }
2673 else
2674#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002675#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2676 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002677 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002678 if( ( ret = ssl_parse_encrypted_pms( ssl,
2679 ssl->in_msg + 4,
2680 ssl->in_msg + ssl->in_msglen,
2681 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002682 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002683 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002684 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
2686 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002687 else
2688#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2689 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002690 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002691 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2692 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
Paul Bakkerff60ee62010-03-16 21:09:09 +00002694 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2695 {
2696 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2697 return( ret );
2698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699
Paul Bakker5121ce52009-01-03 21:22:43 +00002700 ssl->state++;
2701
2702 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2703
2704 return( 0 );
2705}
2706
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002707#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2708 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002709 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2710 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002711static int ssl_parse_certificate_verify( ssl_context *ssl )
2712{
Paul Bakkered27a042013-04-18 22:46:23 +02002713 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002714 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002715
2716 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2717
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002718 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002719 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002720 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002721 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002722 {
2723 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2724 ssl->state++;
2725 return( 0 );
2726 }
2727
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002728 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002729 return( ret );
2730}
2731#else
2732static int ssl_parse_certificate_verify( ssl_context *ssl )
2733{
2734 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002735 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002736 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002737 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002738 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002739#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002740 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002741#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002742 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002743 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2744
2745 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2746
2747 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002748 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002749 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002750 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2751 {
2752 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2753 ssl->state++;
2754 return( 0 );
2755 }
2756
Paul Bakkered27a042013-04-18 22:46:23 +02002757 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002758 {
2759 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2760 ssl->state++;
2761 return( 0 );
2762 }
2763
Paul Bakker48916f92012-09-16 19:57:18 +00002764 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002765
2766 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2767 {
2768 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2769 return( ret );
2770 }
2771
2772 ssl->state++;
2773
2774 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002777 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 }
2779
2780 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2781 {
2782 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002783 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002784 }
2785
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002786 /*
2787 * 0 . 0 handshake type
2788 * 1 . 3 handshake length
2789 * 4 . 5 sig alg (TLS 1.2 only)
2790 * 4+n . 5+n signature length (n = sa_len)
2791 * 6+n . 6+n+m signature (m = sig_len)
2792 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002793
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002794#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2795 defined(POLARSSL_SSL_PROTO_TLS1_1)
2796 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002797 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002798 sa_len = 0;
2799
Paul Bakkerc70b9822013-04-07 22:00:46 +02002800 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002801 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002802
2803 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2804 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2805 POLARSSL_PK_ECDSA ) )
2806 {
2807 hash_start += 16;
2808 hashlen -= 16;
2809 md_alg = POLARSSL_MD_SHA1;
2810 }
Paul Bakker926af752012-11-23 13:38:07 +01002811 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002812 else
2813#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002814#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2815 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002816 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002817 sa_len = 2;
2818
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002820 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002822 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002824 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2825 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002826 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2827 }
2828
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002829 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002830
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002831 /* Info from md_alg will be used instead */
2832 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002833
2834 /*
2835 * Signature
2836 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002837 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2838 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002839 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002840 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2841 " for verify message" ) );
2842 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002843 }
2844
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002845 /*
2846 * Check the certificate's key type matches the signature alg
2847 */
2848 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2849 {
2850 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2851 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2852 }
Paul Bakker577e0062013-08-28 11:57:20 +02002853 }
2854 else
2855#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2856 {
2857 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002858 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002859 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002860
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002861 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002862
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002863 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002864 {
2865 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002866 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002867 }
2868
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002869 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002870 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002871 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002873 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002874 return( ret );
2875 }
2876
2877 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2878
Paul Bakkered27a042013-04-18 22:46:23 +02002879 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002881#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2882 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2883 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002884
Paul Bakkera503a632013-08-14 13:48:06 +02002885#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002886static int ssl_write_new_session_ticket( ssl_context *ssl )
2887{
2888 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002889 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002890 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002891
2892 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2893
2894 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2895 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2896
2897 /*
2898 * struct {
2899 * uint32 ticket_lifetime_hint;
2900 * opaque ticket<0..2^16-1>;
2901 * } NewSessionTicket;
2902 *
2903 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2904 * 8 . 9 ticket_len (n)
2905 * 10 . 9+n ticket content
2906 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002907
2908 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2909 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2910 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2911 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002912
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002913 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2914 {
2915 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2916 tlen = 0;
2917 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002918
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002919 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2920 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002921
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002922 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002923
2924 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2925 {
2926 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2927 return( ret );
2928 }
2929
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002930 /* No need to remember writing a NewSessionTicket any more */
2931 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002932
2933 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2934
2935 return( 0 );
2936}
Paul Bakkera503a632013-08-14 13:48:06 +02002937#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002938
Paul Bakker5121ce52009-01-03 21:22:43 +00002939/*
Paul Bakker1961b702013-01-25 14:49:24 +01002940 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 */
Paul Bakker1961b702013-01-25 14:49:24 +01002942int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002943{
2944 int ret = 0;
2945
Paul Bakker1961b702013-01-25 14:49:24 +01002946 if( ssl->state == SSL_HANDSHAKE_OVER )
2947 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
Paul Bakker1961b702013-01-25 14:49:24 +01002949 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2950
2951 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2952 return( ret );
2953
2954 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 {
Paul Bakker1961b702013-01-25 14:49:24 +01002956 case SSL_HELLO_REQUEST:
2957 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 break;
2959
Paul Bakker1961b702013-01-25 14:49:24 +01002960 /*
2961 * <== ClientHello
2962 */
2963 case SSL_CLIENT_HELLO:
2964 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002965 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002966
2967 /*
2968 * ==> ServerHello
2969 * Certificate
2970 * ( ServerKeyExchange )
2971 * ( CertificateRequest )
2972 * ServerHelloDone
2973 */
2974 case SSL_SERVER_HELLO:
2975 ret = ssl_write_server_hello( ssl );
2976 break;
2977
2978 case SSL_SERVER_CERTIFICATE:
2979 ret = ssl_write_certificate( ssl );
2980 break;
2981
2982 case SSL_SERVER_KEY_EXCHANGE:
2983 ret = ssl_write_server_key_exchange( ssl );
2984 break;
2985
2986 case SSL_CERTIFICATE_REQUEST:
2987 ret = ssl_write_certificate_request( ssl );
2988 break;
2989
2990 case SSL_SERVER_HELLO_DONE:
2991 ret = ssl_write_server_hello_done( ssl );
2992 break;
2993
2994 /*
2995 * <== ( Certificate/Alert )
2996 * ClientKeyExchange
2997 * ( CertificateVerify )
2998 * ChangeCipherSpec
2999 * Finished
3000 */
3001 case SSL_CLIENT_CERTIFICATE:
3002 ret = ssl_parse_certificate( ssl );
3003 break;
3004
3005 case SSL_CLIENT_KEY_EXCHANGE:
3006 ret = ssl_parse_client_key_exchange( ssl );
3007 break;
3008
3009 case SSL_CERTIFICATE_VERIFY:
3010 ret = ssl_parse_certificate_verify( ssl );
3011 break;
3012
3013 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3014 ret = ssl_parse_change_cipher_spec( ssl );
3015 break;
3016
3017 case SSL_CLIENT_FINISHED:
3018 ret = ssl_parse_finished( ssl );
3019 break;
3020
3021 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003022 * ==> ( NewSessionTicket )
3023 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003024 * Finished
3025 */
3026 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003027#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003028 if( ssl->handshake->new_session_ticket != 0 )
3029 ret = ssl_write_new_session_ticket( ssl );
3030 else
Paul Bakkera503a632013-08-14 13:48:06 +02003031#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003032 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003033 break;
3034
3035 case SSL_SERVER_FINISHED:
3036 ret = ssl_write_finished( ssl );
3037 break;
3038
3039 case SSL_FLUSH_BUFFERS:
3040 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3041 ssl->state = SSL_HANDSHAKE_WRAPUP;
3042 break;
3043
3044 case SSL_HANDSHAKE_WRAPUP:
3045 ssl_handshake_wrapup( ssl );
3046 break;
3047
3048 default:
3049 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3050 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003051 }
3052
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 return( ret );
3054}
Paul Bakker5121ce52009-01-03 21:22:43 +00003055#endif