blob: acf2ef24cda66cd48640ba6bfa92ab81e1471929 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
31#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010032#if defined(POLARSSL_ECP_C)
33#include "polarssl/ecp.h"
34#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker7dc4c442014-02-01 22:50:26 +010036#if defined(POLARSSL_PLATFORM_C)
37#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020038#else
39#define polarssl_malloc malloc
40#define polarssl_free free
41#endif
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdlib.h>
44#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020045
46#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkera503a632013-08-14 13:48:06 +020050#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020051/*
52 * Serialize a session in the following format:
53 * 0 . n-1 session structure, n = sizeof(ssl_session)
54 * n . n+2 peer_cert length = m (0 if no certificate)
55 * n+3 . n+2+m peer cert ASN.1
56 *
57 * Assumes ticket is NULL (always true on server side).
58 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020059static int ssl_save_session( const ssl_session *session,
60 unsigned char *buf, size_t buf_len,
61 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020062{
63 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020064 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020066 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020068
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020069 if( left < sizeof( ssl_session ) )
70 return( -1 );
71
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020072 memcpy( p, session, sizeof( ssl_session ) );
73 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020074 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077 if( session->peer_cert == NULL )
78 cert_len = 0;
79 else
80 cert_len = session->peer_cert->raw.len;
81
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020082 if( left < 3 + cert_len )
83 return( -1 );
84
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020085 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
86 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
87 *p++ = (unsigned char)( cert_len & 0xFF );
88
89 if( session->peer_cert != NULL )
90 memcpy( p, session->peer_cert->raw.p, cert_len );
91
92 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094
95 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020096
97 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020098}
99
100/*
101 * Unserialise session, see ssl_save_session()
102 */
103static int ssl_load_session( ssl_session *session,
104 const unsigned char *buf, size_t len )
105{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200106 const unsigned char *p = buf;
107 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200109 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200111
112 if( p + sizeof( ssl_session ) > end )
113 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
114
115 memcpy( session, p, sizeof( ssl_session ) );
116 p += sizeof( ssl_session );
117
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200118#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200119 if( p + 3 > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
123 p += 3;
124
125 if( cert_len == 0 )
126 {
127 session->peer_cert = NULL;
128 }
129 else
130 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200131 int ret;
132
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200133 if( p + cert_len > end )
134 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
135
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200136 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200137
138 if( session->peer_cert == NULL )
139 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
140
Paul Bakkerb6b09562013-09-18 14:17:41 +0200141 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142
Paul Bakkerddf26b42013-09-18 13:46:23 +0200143 if( ( ret = x509_crt_parse( session->peer_cert, p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200147 session->peer_cert = NULL;
148 return( ret );
149 }
150
151 p += cert_len;
152 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154
155 if( p != end )
156 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
157
158 return( 0 );
159}
160
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200161/*
162 * Create session ticket, secured as recommended in RFC 5077 section 4:
163 *
164 * struct {
165 * opaque key_name[16];
166 * opaque iv[16];
167 * opaque encrypted_state<0..2^16-1>;
168 * opaque mac[32];
169 * } ticket;
170 *
171 * (the internal state structure differs, however).
172 */
173static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
174{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200175 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200176 unsigned char * const start = ssl->out_msg + 10;
177 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200178 unsigned char *state;
179 unsigned char iv[16];
180 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200181
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200182 *tlen = 0;
183
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200184 if( ssl->ticket_keys == NULL )
185 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
186
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200187 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200188 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189 p += 16;
190
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200191 /* Generate and write IV (with a copy for aes_crypt) */
192 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
193 return( ret );
194 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200195 p += 16;
196
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200197 /*
198 * Dump session state
199 *
200 * After the session state itself, we still need room for 16 bytes of
201 * padding and 32 bytes of MAC, so there's only so much room left
202 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200203 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200204 if( ssl_save_session( ssl->session_negotiate, state,
205 SSL_MAX_CONTENT_LEN - (state - ssl->out_ctr) - 48,
206 &clear_len ) != 0 )
207 {
208 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
209 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200210 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200211
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200212 /* Apply PKCS padding */
213 pad_len = 16 - clear_len % 16;
214 enc_len = clear_len + pad_len;
215 for( i = clear_len; i < enc_len; i++ )
216 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200217
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 /* Encrypt */
219 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
220 enc_len, iv, state, state ) ) != 0 )
221 {
222 return( ret );
223 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200224
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200225 /* Write length */
226 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
227 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
228 p = state + enc_len;
229
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200230 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
231 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232 p += 32;
233
234 *tlen = p - start;
235
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200236 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200237
238 return( 0 );
239}
240
241/*
242 * Load session ticket (see ssl_write_ticket for structure)
243 */
244static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200245 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200246 size_t len )
247{
248 int ret;
249 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200250 unsigned char *key_name = buf;
251 unsigned char *iv = buf + 16;
252 unsigned char *enc_len_p = iv + 16;
253 unsigned char *ticket = enc_len_p + 2;
254 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200255 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200256 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100257 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258
259 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200260
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200261 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200262 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
263
264 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
265 mac = ticket + enc_len;
266
267 if( len != enc_len + 66 )
268 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
269
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100270 /* Check name, in constant time though it's not a big secret */
271 diff = 0;
272 for( i = 0; i < 16; i++ )
273 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
274 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100276 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200277 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
278 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100279
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200280 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100281 diff |= mac[i] ^ computed_mac[i];
282
283 /* Now return if ticket is not authentic, since we want to avoid
284 * decrypting arbitrary attacker-chosen data */
285 if( diff != 0 )
286 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200287
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200288 /* Decrypt */
289 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
290 enc_len, iv, ticket, ticket ) ) != 0 )
291 {
292 return( ret );
293 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200294
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200295 /* Check PKCS padding */
296 pad_len = ticket[enc_len - 1];
297
298 ret = 0;
299 for( i = 2; i < pad_len; i++ )
300 if( ticket[enc_len - i] != pad_len )
301 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
302 if( ret != 0 )
303 return( ret );
304
305 clear_len = enc_len - pad_len;
306
307 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
308
309 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200310 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
311 {
312 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100313 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200314 return( ret );
315 }
316
Paul Bakker606b4ba2013-08-14 16:52:14 +0200317#if defined(POLARSSL_HAVE_TIME)
318 /* Check if still valid */
319 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
320 {
321 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100322 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200323 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
324 }
325#endif
326
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200327 /*
328 * Keep the session ID sent by the client, since we MUST send it back to
329 * inform him we're accepting the ticket (RFC 5077 section 3.4)
330 */
331 session.length = ssl->session_negotiate->length;
332 memcpy( &session.id, ssl->session_negotiate->id, session.length );
333
334 ssl_session_free( ssl->session_negotiate );
335 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
336 memset( &session, 0, sizeof( ssl_session ) );
337
338 return( 0 );
339}
Paul Bakkera503a632013-08-14 13:48:06 +0200340#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200341
Paul Bakker0be444a2013-08-27 21:55:01 +0200342#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200343/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200344 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
345 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200346 */
347static int ssl_sni_wrapper( ssl_context *ssl,
348 const unsigned char* name, size_t len )
349{
350 int ret;
351 ssl_key_cert *key_cert_ori = ssl->key_cert;
352
353 ssl->key_cert = NULL;
354 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200355 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200356
357 ssl->key_cert = key_cert_ori;
358
359 return( ret );
360}
361
Paul Bakker5701cdc2012-09-27 21:49:42 +0000362static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000363 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000364 size_t len )
365{
366 int ret;
367 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000368 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000369
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100370 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
371
Paul Bakker5701cdc2012-09-27 21:49:42 +0000372 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
373 if( servername_list_size + 2 != len )
374 {
375 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
376 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
377 }
378
379 p = buf + 2;
380 while( servername_list_size > 0 )
381 {
382 hostname_len = ( ( p[1] << 8 ) | p[2] );
383 if( hostname_len + 3 > servername_list_size )
384 {
385 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
386 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
387 }
388
389 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
390 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200391 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000392 if( ret != 0 )
393 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100394 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000395 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
396 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
397 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
398 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000399 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000400 }
401
402 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000403 p += hostname_len + 3;
404 }
405
406 if( servername_list_size != 0 )
407 {
408 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000410 }
411
412 return( 0 );
413}
Paul Bakker0be444a2013-08-27 21:55:01 +0200414#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000415
Paul Bakker48916f92012-09-16 19:57:18 +0000416static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000417 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000418 size_t len )
419{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000420 int ret;
421
Paul Bakker48916f92012-09-16 19:57:18 +0000422 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
423 {
424 if( len != 1 || buf[0] != 0x0 )
425 {
426 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000427
428 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
429 return( ret );
430
Paul Bakker48916f92012-09-16 19:57:18 +0000431 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
432 }
433
434 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
435 }
436 else
437 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100438 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000439 if( len != 1 + ssl->verify_data_len ||
440 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100441 safer_memcmp( buf + 1, ssl->peer_verify_data,
442 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000443 {
444 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000445
446 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
447 return( ret );
448
Paul Bakker48916f92012-09-16 19:57:18 +0000449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
450 }
451 }
452
453 return( 0 );
454}
455
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200456#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000457static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
458 const unsigned char *buf,
459 size_t len )
460{
461 size_t sig_alg_list_size;
462 const unsigned char *p;
463
464 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
465 if( sig_alg_list_size + 2 != len ||
466 sig_alg_list_size %2 != 0 )
467 {
468 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
469 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
470 }
471
472 p = buf + 2;
473 while( sig_alg_list_size > 0 )
474 {
Manuel Pégourié-Gonnardd11eb7c2013-08-22 15:57:15 +0200475 /*
476 * For now, just ignore signature algorithm and rely on offered
477 * ciphersuites only. To be fixed later.
478 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200479#if defined(POLARSSL_SHA512_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000480 if( p[0] == SSL_HASH_SHA512 )
481 {
482 ssl->handshake->sig_alg = SSL_HASH_SHA512;
483 break;
484 }
485 if( p[0] == SSL_HASH_SHA384 )
486 {
487 ssl->handshake->sig_alg = SSL_HASH_SHA384;
488 break;
489 }
490#endif
Paul Bakker9e36f042013-06-30 14:34:05 +0200491#if defined(POLARSSL_SHA256_C)
Paul Bakker23f36802012-09-28 14:15:14 +0000492 if( p[0] == SSL_HASH_SHA256 )
493 {
494 ssl->handshake->sig_alg = SSL_HASH_SHA256;
495 break;
496 }
497 if( p[0] == SSL_HASH_SHA224 )
498 {
499 ssl->handshake->sig_alg = SSL_HASH_SHA224;
500 break;
501 }
502#endif
503 if( p[0] == SSL_HASH_SHA1 )
504 {
505 ssl->handshake->sig_alg = SSL_HASH_SHA1;
506 break;
507 }
508 if( p[0] == SSL_HASH_MD5 )
509 {
510 ssl->handshake->sig_alg = SSL_HASH_MD5;
511 break;
512 }
513
514 sig_alg_list_size -= 2;
515 p += 2;
516 }
517
518 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
519 ssl->handshake->sig_alg ) );
520
521 return( 0 );
522}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200523#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000524
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200525#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200526static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
527 const unsigned char *buf,
528 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100529{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200530 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100531 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200532 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100533
534 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
535 if( list_size + 2 != len ||
536 list_size % 2 != 0 )
537 {
538 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
539 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
540 }
541
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100542 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200543 * and leave room for a final 0 */
544 our_size = list_size / 2 + 1;
545 if( our_size > POLARSSL_ECP_DP_MAX )
546 our_size = POLARSSL_ECP_DP_MAX;
547
548 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
549 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
550
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200551 /* explicit void pointer cast for buggy MS compiler */
552 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 ssl->handshake->curves = curves;
554
Paul Bakker41c83d32013-03-20 14:39:14 +0100555 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200556 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200558 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200559
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200560 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200562 *curves++ = curve_info;
563 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100564 }
565
566 list_size -= 2;
567 p += 2;
568 }
569
570 return( 0 );
571}
572
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200573static int ssl_parse_supported_point_formats( ssl_context *ssl,
574 const unsigned char *buf,
575 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100576{
577 size_t list_size;
578 const unsigned char *p;
579
580 list_size = buf[0];
581 if( list_size + 1 != len )
582 {
583 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
584 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
585 }
586
587 p = buf + 2;
588 while( list_size > 0 )
589 {
590 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
591 p[0] == POLARSSL_ECP_PF_COMPRESSED )
592 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200593 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200594 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100595 return( 0 );
596 }
597
598 list_size--;
599 p++;
600 }
601
602 return( 0 );
603}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200604#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100605
Paul Bakker05decb22013-08-15 13:33:48 +0200606#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200607static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
608 const unsigned char *buf,
609 size_t len )
610{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200611 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200617 ssl->session_negotiate->mfl_code = buf[0];
618
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200619 return( 0 );
620}
Paul Bakker05decb22013-08-15 13:33:48 +0200621#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200622
Paul Bakker1f2bc622013-08-15 13:45:55 +0200623#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200624static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
625 const unsigned char *buf,
626 size_t len )
627{
628 if( len != 0 )
629 {
630 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
631 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
632 }
633
634 ((void) buf);
635
636 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
637
638 return( 0 );
639}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200640#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200641
Paul Bakkera503a632013-08-14 13:48:06 +0200642#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200643static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200644 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200645 size_t len )
646{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200647 int ret;
648
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200649 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
650 return( 0 );
651
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200652 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653 ssl->handshake->new_session_ticket = 1;
654
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200655 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
656
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200657 if( len == 0 )
658 return( 0 );
659
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200660 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
661 {
662 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
663 return( 0 );
664 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200665
666 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200667 * Failures are ok: just ignore the ticket and proceed.
668 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200669 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
670 {
671 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200672 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200673 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200674
675 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
676
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200677 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200679 /* Don't send a new ticket after all, this one is OK */
680 ssl->handshake->new_session_ticket = 0;
681
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200682 return( 0 );
683}
Paul Bakkera503a632013-08-14 13:48:06 +0200684#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200685
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200686#if defined(POLARSSL_SSL_ALPN)
687static int ssl_parse_alpn_ext( ssl_context *ssl,
688 unsigned char *buf, size_t len )
689{
690 size_t list_len, cur_len;
691 const unsigned char *theirs, *start, *end;
692 const char **ours;
693
694 /* If ALPN not configured, just ignore the extension */
695 if( ssl->alpn_list == NULL )
696 return( 0 );
697
698 /*
699 * opaque ProtocolName<1..2^8-1>;
700 *
701 * struct {
702 * ProtocolName protocol_name_list<2..2^16-1>
703 * } ProtocolNameList;
704 */
705
706 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
707 if( len < 4 )
708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
709
710 list_len = ( buf[0] << 8 ) | buf[1];
711 if( list_len != len - 2 )
712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
713
714 /*
715 * Use our order of preference
716 */
717 start = buf + 2;
718 end = buf + len;
719 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
720 {
721 for( theirs = start; theirs != end; theirs += cur_len )
722 {
723 /* If the list is well formed, we should get equality first */
724 if( theirs > end )
725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
726
727 cur_len = *theirs++;
728
729 /* Empty strings MUST NOT be included */
730 if( cur_len == 0 )
731 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
732
733 if( cur_len == strlen( *ours ) &&
734 memcmp( theirs, *ours, cur_len ) == 0 )
735 {
736 ssl->alpn_chosen = *ours;
737 return( 0 );
738 }
739 }
740 }
741
742 /* If we get there, no match was found */
743 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
744 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
745 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
746}
747#endif /* POLARSSL_SSL_ALPN */
748
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100749/*
750 * Auxiliary functions for ServerHello parsing and related actions
751 */
752
753#if defined(POLARSSL_X509_CRT_PARSE_C)
754/*
755 * Return 1 if the given EC key uses the given curve, 0 otherwise
756 */
757#if defined(POLARSSL_ECDSA_C)
758static int ssl_key_matches_curves( pk_context *pk,
759 const ecp_curve_info **curves )
760{
761 const ecp_curve_info **crv = curves;
762 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
763
764 while( *crv != NULL )
765 {
766 if( (*crv)->grp_id == grp_id )
767 return( 1 );
768 crv++;
769 }
770
771 return( 0 );
772}
773#endif /* POLARSSL_ECDSA_C */
774
775/*
776 * Try picking a certificate for this ciphersuite,
777 * return 0 on success and -1 on failure.
778 */
779static int ssl_pick_cert( ssl_context *ssl,
780 const ssl_ciphersuite_t * ciphersuite_info )
781{
782 ssl_key_cert *cur, *list;
783 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
784
785#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
786 if( ssl->handshake->sni_key_cert != NULL )
787 list = ssl->handshake->sni_key_cert;
788 else
789#endif
790 list = ssl->handshake->key_cert;
791
792 if( pk_alg == POLARSSL_PK_NONE )
793 return( 0 );
794
795 for( cur = list; cur != NULL; cur = cur->next )
796 {
797 if( ! pk_can_do( cur->key, pk_alg ) )
798 continue;
799
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200800 /*
801 * This avoids sending the client a cert it'll reject based on
802 * keyUsage or other extensions.
803 *
804 * It also allows the user to provision different certificates for
805 * different uses based on keyUsage, eg if they want to avoid signing
806 * and decrypting with the same RSA key.
807 */
808 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
809 SSL_IS_SERVER ) != 0 )
810 {
811 continue;
812 }
813
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100814#if defined(POLARSSL_ECDSA_C)
815 if( pk_alg == POLARSSL_PK_ECDSA )
816 {
817 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
818 break;
819 }
820 else
821#endif
822 break;
823 }
824
825 if( cur == NULL )
826 return( -1 );
827
828 ssl->handshake->key_cert = cur;
829 return( 0 );
830}
831#endif /* POLARSSL_X509_CRT_PARSE_C */
832
833/*
834 * Check if a given ciphersuite is suitable for use with our config/keys/etc
835 * Sets ciphersuite_info only if the suite matches.
836 */
837static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
838 const ssl_ciphersuite_t **ciphersuite_info )
839{
840 const ssl_ciphersuite_t *suite_info;
841
842 suite_info = ssl_ciphersuite_from_id( suite_id );
843 if( suite_info == NULL )
844 {
845 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
846 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
847 }
848
849 if( suite_info->min_minor_ver > ssl->minor_ver ||
850 suite_info->max_minor_ver < ssl->minor_ver )
851 return( 0 );
852
853#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
854 if( ssl_ciphersuite_uses_ec( suite_info ) &&
855 ( ssl->handshake->curves == NULL ||
856 ssl->handshake->curves[0] == NULL ) )
857 return( 0 );
858#endif
859
860#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
861 /* If the ciphersuite requires a pre-shared key and we don't
862 * have one, skip it now rather than failing later */
863 if( ssl_ciphersuite_uses_psk( suite_info ) &&
864 ssl->f_psk == NULL &&
865 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
866 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
867 return( 0 );
868#endif
869
870#if defined(POLARSSL_X509_CRT_PARSE_C)
871 /*
872 * Final check: if ciphersuite requires us to have a
873 * certificate/key of a particular type:
874 * - select the appropriate certificate if we have one, or
875 * - try the next ciphersuite if we don't
876 * This must be done last since we modify the key_cert list.
877 */
878 if( ssl_pick_cert( ssl, suite_info ) != 0 )
879 return( 0 );
880#endif
881
882 *ciphersuite_info = suite_info;
883 return( 0 );
884}
885
Paul Bakker78a8c712013-03-06 17:01:52 +0100886#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
887static int ssl_parse_client_hello_v2( ssl_context *ssl )
888{
889 int ret;
890 unsigned int i, j;
891 size_t n;
892 unsigned int ciph_len, sess_len, chal_len;
893 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200894 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200895 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100896
897 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
898
899 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
900 {
901 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
902
903 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
904 return( ret );
905
906 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
907 }
908
909 buf = ssl->in_hdr;
910
911 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
912
913 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
914 buf[2] ) );
915 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
916 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
917 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
918 buf[3], buf[4] ) );
919
920 /*
921 * SSLv2 Client Hello
922 *
923 * Record layer:
924 * 0 . 1 message length
925 *
926 * SSL layer:
927 * 2 . 2 message type
928 * 3 . 4 protocol version
929 */
930 if( buf[2] != SSL_HS_CLIENT_HELLO ||
931 buf[3] != SSL_MAJOR_VERSION_3 )
932 {
933 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
934 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
935 }
936
937 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
938
939 if( n < 17 || n > 512 )
940 {
941 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
942 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
943 }
944
945 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200946 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
947 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100948
949 if( ssl->minor_ver < ssl->min_minor_ver )
950 {
951 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
952 " [%d:%d] < [%d:%d]", ssl->major_ver, ssl->minor_ver,
953 ssl->min_major_ver, ssl->min_minor_ver ) );
954
955 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
956 SSL_ALERT_MSG_PROTOCOL_VERSION );
957 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
958 }
959
Paul Bakker2fbefde2013-06-29 16:01:15 +0200960 ssl->handshake->max_major_ver = buf[3];
961 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100962
963 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
964 {
965 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
966 return( ret );
967 }
968
969 ssl->handshake->update_checksum( ssl, buf + 2, n );
970
971 buf = ssl->in_msg;
972 n = ssl->in_left - 5;
973
974 /*
975 * 0 . 1 ciphersuitelist length
976 * 2 . 3 session id length
977 * 4 . 5 challenge length
978 * 6 . .. ciphersuitelist
979 * .. . .. session id
980 * .. . .. challenge
981 */
982 SSL_DEBUG_BUF( 4, "record contents", buf, n );
983
984 ciph_len = ( buf[0] << 8 ) | buf[1];
985 sess_len = ( buf[2] << 8 ) | buf[3];
986 chal_len = ( buf[4] << 8 ) | buf[5];
987
988 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
989 ciph_len, sess_len, chal_len ) );
990
991 /*
992 * Make sure each parameter length is valid
993 */
994 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
995 {
996 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
997 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
998 }
999
1000 if( sess_len > 32 )
1001 {
1002 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1003 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1004 }
1005
1006 if( chal_len < 8 || chal_len > 32 )
1007 {
1008 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1009 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1010 }
1011
1012 if( n != 6 + ciph_len + sess_len + chal_len )
1013 {
1014 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1015 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1016 }
1017
1018 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1019 buf + 6, ciph_len );
1020 SSL_DEBUG_BUF( 3, "client hello, session id",
1021 buf + 6 + ciph_len, sess_len );
1022 SSL_DEBUG_BUF( 3, "client hello, challenge",
1023 buf + 6 + ciph_len + sess_len, chal_len );
1024
1025 p = buf + 6 + ciph_len;
1026 ssl->session_negotiate->length = sess_len;
1027 memset( ssl->session_negotiate->id, 0, sizeof( ssl->session_negotiate->id ) );
1028 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1029
1030 p += sess_len;
1031 memset( ssl->handshake->randbytes, 0, 64 );
1032 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1033
1034 /*
1035 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1036 */
1037 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1038 {
1039 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1040 {
1041 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1042 if( ssl->renegotiation == SSL_RENEGOTIATION )
1043 {
1044 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1045
1046 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1047 return( ret );
1048
1049 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1050 }
1051 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1052 break;
1053 }
1054 }
1055
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001056 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001057 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001058#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1059 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1060 {
1061 for( i = 0; ciphersuites[i] != 0; i++ )
1062#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001063 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001064 {
1065 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001066#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001067 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001068 if( p[0] != 0 ||
1069 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1070 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1071 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001072
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001073 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1074 &ciphersuite_info ) ) != 0 )
1075 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001076
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001077 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001078 goto have_ciphersuite_v2;
1079 }
1080 }
1081
1082 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1083
1084 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1085
1086have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001087 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001088 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001089 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001090
1091 /*
1092 * SSLv2 Client Hello relevant renegotiation security checks
1093 */
1094 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1095 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1098
1099 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1100 return( ret );
1101
1102 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1103 }
1104
1105 ssl->in_left = 0;
1106 ssl->state++;
1107
1108 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1109
1110 return( 0 );
1111}
1112#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1113
Paul Bakker5121ce52009-01-03 21:22:43 +00001114static int ssl_parse_client_hello( ssl_context *ssl )
1115{
Paul Bakker23986e52011-04-24 08:57:21 +00001116 int ret;
1117 unsigned int i, j;
1118 size_t n;
1119 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001120 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001121 unsigned int ext_len = 0;
1122 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001123 int renegotiation_info_seen = 0;
1124 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001125 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001126 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001127
1128 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1129
Paul Bakker48916f92012-09-16 19:57:18 +00001130 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1131 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001132 {
1133 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1134 return( ret );
1135 }
1136
1137 buf = ssl->in_hdr;
1138
Paul Bakker78a8c712013-03-06 17:01:52 +01001139#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1140 if( ( buf[0] & 0x80 ) != 0 )
1141 return ssl_parse_client_hello_v2( ssl );
1142#endif
1143
Paul Bakkerec636f32012-09-09 19:17:02 +00001144 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1145
1146 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1147 buf[0] ) );
1148 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1149 ( buf[3] << 8 ) | buf[4] ) );
1150 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1151 buf[1], buf[2] ) );
1152
1153 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001154 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001155 *
1156 * Record layer:
1157 * 0 . 0 message type
1158 * 1 . 2 protocol version
1159 * 3 . 4 message length
1160 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001161
1162 /* According to RFC 5246 Appendix E.1, the version here is typically
1163 * "{03,00}, the lowest version number supported by the client, [or] the
1164 * value of ClientHello.client_version", so the only meaningful check here
1165 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001166 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001167 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001169 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1170 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Paul Bakkerec636f32012-09-09 19:17:02 +00001173 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
Paul Bakker4f42c112014-04-17 14:48:23 +02001175 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001176 {
1177 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1178 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1179 }
1180
Paul Bakker48916f92012-09-16 19:57:18 +00001181 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1182 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001183 {
1184 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1185 return( ret );
1186 }
1187
1188 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001189 if( !ssl->renegotiation )
1190 n = ssl->in_left - 5;
1191 else
1192 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001193
Paul Bakker48916f92012-09-16 19:57:18 +00001194 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001195
1196 /*
1197 * SSL layer:
1198 * 0 . 0 handshake type
1199 * 1 . 3 handshake length
1200 * 4 . 5 protocol version
1201 * 6 . 9 UNIX time()
1202 * 10 . 37 random bytes
1203 * 38 . 38 session id length
1204 * 39 . 38+x session id
1205 * 39+x . 40+x ciphersuitelist length
1206 * 41+x . .. ciphersuitelist
1207 * .. . .. compression alg.
1208 * .. . .. extensions
1209 */
1210 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1211
1212 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1213 buf[0] ) );
1214 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1215 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1216 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1217 buf[4], buf[5] ) );
1218
1219 /*
1220 * Check the handshake type and protocol version
1221 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001222 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001223 {
1224 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1225 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1226 }
1227
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001228 ssl->major_ver = buf[4];
1229 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001230
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001231 ssl->handshake->max_major_ver = ssl->major_ver;
1232 ssl->handshake->max_minor_ver = ssl->minor_ver;
1233
1234 if( ssl->major_ver < ssl->min_major_ver ||
1235 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001236 {
1237 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001238 " [%d:%d] < [%d:%d]",
1239 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001240 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001241
1242 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1243 SSL_ALERT_MSG_PROTOCOL_VERSION );
1244
1245 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1246 }
1247
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001248 if( ssl->major_ver > ssl->max_major_ver )
1249 {
1250 ssl->major_ver = ssl->max_major_ver;
1251 ssl->minor_ver = ssl->max_minor_ver;
1252 }
1253 else if( ssl->minor_ver > ssl->max_minor_ver )
1254 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001255
Paul Bakker48916f92012-09-16 19:57:18 +00001256 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001257
1258 /*
1259 * Check the handshake message length
1260 */
1261 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1262 {
1263 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1264 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1265 }
1266
1267 /*
1268 * Check the session length
1269 */
1270 sess_len = buf[38];
1271
1272 if( sess_len > 32 )
1273 {
1274 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1275 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1276 }
1277
Paul Bakker48916f92012-09-16 19:57:18 +00001278 ssl->session_negotiate->length = sess_len;
1279 memset( ssl->session_negotiate->id, 0,
1280 sizeof( ssl->session_negotiate->id ) );
1281 memcpy( ssl->session_negotiate->id, buf + 39,
1282 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001283
1284 /*
1285 * Check the ciphersuitelist length
1286 */
1287 ciph_len = ( buf[39 + sess_len] << 8 )
1288 | ( buf[40 + sess_len] );
1289
Paul Bakker4f42c112014-04-17 14:48:23 +02001290 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001291 {
1292 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1293 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1294 }
1295
1296 /*
1297 * Check the compression algorithms length
1298 */
1299 comp_len = buf[41 + sess_len + ciph_len];
1300
1301 if( comp_len < 1 || comp_len > 16 )
1302 {
1303 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1304 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1305 }
1306
Paul Bakker48916f92012-09-16 19:57:18 +00001307 /*
1308 * Check the extension length
1309 */
1310 if( n > 42 + sess_len + ciph_len + comp_len )
1311 {
1312 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1313 | ( buf[43 + sess_len + ciph_len + comp_len] );
1314
1315 if( ( ext_len > 0 && ext_len < 4 ) ||
1316 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1319 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1321 }
1322 }
1323
1324 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001325#if defined(POLARSSL_ZLIB_SUPPORT)
1326 for( i = 0; i < comp_len; ++i )
1327 {
Paul Bakker48916f92012-09-16 19:57:18 +00001328 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 {
Paul Bakker48916f92012-09-16 19:57:18 +00001330 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001331 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001334#endif
1335
Paul Bakkerec636f32012-09-09 19:17:02 +00001336 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1337 buf + 6, 32 );
1338 SSL_DEBUG_BUF( 3, "client hello, session id",
1339 buf + 38, sess_len );
1340 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1341 buf + 41 + sess_len, ciph_len );
1342 SSL_DEBUG_BUF( 3, "client hello, compression",
1343 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Paul Bakkerec636f32012-09-09 19:17:02 +00001345 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001346 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1347 */
1348 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1349 {
1350 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1351 {
1352 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1353 if( ssl->renegotiation == SSL_RENEGOTIATION )
1354 {
1355 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001356
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 }
1362 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1363 break;
1364 }
1365 }
1366
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001368
1369 while( ext_len )
1370 {
1371 unsigned int ext_id = ( ( ext[0] << 8 )
1372 | ( ext[1] ) );
1373 unsigned int ext_size = ( ( ext[2] << 8 )
1374 | ( ext[3] ) );
1375
1376 if( ext_size + 4 > ext_len )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1379 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1380 }
1381 switch( ext_id )
1382 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001383#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001384 case TLS_EXT_SERVERNAME:
1385 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1386 if( ssl->f_sni == NULL )
1387 break;
1388
1389 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1390 if( ret != 0 )
1391 return( ret );
1392 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001393#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001394
Paul Bakker48916f92012-09-16 19:57:18 +00001395 case TLS_EXT_RENEGOTIATION_INFO:
1396 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1397 renegotiation_info_seen = 1;
1398
Paul Bakker23f36802012-09-28 14:15:14 +00001399 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1400 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001401 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001402 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001403
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001404#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001405 case TLS_EXT_SIG_ALG:
1406 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1407 if( ssl->renegotiation == SSL_RENEGOTIATION )
1408 break;
1409
1410 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1411 if( ret != 0 )
1412 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001413 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001414#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001415
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001416#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001417 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1418 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1419
1420 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1421 if( ret != 0 )
1422 return( ret );
1423 break;
1424
1425 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1426 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001427 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001428
1429 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1430 if( ret != 0 )
1431 return( ret );
1432 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001433#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001434
Paul Bakker05decb22013-08-15 13:33:48 +02001435#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001436 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1437 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1438
1439 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1440 if( ret != 0 )
1441 return( ret );
1442 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001443#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001444
Paul Bakker1f2bc622013-08-15 13:45:55 +02001445#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001446 case TLS_EXT_TRUNCATED_HMAC:
1447 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1448
1449 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1450 if( ret != 0 )
1451 return( ret );
1452 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001453#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001454
Paul Bakkera503a632013-08-14 13:48:06 +02001455#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001456 case TLS_EXT_SESSION_TICKET:
1457 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1458
1459 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1460 if( ret != 0 )
1461 return( ret );
1462 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001463#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001464
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001465#if defined(POLARSSL_SSL_ALPN)
1466 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001467 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001468
1469 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1470 if( ret != 0 )
1471 return( ret );
1472 break;
1473#endif /* POLARSSL_SSL_SESSION_TICKETS */
1474
Paul Bakker48916f92012-09-16 19:57:18 +00001475 default:
1476 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1477 ext_id ) );
1478 }
1479
1480 ext_len -= 4 + ext_size;
1481 ext += 4 + ext_size;
1482
1483 if( ext_len > 0 && ext_len < 4 )
1484 {
1485 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1486 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1487 }
1488 }
1489
1490 /*
1491 * Renegotiation security checks
1492 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001493 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1494 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1495 {
1496 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1497 handshake_failure = 1;
1498 }
1499 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1500 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1501 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001502 {
1503 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001504 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001505 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001506 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1507 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1508 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001509 {
1510 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001511 handshake_failure = 1;
1512 }
1513 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1514 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1515 renegotiation_info_seen == 1 )
1516 {
1517 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1518 handshake_failure = 1;
1519 }
1520
1521 if( handshake_failure == 1 )
1522 {
1523 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1524 return( ret );
1525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1527 }
Paul Bakker380da532012-04-18 16:10:25 +00001528
Paul Bakker41c83d32013-03-20 14:39:14 +01001529 /*
1530 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001531 * (At the end because we need information from the EC-based extensions
1532 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001533 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001534 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001535 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001536#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1537 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1538 {
1539 for( i = 0; ciphersuites[i] != 0; i++ )
1540#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001541 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001542 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001543 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001544#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001545 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001546 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1547 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1548 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001549
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001550 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1551 &ciphersuite_info ) ) != 0 )
1552 return( ret );
1553
1554 if( ciphersuite_info != NULL )
1555 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001556 }
1557 }
1558
1559 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1560
1561 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1562 return( ret );
1563
1564 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1565
1566have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001567 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001568 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1569 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1570
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 ssl->in_left = 0;
1572 ssl->state++;
1573
1574 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1575
1576 return( 0 );
1577}
1578
Paul Bakker1f2bc622013-08-15 13:45:55 +02001579#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001580static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1581 unsigned char *buf,
1582 size_t *olen )
1583{
1584 unsigned char *p = buf;
1585
1586 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1587 {
1588 *olen = 0;
1589 return;
1590 }
1591
1592 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1593
1594 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1595 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1596
1597 *p++ = 0x00;
1598 *p++ = 0x00;
1599
1600 *olen = 4;
1601}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001602#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001603
Paul Bakkera503a632013-08-14 13:48:06 +02001604#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001605static void ssl_write_session_ticket_ext( ssl_context *ssl,
1606 unsigned char *buf,
1607 size_t *olen )
1608{
1609 unsigned char *p = buf;
1610
1611 if( ssl->handshake->new_session_ticket == 0 )
1612 {
1613 *olen = 0;
1614 return;
1615 }
1616
1617 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1618
1619 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1620 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1621
1622 *p++ = 0x00;
1623 *p++ = 0x00;
1624
1625 *olen = 4;
1626}
Paul Bakkera503a632013-08-14 13:48:06 +02001627#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001628
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001629static void ssl_write_renegotiation_ext( ssl_context *ssl,
1630 unsigned char *buf,
1631 size_t *olen )
1632{
1633 unsigned char *p = buf;
1634
1635 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1636 {
1637 *olen = 0;
1638 return;
1639 }
1640
1641 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1642
1643 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1644 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1645
1646 *p++ = 0x00;
1647 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1648 *p++ = ssl->verify_data_len * 2 & 0xFF;
1649
1650 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1651 p += ssl->verify_data_len;
1652 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1653 p += ssl->verify_data_len;
1654
1655 *olen = 5 + ssl->verify_data_len * 2;
1656}
1657
Paul Bakker05decb22013-08-15 13:33:48 +02001658#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001659static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1660 unsigned char *buf,
1661 size_t *olen )
1662{
1663 unsigned char *p = buf;
1664
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001665 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1666 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001667 *olen = 0;
1668 return;
1669 }
1670
1671 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1672
1673 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1674 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1675
1676 *p++ = 0x00;
1677 *p++ = 1;
1678
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001679 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001680
1681 *olen = 5;
1682}
Paul Bakker05decb22013-08-15 13:33:48 +02001683#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001684
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001685#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001686static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1687 unsigned char *buf,
1688 size_t *olen )
1689{
1690 unsigned char *p = buf;
1691 ((void) ssl);
1692
Paul Bakker677377f2013-10-28 12:54:26 +01001693 if( ( ssl->handshake->cli_exts &
1694 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1695 {
1696 *olen = 0;
1697 return;
1698 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001699
1700 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1701
1702 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1703 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1704
1705 *p++ = 0x00;
1706 *p++ = 2;
1707
1708 *p++ = 1;
1709 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1710
1711 *olen = 6;
1712}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001713#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001714
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001715#if defined(POLARSSL_SSL_ALPN )
1716static void ssl_write_alpn_ext( ssl_context *ssl,
1717 unsigned char *buf, size_t *olen )
1718{
1719 if( ssl->alpn_chosen == NULL )
1720 {
1721 *olen = 0;
1722 return;
1723 }
1724
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001725 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001726
1727 /*
1728 * 0 . 1 ext identifier
1729 * 2 . 3 ext length
1730 * 4 . 5 protocol list length
1731 * 6 . 6 protocol name length
1732 * 7 . 7+n protocol name
1733 */
1734 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1735 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1736
1737 *olen = 7 + strlen( ssl->alpn_chosen );
1738
1739 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1740 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1741
1742 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1743 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1744
1745 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1746
1747 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1748}
1749#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1750
Paul Bakker5121ce52009-01-03 21:22:43 +00001751static int ssl_write_server_hello( ssl_context *ssl )
1752{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001753#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001755#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001756 int ret;
1757 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 unsigned char *buf, *p;
1759
1760 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1761
Paul Bakkera9a028e2013-11-21 17:31:06 +01001762 if( ssl->f_rng == NULL )
1763 {
1764 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1765 return( POLARSSL_ERR_SSL_NO_RNG );
1766 }
1767
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 /*
1769 * 0 . 0 handshake type
1770 * 1 . 3 handshake length
1771 * 4 . 5 protocol version
1772 * 6 . 9 UNIX time()
1773 * 10 . 37 random bytes
1774 */
1775 buf = ssl->out_msg;
1776 p = buf + 4;
1777
1778 *p++ = (unsigned char) ssl->major_ver;
1779 *p++ = (unsigned char) ssl->minor_ver;
1780
1781 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1782 buf[4], buf[5] ) );
1783
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001784#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 t = time( NULL );
1786 *p++ = (unsigned char)( t >> 24 );
1787 *p++ = (unsigned char)( t >> 16 );
1788 *p++ = (unsigned char)( t >> 8 );
1789 *p++ = (unsigned char)( t );
1790
1791 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001792#else
1793 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1794 return( ret );
1795
1796 p += 4;
1797#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Paul Bakkera3d195c2011-11-27 21:07:34 +00001799 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1800 return( ret );
1801
1802 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
1806 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1807
1808 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001809 * Resume is 0 by default, see ssl_handshake_init().
1810 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1811 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001813 if( ssl->handshake->resume == 0 &&
1814 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001815 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001816 ssl->f_get_cache != NULL &&
1817 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1818 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001819 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001820 ssl->handshake->resume = 1;
1821 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001823 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 {
1825 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001826 * New session, create a new session id,
1827 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 ssl->state++;
1830
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001831#if defined(POLARSSL_HAVE_TIME)
1832 ssl->session_negotiate->start = time( NULL );
1833#endif
1834
Paul Bakkera503a632013-08-14 13:48:06 +02001835#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001836 if( ssl->handshake->new_session_ticket != 0 )
1837 {
1838 ssl->session_negotiate->length = n = 0;
1839 memset( ssl->session_negotiate->id, 0, 32 );
1840 }
1841 else
1842#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001843 {
1844 ssl->session_negotiate->length = n = 32;
1845 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001846 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001847 return( ret );
1848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
1850 else
1851 {
1852 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001853 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001855 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001857
1858 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1859 {
1860 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1861 return( ret );
1862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 }
1864
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001865 /*
1866 * 38 . 38 session id length
1867 * 39 . 38+n session id
1868 * 39+n . 40+n chosen ciphersuite
1869 * 41+n . 41+n chosen compression alg.
1870 * 42+n . 43+n extensions length
1871 * 44+n . 43+n+m extensions
1872 */
1873 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001874 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1875 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1878 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1879 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001880 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Paul Bakker48916f92012-09-16 19:57:18 +00001882 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1883 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1884 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001886 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1887 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001888 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001889 ssl->session_negotiate->compression ) );
1890
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001891 /*
1892 * First write extensions, then the total length
1893 */
1894 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1895 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001896
Paul Bakker05decb22013-08-15 13:33:48 +02001897#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001898 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1899 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001900#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001901
Paul Bakker1f2bc622013-08-15 13:45:55 +02001902#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001903 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1904 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001905#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001906
Paul Bakkera503a632013-08-14 13:48:06 +02001907#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001908 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1909 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001910#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001911
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001912#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001913 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1914 ext_len += olen;
1915#endif
1916
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001917#if defined(POLARSSL_SSL_ALPN)
1918 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1919 ext_len += olen;
1920#endif
1921
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001922 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001923
Paul Bakkera7036632014-04-30 10:15:38 +02001924 if( ext_len > 0 )
1925 {
1926 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1927 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1928 p += ext_len;
1929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
1931 ssl->out_msglen = p - buf;
1932 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1933 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1934
1935 ret = ssl_write_record( ssl );
1936
1937 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1938
1939 return( ret );
1940}
1941
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001942#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1943 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001944 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1945 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001946static int ssl_write_certificate_request( ssl_context *ssl )
1947{
Paul Bakkered27a042013-04-18 22:46:23 +02001948 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1949 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001950
1951 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1952
1953 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001954 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1956 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001957 {
1958 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1959 ssl->state++;
1960 return( 0 );
1961 }
1962
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001963 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964 return( ret );
1965}
1966#else
1967static int ssl_write_certificate_request( ssl_context *ssl )
1968{
1969 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1970 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001971 size_t dn_size, total_dn_size; /* excluding length bytes */
1972 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001973 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001974 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001975
1976 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1977
1978 ssl->state++;
1979
Paul Bakkerfbb17802013-04-17 19:10:21 +02001980 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001981 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001982 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001983 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001984 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001986 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 return( 0 );
1988 }
1989
1990 /*
1991 * 0 . 0 handshake type
1992 * 1 . 3 handshake length
1993 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001994 * 5 .. m-1 cert types
1995 * m .. m+1 sig alg length (TLS 1.2 only)
1996 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001997 * n .. n+1 length of all DNs
1998 * n+2 .. n+3 length of DN 1
1999 * n+4 .. ... Distinguished Name #1
2000 * ... .. ... length of DN 2, etc.
2001 */
2002 buf = ssl->out_msg;
2003 p = buf + 4;
2004
2005 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002006 * Supported certificate types
2007 *
2008 * ClientCertificateType certificate_types<1..2^8-1>;
2009 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002010 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002011 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002012
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002013#if defined(POLARSSL_RSA_C)
2014 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2015#endif
2016#if defined(POLARSSL_ECDSA_C)
2017 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2018#endif
2019
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002020 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002021 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002022
Paul Bakker577e0062013-08-28 11:57:20 +02002023 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002024#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002025 /*
2026 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002027 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002028 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2029 *
2030 * struct {
2031 * HashAlgorithm hash;
2032 * SignatureAlgorithm signature;
2033 * } SignatureAndHashAlgorithm;
2034 *
2035 * enum { (255) } HashAlgorithm;
2036 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002037 */
Paul Bakker21dca692013-01-03 11:41:08 +01002038 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002039 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002040 /*
2041 * Only use current running hash algorithm that is already required
2042 * for requested ciphersuite.
2043 */
Paul Bakker926af752012-11-23 13:38:07 +01002044 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2045
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002046 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2047 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002048 {
2049 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2050 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002051
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002052 /*
2053 * Supported signature algorithms
2054 */
2055#if defined(POLARSSL_RSA_C)
2056 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2057 p[2 + sa_len++] = SSL_SIG_RSA;
2058#endif
2059#if defined(POLARSSL_ECDSA_C)
2060 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2061 p[2 + sa_len++] = SSL_SIG_ECDSA;
2062#endif
Paul Bakker926af752012-11-23 13:38:07 +01002063
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002064 p[0] = (unsigned char)( sa_len >> 8 );
2065 p[1] = (unsigned char)( sa_len );
2066 sa_len += 2;
2067 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002068 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002069#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002070
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002071 /*
2072 * DistinguishedName certificate_authorities<0..2^16-1>;
2073 * opaque DistinguishedName<1..2^16-1>;
2074 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002075 p += 2;
2076 crt = ssl->ca_chain;
2077
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002078 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002079 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002080 {
2081 if( p - buf > 4096 )
2082 break;
2083
Paul Bakker926af752012-11-23 13:38:07 +01002084 dn_size = crt->subject_raw.len;
2085 *p++ = (unsigned char)( dn_size >> 8 );
2086 *p++ = (unsigned char)( dn_size );
2087 memcpy( p, crt->subject_raw.p, dn_size );
2088 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002089
Paul Bakker926af752012-11-23 13:38:07 +01002090 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2091
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002092 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002093 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002094 }
2095
Paul Bakker926af752012-11-23 13:38:07 +01002096 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002097 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2098 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002099 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2100 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002101
2102 ret = ssl_write_record( ssl );
2103
2104 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2105
2106 return( ret );
2107}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002108#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2109 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002110 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2111 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002112
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002113#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2114 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2115static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2116{
2117 int ret;
2118
2119 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2120 {
2121 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2122 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2123 }
2124
2125 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2126 pk_ec( *ssl_own_key( ssl ) ),
2127 POLARSSL_ECDH_OURS ) ) != 0 )
2128 {
2129 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2130 return( ret );
2131 }
2132
2133 return( 0 );
2134}
2135#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2136 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2137
Paul Bakker41c83d32013-03-20 14:39:14 +01002138static int ssl_write_server_key_exchange( ssl_context *ssl )
2139{
Paul Bakker23986e52011-04-24 08:57:21 +00002140 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002141 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002142 const ssl_ciphersuite_t *ciphersuite_info =
2143 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002144
2145#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2146 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2147 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002148 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002149 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002150 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002151 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002152 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002153 ((void) dig_signed);
2154 ((void) dig_signed_len);
2155#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002156
Paul Bakker5121ce52009-01-03 21:22:43 +00002157 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2158
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002159#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2160 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2161 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002162 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2163 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2164 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002165 {
2166 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2167 ssl->state++;
2168 return( 0 );
2169 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002170#endif
2171
2172#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2173 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2174 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2175 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2176 {
2177 ssl_get_ecdh_params_from_cert( ssl );
2178
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002179 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002180 ssl->state++;
2181 return( 0 );
2182 }
2183#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002184
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002185#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2186 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2187 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2188 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002189 {
2190 /* TODO: Support identity hints */
2191 *(p++) = 0x00;
2192 *(p++) = 0x00;
2193
2194 n += 2;
2195 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002196#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2197 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002198
2199#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2200 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2201 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2202 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002203 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002204 /*
2205 * Ephemeral DH parameters:
2206 *
2207 * struct {
2208 * opaque dh_p<1..2^16-1>;
2209 * opaque dh_g<1..2^16-1>;
2210 * opaque dh_Ys<1..2^16-1>;
2211 * } ServerDHParams;
2212 */
2213 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2214 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2215 {
2216 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2217 return( ret );
2218 }
Paul Bakker48916f92012-09-16 19:57:18 +00002219
Paul Bakker41c83d32013-03-20 14:39:14 +01002220 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002221 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002222 p,
2223 &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002224 {
2225 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2226 return( ret );
2227 }
2228
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002229 dig_signed = p;
2230 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002231
2232 p += len;
2233 n += len;
2234
Paul Bakker41c83d32013-03-20 14:39:14 +01002235 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2236 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2237 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2238 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2239 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002240#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2241 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002242
Gergely Budai987bfb52014-01-19 21:48:42 +01002243#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002244 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002245 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2246 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002247 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002248 /*
2249 * Ephemeral ECDH parameters:
2250 *
2251 * struct {
2252 * ECParameters curve_params;
2253 * ECPoint public;
2254 * } ServerECDHParams;
2255 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002256 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002257#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002258 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002259
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002260 /* Match our preference list against the offered curves */
2261 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2262 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2263 if( (*curve)->grp_id == *gid )
2264 goto curve_matching_done;
2265
2266curve_matching_done:
2267#else
2268 curve = ssl->handshake->curves;
2269#endif
2270
2271 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002272 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002273 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2274 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002275 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002276
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002277 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002278
Paul Bakker41c83d32013-03-20 14:39:14 +01002279 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002280 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002281 {
2282 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2283 return( ret );
2284 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002285
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002286 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2287 p, SSL_MAX_CONTENT_LEN - n,
2288 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002289 {
2290 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2291 return( ret );
2292 }
2293
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002294 dig_signed = p;
2295 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002296
2297 p += len;
2298 n += len;
2299
Paul Bakker41c83d32013-03-20 14:39:14 +01002300 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2301 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002302#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002303
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002304#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002305 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2306 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002307 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002308 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2309 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002310 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002311 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002312 unsigned int hashlen = 0;
2313 unsigned char hash[64];
2314 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002315
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002316 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002317 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2318 */
Paul Bakker577e0062013-08-28 11:57:20 +02002319#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002320 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2321 {
2322 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2323
2324 if( md_alg == POLARSSL_MD_NONE )
2325 {
2326 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2327 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2328 }
2329 }
Paul Bakker577e0062013-08-28 11:57:20 +02002330 else
2331#endif
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002332#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2333 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker577e0062013-08-28 11:57:20 +02002334 if ( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002335 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2336 {
2337 md_alg = POLARSSL_MD_SHA1;
2338 }
2339 else
Paul Bakker577e0062013-08-28 11:57:20 +02002340#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002341 {
2342 md_alg = POLARSSL_MD_NONE;
2343 }
2344
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002345 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002346 * Compute the hash to be signed
2347 */
Paul Bakker577e0062013-08-28 11:57:20 +02002348#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2349 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002350 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002351 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002352 md5_context md5;
2353 sha1_context sha1;
2354
2355 /*
2356 * digitally-signed struct {
2357 * opaque md5_hash[16];
2358 * opaque sha_hash[20];
2359 * };
2360 *
2361 * md5_hash
2362 * MD5(ClientHello.random + ServerHello.random
2363 * + ServerParams);
2364 * sha_hash
2365 * SHA(ClientHello.random + ServerHello.random
2366 * + ServerParams);
2367 */
2368 md5_starts( &md5 );
2369 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002370 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002371 md5_finish( &md5, hash );
2372
2373 sha1_starts( &sha1 );
2374 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002375 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002376 sha1_finish( &sha1, hash + 16 );
2377
2378 hashlen = 36;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002379 }
2380 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002381#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2382 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002383#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2384 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002385 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002386 {
2387 md_context_t ctx;
2388
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002389 /* Info from md_alg will be used instead */
2390 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002391
2392 /*
2393 * digitally-signed struct {
2394 * opaque client_random[32];
2395 * opaque server_random[32];
2396 * ServerDHParams params;
2397 * };
2398 */
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002399 if( ( ret = md_init_ctx( &ctx, md_info_from_type(md_alg) ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002400 {
2401 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2402 return( ret );
2403 }
2404
2405 md_starts( &ctx );
2406 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002407 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002408 md_finish( &ctx, hash );
Paul Bakker61d113b2013-07-04 11:51:43 +02002409
2410 if( ( ret = md_free_ctx( &ctx ) ) != 0 )
2411 {
2412 SSL_DEBUG_RET( 1, "md_free_ctx", ret );
2413 return( ret );
2414 }
2415
Paul Bakker23f36802012-09-28 14:15:14 +00002416 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002417 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002418#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2419 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002420 {
2421 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002422 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Paul Bakker577e0062013-08-28 11:57:20 +02002423 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002424
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002425 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2426 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002428 /*
2429 * Make the signature
2430 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002431 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002432 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002433 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2434 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002435 }
Paul Bakker23f36802012-09-28 14:15:14 +00002436
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002437#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002438 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2439 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002440 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002441 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002442
2443 n += 2;
2444 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002445#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002447 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002448 p + 2 , &signature_len,
2449 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002450 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002451 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002452 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002453 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002454
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002455 *(p++) = (unsigned char)( signature_len >> 8 );
2456 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002457 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002458
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002459 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002460
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002461 p += signature_len;
2462 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002463 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002464#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002465 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2466 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002467
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002469 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2470 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2471
2472 ssl->state++;
2473
2474 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2475 {
2476 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2477 return( ret );
2478 }
2479
2480 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2481
2482 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483}
2484
2485static int ssl_write_server_hello_done( ssl_context *ssl )
2486{
2487 int ret;
2488
2489 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2490
2491 ssl->out_msglen = 4;
2492 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2493 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2494
2495 ssl->state++;
2496
2497 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2498 {
2499 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2500 return( ret );
2501 }
2502
2503 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2504
2505 return( 0 );
2506}
2507
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2509 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2510static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2511 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002512{
2513 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002514 size_t n;
2515
2516 /*
2517 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2518 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002519 if( *p + 2 > end )
2520 {
2521 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2522 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2523 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002524
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002525 n = ( (*p)[0] << 8 ) | (*p)[1];
2526 *p += 2;
2527
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002528 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002529 {
2530 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2531 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2532 }
2533
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002534 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002535 {
2536 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2537 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2538 }
2539
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002540 *p += n;
2541
Paul Bakker70df2fb2013-04-17 17:19:09 +02002542 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2543
Paul Bakker70df2fb2013-04-17 17:19:09 +02002544 return( ret );
2545}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002546#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2547 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002548
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002549#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2550 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002551static int ssl_parse_encrypted_pms( ssl_context *ssl,
2552 const unsigned char *p,
2553 const unsigned char *end,
2554 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002555{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002556 int ret;
2557 size_t len = pk_get_len( ssl_own_key( ssl ) );
2558 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002559
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002560 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002561 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002562 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002563 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2564 }
2565
2566 /*
2567 * Decrypt the premaster using own private RSA key
2568 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002569#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2570 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002571 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2572 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002573 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2574 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002575 {
2576 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2577 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2578 }
2579 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002580#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002581
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002582 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002583 {
2584 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2585 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2586 }
2587
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002588 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2589 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002590 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002591 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002592
2593 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002594 pms[0] != ssl->handshake->max_major_ver ||
2595 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002596 {
2597 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2598
2599 /*
2600 * Protection against Bleichenbacher's attack:
2601 * invalid PKCS#1 v1.5 padding must not cause
2602 * the connection to end immediately; instead,
2603 * send a bad_record_mac later in the handshake.
2604 */
2605 ssl->handshake->pmslen = 48;
2606
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002607 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002608 if( ret != 0 )
2609 return( ret );
2610 }
2611
2612 return( ret );
2613}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002614#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2615 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002616
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002617#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002618static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2619 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002620{
Paul Bakker6db455e2013-09-18 17:29:31 +02002621 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002622 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002623
Paul Bakker6db455e2013-09-18 17:29:31 +02002624 if( ssl->f_psk == NULL &&
2625 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2626 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002627 {
2628 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2629 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2630 }
2631
2632 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002633 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002634 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002635 if( *p + 2 > end )
2636 {
2637 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2638 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2639 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002640
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 n = ( (*p)[0] << 8 ) | (*p)[1];
2642 *p += 2;
2643
2644 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002645 {
2646 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2647 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2648 }
2649
Paul Bakker6db455e2013-09-18 17:29:31 +02002650 if( ssl->f_psk != NULL )
2651 {
2652 if( ( ret != ssl->f_psk( ssl->p_psk, ssl, *p, n ) ) != 0 )
2653 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2654 }
2655
2656 if( ret == 0 )
2657 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002658 /* Identity is not a big secret since clients send it in the clear,
2659 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002660 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002661 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002662 {
2663 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2664 }
2665 }
2666
2667 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002668 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002670 if( ( ret = ssl_send_alert_message( ssl,
2671 SSL_ALERT_LEVEL_FATAL,
2672 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2673 {
2674 return( ret );
2675 }
2676
2677 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002678 }
2679
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002681 ret = 0;
2682
Paul Bakkerfbb17802013-04-17 19:10:21 +02002683 return( ret );
2684}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002685#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002686
Paul Bakker5121ce52009-01-03 21:22:43 +00002687static int ssl_parse_client_key_exchange( ssl_context *ssl )
2688{
Paul Bakker23986e52011-04-24 08:57:21 +00002689 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002690 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002691
Paul Bakker41c83d32013-03-20 14:39:14 +01002692 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002693
2694 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2695
2696 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2697 {
2698 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2699 return( ret );
2700 }
2701
2702 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2703 {
2704 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002705 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 }
2707
2708 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2709 {
2710 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002711 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 }
2713
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002715 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002717 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002718 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002719
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002721 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002722 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2723 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002725
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002726 if( p != end )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2729 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2730 }
2731
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732 ssl->handshake->pmslen = ssl->handshake->dhm_ctx.len;
2733
2734 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2735 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002736 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002737 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002738 {
2739 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2740 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2741 }
2742
2743 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002744 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002745 else
2746#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002747#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002748 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2749 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2750 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002751 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002752 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2753 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2754 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002755 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002756 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002757 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002758 {
2759 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2760 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2761 }
2762
2763 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2764
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002765 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2766 &ssl->handshake->pmslen,
2767 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002768 POLARSSL_MPI_MAX_SIZE,
2769 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002770 {
2771 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2772 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2773 }
2774
2775 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002778#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002779 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2780 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2781 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002782#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2783 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002784 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002785 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002786 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002787
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002788 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002789 {
2790 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2791 return( ret );
2792 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002793
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002794 if( p != end )
2795 {
2796 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2797 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2798 }
2799
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002800 if( ( ret = ssl_psk_derive_premaster( ssl,
2801 ciphersuite_info->key_exchange ) ) != 0 )
2802 {
2803 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2804 return( ret );
2805 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002806 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002807 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002808#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002809#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2810 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2811 {
2812 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002813 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002814
2815 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2816 {
2817 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2818 return( ret );
2819 }
2820
2821 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2822 {
2823 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2824 return( ret );
2825 }
2826
2827 if( ( ret = ssl_psk_derive_premaster( ssl,
2828 ciphersuite_info->key_exchange ) ) != 0 )
2829 {
2830 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2831 return( ret );
2832 }
2833 }
2834 else
2835#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002836#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2837 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2838 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002839 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002840 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002841
2842 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2843 {
2844 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2845 return( ret );
2846 }
2847 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2848 {
2849 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2850 return( ret );
2851 }
2852
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002853 if( p != end )
2854 {
2855 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2856 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2857 }
2858
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002859 if( ( ret = ssl_psk_derive_premaster( ssl,
2860 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002861 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002862 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2863 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002864 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002865 }
2866 else
2867#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002868#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2869 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2870 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002871 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002872 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002873
2874 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2875 {
2876 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2877 return( ret );
2878 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002879
2880 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2881 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002882 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002883 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2884 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002885 }
2886
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002887 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2888
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002889 if( ( ret = ssl_psk_derive_premaster( ssl,
2890 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002891 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002892 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002893 return( ret );
2894 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002895 }
2896 else
2897#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002898#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2899 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002900 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002901 if( ( ret = ssl_parse_encrypted_pms( ssl,
2902 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002903 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002904 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002905 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002906 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002907 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 }
2909 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002910 else
2911#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2912 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002913 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
2915 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002916
Paul Bakkerff60ee62010-03-16 21:09:09 +00002917 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2918 {
2919 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2920 return( ret );
2921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 ssl->state++;
2924
2925 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2926
2927 return( 0 );
2928}
2929
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002930#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2931 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002932 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2933 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002934static int ssl_parse_certificate_verify( ssl_context *ssl )
2935{
Paul Bakkered27a042013-04-18 22:46:23 +02002936 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002937 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
2939 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2940
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002941 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002943 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002944 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002945 {
2946 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2947 ssl->state++;
2948 return( 0 );
2949 }
2950
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002951 SSL_DEBUG_MSG( 1, ( "should not happen" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002952 return( ret );
2953}
2954#else
2955static int ssl_parse_certificate_verify( ssl_context *ssl )
2956{
2957 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002958 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002959 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002960 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002961 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002962#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002963 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002964#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002965 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2967
2968 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2969
2970 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002972 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2974 {
2975 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2976 ssl->state++;
2977 return( 0 );
2978 }
2979
Paul Bakkered27a042013-04-18 22:46:23 +02002980 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002981 {
2982 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2983 ssl->state++;
2984 return( 0 );
2985 }
2986
Paul Bakker48916f92012-09-16 19:57:18 +00002987 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
2989 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2990 {
2991 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2992 return( ret );
2993 }
2994
2995 ssl->state++;
2996
2997 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2998 {
2999 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003000 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003001 }
3002
3003 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3004 {
3005 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003006 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003007 }
3008
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003009 /*
3010 * 0 . 0 handshake type
3011 * 1 . 3 handshake length
3012 * 4 . 5 sig alg (TLS 1.2 only)
3013 * 4+n . 5+n signature length (n = sa_len)
3014 * 6+n . 6+n+m signature (m = sig_len)
3015 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003016
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003017#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3018 defined(POLARSSL_SSL_PROTO_TLS1_1)
3019 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003020 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003021 sa_len = 0;
3022
Paul Bakkerc70b9822013-04-07 22:00:46 +02003023 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003024 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003025
3026 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3027 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3028 POLARSSL_PK_ECDSA ) )
3029 {
3030 hash_start += 16;
3031 hashlen -= 16;
3032 md_alg = POLARSSL_MD_SHA1;
3033 }
Paul Bakker926af752012-11-23 13:38:07 +01003034 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035 else
3036#endif
Paul Bakker577e0062013-08-28 11:57:20 +02003037#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3038 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003039 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003040 sa_len = 2;
3041
Paul Bakker5121ce52009-01-03 21:22:43 +00003042 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003043 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003044 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003045 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003046 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003047 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3048 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003049 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3050 }
3051
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003052 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003053
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003054 /* Info from md_alg will be used instead */
3055 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003056
3057 /*
3058 * Signature
3059 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003060 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3061 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003062 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003063 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3064 " for verify message" ) );
3065 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003066 }
3067
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003068 /*
3069 * Check the certificate's key type matches the signature alg
3070 */
3071 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3072 {
3073 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3074 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3075 }
Paul Bakker577e0062013-08-28 11:57:20 +02003076 }
3077 else
3078#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3079 {
3080 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003081 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003082 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003083
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003084 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003085
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003086 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 {
3088 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003089 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003090 }
3091
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003092 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003093 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003094 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003096 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003097 return( ret );
3098 }
3099
3100 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3101
Paul Bakkered27a042013-04-18 22:46:23 +02003102 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003103}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003104#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3105 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3106 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Paul Bakkera503a632013-08-14 13:48:06 +02003108#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003109static int ssl_write_new_session_ticket( ssl_context *ssl )
3110{
3111 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003112 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003113 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003114
3115 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3116
3117 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3118 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3119
3120 /*
3121 * struct {
3122 * uint32 ticket_lifetime_hint;
3123 * opaque ticket<0..2^16-1>;
3124 * } NewSessionTicket;
3125 *
3126 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3127 * 8 . 9 ticket_len (n)
3128 * 10 . 9+n ticket content
3129 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003130
3131 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3132 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3133 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3134 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003135
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003136 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3137 {
3138 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3139 tlen = 0;
3140 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003141
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003142 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3143 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003144
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003145 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003146
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003147 /*
3148 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3149 * ChangeCipherSpec share the same state.
3150 */
3151 ssl->handshake->new_session_ticket = 0;
3152
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003153 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3154 {
3155 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3156 return( ret );
3157 }
3158
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003159 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3160
3161 return( 0 );
3162}
Paul Bakkera503a632013-08-14 13:48:06 +02003163#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003164
Paul Bakker5121ce52009-01-03 21:22:43 +00003165/*
Paul Bakker1961b702013-01-25 14:49:24 +01003166 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 */
Paul Bakker1961b702013-01-25 14:49:24 +01003168int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003169{
3170 int ret = 0;
3171
Paul Bakker1961b702013-01-25 14:49:24 +01003172 if( ssl->state == SSL_HANDSHAKE_OVER )
3173 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Paul Bakker1961b702013-01-25 14:49:24 +01003175 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3176
3177 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3178 return( ret );
3179
3180 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003181 {
Paul Bakker1961b702013-01-25 14:49:24 +01003182 case SSL_HELLO_REQUEST:
3183 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003184 break;
3185
Paul Bakker1961b702013-01-25 14:49:24 +01003186 /*
3187 * <== ClientHello
3188 */
3189 case SSL_CLIENT_HELLO:
3190 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003191 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003192
3193 /*
3194 * ==> ServerHello
3195 * Certificate
3196 * ( ServerKeyExchange )
3197 * ( CertificateRequest )
3198 * ServerHelloDone
3199 */
3200 case SSL_SERVER_HELLO:
3201 ret = ssl_write_server_hello( ssl );
3202 break;
3203
3204 case SSL_SERVER_CERTIFICATE:
3205 ret = ssl_write_certificate( ssl );
3206 break;
3207
3208 case SSL_SERVER_KEY_EXCHANGE:
3209 ret = ssl_write_server_key_exchange( ssl );
3210 break;
3211
3212 case SSL_CERTIFICATE_REQUEST:
3213 ret = ssl_write_certificate_request( ssl );
3214 break;
3215
3216 case SSL_SERVER_HELLO_DONE:
3217 ret = ssl_write_server_hello_done( ssl );
3218 break;
3219
3220 /*
3221 * <== ( Certificate/Alert )
3222 * ClientKeyExchange
3223 * ( CertificateVerify )
3224 * ChangeCipherSpec
3225 * Finished
3226 */
3227 case SSL_CLIENT_CERTIFICATE:
3228 ret = ssl_parse_certificate( ssl );
3229 break;
3230
3231 case SSL_CLIENT_KEY_EXCHANGE:
3232 ret = ssl_parse_client_key_exchange( ssl );
3233 break;
3234
3235 case SSL_CERTIFICATE_VERIFY:
3236 ret = ssl_parse_certificate_verify( ssl );
3237 break;
3238
3239 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3240 ret = ssl_parse_change_cipher_spec( ssl );
3241 break;
3242
3243 case SSL_CLIENT_FINISHED:
3244 ret = ssl_parse_finished( ssl );
3245 break;
3246
3247 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003248 * ==> ( NewSessionTicket )
3249 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003250 * Finished
3251 */
3252 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003253#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003254 if( ssl->handshake->new_session_ticket != 0 )
3255 ret = ssl_write_new_session_ticket( ssl );
3256 else
Paul Bakkera503a632013-08-14 13:48:06 +02003257#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003258 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003259 break;
3260
3261 case SSL_SERVER_FINISHED:
3262 ret = ssl_write_finished( ssl );
3263 break;
3264
3265 case SSL_FLUSH_BUFFERS:
3266 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3267 ssl->state = SSL_HANDSHAKE_WRAPUP;
3268 break;
3269
3270 case SSL_HANDSHAKE_WRAPUP:
3271 ssl_handshake_wrapup( ssl );
3272 break;
3273
3274 default:
3275 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003277 }
3278
Paul Bakker5121ce52009-01-03 21:22:43 +00003279 return( ret );
3280}
Paul Bakker5121ce52009-01-03 21:22:43 +00003281#endif