blob: 7d81fc90c4560ba5c692775fc57d16b251908536 [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;
257 unsigned char pad_len;
258
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é-Gonnard990c51a2013-08-03 15:37:58 +0200270 /* Check name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( memcmp( key_name, ssl->ticket_keys->key_name, 16 ) != 0 )
272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200273
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200274 /* Check mac */
275 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
276 computed_mac, 0 );
277 ret = 0;
278 for( i = 0; i < 32; i++ )
279 if( mac[i] != computed_mac[i] )
280 ret = POLARSSL_ERR_SSL_INVALID_MAC;
281 if( ret != 0 )
282 return( ret );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200284 /* Decrypt */
285 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
286 enc_len, iv, ticket, ticket ) ) != 0 )
287 {
288 return( ret );
289 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200290
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200291 /* Check PKCS padding */
292 pad_len = ticket[enc_len - 1];
293
294 ret = 0;
295 for( i = 2; i < pad_len; i++ )
296 if( ticket[enc_len - i] != pad_len )
297 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
298 if( ret != 0 )
299 return( ret );
300
301 clear_len = enc_len - pad_len;
302
303 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
304
305 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200306 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
307 {
308 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
309 memset( &session, 0, sizeof( ssl_session ) );
310 return( ret );
311 }
312
Paul Bakker606b4ba2013-08-14 16:52:14 +0200313#if defined(POLARSSL_HAVE_TIME)
314 /* Check if still valid */
315 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
316 {
317 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
318 memset( &session, 0, sizeof( ssl_session ) );
319 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
320 }
321#endif
322
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200323 /*
324 * Keep the session ID sent by the client, since we MUST send it back to
325 * inform him we're accepting the ticket (RFC 5077 section 3.4)
326 */
327 session.length = ssl->session_negotiate->length;
328 memcpy( &session.id, ssl->session_negotiate->id, session.length );
329
330 ssl_session_free( ssl->session_negotiate );
331 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
332 memset( &session, 0, sizeof( ssl_session ) );
333
334 return( 0 );
335}
Paul Bakkera503a632013-08-14 13:48:06 +0200336#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337
Paul Bakker0be444a2013-08-27 21:55:01 +0200338#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200339/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200340 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
341 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200342 */
343static int ssl_sni_wrapper( ssl_context *ssl,
344 const unsigned char* name, size_t len )
345{
346 int ret;
347 ssl_key_cert *key_cert_ori = ssl->key_cert;
348
349 ssl->key_cert = NULL;
350 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200351 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200352
353 ssl->key_cert = key_cert_ori;
354
355 return( ret );
356}
357
Paul Bakker5701cdc2012-09-27 21:49:42 +0000358static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000359 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000360 size_t len )
361{
362 int ret;
363 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000364 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000365
366 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
367 if( servername_list_size + 2 != len )
368 {
369 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
370 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
371 }
372
373 p = buf + 2;
374 while( servername_list_size > 0 )
375 {
376 hostname_len = ( ( p[1] << 8 ) | p[2] );
377 if( hostname_len + 3 > servername_list_size )
378 {
379 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
380 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
381 }
382
383 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
384 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000386 if( ret != 0 )
387 {
388 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
389 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
390 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
391 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000392 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000393 }
394
395 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000396 p += hostname_len + 3;
397 }
398
399 if( servername_list_size != 0 )
400 {
401 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
402 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000403 }
404
405 return( 0 );
406}
Paul Bakker0be444a2013-08-27 21:55:01 +0200407#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000408
Paul Bakker48916f92012-09-16 19:57:18 +0000409static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000410 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000411 size_t len )
412{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000413 int ret;
414
Paul Bakker48916f92012-09-16 19:57:18 +0000415 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
416 {
417 if( len != 1 || buf[0] != 0x0 )
418 {
419 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000420
421 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
422 return( ret );
423
Paul Bakker48916f92012-09-16 19:57:18 +0000424 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
425 }
426
427 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
428 }
429 else
430 {
431 if( len != 1 + ssl->verify_data_len ||
432 buf[0] != ssl->verify_data_len ||
433 memcmp( buf + 1, ssl->peer_verify_data, ssl->verify_data_len ) != 0 )
434 {
435 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000436
437 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
438 return( ret );
439
Paul Bakker48916f92012-09-16 19:57:18 +0000440 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
441 }
442 }
443
444 return( 0 );
445}
446
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200447#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000448static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
449 const unsigned char *buf,
450 size_t len )
451{
452 size_t sig_alg_list_size;
453 const unsigned char *p;
454
455 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
456 if( sig_alg_list_size + 2 != len ||
457 sig_alg_list_size %2 != 0 )
458 {
459 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
460 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
461 }
462
463 p = buf + 2;
464 while( sig_alg_list_size > 0 )
465 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200466 /*
467 * For now, just ignore signature algorithm and rely on offered
468 * ciphersuites only. To be fixed later.
469 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200470#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000471 if( p[0] == SSL_HASH_SHA512 )
472 {
473 ssl->handshake->sig_alg = SSL_HASH_SHA512;
474 break;
475 }
476 if( p[0] == SSL_HASH_SHA384 )
477 {
478 ssl->handshake->sig_alg = SSL_HASH_SHA384;
479 break;
480 }
481#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200482#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000483 if( p[0] == SSL_HASH_SHA256 )
484 {
485 ssl->handshake->sig_alg = SSL_HASH_SHA256;
486 break;
487 }
488 if( p[0] == SSL_HASH_SHA224 )
489 {
490 ssl->handshake->sig_alg = SSL_HASH_SHA224;
491 break;
492 }
493#endif
494 if( p[0] == SSL_HASH_SHA1 )
495 {
496 ssl->handshake->sig_alg = SSL_HASH_SHA1;
497 break;
498 }
499 if( p[0] == SSL_HASH_MD5 )
500 {
501 ssl->handshake->sig_alg = SSL_HASH_MD5;
502 break;
503 }
504
505 sig_alg_list_size -= 2;
506 p += 2;
507 }
508
509 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
510 ssl->handshake->sig_alg ) );
511
512 return( 0 );
513}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200514#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000515
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200516#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200517static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
518 const unsigned char *buf,
519 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100520{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200523 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524
525 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
526 if( list_size + 2 != len ||
527 list_size % 2 != 0 )
528 {
529 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
530 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
531 }
532
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200533 /* Don't allow our peer to make use allocated too much memory,
534 * and leave room for a final 0 */
535 our_size = list_size / 2 + 1;
536 if( our_size > POLARSSL_ECP_DP_MAX )
537 our_size = POLARSSL_ECP_DP_MAX;
538
539 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
540 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
541
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200542 /* explicit void pointer cast for buggy MS compiler */
543 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200544 ssl->handshake->curves = curves;
545
Paul Bakker41c83d32013-03-20 14:39:14 +0100546 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200547 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100548 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200549 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200550
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200551 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100552 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 *curves++ = curve_info;
554 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100555 }
556
557 list_size -= 2;
558 p += 2;
559 }
560
561 return( 0 );
562}
563
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200564static int ssl_parse_supported_point_formats( ssl_context *ssl,
565 const unsigned char *buf,
566 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100567{
568 size_t list_size;
569 const unsigned char *p;
570
571 list_size = buf[0];
572 if( list_size + 1 != len )
573 {
574 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
575 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
576 }
577
578 p = buf + 2;
579 while( list_size > 0 )
580 {
581 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
582 p[0] == POLARSSL_ECP_PF_COMPRESSED )
583 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200584 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200585 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100586 return( 0 );
587 }
588
589 list_size--;
590 p++;
591 }
592
593 return( 0 );
594}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200595#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100596
Paul Bakker05decb22013-08-15 13:33:48 +0200597#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200598static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
599 const unsigned char *buf,
600 size_t len )
601{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200602 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200603 {
604 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
605 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
606 }
607
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200608 ssl->session_negotiate->mfl_code = buf[0];
609
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200610 return( 0 );
611}
Paul Bakker05decb22013-08-15 13:33:48 +0200612#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200613
Paul Bakker1f2bc622013-08-15 13:45:55 +0200614#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200615static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
616 const unsigned char *buf,
617 size_t len )
618{
619 if( len != 0 )
620 {
621 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
622 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
623 }
624
625 ((void) buf);
626
627 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
628
629 return( 0 );
630}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200631#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200632
Paul Bakkera503a632013-08-14 13:48:06 +0200633#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200634static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200635 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200636 size_t len )
637{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200638 int ret;
639
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200640 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
641 return( 0 );
642
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200643 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200644 ssl->handshake->new_session_ticket = 1;
645
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200646 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
647
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200648 if( len == 0 )
649 return( 0 );
650
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200651 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
652 {
653 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
654 return( 0 );
655 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200656
657 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200658 * Failures are ok: just ignore the ticket and proceed.
659 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200660 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
661 {
662 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200664 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665
666 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
667
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200668 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200670 /* Don't send a new ticket after all, this one is OK */
671 ssl->handshake->new_session_ticket = 0;
672
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200673 return( 0 );
674}
Paul Bakkera503a632013-08-14 13:48:06 +0200675#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200676
Paul Bakker78a8c712013-03-06 17:01:52 +0100677#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
678static int ssl_parse_client_hello_v2( ssl_context *ssl )
679{
680 int ret;
681 unsigned int i, j;
682 size_t n;
683 unsigned int ciph_len, sess_len, chal_len;
684 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200685 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200686 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100687
688 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
689
690 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
691 {
692 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
693
694 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
695 return( ret );
696
697 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
698 }
699
700 buf = ssl->in_hdr;
701
702 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
703
704 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
705 buf[2] ) );
706 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
707 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
708 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
709 buf[3], buf[4] ) );
710
711 /*
712 * SSLv2 Client Hello
713 *
714 * Record layer:
715 * 0 . 1 message length
716 *
717 * SSL layer:
718 * 2 . 2 message type
719 * 3 . 4 protocol version
720 */
721 if( buf[2] != SSL_HS_CLIENT_HELLO ||
722 buf[3] != SSL_MAJOR_VERSION_3 )
723 {
724 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
726 }
727
728 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
729
730 if( n < 17 || n > 512 )
731 {
732 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
733 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
734 }
735
736 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200737 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
738 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100739
740 if( ssl->minor_ver < ssl->min_minor_ver )
741 {
742 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
743 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
744 ssl->min_major_ver, ssl->min_minor_ver ) );
745
746 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
747 SSL_ALERT_MSG_PROTOCOL_VERSION );
748 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
749 }
750
Paul Bakker2fbefde2013-06-29 16:01:15 +0200751 ssl->handshake->max_major_ver = buf[3];
752 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100753
754 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
755 {
756 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
757 return( ret );
758 }
759
760 ssl->handshake->update_checksum( ssl, buf + 2, n );
761
762 buf = ssl->in_msg;
763 n = ssl->in_left - 5;
764
765 /*
766 * 0 . 1 ciphersuitelist length
767 * 2 . 3 session id length
768 * 4 . 5 challenge length
769 * 6 . .. ciphersuitelist
770 * .. . .. session id
771 * .. . .. challenge
772 */
773 SSL_DEBUG_BUF( 4, "record contents", buf, n );
774
775 ciph_len = ( buf[0] << 8 ) | buf[1];
776 sess_len = ( buf[2] << 8 ) | buf[3];
777 chal_len = ( buf[4] << 8 ) | buf[5];
778
779 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
780 ciph_len, sess_len, chal_len ) );
781
782 /*
783 * Make sure each parameter length is valid
784 */
785 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
786 {
787 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
788 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
789 }
790
791 if( sess_len > 32 )
792 {
793 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
794 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
795 }
796
797 if( chal_len < 8 || chal_len > 32 )
798 {
799 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
800 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
801 }
802
803 if( n != 6 + ciph_len + sess_len + chal_len )
804 {
805 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
806 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
807 }
808
809 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
810 buf + 6, ciph_len );
811 SSL_DEBUG_BUF( 3, "client hello, session id",
812 buf + 6 + ciph_len, sess_len );
813 SSL_DEBUG_BUF( 3, "client hello, challenge",
814 buf + 6 + ciph_len + sess_len, chal_len );
815
816 p = buf + 6 + ciph_len;
817 ssl->session_negotiate->length = sess_len;
818 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
819 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
820
821 p += sess_len;
822 memset( ssl->handshake->randbytes, 0, 64 );
823 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
824
825 /*
826 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
827 */
828 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
829 {
830 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
831 {
832 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
833 if( ssl->renegotiation == SSL_RENEGOTIATION )
834 {
835 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
836
837 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
838 return( ret );
839
840 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
841 }
842 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
843 break;
844 }
845 }
846
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200847 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
848 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +0100849 {
850 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
851 {
Paul Bakker41c83d32013-03-20 14:39:14 +0100852 // Only allow non-ECC ciphersuites as we do not have extensions
853 //
Paul Bakker59c28a22013-06-29 15:33:42 +0200854 if( p[0] == 0 && p[1] == 0 &&
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200855 ( ( ciphersuites[i] >> 8 ) & 0xFF ) == 0 &&
856 p[2] == ( ciphersuites[i] & 0xFF ) )
Paul Bakker59c28a22013-06-29 15:33:42 +0200857 {
858 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
859
860 if( ciphersuite_info == NULL )
861 {
862 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %02x not found",
863 ciphersuites[i] ) );
864 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
865 }
866
Paul Bakker2fbefde2013-06-29 16:01:15 +0200867 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
868 ciphersuite_info->max_minor_ver < ssl->minor_ver )
869 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +0200870
Paul Bakker78a8c712013-03-06 17:01:52 +0100871 goto have_ciphersuite_v2;
Paul Bakker59c28a22013-06-29 15:33:42 +0200872 }
Paul Bakker78a8c712013-03-06 17:01:52 +0100873 }
874 }
875
876 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
877
878 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
879
880have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200881 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +0200882 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +0100883 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +0100884
885 /*
886 * SSLv2 Client Hello relevant renegotiation security checks
887 */
888 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
889 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
890 {
891 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
892
893 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
894 return( ret );
895
896 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
897 }
898
899 ssl->in_left = 0;
900 ssl->state++;
901
902 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
903
904 return( 0 );
905}
906#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
907
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200908#if defined(POLARSSL_X509_CRT_PARSE_C)
909#if defined(POLARSSL_ECDSA_C)
910static int ssl_key_matches_curves( pk_context *pk,
911 const ecp_curve_info **curves )
912{
913 const ecp_curve_info **crv = curves;
914 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
915
916 while( *crv != NULL )
917 {
918 if( (*crv)->grp_id == grp_id )
919 return( 1 );
920 crv++;
921 }
922
923 return( 0 );
924}
925#endif /* POLARSSL_ECDSA_C */
926
927/*
928 * Try picking a certificate for this ciphersuite,
929 * return 0 on success and -1 on failure.
930 */
931static int ssl_pick_cert( ssl_context *ssl,
932 const ssl_ciphersuite_t * ciphersuite_info )
933{
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200934 ssl_key_cert *cur, *list;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200935 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
936
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200937#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
938 if( ssl->handshake->sni_key_cert != NULL )
939 list = ssl->handshake->sni_key_cert;
940 else
941#endif
942 list = ssl->handshake->key_cert;
943
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200944 if( pk_alg == POLARSSL_PK_NONE )
945 return( 0 );
946
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200947 for( cur = list; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200948 {
949 if( ! pk_can_do( cur->key, pk_alg ) )
950 continue;
951
952#if defined(POLARSSL_ECDSA_C)
953 if( pk_alg == POLARSSL_PK_ECDSA )
954 {
955 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
956 break;
957 }
958 else
959#endif
960 break;
961 }
962
963 if( cur == NULL )
964 return( -1 );
965
966 ssl->handshake->key_cert = cur;
967 return( 0 );
968}
969#endif /* POLARSSL_X509_CRT_PARSE_C */
970
Paul Bakker5121ce52009-01-03 21:22:43 +0000971static int ssl_parse_client_hello( ssl_context *ssl )
972{
Paul Bakker23986e52011-04-24 08:57:21 +0000973 int ret;
974 unsigned int i, j;
975 size_t n;
976 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +0000977 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +0000978 unsigned int ext_len = 0;
979 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000980 int renegotiation_info_seen = 0;
981 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200982 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +0100983 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +0000984
985 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
986
Paul Bakker48916f92012-09-16 19:57:18 +0000987 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
988 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000989 {
990 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
991 return( ret );
992 }
993
994 buf = ssl->in_hdr;
995
Paul Bakker78a8c712013-03-06 17:01:52 +0100996#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
997 if( ( buf[0] & 0x80 ) != 0 )
998 return ssl_parse_client_hello_v2( ssl );
999#endif
1000
Paul Bakkerec636f32012-09-09 19:17:02 +00001001 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1002
1003 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1004 buf[0] ) );
1005 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1006 ( buf[3] << 8 ) | buf[4] ) );
1007 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1008 buf[1], buf[2] ) );
1009
1010 /*
1011 * SSLv3 Client Hello
1012 *
1013 * Record layer:
1014 * 0 . 0 message type
1015 * 1 . 2 protocol version
1016 * 3 . 4 message length
1017 */
1018 if( buf[0] != SSL_MSG_HANDSHAKE ||
1019 buf[1] != SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001020 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001021 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1022 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001024
Paul Bakkerec636f32012-09-09 19:17:02 +00001025 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001026
Manuel Pégourié-Gonnard72882b22013-08-02 13:36:00 +02001027 if( n < 45 || n > 2048 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001028 {
1029 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1030 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1031 }
1032
Paul Bakker48916f92012-09-16 19:57:18 +00001033 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1034 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001035 {
1036 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1037 return( ret );
1038 }
1039
1040 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001041 if( !ssl->renegotiation )
1042 n = ssl->in_left - 5;
1043 else
1044 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001045
Paul Bakker48916f92012-09-16 19:57:18 +00001046 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001047
1048 /*
1049 * SSL layer:
1050 * 0 . 0 handshake type
1051 * 1 . 3 handshake length
1052 * 4 . 5 protocol version
1053 * 6 . 9 UNIX time()
1054 * 10 . 37 random bytes
1055 * 38 . 38 session id length
1056 * 39 . 38+x session id
1057 * 39+x . 40+x ciphersuitelist length
1058 * 41+x . .. ciphersuitelist
1059 * .. . .. compression alg.
1060 * .. . .. extensions
1061 */
1062 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1063
1064 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1065 buf[0] ) );
1066 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1067 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1068 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1069 buf[4], buf[5] ) );
1070
1071 /*
1072 * Check the handshake type and protocol version
1073 */
1074 if( buf[0] != SSL_HS_CLIENT_HELLO ||
1075 buf[4] != SSL_MAJOR_VERSION_3 )
1076 {
1077 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1078 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1079 }
1080
1081 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001082 ssl->minor_ver = ( buf[5] <= ssl->max_minor_ver )
1083 ? buf[5] : ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001084
Paul Bakker1d29fb52012-09-28 13:28:45 +00001085 if( ssl->minor_ver < ssl->min_minor_ver )
1086 {
1087 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
1088 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001089 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001090
1091 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1092 SSL_ALERT_MSG_PROTOCOL_VERSION );
1093
1094 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1095 }
1096
Paul Bakker2fbefde2013-06-29 16:01:15 +02001097 ssl->handshake->max_major_ver = buf[4];
1098 ssl->handshake->max_minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001099
Paul Bakker48916f92012-09-16 19:57:18 +00001100 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001101
1102 /*
1103 * Check the handshake message length
1104 */
1105 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1106 {
1107 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1108 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1109 }
1110
1111 /*
1112 * Check the session length
1113 */
1114 sess_len = buf[38];
1115
1116 if( sess_len > 32 )
1117 {
1118 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1119 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1120 }
1121
Paul Bakker48916f92012-09-16 19:57:18 +00001122 ssl->session_negotiate->length = sess_len;
1123 memset( ssl->session_negotiate->id, 0,
1124 sizeof( ssl->session_negotiate->id ) );
1125 memcpy( ssl->session_negotiate->id, buf + 39,
1126 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001127
1128 /*
1129 * Check the ciphersuitelist length
1130 */
1131 ciph_len = ( buf[39 + sess_len] << 8 )
1132 | ( buf[40 + sess_len] );
1133
1134 if( ciph_len < 2 || ciph_len > 256 || ( ciph_len % 2 ) != 0 )
1135 {
1136 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1137 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1138 }
1139
1140 /*
1141 * Check the compression algorithms length
1142 */
1143 comp_len = buf[41 + sess_len + ciph_len];
1144
1145 if( comp_len < 1 || comp_len > 16 )
1146 {
1147 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1148 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1149 }
1150
Paul Bakker48916f92012-09-16 19:57:18 +00001151 /*
1152 * Check the extension length
1153 */
1154 if( n > 42 + sess_len + ciph_len + comp_len )
1155 {
1156 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1157 | ( buf[43 + sess_len + ciph_len + comp_len] );
1158
1159 if( ( ext_len > 0 && ext_len < 4 ) ||
1160 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1161 {
1162 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1163 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1164 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1165 }
1166 }
1167
1168 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001169#if defined(POLARSSL_ZLIB_SUPPORT)
1170 for( i = 0; i < comp_len; ++i )
1171 {
Paul Bakker48916f92012-09-16 19:57:18 +00001172 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001173 {
Paul Bakker48916f92012-09-16 19:57:18 +00001174 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001175 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 }
1177 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001178#endif
1179
Paul Bakkerec636f32012-09-09 19:17:02 +00001180 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1181 buf + 6, 32 );
1182 SSL_DEBUG_BUF( 3, "client hello, session id",
1183 buf + 38, sess_len );
1184 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1185 buf + 41 + sess_len, ciph_len );
1186 SSL_DEBUG_BUF( 3, "client hello, compression",
1187 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001188
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001190 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1191 */
1192 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1193 {
1194 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1195 {
1196 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1197 if( ssl->renegotiation == SSL_RENEGOTIATION )
1198 {
1199 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001200
1201 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1202 return( ret );
1203
Paul Bakker48916f92012-09-16 19:57:18 +00001204 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1205 }
1206 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1207 break;
1208 }
1209 }
1210
Paul Bakker48916f92012-09-16 19:57:18 +00001211 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001212
1213 while( ext_len )
1214 {
1215 unsigned int ext_id = ( ( ext[0] << 8 )
1216 | ( ext[1] ) );
1217 unsigned int ext_size = ( ( ext[2] << 8 )
1218 | ( ext[3] ) );
1219
1220 if( ext_size + 4 > ext_len )
1221 {
1222 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1223 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1224 }
1225 switch( ext_id )
1226 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001227#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001228 case TLS_EXT_SERVERNAME:
1229 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1230 if( ssl->f_sni == NULL )
1231 break;
1232
1233 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1234 if( ret != 0 )
1235 return( ret );
1236 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001237#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001238
Paul Bakker48916f92012-09-16 19:57:18 +00001239 case TLS_EXT_RENEGOTIATION_INFO:
1240 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1241 renegotiation_info_seen = 1;
1242
Paul Bakker23f36802012-09-28 14:15:14 +00001243 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1244 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001245 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001246 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001247
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001248#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001249 case TLS_EXT_SIG_ALG:
1250 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1251 if( ssl->renegotiation == SSL_RENEGOTIATION )
1252 break;
1253
1254 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1255 if( ret != 0 )
1256 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001257 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001258#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001259
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001260#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001261 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1262 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1263
1264 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1265 if( ret != 0 )
1266 return( ret );
1267 break;
1268
1269 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1270 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001271 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001272
1273 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1274 if( ret != 0 )
1275 return( ret );
1276 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001277#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001278
Paul Bakker05decb22013-08-15 13:33:48 +02001279#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001280 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1281 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1282
1283 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1284 if( ret != 0 )
1285 return( ret );
1286 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001287#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001288
Paul Bakker1f2bc622013-08-15 13:45:55 +02001289#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001290 case TLS_EXT_TRUNCATED_HMAC:
1291 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1292
1293 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1294 if( ret != 0 )
1295 return( ret );
1296 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001297#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001298
Paul Bakkera503a632013-08-14 13:48:06 +02001299#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001300 case TLS_EXT_SESSION_TICKET:
1301 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1302
1303 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1304 if( ret != 0 )
1305 return( ret );
1306 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001307#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001308
Paul Bakker48916f92012-09-16 19:57:18 +00001309 default:
1310 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1311 ext_id ) );
1312 }
1313
1314 ext_len -= 4 + ext_size;
1315 ext += 4 + ext_size;
1316
1317 if( ext_len > 0 && ext_len < 4 )
1318 {
1319 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1321 }
1322 }
1323
1324 /*
1325 * Renegotiation security checks
1326 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001327 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1328 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1329 {
1330 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1331 handshake_failure = 1;
1332 }
1333 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1334 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1335 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001336 {
1337 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001338 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001339 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001340 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1341 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1342 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001343 {
1344 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001345 handshake_failure = 1;
1346 }
1347 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1348 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1349 renegotiation_info_seen == 1 )
1350 {
1351 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1352 handshake_failure = 1;
1353 }
1354
1355 if( handshake_failure == 1 )
1356 {
1357 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1358 return( ret );
1359
Paul Bakker48916f92012-09-16 19:57:18 +00001360 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1361 }
Paul Bakker380da532012-04-18 16:10:25 +00001362
Paul Bakker41c83d32013-03-20 14:39:14 +01001363 /*
1364 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001365 * (At the end because we need information from the EC-based extensions
1366 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001367 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001368 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
1369 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001370 {
1371 for( j = 0, p = buf + 41 + sess_len; j < ciph_len;
1372 j += 2, p += 2 )
1373 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001374 if( p[0] == ( ( ciphersuites[i] >> 8 ) & 0xFF ) &&
1375 p[1] == ( ( ciphersuites[i] ) & 0xFF ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001376 {
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001377 ciphersuite_info = ssl_ciphersuite_from_id( ciphersuites[i] );
Paul Bakker41c83d32013-03-20 14:39:14 +01001378
1379 if( ciphersuite_info == NULL )
1380 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001381 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found",
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001382 ciphersuites[i] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +01001383 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
1384 }
1385
Paul Bakker2fbefde2013-06-29 16:01:15 +02001386 if( ciphersuite_info->min_minor_ver > ssl->minor_ver ||
1387 ciphersuite_info->max_minor_ver < ssl->minor_ver )
1388 continue;
1389
Paul Bakker5fd49172013-08-19 13:29:26 +02001390#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001391 if( ssl_ciphersuite_uses_ec( ciphersuite_info ) &&
Paul Bakkercaa3af42013-09-26 13:32:43 +02001392 ( ssl->handshake->curves == NULL ||
1393 ssl->handshake->curves[0] == NULL ) )
Paul Bakker41c83d32013-03-20 14:39:14 +01001394 continue;
Paul Bakker5fd49172013-08-19 13:29:26 +02001395#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001396
Manuel Pégourié-Gonnard21ef42f2013-10-27 14:47:25 +01001397#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1398 /* If the ciphersuite requires a pre-shared key and we don't
1399 * have one, skip it now rather than failing later */
1400 if( ssl_ciphersuite_uses_psk( ciphersuite_info ) &&
1401 ssl->f_psk == NULL &&
1402 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1403 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
1404 continue;
1405#endif
1406
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001407#if defined(POLARSSL_X509_CRT_PARSE_C)
1408 /*
1409 * Final check: if ciphersuite requires us to have a
1410 * certificate/key of a particular type:
1411 * - select the appropriate certificate if we have one, or
1412 * - try the next ciphersuite if we don't
1413 * This must be done last since we modify the key_cert list.
1414 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001415 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1416 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001417#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001418
Paul Bakker41c83d32013-03-20 14:39:14 +01001419 goto have_ciphersuite;
1420 }
1421 }
1422 }
1423
1424 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1425
1426 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1427 return( ret );
1428
1429 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1430
1431have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001432 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001433 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1434 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1435
Paul Bakker5121ce52009-01-03 21:22:43 +00001436 ssl->in_left = 0;
1437 ssl->state++;
1438
1439 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1440
1441 return( 0 );
1442}
1443
Paul Bakker1f2bc622013-08-15 13:45:55 +02001444#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001445static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1446 unsigned char *buf,
1447 size_t *olen )
1448{
1449 unsigned char *p = buf;
1450
1451 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1452 {
1453 *olen = 0;
1454 return;
1455 }
1456
1457 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1458
1459 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1460 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1461
1462 *p++ = 0x00;
1463 *p++ = 0x00;
1464
1465 *olen = 4;
1466}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001467#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001468
Paul Bakkera503a632013-08-14 13:48:06 +02001469#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001470static void ssl_write_session_ticket_ext( ssl_context *ssl,
1471 unsigned char *buf,
1472 size_t *olen )
1473{
1474 unsigned char *p = buf;
1475
1476 if( ssl->handshake->new_session_ticket == 0 )
1477 {
1478 *olen = 0;
1479 return;
1480 }
1481
1482 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1483
1484 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1485 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1486
1487 *p++ = 0x00;
1488 *p++ = 0x00;
1489
1490 *olen = 4;
1491}
Paul Bakkera503a632013-08-14 13:48:06 +02001492#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001493
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001494static void ssl_write_renegotiation_ext( ssl_context *ssl,
1495 unsigned char *buf,
1496 size_t *olen )
1497{
1498 unsigned char *p = buf;
1499
1500 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1501 {
1502 *olen = 0;
1503 return;
1504 }
1505
1506 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1507
1508 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1509 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1510
1511 *p++ = 0x00;
1512 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1513 *p++ = ssl->verify_data_len * 2 & 0xFF;
1514
1515 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1516 p += ssl->verify_data_len;
1517 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1518 p += ssl->verify_data_len;
1519
1520 *olen = 5 + ssl->verify_data_len * 2;
1521}
1522
Paul Bakker05decb22013-08-15 13:33:48 +02001523#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001524static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1525 unsigned char *buf,
1526 size_t *olen )
1527{
1528 unsigned char *p = buf;
1529
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001530 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1531 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001532 *olen = 0;
1533 return;
1534 }
1535
1536 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1537
1538 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1539 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1540
1541 *p++ = 0x00;
1542 *p++ = 1;
1543
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001544 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001545
1546 *olen = 5;
1547}
Paul Bakker05decb22013-08-15 13:33:48 +02001548#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001549
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001550#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001551static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1552 unsigned char *buf,
1553 size_t *olen )
1554{
1555 unsigned char *p = buf;
1556 ((void) ssl);
1557
Paul Bakker677377f2013-10-28 12:54:26 +01001558 if( ( ssl->handshake->cli_exts &
1559 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1560 {
1561 *olen = 0;
1562 return;
1563 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001564
1565 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1566
1567 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1568 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1569
1570 *p++ = 0x00;
1571 *p++ = 2;
1572
1573 *p++ = 1;
1574 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1575
1576 *olen = 6;
1577}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001578#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001579
Paul Bakker5121ce52009-01-03 21:22:43 +00001580static int ssl_write_server_hello( ssl_context *ssl )
1581{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001582#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001584#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001585 int ret;
1586 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001587 unsigned char *buf, *p;
1588
1589 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1590
1591 /*
1592 * 0 . 0 handshake type
1593 * 1 . 3 handshake length
1594 * 4 . 5 protocol version
1595 * 6 . 9 UNIX time()
1596 * 10 . 37 random bytes
1597 */
1598 buf = ssl->out_msg;
1599 p = buf + 4;
1600
1601 *p++ = (unsigned char) ssl->major_ver;
1602 *p++ = (unsigned char) ssl->minor_ver;
1603
1604 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1605 buf[4], buf[5] ) );
1606
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001607#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 t = time( NULL );
1609 *p++ = (unsigned char)( t >> 24 );
1610 *p++ = (unsigned char)( t >> 16 );
1611 *p++ = (unsigned char)( t >> 8 );
1612 *p++ = (unsigned char)( t );
1613
1614 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001615#else
1616 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1617 return( ret );
1618
1619 p += 4;
1620#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Paul Bakkera3d195c2011-11-27 21:07:34 +00001622 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1623 return( ret );
1624
1625 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Paul Bakker48916f92012-09-16 19:57:18 +00001627 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1630
1631 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001632 * Resume is 0 by default, see ssl_handshake_init().
1633 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1634 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001635 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001636 if( ssl->handshake->resume == 0 &&
1637 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001638 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001639 ssl->f_get_cache != NULL &&
1640 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1641 {
1642 ssl->handshake->resume = 1;
1643 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001645 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
1647 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001648 * New session, create a new session id,
1649 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001650 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001651 ssl->state++;
1652
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001653#if defined(POLARSSL_HAVE_TIME)
1654 ssl->session_negotiate->start = time( NULL );
1655#endif
1656
Paul Bakkera503a632013-08-14 13:48:06 +02001657#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001658 if( ssl->handshake->new_session_ticket != 0 )
1659 {
1660 ssl->session_negotiate->length = n = 0;
1661 memset( ssl->session_negotiate->id, 0, 32 );
1662 }
1663 else
1664#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001665 {
1666 ssl->session_negotiate->length = n = 32;
1667 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001668 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001669 return( ret );
1670 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 }
1672 else
1673 {
1674 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001675 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001677 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001679
1680 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1681 {
1682 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1683 return( ret );
1684 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 }
1686
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001687 /*
1688 * 38 . 38 session id length
1689 * 39 . 38+n session id
1690 * 39+n . 40+n chosen ciphersuite
1691 * 41+n . 41+n chosen compression alg.
1692 * 42+n . 43+n extensions length
1693 * 44+n . 43+n+m extensions
1694 */
1695 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001696 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1697 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001698
1699 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1700 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1701 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001702 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Paul Bakker48916f92012-09-16 19:57:18 +00001704 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1705 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1706 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001708 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1709 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001710 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001711 ssl->session_negotiate->compression ) );
1712
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001713 /*
1714 * First write extensions, then the total length
1715 */
1716 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1717 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001718
Paul Bakker05decb22013-08-15 13:33:48 +02001719#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001720 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1721 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001722#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001723
Paul Bakker1f2bc622013-08-15 13:45:55 +02001724#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001725 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1726 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001727#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001728
Paul Bakkera503a632013-08-14 13:48:06 +02001729#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001730 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1731 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001732#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001733
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001734#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001735 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1736 ext_len += olen;
1737#endif
1738
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001739 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001740
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001741 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1742 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1743 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 ssl->out_msglen = p - buf;
1746 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1747 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1748
1749 ret = ssl_write_record( ssl );
1750
1751 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1752
1753 return( ret );
1754}
1755
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001756#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1757 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001758 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1759 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001760static int ssl_write_certificate_request( ssl_context *ssl )
1761{
Paul Bakkered27a042013-04-18 22:46:23 +02001762 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1763 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001764
1765 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1766
1767 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001768 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1769 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001770 {
1771 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1772 ssl->state++;
1773 return( 0 );
1774 }
1775
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001776 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001777 return( ret );
1778}
1779#else
1780static int ssl_write_certificate_request( ssl_context *ssl )
1781{
1782 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1783 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001784 size_t dn_size, total_dn_size; /* excluding length bytes */
1785 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001786 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001787 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
1789 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1790
1791 ssl->state++;
1792
Paul Bakkerfbb17802013-04-17 19:10:21 +02001793 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001794 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001795 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001796 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001797 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001798 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 return( 0 );
1800 }
1801
1802 /*
1803 * 0 . 0 handshake type
1804 * 1 . 3 handshake length
1805 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001806 * 5 .. m-1 cert types
1807 * m .. m+1 sig alg length (TLS 1.2 only)
1808 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001809 * n .. n+1 length of all DNs
1810 * n+2 .. n+3 length of DN 1
1811 * n+4 .. ... Distinguished Name #1
1812 * ... .. ... length of DN 2, etc.
1813 */
1814 buf = ssl->out_msg;
1815 p = buf + 4;
1816
1817 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001818 * Supported certificate types
1819 *
1820 * ClientCertificateType certificate_types<1..2^8-1>;
1821 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001822 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001823 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001824
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001825#if defined(POLARSSL_RSA_C)
1826 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1827#endif
1828#if defined(POLARSSL_ECDSA_C)
1829 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1830#endif
1831
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001832 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001833 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001834
Paul Bakker577e0062013-08-28 11:57:20 +02001835 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001836#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001837 /*
1838 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001839 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001840 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1841 *
1842 * struct {
1843 * HashAlgorithm hash;
1844 * SignatureAlgorithm signature;
1845 * } SignatureAndHashAlgorithm;
1846 *
1847 * enum { (255) } HashAlgorithm;
1848 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001849 */
Paul Bakker21dca692013-01-03 11:41:08 +01001850 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001851 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001852 /*
1853 * Only use current running hash algorithm that is already required
1854 * for requested ciphersuite.
1855 */
Paul Bakker926af752012-11-23 13:38:07 +01001856 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1857
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001858 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1859 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001860 {
1861 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1862 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001863
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001864 /*
1865 * Supported signature algorithms
1866 */
1867#if defined(POLARSSL_RSA_C)
1868 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1869 p[2 + sa_len++] = SSL_SIG_RSA;
1870#endif
1871#if defined(POLARSSL_ECDSA_C)
1872 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1873 p[2 + sa_len++] = SSL_SIG_ECDSA;
1874#endif
Paul Bakker926af752012-11-23 13:38:07 +01001875
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001876 p[0] = (unsigned char)( sa_len >> 8 );
1877 p[1] = (unsigned char)( sa_len );
1878 sa_len += 2;
1879 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001880 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001881#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001883 /*
1884 * DistinguishedName certificate_authorities<0..2^16-1>;
1885 * opaque DistinguishedName<1..2^16-1>;
1886 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001887 p += 2;
1888 crt = ssl->ca_chain;
1889
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001890 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001891 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 {
1893 if( p - buf > 4096 )
1894 break;
1895
Paul Bakker926af752012-11-23 13:38:07 +01001896 dn_size = crt->subject_raw.len;
1897 *p++ = (unsigned char)( dn_size >> 8 );
1898 *p++ = (unsigned char)( dn_size );
1899 memcpy( p, crt->subject_raw.p, dn_size );
1900 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Paul Bakker926af752012-11-23 13:38:07 +01001902 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1903
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001904 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001905 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001906 }
1907
Paul Bakker926af752012-11-23 13:38:07 +01001908 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001909 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1910 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001911 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1912 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
1914 ret = ssl_write_record( ssl );
1915
1916 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1917
1918 return( ret );
1919}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001920#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1921 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1922 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001923
Paul Bakker41c83d32013-03-20 14:39:14 +01001924static int ssl_write_server_key_exchange( ssl_context *ssl )
1925{
Paul Bakker23986e52011-04-24 08:57:21 +00001926 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001927 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001928 const ssl_ciphersuite_t *ciphersuite_info =
1929 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001930
1931#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1932 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1933 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001934 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001935 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001936 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001937 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001938 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001939 ((void) dig_signed);
1940 ((void) dig_signed_len);
1941#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001942
Paul Bakker5121ce52009-01-03 21:22:43 +00001943 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1944
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001945 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1946 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001948 {
1949 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1950 ssl->state++;
1951 return( 0 );
1952 }
1953
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001954#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1955 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1956 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1957 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001958 {
1959 /* TODO: Support identity hints */
1960 *(p++) = 0x00;
1961 *(p++) = 0x00;
1962
1963 n += 2;
1964 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001965#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1966 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001967
1968#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1969 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1970 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001972 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001973 /*
1974 * Ephemeral DH parameters:
1975 *
1976 * struct {
1977 * opaque dh_p<1..2^16-1>;
1978 * opaque dh_g<1..2^16-1>;
1979 * opaque dh_Ys<1..2^16-1>;
1980 * } ServerDHParams;
1981 */
1982 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1983 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1984 {
1985 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1986 return( ret );
1987 }
Paul Bakker48916f92012-09-16 19:57:18 +00001988
Paul Bakker41c83d32013-03-20 14:39:14 +01001989 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001990 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001991 p,
1992 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001993 {
1994 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1995 return( ret );
1996 }
1997
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001998 dig_signed = p;
1999 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002000
2001 p += len;
2002 n += len;
2003
Paul Bakker41c83d32013-03-20 14:39:14 +01002004 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2005 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2006 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2007 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2008 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002009#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2010 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002011
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002012#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002013 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2014 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2015
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002016 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2018 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002019 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002020 /*
2021 * Ephemeral ECDH parameters:
2022 *
2023 * struct {
2024 * ECParameters curve_params;
2025 * ECPoint public;
2026 * } ServerECDHParams;
2027 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002028 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002029 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002030 {
2031 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2032 return( ret );
2033 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002034
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002035 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2036 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2037
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002038 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2039 p, SSL_MAX_CONTENT_LEN - n,
2040 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002041 {
2042 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2043 return( ret );
2044 }
2045
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002046 dig_signed = p;
2047 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002048
2049 p += len;
2050 n += len;
2051
Paul Bakker41c83d32013-03-20 14:39:14 +01002052 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2053 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002054#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002055 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2056 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002057
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002058#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002059 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2060 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002061 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002062 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2063 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002064 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002065 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002066 unsigned int hashlen = 0;
2067 unsigned char hash[64];
2068 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002069
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002070 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002071 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2072 */
Paul Bakker577e0062013-08-28 11:57:20 +02002073#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002074 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2075 {
2076 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2077
2078 if( md_alg == POLARSSL_MD_NONE )
2079 {
2080 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2081 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2082 }
2083 }
Paul Bakker577e0062013-08-28 11:57:20 +02002084 else
2085#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002086#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2087 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002088 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002089 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2090 {
2091 md_alg = POLARSSL_MD_SHA1;
2092 }
2093 else
Paul Bakker577e0062013-08-28 11:57:20 +02002094#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002095 {
2096 md_alg = POLARSSL_MD_NONE;
2097 }
2098
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002099 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002100 * Compute the hash to be signed
2101 */
Paul Bakker577e0062013-08-28 11:57:20 +02002102#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2103 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002104 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002105 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002106 md5_context md5;
2107 sha1_context sha1;
2108
2109 /*
2110 * digitally-signed struct {
2111 * opaque md5_hash[16];
2112 * opaque sha_hash[20];
2113 * };
2114 *
2115 * md5_hash
2116 * MD5(ClientHello.random + ServerHello.random
2117 * + ServerParams);
2118 * sha_hash
2119 * SHA(ClientHello.random + ServerHello.random
2120 * + ServerParams);
2121 */
2122 md5_starts( &md5 );
2123 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002124 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002125 md5_finish( &md5, hash );
2126
2127 sha1_starts( &sha1 );
2128 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002129 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002130 sha1_finish( &sha1, hash + 16 );
2131
2132 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002133 }
2134 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002135#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2136 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002137#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2138 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002139 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002140 {
2141 md_context_t ctx;
2142
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002143 /* Info from md_alg will be used instead */
2144 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002145
2146 /*
2147 * digitally-signed struct {
2148 * opaque client_random[32];
2149 * opaque server_random[32];
2150 * ServerDHParams params;
2151 * };
2152 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002153 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002154 {
2155 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2156 return( ret );
2157 }
2158
2159 md_starts( &ctx );
2160 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002161 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002162 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002163
2164 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2165 {
2166 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2167 return( ret );
2168 }
2169
Paul Bakker23f36802012-09-28 14:15:14 +00002170 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002171 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002172#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2173 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002174 {
2175 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002176 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002177 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002178
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002179 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2180 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002182 /*
2183 * Make the signature
2184 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002185 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002186 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002187 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2188 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002189 }
Paul Bakker23f36802012-09-28 14:15:14 +00002190
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002191#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002192 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2193 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002195 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002196
2197 n += 2;
2198 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002199#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002200
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002201 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002202 p + 2 , &signature_len,
2203 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002204 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002205 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002206 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002207 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002208
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002209 *(p++) = (unsigned char)( signature_len >> 8 );
2210 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002211 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002213 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002214
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002215 p += signature_len;
2216 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002217 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002218#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002219 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2220 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002221
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002222 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2224 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2225
2226 ssl->state++;
2227
2228 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2229 {
2230 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2231 return( ret );
2232 }
2233
2234 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2235
2236 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002237}
2238
2239static int ssl_write_server_hello_done( ssl_context *ssl )
2240{
2241 int ret;
2242
2243 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2244
2245 ssl->out_msglen = 4;
2246 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2247 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2248
2249 ssl->state++;
2250
2251 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2252 {
2253 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2254 return( ret );
2255 }
2256
2257 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2258
2259 return( 0 );
2260}
2261
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002262#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2263 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2264static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2265 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002266{
2267 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268 size_t n;
2269
2270 /*
2271 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2272 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002273 if( *p + 2 > end )
2274 {
2275 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2276 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2277 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002278
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002279 n = ( (*p)[0] << 8 ) | (*p)[1];
2280 *p += 2;
2281
2282 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002283 {
2284 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2285 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2286 }
2287
2288 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002289 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002290 {
2291 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2292 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2293 }
2294
2295 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2296
Paul Bakker70df2fb2013-04-17 17:19:09 +02002297 return( ret );
2298}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002299#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2300 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002301
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002302#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2303 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002304static int ssl_parse_encrypted_pms( ssl_context *ssl,
2305 const unsigned char *p,
2306 const unsigned char *end,
2307 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002308{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002309 int ret;
2310 size_t len = pk_get_len( ssl_own_key( ssl ) );
2311 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002312
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002313 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002314 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002315 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002316 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2317 }
2318
2319 /*
2320 * Decrypt the premaster using own private RSA key
2321 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002322#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2323 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002324 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2325 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002326 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2327 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002328 {
2329 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2331 }
2332 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002333#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002334
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002335 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002336 {
2337 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2338 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2339 }
2340
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002341 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2342 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002343 sizeof(ssl->handshake->premaster),
2344 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002345
2346 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002347 pms[0] != ssl->handshake->max_major_ver ||
2348 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002349 {
2350 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2351
2352 /*
2353 * Protection against Bleichenbacher's attack:
2354 * invalid PKCS#1 v1.5 padding must not cause
2355 * the connection to end immediately; instead,
2356 * send a bad_record_mac later in the handshake.
2357 */
2358 ssl->handshake->pmslen = 48;
2359
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002360 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002361 if( ret != 0 )
2362 return( ret );
2363 }
2364
2365 return( ret );
2366}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002367#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2368 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002369
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002370#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2372 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002373{
Paul Bakker6db455e2013-09-18 17:29:31 +02002374 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002375 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002376
Paul Bakker6db455e2013-09-18 17:29:31 +02002377 if( ssl->f_psk == NULL &&
2378 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2379 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002380 {
2381 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2382 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2383 }
2384
2385 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002387 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002388 if( *p + 2 > end )
2389 {
2390 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2392 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002393
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002394 n = ( (*p)[0] << 8 ) | (*p)[1];
2395 *p += 2;
2396
2397 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002398 {
2399 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2400 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2401 }
2402
Paul Bakker6db455e2013-09-18 17:29:31 +02002403 if( ssl->f_psk != NULL )
2404 {
2405 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2406 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2407 }
2408
2409 if( ret == 0 )
2410 {
2411 if( n != ssl->psk_identity_len ||
2412 memcmp( ssl->psk_identity, *p, n ) != 0 )
2413 {
2414 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2415 }
2416 }
2417
2418 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002419 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002421 if( ( ret = ssl_send_alert_message( ssl,
2422 SSL_ALERT_LEVEL_FATAL,
2423 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2424 {
2425 return( ret );
2426 }
2427
2428 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002429 }
2430
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002431 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002432 ret = 0;
2433
Paul Bakkerfbb17802013-04-17 19:10:21 +02002434 return( ret );
2435}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002436#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002437
Paul Bakker5121ce52009-01-03 21:22:43 +00002438static int ssl_parse_client_key_exchange( ssl_context *ssl )
2439{
Paul Bakker23986e52011-04-24 08:57:21 +00002440 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002441 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002442
Paul Bakker41c83d32013-03-20 14:39:14 +01002443 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002444
2445 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2446
2447 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2448 {
2449 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2450 return( ret );
2451 }
2452
2453 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2454 {
2455 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002456 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458
2459 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2460 {
2461 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002462 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 }
2464
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002466 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002468 unsigned char *p = ssl->in_msg + 4;
2469 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2470
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002473 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2474 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476
2477 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2478
2479 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2480 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002481 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002482 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002483 {
2484 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2485 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2486 }
2487
2488 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002489 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002490 else
2491#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002492#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2493 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2494 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2495 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002496 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002497 size_t n = ssl->in_msg[3];
2498
2499 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2500 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002501 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002502 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2503 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002504 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002506 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2507 ssl->in_msg + 4, n ) ) != 0 )
2508 {
2509 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2510 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2511 }
2512
2513 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2514
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2516 &ssl->handshake->pmslen,
2517 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002518 POLARSSL_MPI_MAX_SIZE,
2519 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 {
2521 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2522 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2523 }
2524
2525 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002526 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002527 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002528#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2529 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2531 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002532 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002533 unsigned char *p = ssl->in_msg + 4;
2534 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2535
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002536 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002537 {
2538 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2539 return( ret );
2540 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002542 if( ( ret = ssl_psk_derive_premaster( ssl,
2543 ciphersuite_info->key_exchange ) ) != 0 )
2544 {
2545 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2546 return( ret );
2547 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002548 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002549 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002551#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2552 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2553 {
2554 unsigned char *p = ssl->in_msg + 4;
2555 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2556
2557 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2558 {
2559 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2560 return( ret );
2561 }
2562
2563 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2564 {
2565 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2566 return( ret );
2567 }
2568
2569 if( ( ret = ssl_psk_derive_premaster( ssl,
2570 ciphersuite_info->key_exchange ) ) != 0 )
2571 {
2572 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2573 return( ret );
2574 }
2575 }
2576 else
2577#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002578#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2579 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2580 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002581 unsigned char *p = ssl->in_msg + 4;
2582 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002583
2584 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2585 {
2586 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2587 return( ret );
2588 }
2589 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2590 {
2591 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2592 return( ret );
2593 }
2594
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002595 if( ( ret = ssl_psk_derive_premaster( ssl,
2596 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002597 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002598 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2599 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002600 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002601 }
2602 else
2603#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002604#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2605 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2606 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002607 unsigned char *p = ssl->in_msg + 4;
2608 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2609
2610 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2611 {
2612 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2613 return( ret );
2614 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002615
2616 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2617 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002618 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002619 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2620 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002621 }
2622
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002623 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2624
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002625 if( ( ret = ssl_psk_derive_premaster( ssl,
2626 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002627 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002628 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002629 return( ret );
2630 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002631 }
2632 else
2633#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002634#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2635 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002636 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002637 if( ( ret = ssl_parse_encrypted_pms( ssl,
2638 ssl->in_msg + 4,
2639 ssl->in_msg + ssl->in_msglen,
2640 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002641 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002642 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002643 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002644 }
2645 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 else
2647#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2648 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002649 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002650 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2651 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002652
Paul Bakkerff60ee62010-03-16 21:09:09 +00002653 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2654 {
2655 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2656 return( ret );
2657 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002658
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 ssl->state++;
2660
2661 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2662
2663 return( 0 );
2664}
2665
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002666#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2667 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002668 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2669 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002670static int ssl_parse_certificate_verify( ssl_context *ssl )
2671{
Paul Bakkered27a042013-04-18 22:46:23 +02002672 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002673 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002674
2675 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2676
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002678 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002679 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002680 {
2681 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2682 ssl->state++;
2683 return( 0 );
2684 }
2685
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002686 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002687 return( ret );
2688}
2689#else
2690static int ssl_parse_certificate_verify( ssl_context *ssl )
2691{
2692 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002693 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002694 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002695 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002696 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002697#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002698 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002699#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002700 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002701 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2702
2703 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2704
2705 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002706 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002707 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2708 {
2709 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2710 ssl->state++;
2711 return( 0 );
2712 }
2713
Paul Bakkered27a042013-04-18 22:46:23 +02002714 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 {
2716 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2717 ssl->state++;
2718 return( 0 );
2719 }
2720
Paul Bakker48916f92012-09-16 19:57:18 +00002721 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002722
2723 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2724 {
2725 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2726 return( ret );
2727 }
2728
2729 ssl->state++;
2730
2731 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2732 {
2733 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002734 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 }
2736
2737 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2738 {
2739 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002740 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002741 }
2742
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002743 /*
2744 * 0 . 0 handshake type
2745 * 1 . 3 handshake length
2746 * 4 . 5 sig alg (TLS 1.2 only)
2747 * 4+n . 5+n signature length (n = sa_len)
2748 * 6+n . 6+n+m signature (m = sig_len)
2749 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002751#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_1)
2753 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002754 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002755 sa_len = 0;
2756
Paul Bakkerc70b9822013-04-07 22:00:46 +02002757 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002758 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002759
2760 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2761 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2762 POLARSSL_PK_ECDSA ) )
2763 {
2764 hash_start += 16;
2765 hashlen -= 16;
2766 md_alg = POLARSSL_MD_SHA1;
2767 }
Paul Bakker926af752012-11-23 13:38:07 +01002768 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002769 else
2770#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002771#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2772 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002773 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002774 sa_len = 2;
2775
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002777 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002778 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002779 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002780 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002781 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2782 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002783 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2784 }
2785
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002786 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002787
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002788 /* Info from md_alg will be used instead */
2789 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002790
2791 /*
2792 * Signature
2793 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002794 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2795 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002796 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002797 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2798 " for verify message" ) );
2799 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002800 }
2801
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002802 /*
2803 * Check the certificate's key type matches the signature alg
2804 */
2805 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2806 {
2807 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2808 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2809 }
Paul Bakker577e0062013-08-28 11:57:20 +02002810 }
2811 else
2812#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2813 {
2814 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002815 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002816 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002817
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002818 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002819
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002820 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 {
2822 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002823 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002824 }
2825
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002826 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002827 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002828 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002829 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002830 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002831 return( ret );
2832 }
2833
2834 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2835
Paul Bakkered27a042013-04-18 22:46:23 +02002836 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002838#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2839 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2840 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Paul Bakkera503a632013-08-14 13:48:06 +02002842#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002843static int ssl_write_new_session_ticket( ssl_context *ssl )
2844{
2845 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002846 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002847 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002848
2849 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2850
2851 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2852 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2853
2854 /*
2855 * struct {
2856 * uint32 ticket_lifetime_hint;
2857 * opaque ticket<0..2^16-1>;
2858 * } NewSessionTicket;
2859 *
2860 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2861 * 8 . 9 ticket_len (n)
2862 * 10 . 9+n ticket content
2863 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002864
2865 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2866 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2867 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2868 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002869
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002870 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2871 {
2872 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2873 tlen = 0;
2874 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002875
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002876 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2877 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002878
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002879 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002880
2881 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2882 {
2883 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2884 return( ret );
2885 }
2886
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002887 /* No need to remember writing a NewSessionTicket any more */
2888 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002889
2890 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2891
2892 return( 0 );
2893}
Paul Bakkera503a632013-08-14 13:48:06 +02002894#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002895
Paul Bakker5121ce52009-01-03 21:22:43 +00002896/*
Paul Bakker1961b702013-01-25 14:49:24 +01002897 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 */
Paul Bakker1961b702013-01-25 14:49:24 +01002899int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002900{
2901 int ret = 0;
2902
Paul Bakker1961b702013-01-25 14:49:24 +01002903 if( ssl->state == SSL_HANDSHAKE_OVER )
2904 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Paul Bakker1961b702013-01-25 14:49:24 +01002906 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2907
2908 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2909 return( ret );
2910
2911 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 {
Paul Bakker1961b702013-01-25 14:49:24 +01002913 case SSL_HELLO_REQUEST:
2914 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 break;
2916
Paul Bakker1961b702013-01-25 14:49:24 +01002917 /*
2918 * <== ClientHello
2919 */
2920 case SSL_CLIENT_HELLO:
2921 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002923
2924 /*
2925 * ==> ServerHello
2926 * Certificate
2927 * ( ServerKeyExchange )
2928 * ( CertificateRequest )
2929 * ServerHelloDone
2930 */
2931 case SSL_SERVER_HELLO:
2932 ret = ssl_write_server_hello( ssl );
2933 break;
2934
2935 case SSL_SERVER_CERTIFICATE:
2936 ret = ssl_write_certificate( ssl );
2937 break;
2938
2939 case SSL_SERVER_KEY_EXCHANGE:
2940 ret = ssl_write_server_key_exchange( ssl );
2941 break;
2942
2943 case SSL_CERTIFICATE_REQUEST:
2944 ret = ssl_write_certificate_request( ssl );
2945 break;
2946
2947 case SSL_SERVER_HELLO_DONE:
2948 ret = ssl_write_server_hello_done( ssl );
2949 break;
2950
2951 /*
2952 * <== ( Certificate/Alert )
2953 * ClientKeyExchange
2954 * ( CertificateVerify )
2955 * ChangeCipherSpec
2956 * Finished
2957 */
2958 case SSL_CLIENT_CERTIFICATE:
2959 ret = ssl_parse_certificate( ssl );
2960 break;
2961
2962 case SSL_CLIENT_KEY_EXCHANGE:
2963 ret = ssl_parse_client_key_exchange( ssl );
2964 break;
2965
2966 case SSL_CERTIFICATE_VERIFY:
2967 ret = ssl_parse_certificate_verify( ssl );
2968 break;
2969
2970 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2971 ret = ssl_parse_change_cipher_spec( ssl );
2972 break;
2973
2974 case SSL_CLIENT_FINISHED:
2975 ret = ssl_parse_finished( ssl );
2976 break;
2977
2978 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002979 * ==> ( NewSessionTicket )
2980 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002981 * Finished
2982 */
2983 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002984#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002985 if( ssl->handshake->new_session_ticket != 0 )
2986 ret = ssl_write_new_session_ticket( ssl );
2987 else
Paul Bakkera503a632013-08-14 13:48:06 +02002988#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002989 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002990 break;
2991
2992 case SSL_SERVER_FINISHED:
2993 ret = ssl_write_finished( ssl );
2994 break;
2995
2996 case SSL_FLUSH_BUFFERS:
2997 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2998 ssl->state = SSL_HANDSHAKE_WRAPUP;
2999 break;
3000
3001 case SSL_HANDSHAKE_WRAPUP:
3002 ssl_handshake_wrapup( ssl );
3003 break;
3004
3005 default:
3006 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3007 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003008 }
3009
Paul Bakker5121ce52009-01-03 21:22:43 +00003010 return( ret );
3011}
Paul Bakker5121ce52009-01-03 21:22:43 +00003012#endif