blob: 42ff46732102a4dd03792e84e5519e04c0ac8e75 [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é-Gonnard3ebb2cd2013-09-23 17:00:18 +02001397#if defined(POLARSSL_X509_CRT_PARSE_C)
1398 /*
1399 * Final check: if ciphersuite requires us to have a
1400 * certificate/key of a particular type:
1401 * - select the appropriate certificate if we have one, or
1402 * - try the next ciphersuite if we don't
1403 * This must be done last since we modify the key_cert list.
1404 */
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02001405 if( ssl_pick_cert( ssl, ciphersuite_info ) != 0 )
1406 continue;
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02001407#endif
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001408
Paul Bakker41c83d32013-03-20 14:39:14 +01001409 goto have_ciphersuite;
1410 }
1411 }
1412 }
1413
1414 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1415
1416 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1417 return( ret );
1418
1419 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1420
1421have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001422 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001423 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1424 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1425
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 ssl->in_left = 0;
1427 ssl->state++;
1428
1429 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1430
1431 return( 0 );
1432}
1433
Paul Bakker1f2bc622013-08-15 13:45:55 +02001434#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001435static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1436 unsigned char *buf,
1437 size_t *olen )
1438{
1439 unsigned char *p = buf;
1440
1441 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1442 {
1443 *olen = 0;
1444 return;
1445 }
1446
1447 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1448
1449 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1450 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1451
1452 *p++ = 0x00;
1453 *p++ = 0x00;
1454
1455 *olen = 4;
1456}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001457#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001458
Paul Bakkera503a632013-08-14 13:48:06 +02001459#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001460static void ssl_write_session_ticket_ext( ssl_context *ssl,
1461 unsigned char *buf,
1462 size_t *olen )
1463{
1464 unsigned char *p = buf;
1465
1466 if( ssl->handshake->new_session_ticket == 0 )
1467 {
1468 *olen = 0;
1469 return;
1470 }
1471
1472 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1473
1474 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1475 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1476
1477 *p++ = 0x00;
1478 *p++ = 0x00;
1479
1480 *olen = 4;
1481}
Paul Bakkera503a632013-08-14 13:48:06 +02001482#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001483
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001484static void ssl_write_renegotiation_ext( ssl_context *ssl,
1485 unsigned char *buf,
1486 size_t *olen )
1487{
1488 unsigned char *p = buf;
1489
1490 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1491 {
1492 *olen = 0;
1493 return;
1494 }
1495
1496 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1497
1498 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1499 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1500
1501 *p++ = 0x00;
1502 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1503 *p++ = ssl->verify_data_len * 2 & 0xFF;
1504
1505 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1506 p += ssl->verify_data_len;
1507 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1508 p += ssl->verify_data_len;
1509
1510 *olen = 5 + ssl->verify_data_len * 2;
1511}
1512
Paul Bakker05decb22013-08-15 13:33:48 +02001513#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001514static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1515 unsigned char *buf,
1516 size_t *olen )
1517{
1518 unsigned char *p = buf;
1519
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001520 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1521 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001522 *olen = 0;
1523 return;
1524 }
1525
1526 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1527
1528 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1529 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1530
1531 *p++ = 0x00;
1532 *p++ = 1;
1533
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001534 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001535
1536 *olen = 5;
1537}
Paul Bakker05decb22013-08-15 13:33:48 +02001538#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001539
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001540#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001541static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1542 unsigned char *buf,
1543 size_t *olen )
1544{
1545 unsigned char *p = buf;
1546 ((void) ssl);
1547
Paul Bakker677377f2013-10-28 12:54:26 +01001548 if( ( ssl->handshake->cli_exts &
1549 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1550 {
1551 *olen = 0;
1552 return;
1553 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001554
1555 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1556
1557 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1558 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1559
1560 *p++ = 0x00;
1561 *p++ = 2;
1562
1563 *p++ = 1;
1564 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1565
1566 *olen = 6;
1567}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001568#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001569
Paul Bakker5121ce52009-01-03 21:22:43 +00001570static int ssl_write_server_hello( ssl_context *ssl )
1571{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001572#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001573 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001574#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001575 int ret;
1576 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 unsigned char *buf, *p;
1578
1579 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1580
1581 /*
1582 * 0 . 0 handshake type
1583 * 1 . 3 handshake length
1584 * 4 . 5 protocol version
1585 * 6 . 9 UNIX time()
1586 * 10 . 37 random bytes
1587 */
1588 buf = ssl->out_msg;
1589 p = buf + 4;
1590
1591 *p++ = (unsigned char) ssl->major_ver;
1592 *p++ = (unsigned char) ssl->minor_ver;
1593
1594 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1595 buf[4], buf[5] ) );
1596
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001597#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 t = time( NULL );
1599 *p++ = (unsigned char)( t >> 24 );
1600 *p++ = (unsigned char)( t >> 16 );
1601 *p++ = (unsigned char)( t >> 8 );
1602 *p++ = (unsigned char)( t );
1603
1604 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001605#else
1606 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1607 return( ret );
1608
1609 p += 4;
1610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Paul Bakkera3d195c2011-11-27 21:07:34 +00001612 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1613 return( ret );
1614
1615 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Paul Bakker48916f92012-09-16 19:57:18 +00001617 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001618
1619 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1620
1621 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001622 * Resume is 0 by default, see ssl_handshake_init().
1623 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1624 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001625 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001626 if( ssl->handshake->resume == 0 &&
1627 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001628 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001629 ssl->f_get_cache != NULL &&
1630 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1631 {
1632 ssl->handshake->resume = 1;
1633 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001635 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636 {
1637 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001638 * New session, create a new session id,
1639 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 ssl->state++;
1642
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001643#if defined(POLARSSL_HAVE_TIME)
1644 ssl->session_negotiate->start = time( NULL );
1645#endif
1646
Paul Bakkera503a632013-08-14 13:48:06 +02001647#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001648 if( ssl->handshake->new_session_ticket != 0 )
1649 {
1650 ssl->session_negotiate->length = n = 0;
1651 memset( ssl->session_negotiate->id, 0, 32 );
1652 }
1653 else
1654#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001655 {
1656 ssl->session_negotiate->length = n = 32;
1657 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001658 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001659 return( ret );
1660 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001661 }
1662 else
1663 {
1664 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001665 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001666 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001667 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001669
1670 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1671 {
1672 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1673 return( ret );
1674 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 }
1676
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001677 /*
1678 * 38 . 38 session id length
1679 * 39 . 38+n session id
1680 * 39+n . 40+n chosen ciphersuite
1681 * 41+n . 41+n chosen compression alg.
1682 * 42+n . 43+n extensions length
1683 * 44+n . 43+n+m extensions
1684 */
1685 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001686 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1687 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
1689 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1690 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1691 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001692 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001693
Paul Bakker48916f92012-09-16 19:57:18 +00001694 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1695 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1696 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001698 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1699 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001700 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001701 ssl->session_negotiate->compression ) );
1702
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001703 /*
1704 * First write extensions, then the total length
1705 */
1706 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1707 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001708
Paul Bakker05decb22013-08-15 13:33:48 +02001709#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001710 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1711 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001712#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001713
Paul Bakker1f2bc622013-08-15 13:45:55 +02001714#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001715 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1716 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001717#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001718
Paul Bakkera503a632013-08-14 13:48:06 +02001719#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001720 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1721 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001722#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001723
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001724#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001725 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1726 ext_len += olen;
1727#endif
1728
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001729 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001730
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001731 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1732 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1733 p += ext_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
1735 ssl->out_msglen = p - buf;
1736 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1737 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1738
1739 ret = ssl_write_record( ssl );
1740
1741 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1742
1743 return( ret );
1744}
1745
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001746#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1747 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001748 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1749 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001750static int ssl_write_certificate_request( ssl_context *ssl )
1751{
Paul Bakkered27a042013-04-18 22:46:23 +02001752 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1753 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001754
1755 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1756
1757 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001758 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1759 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001760 {
1761 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1762 ssl->state++;
1763 return( 0 );
1764 }
1765
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001766 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001767 return( ret );
1768}
1769#else
1770static int ssl_write_certificate_request( ssl_context *ssl )
1771{
1772 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1773 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001774 size_t dn_size, total_dn_size; /* excluding length bytes */
1775 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001776 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001777 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
1779 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1780
1781 ssl->state++;
1782
Paul Bakkerfbb17802013-04-17 19:10:21 +02001783 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001784 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001785 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001786 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001788 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 return( 0 );
1790 }
1791
1792 /*
1793 * 0 . 0 handshake type
1794 * 1 . 3 handshake length
1795 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001796 * 5 .. m-1 cert types
1797 * m .. m+1 sig alg length (TLS 1.2 only)
1798 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 * n .. n+1 length of all DNs
1800 * n+2 .. n+3 length of DN 1
1801 * n+4 .. ... Distinguished Name #1
1802 * ... .. ... length of DN 2, etc.
1803 */
1804 buf = ssl->out_msg;
1805 p = buf + 4;
1806
1807 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001808 * Supported certificate types
1809 *
1810 * ClientCertificateType certificate_types<1..2^8-1>;
1811 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001813 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01001814
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001815#if defined(POLARSSL_RSA_C)
1816 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
1817#endif
1818#if defined(POLARSSL_ECDSA_C)
1819 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
1820#endif
1821
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001822 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001823 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01001824
Paul Bakker577e0062013-08-28 11:57:20 +02001825 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001826#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01001827 /*
1828 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01001829 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001830 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
1831 *
1832 * struct {
1833 * HashAlgorithm hash;
1834 * SignatureAlgorithm signature;
1835 * } SignatureAndHashAlgorithm;
1836 *
1837 * enum { (255) } HashAlgorithm;
1838 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01001839 */
Paul Bakker21dca692013-01-03 11:41:08 +01001840 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01001841 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001842 /*
1843 * Only use current running hash algorithm that is already required
1844 * for requested ciphersuite.
1845 */
Paul Bakker926af752012-11-23 13:38:07 +01001846 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
1847
Paul Bakkerb7149bc2013-03-20 15:30:09 +01001848 if( ssl->transform_negotiate->ciphersuite_info->mac ==
1849 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01001850 {
1851 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
1852 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02001853
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001854 /*
1855 * Supported signature algorithms
1856 */
1857#if defined(POLARSSL_RSA_C)
1858 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1859 p[2 + sa_len++] = SSL_SIG_RSA;
1860#endif
1861#if defined(POLARSSL_ECDSA_C)
1862 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
1863 p[2 + sa_len++] = SSL_SIG_ECDSA;
1864#endif
Paul Bakker926af752012-11-23 13:38:07 +01001865
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001866 p[0] = (unsigned char)( sa_len >> 8 );
1867 p[1] = (unsigned char)( sa_len );
1868 sa_len += 2;
1869 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01001870 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001871#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001873 /*
1874 * DistinguishedName certificate_authorities<0..2^16-1>;
1875 * opaque DistinguishedName<1..2^16-1>;
1876 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 p += 2;
1878 crt = ssl->ca_chain;
1879
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001880 total_dn_size = 0;
Paul Bakker29087132010-03-21 21:03:34 +00001881 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00001882 {
1883 if( p - buf > 4096 )
1884 break;
1885
Paul Bakker926af752012-11-23 13:38:07 +01001886 dn_size = crt->subject_raw.len;
1887 *p++ = (unsigned char)( dn_size >> 8 );
1888 *p++ = (unsigned char)( dn_size );
1889 memcpy( p, crt->subject_raw.p, dn_size );
1890 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001891
Paul Bakker926af752012-11-23 13:38:07 +01001892 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
1893
Paul Bakkerbc3d9842012-11-26 16:12:02 +01001894 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01001895 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 }
1897
Paul Bakker926af752012-11-23 13:38:07 +01001898 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1900 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001901 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
1902 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
1904 ret = ssl_write_record( ssl );
1905
1906 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
1907
1908 return( ret );
1909}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001910#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
1911 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
1912 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00001913
Paul Bakker41c83d32013-03-20 14:39:14 +01001914static int ssl_write_server_key_exchange( ssl_context *ssl )
1915{
Paul Bakker23986e52011-04-24 08:57:21 +00001916 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001917 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001918 const ssl_ciphersuite_t *ciphersuite_info =
1919 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001920
1921#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1922 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1923 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001924 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02001925 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001926 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001927 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02001928 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02001929 ((void) dig_signed);
1930 ((void) dig_signed_len);
1931#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001932
Paul Bakker5121ce52009-01-03 21:22:43 +00001933 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
1934
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02001935 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
1936 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
1937 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 {
1939 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
1940 ssl->state++;
1941 return( 0 );
1942 }
1943
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001944#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
1945 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1946 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1947 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001948 {
1949 /* TODO: Support identity hints */
1950 *(p++) = 0x00;
1951 *(p++) = 0x00;
1952
1953 n += 2;
1954 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001955#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
1956 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957
1958#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
1959 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
1960 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
1961 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00001962 {
Paul Bakker41c83d32013-03-20 14:39:14 +01001963 /*
1964 * Ephemeral DH parameters:
1965 *
1966 * struct {
1967 * opaque dh_p<1..2^16-1>;
1968 * opaque dh_g<1..2^16-1>;
1969 * opaque dh_Ys<1..2^16-1>;
1970 * } ServerDHParams;
1971 */
1972 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
1973 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
1974 {
1975 SSL_DEBUG_RET( 1, "mpi_copy", ret );
1976 return( ret );
1977 }
Paul Bakker48916f92012-09-16 19:57:18 +00001978
Paul Bakker41c83d32013-03-20 14:39:14 +01001979 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001980 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001981 p,
1982 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01001983 {
1984 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
1985 return( ret );
1986 }
1987
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02001988 dig_signed = p;
1989 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990
1991 p += len;
1992 n += len;
1993
Paul Bakker41c83d32013-03-20 14:39:14 +01001994 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
1995 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
1996 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
1997 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
1998 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001999#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2000 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002001
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002002#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002003 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2004 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2005
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002006 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002007 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2008 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002010 /*
2011 * Ephemeral ECDH parameters:
2012 *
2013 * struct {
2014 * ECParameters curve_params;
2015 * ECPoint public;
2016 * } ServerECDHParams;
2017 */
Paul Bakker41c83d32013-03-20 14:39:14 +01002018 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02002019 ssl->handshake->curves[0]->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002020 {
2021 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2022 return( ret );
2023 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002024
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +02002025 SSL_DEBUG_MSG( 2, ( "ECDH curve size: %d",
2026 (int) ssl->handshake->ecdh_ctx.grp.nbits ) );
2027
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002028 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2029 p, SSL_MAX_CONTENT_LEN - n,
2030 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002031 {
2032 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2033 return( ret );
2034 }
2035
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002036 dig_signed = p;
2037 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002038
2039 p += len;
2040 n += len;
2041
Paul Bakker41c83d32013-03-20 14:39:14 +01002042 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2043 }
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002044#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002045 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2046 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002047
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002048#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002049 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2050 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002051 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002052 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2053 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002054 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002055 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002056 unsigned int hashlen = 0;
2057 unsigned char hash[64];
2058 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002059
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002060 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002061 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2062 */
Paul Bakker577e0062013-08-28 11:57:20 +02002063#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002064 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2065 {
2066 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2067
2068 if( md_alg == POLARSSL_MD_NONE )
2069 {
2070 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2071 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2072 }
2073 }
Paul Bakker577e0062013-08-28 11:57:20 +02002074 else
2075#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002076#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2077 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002078 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002079 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2080 {
2081 md_alg = POLARSSL_MD_SHA1;
2082 }
2083 else
Paul Bakker577e0062013-08-28 11:57:20 +02002084#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002085 {
2086 md_alg = POLARSSL_MD_NONE;
2087 }
2088
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002089 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002090 * Compute the hash to be signed
2091 */
Paul Bakker577e0062013-08-28 11:57:20 +02002092#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2093 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002094 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002095 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002096 md5_context md5;
2097 sha1_context sha1;
2098
2099 /*
2100 * digitally-signed struct {
2101 * opaque md5_hash[16];
2102 * opaque sha_hash[20];
2103 * };
2104 *
2105 * md5_hash
2106 * MD5(ClientHello.random + ServerHello.random
2107 * + ServerParams);
2108 * sha_hash
2109 * SHA(ClientHello.random + ServerHello.random
2110 * + ServerParams);
2111 */
2112 md5_starts( &md5 );
2113 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002114 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002115 md5_finish( &md5, hash );
2116
2117 sha1_starts( &sha1 );
2118 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002119 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002120 sha1_finish( &sha1, hash + 16 );
2121
2122 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002123 }
2124 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002125#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2126 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002127#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2128 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002129 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002130 {
2131 md_context_t ctx;
2132
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002133 /* Info from md_alg will be used instead */
2134 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002135
2136 /*
2137 * digitally-signed struct {
2138 * opaque client_random[32];
2139 * opaque server_random[32];
2140 * ServerDHParams params;
2141 * };
2142 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002143 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002144 {
2145 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2146 return( ret );
2147 }
2148
2149 md_starts( &ctx );
2150 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002151 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002152 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002153
2154 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2155 {
2156 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2157 return( ret );
2158 }
2159
Paul Bakker23f36802012-09-28 14:15:14 +00002160 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002161 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002162#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2163 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002164 {
2165 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002166 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002167 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002168
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002169 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2170 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002171
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002172 /*
2173 * Make the signature
2174 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002175 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002176 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002177 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2178 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002179 }
Paul Bakker23f36802012-09-28 14:15:14 +00002180
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002181#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002182 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2183 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002184 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002185 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002186
2187 n += 2;
2188 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002189#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002190
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002191 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002192 p + 2 , &signature_len,
2193 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002194 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002195 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002196 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002197 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002198
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002199 *(p++) = (unsigned char)( signature_len >> 8 );
2200 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002201 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002202
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002203 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002204
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002205 p += signature_len;
2206 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002207 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002208#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002209 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2210 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002211
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002212 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2214 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2215
2216 ssl->state++;
2217
2218 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2219 {
2220 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2221 return( ret );
2222 }
2223
2224 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2225
2226 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002227}
2228
2229static int ssl_write_server_hello_done( ssl_context *ssl )
2230{
2231 int ret;
2232
2233 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2234
2235 ssl->out_msglen = 4;
2236 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2237 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2238
2239 ssl->state++;
2240
2241 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2242 {
2243 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2244 return( ret );
2245 }
2246
2247 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2248
2249 return( 0 );
2250}
2251
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002252#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2253 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2254static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2255 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002256{
2257 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002258 size_t n;
2259
2260 /*
2261 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2262 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002263 if( *p + 2 > end )
2264 {
2265 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2266 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2267 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002268
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002269 n = ( (*p)[0] << 8 ) | (*p)[1];
2270 *p += 2;
2271
2272 if( n < 1 || n > ssl->handshake->dhm_ctx.len || *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002273 {
2274 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2275 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2276 }
2277
2278 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx,
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002279 *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002280 {
2281 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2282 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2283 }
2284
2285 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2286
Paul Bakker70df2fb2013-04-17 17:19:09 +02002287 return( ret );
2288}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002289#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2290 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002291
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002292#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2293 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002294static int ssl_parse_encrypted_pms( ssl_context *ssl,
2295 const unsigned char *p,
2296 const unsigned char *end,
2297 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002298{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002299 int ret;
2300 size_t len = pk_get_len( ssl_own_key( ssl ) );
2301 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002302
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002303 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002304 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002305 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002306 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2307 }
2308
2309 /*
2310 * Decrypt the premaster using own private RSA key
2311 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002312#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2313 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002314 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2315 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002316 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2317 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002318 {
2319 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2321 }
2322 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002323#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002324
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002325 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002326 {
2327 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2328 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2329 }
2330
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002331 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2332 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002333 sizeof(ssl->handshake->premaster),
2334 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002335
2336 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002337 pms[0] != ssl->handshake->max_major_ver ||
2338 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002339 {
2340 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2341
2342 /*
2343 * Protection against Bleichenbacher's attack:
2344 * invalid PKCS#1 v1.5 padding must not cause
2345 * the connection to end immediately; instead,
2346 * send a bad_record_mac later in the handshake.
2347 */
2348 ssl->handshake->pmslen = 48;
2349
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002350 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002351 if( ret != 0 )
2352 return( ret );
2353 }
2354
2355 return( ret );
2356}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002357#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2358 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002359
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002360#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002361static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2362 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002363{
Paul Bakker6db455e2013-09-18 17:29:31 +02002364 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002365 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002366
Paul Bakker6db455e2013-09-18 17:29:31 +02002367 if( ssl->f_psk == NULL &&
2368 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2369 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002370 {
2371 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2372 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2373 }
2374
2375 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002377 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002378 if( *p + 2 > end )
2379 {
2380 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2381 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2382 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002383
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002384 n = ( (*p)[0] << 8 ) | (*p)[1];
2385 *p += 2;
2386
2387 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002388 {
2389 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2390 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2391 }
2392
Paul Bakker6db455e2013-09-18 17:29:31 +02002393 if( ssl->f_psk != NULL )
2394 {
2395 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2396 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2397 }
2398
2399 if( ret == 0 )
2400 {
2401 if( n != ssl->psk_identity_len ||
2402 memcmp( ssl->psk_identity, *p, n ) != 0 )
2403 {
2404 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2405 }
2406 }
2407
2408 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002409 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002410 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002411 if( ( ret = ssl_send_alert_message( ssl,
2412 SSL_ALERT_LEVEL_FATAL,
2413 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2414 {
2415 return( ret );
2416 }
2417
2418 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002419 }
2420
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002421 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002422 ret = 0;
2423
Paul Bakkerfbb17802013-04-17 19:10:21 +02002424 return( ret );
2425}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002426#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002427
Paul Bakker5121ce52009-01-03 21:22:43 +00002428static int ssl_parse_client_key_exchange( ssl_context *ssl )
2429{
Paul Bakker23986e52011-04-24 08:57:21 +00002430 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002431 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002432
Paul Bakker41c83d32013-03-20 14:39:14 +01002433 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
2435 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2436
2437 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2438 {
2439 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2440 return( ret );
2441 }
2442
2443 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2444 {
2445 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448
2449 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2450 {
2451 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002452 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002453 }
2454
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002455#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002456 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002458 unsigned char *p = ssl->in_msg + 4;
2459 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2460
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002461 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002462 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002463 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2464 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002466
2467 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2468
2469 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2470 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002471 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002472 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002473 {
2474 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2475 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2476 }
2477
2478 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002479 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002480 else
2481#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002482#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2483 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
2484 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2485 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002486 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002487 size_t n = ssl->in_msg[3];
2488
2489 if( n < 1 || n > mpi_size( &ssl->handshake->ecdh_ctx.grp.P ) * 2 + 2 ||
2490 n + 4 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002491 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002492 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2493 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002494 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002496 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2497 ssl->in_msg + 4, n ) ) != 0 )
2498 {
2499 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2500 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2501 }
2502
2503 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2504
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002505 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2506 &ssl->handshake->pmslen,
2507 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002508 POLARSSL_MPI_MAX_SIZE,
2509 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002510 {
2511 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2512 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2513 }
2514
2515 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002518#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2519 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2521 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002522 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002523 unsigned char *p = ssl->in_msg + 4;
2524 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2525
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002527 {
2528 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2529 return( ret );
2530 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002531
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002532 if( ( ret = ssl_psk_derive_premaster( ssl,
2533 ciphersuite_info->key_exchange ) ) != 0 )
2534 {
2535 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2536 return( ret );
2537 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002538 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002539 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002540#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002541#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2542 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2543 {
2544 unsigned char *p = ssl->in_msg + 4;
2545 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2546
2547 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2548 {
2549 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2550 return( ret );
2551 }
2552
2553 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2554 {
2555 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2556 return( ret );
2557 }
2558
2559 if( ( ret = ssl_psk_derive_premaster( ssl,
2560 ciphersuite_info->key_exchange ) ) != 0 )
2561 {
2562 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2563 return( ret );
2564 }
2565 }
2566 else
2567#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2569 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2570 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002571 unsigned char *p = ssl->in_msg + 4;
2572 unsigned char *end = ssl->in_msg + ssl->in_msglen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573
2574 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2575 {
2576 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2577 return( ret );
2578 }
2579 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2580 {
2581 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2582 return( ret );
2583 }
2584
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002585 if( ( ret = ssl_psk_derive_premaster( ssl,
2586 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002587 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002588 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2589 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002590 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002591 }
2592 else
2593#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002594#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2595 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2596 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002597 unsigned char *p = ssl->in_msg + 4;
2598 unsigned char *end = ssl->in_msg + ssl->in_msglen;
2599
2600 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2601 {
2602 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2603 return( ret );
2604 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002605
2606 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2607 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002608 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002609 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002611 }
2612
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002613 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2614
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002615 if( ( ret = ssl_psk_derive_premaster( ssl,
2616 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002617 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002618 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002619 return( ret );
2620 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002621 }
2622 else
2623#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002624#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2625 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002626 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002627 if( ( ret = ssl_parse_encrypted_pms( ssl,
2628 ssl->in_msg + 4,
2629 ssl->in_msg + ssl->in_msglen,
2630 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002631 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002632 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_ecrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002633 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002634 }
2635 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 else
2637#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2638 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002639 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002640 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2641 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002642
Paul Bakkerff60ee62010-03-16 21:09:09 +00002643 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2644 {
2645 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2646 return( ret );
2647 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002648
Paul Bakker5121ce52009-01-03 21:22:43 +00002649 ssl->state++;
2650
2651 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2652
2653 return( 0 );
2654}
2655
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002656#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2657 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002658 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2659 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002660static int ssl_parse_certificate_verify( ssl_context *ssl )
2661{
Paul Bakkered27a042013-04-18 22:46:23 +02002662 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002663 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002664
2665 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2666
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002667 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002668 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002670 {
2671 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2672 ssl->state++;
2673 return( 0 );
2674 }
2675
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002676 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002677 return( ret );
2678}
2679#else
2680static int ssl_parse_certificate_verify( ssl_context *ssl )
2681{
2682 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002685 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002686 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002687#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002688 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002689#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002690 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002691 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2692
2693 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2694
2695 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002696 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002697 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2698 {
2699 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2700 ssl->state++;
2701 return( 0 );
2702 }
2703
Paul Bakkered27a042013-04-18 22:46:23 +02002704 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 {
2706 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2707 ssl->state++;
2708 return( 0 );
2709 }
2710
Paul Bakker48916f92012-09-16 19:57:18 +00002711 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
2713 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2714 {
2715 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2716 return( ret );
2717 }
2718
2719 ssl->state++;
2720
2721 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2722 {
2723 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002724 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 }
2726
2727 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
2728 {
2729 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002730 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002731 }
2732
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002733 /*
2734 * 0 . 0 handshake type
2735 * 1 . 3 handshake length
2736 * 4 . 5 sig alg (TLS 1.2 only)
2737 * 4+n . 5+n signature length (n = sa_len)
2738 * 6+n . 6+n+m signature (m = sig_len)
2739 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2742 defined(POLARSSL_SSL_PROTO_TLS1_1)
2743 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002744 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002745 sa_len = 0;
2746
Paul Bakkerc70b9822013-04-07 22:00:46 +02002747 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002748 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002749
2750 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
2751 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
2752 POLARSSL_PK_ECDSA ) )
2753 {
2754 hash_start += 16;
2755 hashlen -= 16;
2756 md_alg = POLARSSL_MD_SHA1;
2757 }
Paul Bakker926af752012-11-23 13:38:07 +01002758 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002759 else
2760#endif
Paul Bakker577e0062013-08-28 11:57:20 +02002761#if defined(POLARSSL_SSL_PROTO_TLS1_2)
2762 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002763 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002764 sa_len = 2;
2765
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002767 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002769 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00002770 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002771 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2772 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01002773 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2774 }
2775
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002776 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01002777
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002778 /* Info from md_alg will be used instead */
2779 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002780
2781 /*
2782 * Signature
2783 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002784 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
2785 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002786 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002787 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
2788 " for verify message" ) );
2789 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002790 }
2791
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002792 /*
2793 * Check the certificate's key type matches the signature alg
2794 */
2795 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
2796 {
2797 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
2798 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
2799 }
Paul Bakker577e0062013-08-28 11:57:20 +02002800 }
2801 else
2802#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
2803 {
2804 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002805 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02002806 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02002807
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002808 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01002809
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002810 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 {
2812 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002813 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002814 }
2815
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002816 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002817 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02002818 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002819 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002820 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002821 return( ret );
2822 }
2823
2824 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
2825
Paul Bakkered27a042013-04-18 22:46:23 +02002826 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002827}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002828#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2829 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
2830 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Paul Bakkera503a632013-08-14 13:48:06 +02002832#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002833static int ssl_write_new_session_ticket( ssl_context *ssl )
2834{
2835 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002836 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002837 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002838
2839 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
2840
2841 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2842 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
2843
2844 /*
2845 * struct {
2846 * uint32 ticket_lifetime_hint;
2847 * opaque ticket<0..2^16-1>;
2848 * } NewSessionTicket;
2849 *
2850 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
2851 * 8 . 9 ticket_len (n)
2852 * 10 . 9+n ticket content
2853 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002854
2855 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
2856 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
2857 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
2858 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002859
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02002860 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
2861 {
2862 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
2863 tlen = 0;
2864 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002865
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002866 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
2867 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002868
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02002869 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002870
2871 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2872 {
2873 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2874 return( ret );
2875 }
2876
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002877 /* No need to remember writing a NewSessionTicket any more */
2878 ssl->handshake->new_session_ticket = 0;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002879
2880 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
2881
2882 return( 0 );
2883}
Paul Bakkera503a632013-08-14 13:48:06 +02002884#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002885
Paul Bakker5121ce52009-01-03 21:22:43 +00002886/*
Paul Bakker1961b702013-01-25 14:49:24 +01002887 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00002888 */
Paul Bakker1961b702013-01-25 14:49:24 +01002889int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00002890{
2891 int ret = 0;
2892
Paul Bakker1961b702013-01-25 14:49:24 +01002893 if( ssl->state == SSL_HANDSHAKE_OVER )
2894 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Paul Bakker1961b702013-01-25 14:49:24 +01002896 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
2897
2898 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
2899 return( ret );
2900
2901 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00002902 {
Paul Bakker1961b702013-01-25 14:49:24 +01002903 case SSL_HELLO_REQUEST:
2904 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 break;
2906
Paul Bakker1961b702013-01-25 14:49:24 +01002907 /*
2908 * <== ClientHello
2909 */
2910 case SSL_CLIENT_HELLO:
2911 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00002912 break;
Paul Bakker1961b702013-01-25 14:49:24 +01002913
2914 /*
2915 * ==> ServerHello
2916 * Certificate
2917 * ( ServerKeyExchange )
2918 * ( CertificateRequest )
2919 * ServerHelloDone
2920 */
2921 case SSL_SERVER_HELLO:
2922 ret = ssl_write_server_hello( ssl );
2923 break;
2924
2925 case SSL_SERVER_CERTIFICATE:
2926 ret = ssl_write_certificate( ssl );
2927 break;
2928
2929 case SSL_SERVER_KEY_EXCHANGE:
2930 ret = ssl_write_server_key_exchange( ssl );
2931 break;
2932
2933 case SSL_CERTIFICATE_REQUEST:
2934 ret = ssl_write_certificate_request( ssl );
2935 break;
2936
2937 case SSL_SERVER_HELLO_DONE:
2938 ret = ssl_write_server_hello_done( ssl );
2939 break;
2940
2941 /*
2942 * <== ( Certificate/Alert )
2943 * ClientKeyExchange
2944 * ( CertificateVerify )
2945 * ChangeCipherSpec
2946 * Finished
2947 */
2948 case SSL_CLIENT_CERTIFICATE:
2949 ret = ssl_parse_certificate( ssl );
2950 break;
2951
2952 case SSL_CLIENT_KEY_EXCHANGE:
2953 ret = ssl_parse_client_key_exchange( ssl );
2954 break;
2955
2956 case SSL_CERTIFICATE_VERIFY:
2957 ret = ssl_parse_certificate_verify( ssl );
2958 break;
2959
2960 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
2961 ret = ssl_parse_change_cipher_spec( ssl );
2962 break;
2963
2964 case SSL_CLIENT_FINISHED:
2965 ret = ssl_parse_finished( ssl );
2966 break;
2967
2968 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002969 * ==> ( NewSessionTicket )
2970 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01002971 * Finished
2972 */
2973 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02002974#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002975 if( ssl->handshake->new_session_ticket != 0 )
2976 ret = ssl_write_new_session_ticket( ssl );
2977 else
Paul Bakkera503a632013-08-14 13:48:06 +02002978#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02002979 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01002980 break;
2981
2982 case SSL_SERVER_FINISHED:
2983 ret = ssl_write_finished( ssl );
2984 break;
2985
2986 case SSL_FLUSH_BUFFERS:
2987 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
2988 ssl->state = SSL_HANDSHAKE_WRAPUP;
2989 break;
2990
2991 case SSL_HANDSHAKE_WRAPUP:
2992 ssl_handshake_wrapup( ssl );
2993 break;
2994
2995 default:
2996 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
2997 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00002998 }
2999
Paul Bakker5121ce52009-01-03 21:22:43 +00003000 return( ret );
3001}
Paul Bakker5121ce52009-01-03 21:22:43 +00003002#endif