blob: 4fac0e4b4094438bde63dd5a60d8afa634c5bf53 [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Paul Bakker0be444a2013-08-27 21:55:01 +0200354#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200356 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
357 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200358 */
359static int ssl_sni_wrapper( ssl_context *ssl,
360 const unsigned char* name, size_t len )
361{
362 int ret;
363 ssl_key_cert *key_cert_ori = ssl->key_cert;
364
365 ssl->key_cert = NULL;
366 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200367 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200368
369 ssl->key_cert = key_cert_ori;
370
371 return( ret );
372}
373
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000375 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 size_t len )
377{
378 int ret;
379 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000380 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100382 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
383
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
385 if( servername_list_size + 2 != len )
386 {
387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
389 }
390
391 p = buf + 2;
392 while( servername_list_size > 0 )
393 {
394 hostname_len = ( ( p[1] << 8 ) | p[2] );
395 if( hostname_len + 3 > servername_list_size )
396 {
397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
399 }
400
401 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
402 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200403 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 if( ret != 0 )
405 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100406 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
408 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
410 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000411 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 }
413
414 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000415 p += hostname_len + 3;
416 }
417
418 if( servername_list_size != 0 )
419 {
420 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000422 }
423
424 return( 0 );
425}
Paul Bakker0be444a2013-08-27 21:55:01 +0200426#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427
Paul Bakker48916f92012-09-16 19:57:18 +0000428static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000429 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000430 size_t len )
431{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000432 int ret;
433
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100434#if defined(POLARSSL_SSL_RENEGOTIATION)
435 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
436 {
437 /* Check verify-data in constant-time. The length OTOH is no secret */
438 if( len != 1 + ssl->verify_data_len ||
439 buf[0] != ssl->verify_data_len ||
440 safer_memcmp( buf + 1, ssl->peer_verify_data,
441 ssl->verify_data_len ) != 0 )
442 {
443 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
444
445 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
446 return( ret );
447
448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
449 }
450 }
451 else
452#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000453 {
454 if( len != 1 || buf[0] != 0x0 )
455 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100456 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000457
458 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
459 return( ret );
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
462 }
463
464 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
465 }
Paul Bakker48916f92012-09-16 19:57:18 +0000466
467 return( 0 );
468}
469
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100470#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
471 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000472static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
473 const unsigned char *buf,
474 size_t len )
475{
476 size_t sig_alg_list_size;
477 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200478 const unsigned char *end = buf + len;
479 const int *md_cur;
480
Paul Bakker23f36802012-09-28 14:15:14 +0000481
482 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
483 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200484 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000485 {
486 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
487 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
488 }
489
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200490 /*
491 * For now, ignore the SignatureAlgorithm part and rely on offered
492 * ciphersuites only for that part. To be fixed later.
493 *
494 * So, just look at the HashAlgorithm part.
495 */
496 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
497 for( p = buf + 2; p < end; p += 2 ) {
498 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
499 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200500 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200501 }
Paul Bakker23f36802012-09-28 14:15:14 +0000502 }
Paul Bakker23f36802012-09-28 14:15:14 +0000503 }
504
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200505 /* Some key echanges do not need signatures at all */
506 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
507 return( 0 );
508
509have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000510 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
511 ssl->handshake->sig_alg ) );
512
513 return( 0 );
514}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100515#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
516 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000517
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200518#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200519static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
520 const unsigned char *buf,
521 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100522{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200523 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100524 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200525 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100526
527 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
528 if( list_size + 2 != len ||
529 list_size % 2 != 0 )
530 {
531 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
533 }
534
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200535 /* Should never happen unless client duplicates the extension */
536 if( ssl->handshake->curves != NULL )
537 {
538 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
539 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
540 }
541
Manuel Pégourié-Gonnardc3f6b622014-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 Bakker9af723c2014-05-01 13:03:14 +0200551 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200552 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
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100642#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
643static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
644 const unsigned char *buf,
645 size_t len )
646{
647 if( len != 0 )
648 {
649 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
650 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
651 }
652
653 ((void) buf);
654
655 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
656 ssl->minor_ver != SSL_MINOR_VERSION_0 )
657 {
658 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
659 }
660
661 return( 0 );
662}
663#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
664
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200665#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
666static int ssl_parse_extended_ms_ext( ssl_context *ssl,
667 const unsigned char *buf,
668 size_t len )
669{
670 if( len != 0 )
671 {
672 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
673 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
674 }
675
676 ((void) buf);
677
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200678 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
679 ssl->minor_ver != SSL_MINOR_VERSION_0 )
680 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200681 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200682 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200683
684 return( 0 );
685}
686#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
687
Paul Bakkera503a632013-08-14 13:48:06 +0200688#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200689static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200690 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200691 size_t len )
692{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200693 int ret;
694
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200695 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
696 return( 0 );
697
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200698 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200699 ssl->handshake->new_session_ticket = 1;
700
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200701 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
702
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200703 if( len == 0 )
704 return( 0 );
705
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100706#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200707 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
708 {
709 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
710 return( 0 );
711 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100712#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200713
714 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200715 * Failures are ok: just ignore the ticket and proceed.
716 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200717 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
718 {
719 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200720 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200721 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200722
723 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
724
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200725 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200726
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200727 /* Don't send a new ticket after all, this one is OK */
728 ssl->handshake->new_session_ticket = 0;
729
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200730 return( 0 );
731}
Paul Bakkera503a632013-08-14 13:48:06 +0200732#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200733
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200734#if defined(POLARSSL_SSL_ALPN)
735static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200736 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200737{
Paul Bakker14b16c62014-05-28 11:33:54 +0200738 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200739 const unsigned char *theirs, *start, *end;
740 const char **ours;
741
742 /* If ALPN not configured, just ignore the extension */
743 if( ssl->alpn_list == NULL )
744 return( 0 );
745
746 /*
747 * opaque ProtocolName<1..2^8-1>;
748 *
749 * struct {
750 * ProtocolName protocol_name_list<2..2^16-1>
751 * } ProtocolNameList;
752 */
753
754 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
755 if( len < 4 )
756 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
757
758 list_len = ( buf[0] << 8 ) | buf[1];
759 if( list_len != len - 2 )
760 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
761
762 /*
763 * Use our order of preference
764 */
765 start = buf + 2;
766 end = buf + len;
767 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
768 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200769 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200770 for( theirs = start; theirs != end; theirs += cur_len )
771 {
772 /* If the list is well formed, we should get equality first */
773 if( theirs > end )
774 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
775
776 cur_len = *theirs++;
777
778 /* Empty strings MUST NOT be included */
779 if( cur_len == 0 )
780 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
781
Paul Bakker14b16c62014-05-28 11:33:54 +0200782 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200783 memcmp( theirs, *ours, cur_len ) == 0 )
784 {
785 ssl->alpn_chosen = *ours;
786 return( 0 );
787 }
788 }
789 }
790
791 /* If we get there, no match was found */
792 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
793 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
794 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
795}
796#endif /* POLARSSL_SSL_ALPN */
797
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100798/*
799 * Auxiliary functions for ServerHello parsing and related actions
800 */
801
802#if defined(POLARSSL_X509_CRT_PARSE_C)
803/*
804 * Return 1 if the given EC key uses the given curve, 0 otherwise
805 */
806#if defined(POLARSSL_ECDSA_C)
807static int ssl_key_matches_curves( pk_context *pk,
808 const ecp_curve_info **curves )
809{
810 const ecp_curve_info **crv = curves;
811 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
812
813 while( *crv != NULL )
814 {
815 if( (*crv)->grp_id == grp_id )
816 return( 1 );
817 crv++;
818 }
819
820 return( 0 );
821}
822#endif /* POLARSSL_ECDSA_C */
823
824/*
825 * Try picking a certificate for this ciphersuite,
826 * return 0 on success and -1 on failure.
827 */
828static int ssl_pick_cert( ssl_context *ssl,
829 const ssl_ciphersuite_t * ciphersuite_info )
830{
831 ssl_key_cert *cur, *list;
832 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
833
834#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
835 if( ssl->handshake->sni_key_cert != NULL )
836 list = ssl->handshake->sni_key_cert;
837 else
838#endif
839 list = ssl->handshake->key_cert;
840
841 if( pk_alg == POLARSSL_PK_NONE )
842 return( 0 );
843
844 for( cur = list; cur != NULL; cur = cur->next )
845 {
846 if( ! pk_can_do( cur->key, pk_alg ) )
847 continue;
848
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200849 /*
850 * This avoids sending the client a cert it'll reject based on
851 * keyUsage or other extensions.
852 *
853 * It also allows the user to provision different certificates for
854 * different uses based on keyUsage, eg if they want to avoid signing
855 * and decrypting with the same RSA key.
856 */
857 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
858 SSL_IS_SERVER ) != 0 )
859 {
860 continue;
861 }
862
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863#if defined(POLARSSL_ECDSA_C)
864 if( pk_alg == POLARSSL_PK_ECDSA )
865 {
866 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
867 break;
868 }
869 else
870#endif
871 break;
872 }
873
874 if( cur == NULL )
875 return( -1 );
876
877 ssl->handshake->key_cert = cur;
878 return( 0 );
879}
880#endif /* POLARSSL_X509_CRT_PARSE_C */
881
882/*
883 * Check if a given ciphersuite is suitable for use with our config/keys/etc
884 * Sets ciphersuite_info only if the suite matches.
885 */
886static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
887 const ssl_ciphersuite_t **ciphersuite_info )
888{
889 const ssl_ciphersuite_t *suite_info;
890
891 suite_info = ssl_ciphersuite_from_id( suite_id );
892 if( suite_info == NULL )
893 {
894 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
895 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
896 }
897
898 if( suite_info->min_minor_ver > ssl->minor_ver ||
899 suite_info->max_minor_ver < ssl->minor_ver )
900 return( 0 );
901
902#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
903 if( ssl_ciphersuite_uses_ec( suite_info ) &&
904 ( ssl->handshake->curves == NULL ||
905 ssl->handshake->curves[0] == NULL ) )
906 return( 0 );
907#endif
908
909#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
910 /* If the ciphersuite requires a pre-shared key and we don't
911 * have one, skip it now rather than failing later */
912 if( ssl_ciphersuite_uses_psk( suite_info ) &&
913 ssl->f_psk == NULL &&
914 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
915 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
916 return( 0 );
917#endif
918
919#if defined(POLARSSL_X509_CRT_PARSE_C)
920 /*
921 * Final check: if ciphersuite requires us to have a
922 * certificate/key of a particular type:
923 * - select the appropriate certificate if we have one, or
924 * - try the next ciphersuite if we don't
925 * This must be done last since we modify the key_cert list.
926 */
927 if( ssl_pick_cert( ssl, suite_info ) != 0 )
928 return( 0 );
929#endif
930
931 *ciphersuite_info = suite_info;
932 return( 0 );
933}
934
Paul Bakker78a8c712013-03-06 17:01:52 +0100935#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
936static int ssl_parse_client_hello_v2( ssl_context *ssl )
937{
938 int ret;
939 unsigned int i, j;
940 size_t n;
941 unsigned int ciph_len, sess_len, chal_len;
942 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200943 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200944 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100945
946 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
947
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100948#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +0100949 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
950 {
951 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
952
953 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
954 return( ret );
955
956 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
957 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100958#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +0100959
960 buf = ssl->in_hdr;
961
962 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
963
964 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
965 buf[2] ) );
966 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
967 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
968 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
969 buf[3], buf[4] ) );
970
971 /*
972 * SSLv2 Client Hello
973 *
974 * Record layer:
975 * 0 . 1 message length
976 *
977 * SSL layer:
978 * 2 . 2 message type
979 * 3 . 4 protocol version
980 */
981 if( buf[2] != SSL_HS_CLIENT_HELLO ||
982 buf[3] != SSL_MAJOR_VERSION_3 )
983 {
984 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
985 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
986 }
987
988 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
989
990 if( n < 17 || n > 512 )
991 {
992 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
993 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
994 }
995
996 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200997 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
998 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100999
1000 if( ssl->minor_ver < ssl->min_minor_ver )
1001 {
1002 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001003 " [%d:%d] < [%d:%d]",
1004 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001005 ssl->min_major_ver, ssl->min_minor_ver ) );
1006
1007 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1008 SSL_ALERT_MSG_PROTOCOL_VERSION );
1009 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1010 }
1011
Paul Bakker2fbefde2013-06-29 16:01:15 +02001012 ssl->handshake->max_major_ver = buf[3];
1013 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001014
1015 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1016 {
1017 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1018 return( ret );
1019 }
1020
1021 ssl->handshake->update_checksum( ssl, buf + 2, n );
1022
1023 buf = ssl->in_msg;
1024 n = ssl->in_left - 5;
1025
1026 /*
1027 * 0 . 1 ciphersuitelist length
1028 * 2 . 3 session id length
1029 * 4 . 5 challenge length
1030 * 6 . .. ciphersuitelist
1031 * .. . .. session id
1032 * .. . .. challenge
1033 */
1034 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1035
1036 ciph_len = ( buf[0] << 8 ) | buf[1];
1037 sess_len = ( buf[2] << 8 ) | buf[3];
1038 chal_len = ( buf[4] << 8 ) | buf[5];
1039
1040 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1041 ciph_len, sess_len, chal_len ) );
1042
1043 /*
1044 * Make sure each parameter length is valid
1045 */
1046 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1047 {
1048 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1049 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1050 }
1051
1052 if( sess_len > 32 )
1053 {
1054 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1055 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1056 }
1057
1058 if( chal_len < 8 || chal_len > 32 )
1059 {
1060 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1061 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1062 }
1063
1064 if( n != 6 + ciph_len + sess_len + chal_len )
1065 {
1066 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1067 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1068 }
1069
1070 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1071 buf + 6, ciph_len );
1072 SSL_DEBUG_BUF( 3, "client hello, session id",
1073 buf + 6 + ciph_len, sess_len );
1074 SSL_DEBUG_BUF( 3, "client hello, challenge",
1075 buf + 6 + ciph_len + sess_len, chal_len );
1076
1077 p = buf + 6 + ciph_len;
1078 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001079 memset( ssl->session_negotiate->id, 0,
1080 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001081 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1082
1083 p += sess_len;
1084 memset( ssl->handshake->randbytes, 0, 64 );
1085 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1086
1087 /*
1088 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1089 */
1090 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1091 {
1092 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1093 {
1094 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001095#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001096 if( ssl->renegotiation == SSL_RENEGOTIATION )
1097 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001098 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1099 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001100
1101 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1102 return( ret );
1103
1104 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1105 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001106#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001107 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1108 break;
1109 }
1110 }
1111
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001112#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1113 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1114 {
1115 if( p[0] == 0 &&
1116 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1117 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1118 {
1119 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1120
1121 if( ssl->minor_ver < ssl->max_minor_ver )
1122 {
1123 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1124
1125 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1126 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1127
1128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1129 }
1130
1131 break;
1132 }
1133 }
1134#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1135
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001136 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001137 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001138#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1139 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1140 {
1141 for( i = 0; ciphersuites[i] != 0; i++ )
1142#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001143 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001144 {
1145 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001146#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001147 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001148 if( p[0] != 0 ||
1149 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1150 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1151 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001152
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001153 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1154 &ciphersuite_info ) ) != 0 )
1155 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001156
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001157 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001158 goto have_ciphersuite_v2;
1159 }
1160 }
1161
1162 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1163
1164 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1165
1166have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001167 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001168 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001169 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001170
1171 /*
1172 * SSLv2 Client Hello relevant renegotiation security checks
1173 */
1174 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1175 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1176 {
1177 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1178
1179 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1180 return( ret );
1181
1182 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1183 }
1184
1185 ssl->in_left = 0;
1186 ssl->state++;
1187
1188 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1189
1190 return( 0 );
1191}
1192#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1193
Paul Bakker5121ce52009-01-03 21:22:43 +00001194static int ssl_parse_client_hello( ssl_context *ssl )
1195{
Paul Bakker23986e52011-04-24 08:57:21 +00001196 int ret;
1197 unsigned int i, j;
1198 size_t n;
1199 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001200 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001201 unsigned int ext_len = 0;
1202 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001203#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001204 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001205#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001206 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001207 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001208 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001209
1210 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1211
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001212#if defined(POLARSSL_SSL_RENEGOTIATION)
1213 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1214#endif
1215 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
1217 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1218 return( ret );
1219 }
1220
1221 buf = ssl->in_hdr;
1222
Paul Bakker78a8c712013-03-06 17:01:52 +01001223#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1224 if( ( buf[0] & 0x80 ) != 0 )
1225 return ssl_parse_client_hello_v2( ssl );
1226#endif
1227
Paul Bakkerec636f32012-09-09 19:17:02 +00001228 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1229
1230 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1231 buf[0] ) );
1232 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1233 ( buf[3] << 8 ) | buf[4] ) );
1234 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1235 buf[1], buf[2] ) );
1236
1237 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001238 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001239 *
1240 * Record layer:
1241 * 0 . 0 message type
1242 * 1 . 2 protocol version
1243 * 3 . 4 message length
1244 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001245
1246 /* According to RFC 5246 Appendix E.1, the version here is typically
1247 * "{03,00}, the lowest version number supported by the client, [or] the
1248 * value of ClientHello.client_version", so the only meaningful check here
1249 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001250 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001251 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001252 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001253 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1255 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001256
Paul Bakkerec636f32012-09-09 19:17:02 +00001257 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001258
Paul Bakker4f42c112014-04-17 14:48:23 +02001259 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001260 {
1261 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1262 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1263 }
1264
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001265#if defined(POLARSSL_SSL_RENEGOTIATION)
1266 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1267#endif
1268 if( ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001269 {
1270 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1271 return( ret );
1272 }
1273
1274 buf = ssl->in_msg;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001275#if defined(POLARSSL_SSL_RENEGOTIATION)
1276 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00001277 n = ssl->in_msglen;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001278 else
1279#endif
1280 n = ssl->in_left - 5;
Paul Bakkerec636f32012-09-09 19:17:02 +00001281
Paul Bakker48916f92012-09-16 19:57:18 +00001282 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001283
1284 /*
1285 * SSL layer:
1286 * 0 . 0 handshake type
1287 * 1 . 3 handshake length
1288 * 4 . 5 protocol version
1289 * 6 . 9 UNIX time()
1290 * 10 . 37 random bytes
1291 * 38 . 38 session id length
1292 * 39 . 38+x session id
1293 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001294 * 41+x . 40+y ciphersuitelist
1295 * 41+y . 41+y compression alg length
1296 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001297 * .. . .. extensions
1298 */
1299 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1300
1301 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1302 buf[0] ) );
1303 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1304 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1305 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1306 buf[4], buf[5] ) );
1307
1308 /*
1309 * Check the handshake type and protocol version
1310 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001311 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001312 {
1313 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1314 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1315 }
1316
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001317 ssl->major_ver = buf[4];
1318 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001319
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001320 ssl->handshake->max_major_ver = ssl->major_ver;
1321 ssl->handshake->max_minor_ver = ssl->minor_ver;
1322
1323 if( ssl->major_ver < ssl->min_major_ver ||
1324 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001325 {
1326 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001327 " [%d:%d] < [%d:%d]",
1328 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001329 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001330
1331 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1332 SSL_ALERT_MSG_PROTOCOL_VERSION );
1333
1334 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1335 }
1336
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001337 if( ssl->major_ver > ssl->max_major_ver )
1338 {
1339 ssl->major_ver = ssl->max_major_ver;
1340 ssl->minor_ver = ssl->max_minor_ver;
1341 }
1342 else if( ssl->minor_ver > ssl->max_minor_ver )
1343 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001344
Paul Bakker48916f92012-09-16 19:57:18 +00001345 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001346
1347 /*
1348 * Check the handshake message length
1349 */
1350 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1351 {
1352 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1353 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1354 }
1355
1356 /*
1357 * Check the session length
1358 */
1359 sess_len = buf[38];
1360
Paul Bakker0f651c72014-05-22 15:12:19 +02001361 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001362 {
1363 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1364 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1365 }
1366
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ssl->session_negotiate->length = sess_len;
1368 memset( ssl->session_negotiate->id, 0,
1369 sizeof( ssl->session_negotiate->id ) );
1370 memcpy( ssl->session_negotiate->id, buf + 39,
1371 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001372
1373 /*
1374 * Check the ciphersuitelist length
1375 */
1376 ciph_len = ( buf[39 + sess_len] << 8 )
1377 | ( buf[40 + sess_len] );
1378
Paul Bakker0f651c72014-05-22 15:12:19 +02001379 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001380 {
1381 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1382 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1383 }
1384
1385 /*
1386 * Check the compression algorithms length
1387 */
1388 comp_len = buf[41 + sess_len + ciph_len];
1389
Paul Bakker0f651c72014-05-22 15:12:19 +02001390 if( comp_len < 1 || comp_len > 16 ||
1391 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001392 {
1393 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1395 }
1396
Paul Bakker48916f92012-09-16 19:57:18 +00001397 /*
1398 * Check the extension length
1399 */
1400 if( n > 42 + sess_len + ciph_len + comp_len )
1401 {
1402 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1403 | ( buf[43 + sess_len + ciph_len + comp_len] );
1404
1405 if( ( ext_len > 0 && ext_len < 4 ) ||
1406 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1407 {
1408 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1409 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1410 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1411 }
1412 }
1413
1414 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001415#if defined(POLARSSL_ZLIB_SUPPORT)
1416 for( i = 0; i < comp_len; ++i )
1417 {
Paul Bakker48916f92012-09-16 19:57:18 +00001418 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001419 {
Paul Bakker48916f92012-09-16 19:57:18 +00001420 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001421 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001424#endif
1425
Paul Bakkerec636f32012-09-09 19:17:02 +00001426 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1427 buf + 6, 32 );
1428 SSL_DEBUG_BUF( 3, "client hello, session id",
1429 buf + 38, sess_len );
1430 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1431 buf + 41 + sess_len, ciph_len );
1432 SSL_DEBUG_BUF( 3, "client hello, compression",
1433 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Paul Bakkerec636f32012-09-09 19:17:02 +00001435 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001436 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1437 */
1438 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1439 {
1440 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1441 {
1442 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001443#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001444 if( ssl->renegotiation == SSL_RENEGOTIATION )
1445 {
1446 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001447
1448 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1449 return( ret );
1450
Paul Bakker48916f92012-09-16 19:57:18 +00001451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1452 }
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001453 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001454#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00001455 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1456 break;
1457 }
1458 }
1459
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001460#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1461 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1462 {
1463 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1464 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1465 {
1466 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1467
1468 if( ssl->minor_ver < ssl->max_minor_ver )
1469 {
1470 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1471
1472 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1473 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1474
1475 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1476 }
1477
1478 break;
1479 }
1480 }
1481#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1482
Paul Bakker48916f92012-09-16 19:57:18 +00001483 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001484
1485 while( ext_len )
1486 {
1487 unsigned int ext_id = ( ( ext[0] << 8 )
1488 | ( ext[1] ) );
1489 unsigned int ext_size = ( ( ext[2] << 8 )
1490 | ( ext[3] ) );
1491
1492 if( ext_size + 4 > ext_len )
1493 {
1494 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1495 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1496 }
1497 switch( ext_id )
1498 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001499#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001500 case TLS_EXT_SERVERNAME:
1501 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1502 if( ssl->f_sni == NULL )
1503 break;
1504
1505 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1506 if( ret != 0 )
1507 return( ret );
1508 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001509#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 case TLS_EXT_RENEGOTIATION_INFO:
1512 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001513#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001514 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001515#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001516
Paul Bakker23f36802012-09-28 14:15:14 +00001517 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1518 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001519 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001520 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001521
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001522#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1523 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001524 case TLS_EXT_SIG_ALG:
1525 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001526#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001527 if( ssl->renegotiation == SSL_RENEGOTIATION )
1528 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001529#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001530
1531 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1532 if( ret != 0 )
1533 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001534 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001535#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1536 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001537
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001538#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001539 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1540 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1541
1542 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1543 if( ret != 0 )
1544 return( ret );
1545 break;
1546
1547 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1548 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001549 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001550
1551 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1552 if( ret != 0 )
1553 return( ret );
1554 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001555#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001556
Paul Bakker05decb22013-08-15 13:33:48 +02001557#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001558 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1559 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1560
1561 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1562 if( ret != 0 )
1563 return( ret );
1564 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001565#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001566
Paul Bakker1f2bc622013-08-15 13:45:55 +02001567#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001568 case TLS_EXT_TRUNCATED_HMAC:
1569 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1570
1571 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1572 if( ret != 0 )
1573 return( ret );
1574 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001575#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001576
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001577#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1578 case TLS_EXT_ENCRYPT_THEN_MAC:
1579 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1580
1581 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1582 if( ret != 0 )
1583 return( ret );
1584 break;
1585#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1586
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001587#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1588 case TLS_EXT_EXTENDED_MASTER_SECRET:
1589 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1590
1591 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1592 if( ret != 0 )
1593 return( ret );
1594 break;
1595#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1596
Paul Bakkera503a632013-08-14 13:48:06 +02001597#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001598 case TLS_EXT_SESSION_TICKET:
1599 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1600
1601 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1602 if( ret != 0 )
1603 return( ret );
1604 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001605#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001606
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001607#if defined(POLARSSL_SSL_ALPN)
1608 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001609 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001610
1611 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1612 if( ret != 0 )
1613 return( ret );
1614 break;
1615#endif /* POLARSSL_SSL_SESSION_TICKETS */
1616
Paul Bakker48916f92012-09-16 19:57:18 +00001617 default:
1618 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1619 ext_id ) );
1620 }
1621
1622 ext_len -= 4 + ext_size;
1623 ext += 4 + ext_size;
1624
1625 if( ext_len > 0 && ext_len < 4 )
1626 {
1627 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1628 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1629 }
1630 }
1631
1632 /*
1633 * Renegotiation security checks
1634 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001635 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001636 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1637 {
1638 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1639 handshake_failure = 1;
1640 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001641#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001642 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1643 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1644 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001645 {
1646 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001647 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001648 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001649 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1650 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1651 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001652 {
1653 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001654 handshake_failure = 1;
1655 }
1656 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1657 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1658 renegotiation_info_seen == 1 )
1659 {
1660 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1661 handshake_failure = 1;
1662 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001663#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001664
1665 if( handshake_failure == 1 )
1666 {
1667 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1668 return( ret );
1669
Paul Bakker48916f92012-09-16 19:57:18 +00001670 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1671 }
Paul Bakker380da532012-04-18 16:10:25 +00001672
Paul Bakker41c83d32013-03-20 14:39:14 +01001673 /*
1674 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001675 * (At the end because we need information from the EC-based extensions
1676 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001677 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001678 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001679 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001680#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1681 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1682 {
1683 for( i = 0; ciphersuites[i] != 0; i++ )
1684#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001685 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001686 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001687 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001688#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001689 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001690 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1691 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1692 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001693
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001694 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1695 &ciphersuite_info ) ) != 0 )
1696 return( ret );
1697
1698 if( ciphersuite_info != NULL )
1699 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001700 }
1701 }
1702
1703 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1704
1705 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1706 return( ret );
1707
1708 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1709
1710have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001711 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001712 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1713 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1714
Paul Bakker5121ce52009-01-03 21:22:43 +00001715 ssl->in_left = 0;
1716 ssl->state++;
1717
1718 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1719
1720 return( 0 );
1721}
1722
Paul Bakker1f2bc622013-08-15 13:45:55 +02001723#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001724static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1725 unsigned char *buf,
1726 size_t *olen )
1727{
1728 unsigned char *p = buf;
1729
1730 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1731 {
1732 *olen = 0;
1733 return;
1734 }
1735
1736 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1737
1738 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1739 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1740
1741 *p++ = 0x00;
1742 *p++ = 0x00;
1743
1744 *olen = 4;
1745}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001746#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001747
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001748#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1749static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
1750 unsigned char *buf,
1751 size_t *olen )
1752{
1753 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001754 const ssl_ciphersuite_t *suite = NULL;
1755 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001756
1757 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
1758 ssl->minor_ver == SSL_MINOR_VERSION_0 )
1759 {
1760 *olen = 0;
1761 return;
1762 }
1763
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01001764 /*
1765 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
1766 * from a client and then selects a stream or Authenticated Encryption
1767 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
1768 * encrypt-then-MAC response extension back to the client."
1769 */
1770 if( ( suite = ssl_ciphersuite_from_id(
1771 ssl->session_negotiate->ciphersuite ) ) == NULL ||
1772 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
1773 cipher->mode != POLARSSL_MODE_CBC )
1774 {
1775 *olen = 0;
1776 return;
1777 }
1778
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001779 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
1780
1781 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
1782 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
1783
1784 *p++ = 0x00;
1785 *p++ = 0x00;
1786
1787 *olen = 4;
1788}
1789#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1790
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001791#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1792static void ssl_write_extended_ms_ext( ssl_context *ssl,
1793 unsigned char *buf,
1794 size_t *olen )
1795{
1796 unsigned char *p = buf;
1797
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02001798 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
1799 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001800 {
1801 *olen = 0;
1802 return;
1803 }
1804
1805 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
1806 "extension" ) );
1807
1808 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
1809 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
1810
1811 *p++ = 0x00;
1812 *p++ = 0x00;
1813
1814 *olen = 4;
1815}
1816#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1817
Paul Bakkera503a632013-08-14 13:48:06 +02001818#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001819static void ssl_write_session_ticket_ext( ssl_context *ssl,
1820 unsigned char *buf,
1821 size_t *olen )
1822{
1823 unsigned char *p = buf;
1824
1825 if( ssl->handshake->new_session_ticket == 0 )
1826 {
1827 *olen = 0;
1828 return;
1829 }
1830
1831 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1832
1833 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1834 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1835
1836 *p++ = 0x00;
1837 *p++ = 0x00;
1838
1839 *olen = 4;
1840}
Paul Bakkera503a632013-08-14 13:48:06 +02001841#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001842
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001843static void ssl_write_renegotiation_ext( ssl_context *ssl,
1844 unsigned char *buf,
1845 size_t *olen )
1846{
1847 unsigned char *p = buf;
1848
1849 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1850 {
1851 *olen = 0;
1852 return;
1853 }
1854
1855 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1856
1857 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1858 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1859
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001860#if defined(POLARSSL_SSL_RENEGOTIATION)
1861 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1862 {
1863 *p++ = 0x00;
1864 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1865 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001866
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001867 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1868 p += ssl->verify_data_len;
1869 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1870 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001871
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001872 *olen = 5 + ssl->verify_data_len * 2;
1873 }
1874 else
1875#endif /* POLARSSL_SSL_RENEGOTIATION */
1876 {
1877 *p++ = 0x00;
1878 *p++ = 0x01;
1879 *p++ = 0x00;
1880
1881 *olen = 5;
1882 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001883}
1884
Paul Bakker05decb22013-08-15 13:33:48 +02001885#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001886static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1887 unsigned char *buf,
1888 size_t *olen )
1889{
1890 unsigned char *p = buf;
1891
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001892 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1893 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001894 *olen = 0;
1895 return;
1896 }
1897
1898 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1899
1900 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1901 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1902
1903 *p++ = 0x00;
1904 *p++ = 1;
1905
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001906 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001907
1908 *olen = 5;
1909}
Paul Bakker05decb22013-08-15 13:33:48 +02001910#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +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 +02001913static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1914 unsigned char *buf,
1915 size_t *olen )
1916{
1917 unsigned char *p = buf;
1918 ((void) ssl);
1919
Paul Bakker677377f2013-10-28 12:54:26 +01001920 if( ( ssl->handshake->cli_exts &
1921 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1922 {
1923 *olen = 0;
1924 return;
1925 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001926
1927 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1928
1929 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1930 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1931
1932 *p++ = 0x00;
1933 *p++ = 2;
1934
1935 *p++ = 1;
1936 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1937
1938 *olen = 6;
1939}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001940#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001941
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001942#if defined(POLARSSL_SSL_ALPN )
1943static void ssl_write_alpn_ext( ssl_context *ssl,
1944 unsigned char *buf, size_t *olen )
1945{
1946 if( ssl->alpn_chosen == NULL )
1947 {
1948 *olen = 0;
1949 return;
1950 }
1951
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001952 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001953
1954 /*
1955 * 0 . 1 ext identifier
1956 * 2 . 3 ext length
1957 * 4 . 5 protocol list length
1958 * 6 . 6 protocol name length
1959 * 7 . 7+n protocol name
1960 */
1961 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1962 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1963
1964 *olen = 7 + strlen( ssl->alpn_chosen );
1965
1966 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1967 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1968
1969 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1970 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1971
1972 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1973
1974 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1975}
1976#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1977
Paul Bakker5121ce52009-01-03 21:22:43 +00001978static int ssl_write_server_hello( ssl_context *ssl )
1979{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001980#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001981 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001982#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001983 int ret;
1984 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001985 unsigned char *buf, *p;
1986
1987 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1988
Paul Bakkera9a028e2013-11-21 17:31:06 +01001989 if( ssl->f_rng == NULL )
1990 {
1991 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1992 return( POLARSSL_ERR_SSL_NO_RNG );
1993 }
1994
Paul Bakker5121ce52009-01-03 21:22:43 +00001995 /*
1996 * 0 . 0 handshake type
1997 * 1 . 3 handshake length
1998 * 4 . 5 protocol version
1999 * 6 . 9 UNIX time()
2000 * 10 . 37 random bytes
2001 */
2002 buf = ssl->out_msg;
2003 p = buf + 4;
2004
2005 *p++ = (unsigned char) ssl->major_ver;
2006 *p++ = (unsigned char) ssl->minor_ver;
2007
2008 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
2009 buf[4], buf[5] ) );
2010
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002011#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 t = time( NULL );
2013 *p++ = (unsigned char)( t >> 24 );
2014 *p++ = (unsigned char)( t >> 16 );
2015 *p++ = (unsigned char)( t >> 8 );
2016 *p++ = (unsigned char)( t );
2017
2018 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002019#else
2020 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2021 return( ret );
2022
2023 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002024#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002025
Paul Bakkera3d195c2011-11-27 21:07:34 +00002026 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2027 return( ret );
2028
2029 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002030
Paul Bakker48916f92012-09-16 19:57:18 +00002031 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002032
2033 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2034
2035 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002036 * Resume is 0 by default, see ssl_handshake_init().
2037 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2038 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002039 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002040 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002041#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002042 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002043#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002044 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002045 ssl->f_get_cache != NULL &&
2046 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2047 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002048 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002049 ssl->handshake->resume = 1;
2050 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002051
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002052 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 {
2054 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002055 * New session, create a new session id,
2056 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 ssl->state++;
2059
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002060#if defined(POLARSSL_HAVE_TIME)
2061 ssl->session_negotiate->start = time( NULL );
2062#endif
2063
Paul Bakkera503a632013-08-14 13:48:06 +02002064#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002065 if( ssl->handshake->new_session_ticket != 0 )
2066 {
2067 ssl->session_negotiate->length = n = 0;
2068 memset( ssl->session_negotiate->id, 0, 32 );
2069 }
2070 else
2071#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002072 {
2073 ssl->session_negotiate->length = n = 32;
2074 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002075 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002076 return( ret );
2077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002078 }
2079 else
2080 {
2081 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002082 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002083 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002084 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002085 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002086
2087 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2088 {
2089 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2090 return( ret );
2091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002092 }
2093
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002094 /*
2095 * 38 . 38 session id length
2096 * 39 . 38+n session id
2097 * 39+n . 40+n chosen ciphersuite
2098 * 41+n . 41+n chosen compression alg.
2099 * 42+n . 43+n extensions length
2100 * 44+n . 43+n+m extensions
2101 */
2102 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002103 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2104 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002105
2106 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2107 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2108 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002109 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002110
Paul Bakker48916f92012-09-16 19:57:18 +00002111 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2112 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2113 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002114
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002115 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2116 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002117 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002118 ssl->session_negotiate->compression ) );
2119
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002120 /*
2121 * First write extensions, then the total length
2122 */
2123 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2124 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002125
Paul Bakker05decb22013-08-15 13:33:48 +02002126#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002127 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2128 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002129#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002130
Paul Bakker1f2bc622013-08-15 13:45:55 +02002131#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002132 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2133 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002134#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002135
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002136#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2137 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2138 ext_len += olen;
2139#endif
2140
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002141#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2142 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2143 ext_len += olen;
2144#endif
2145
Paul Bakkera503a632013-08-14 13:48:06 +02002146#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002147 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2148 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002149#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002150
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002151#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002152 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2153 ext_len += olen;
2154#endif
2155
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002156#if defined(POLARSSL_SSL_ALPN)
2157 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2158 ext_len += olen;
2159#endif
2160
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002161 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002162
Paul Bakkera7036632014-04-30 10:15:38 +02002163 if( ext_len > 0 )
2164 {
2165 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2166 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2167 p += ext_len;
2168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 ssl->out_msglen = p - buf;
2171 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2172 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2173
2174 ret = ssl_write_record( ssl );
2175
2176 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2177
2178 return( ret );
2179}
2180
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002181#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2182 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002183 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2184 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002185static int ssl_write_certificate_request( ssl_context *ssl )
2186{
Paul Bakkered27a042013-04-18 22:46:23 +02002187 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188
2189 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2190
2191 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002192 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002193 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2194 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002195 {
2196 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2197 ssl->state++;
2198 return( 0 );
2199 }
2200
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002201 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2202 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002203}
2204#else
2205static int ssl_write_certificate_request( ssl_context *ssl )
2206{
2207 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2208 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002209 size_t dn_size, total_dn_size; /* excluding length bytes */
2210 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002212 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002213
2214 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2215
2216 ssl->state++;
2217
Paul Bakkerfbb17802013-04-17 19:10:21 +02002218 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002219 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002220 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002221 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002222 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002224 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 return( 0 );
2226 }
2227
2228 /*
2229 * 0 . 0 handshake type
2230 * 1 . 3 handshake length
2231 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002232 * 5 .. m-1 cert types
2233 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002234 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002235 * n .. n+1 length of all DNs
2236 * n+2 .. n+3 length of DN 1
2237 * n+4 .. ... Distinguished Name #1
2238 * ... .. ... length of DN 2, etc.
2239 */
2240 buf = ssl->out_msg;
2241 p = buf + 4;
2242
2243 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002244 * Supported certificate types
2245 *
2246 * ClientCertificateType certificate_types<1..2^8-1>;
2247 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002248 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002249 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002250
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002251#if defined(POLARSSL_RSA_C)
2252 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2253#endif
2254#if defined(POLARSSL_ECDSA_C)
2255 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2256#endif
2257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002258 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002259 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002260
Paul Bakker577e0062013-08-28 11:57:20 +02002261 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002262#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002263 /*
2264 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002265 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002266 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2267 *
2268 * struct {
2269 * HashAlgorithm hash;
2270 * SignatureAlgorithm signature;
2271 * } SignatureAndHashAlgorithm;
2272 *
2273 * enum { (255) } HashAlgorithm;
2274 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002275 */
Paul Bakker21dca692013-01-03 11:41:08 +01002276 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002277 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002278 /*
2279 * Only use current running hash algorithm that is already required
2280 * for requested ciphersuite.
2281 */
Paul Bakker926af752012-11-23 13:38:07 +01002282 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2283
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002284 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2285 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002286 {
2287 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2288 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002289
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002290 /*
2291 * Supported signature algorithms
2292 */
2293#if defined(POLARSSL_RSA_C)
2294 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2295 p[2 + sa_len++] = SSL_SIG_RSA;
2296#endif
2297#if defined(POLARSSL_ECDSA_C)
2298 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2299 p[2 + sa_len++] = SSL_SIG_ECDSA;
2300#endif
Paul Bakker926af752012-11-23 13:38:07 +01002301
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002302 p[0] = (unsigned char)( sa_len >> 8 );
2303 p[1] = (unsigned char)( sa_len );
2304 sa_len += 2;
2305 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002306 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002307#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002308
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002309 /*
2310 * DistinguishedName certificate_authorities<0..2^16-1>;
2311 * opaque DistinguishedName<1..2^16-1>;
2312 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002313 p += 2;
2314 crt = ssl->ca_chain;
2315
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002316 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002317 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002318 {
2319 if( p - buf > 4096 )
2320 break;
2321
Paul Bakker926af752012-11-23 13:38:07 +01002322 dn_size = crt->subject_raw.len;
2323 *p++ = (unsigned char)( dn_size >> 8 );
2324 *p++ = (unsigned char)( dn_size );
2325 memcpy( p, crt->subject_raw.p, dn_size );
2326 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002327
Paul Bakker926af752012-11-23 13:38:07 +01002328 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2329
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002330 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002331 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002332 }
2333
Paul Bakker926af752012-11-23 13:38:07 +01002334 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002335 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2336 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002337 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2338 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002339
2340 ret = ssl_write_record( ssl );
2341
2342 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2343
2344 return( ret );
2345}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002346#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2347 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002348 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2349 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002350
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002351#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2352 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2353static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2354{
2355 int ret;
2356
2357 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2358 {
2359 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2360 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2361 }
2362
2363 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2364 pk_ec( *ssl_own_key( ssl ) ),
2365 POLARSSL_ECDH_OURS ) ) != 0 )
2366 {
2367 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2368 return( ret );
2369 }
2370
2371 return( 0 );
2372}
2373#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2374 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2375
Paul Bakker41c83d32013-03-20 14:39:14 +01002376static int ssl_write_server_key_exchange( ssl_context *ssl )
2377{
Paul Bakker23986e52011-04-24 08:57:21 +00002378 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002379 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002380 const ssl_ciphersuite_t *ciphersuite_info =
2381 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002382
2383#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2384 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2385 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002386 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002387 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002388 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002389 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002390 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002391 ((void) dig_signed);
2392 ((void) dig_signed_len);
2393#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002394
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2396
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002397#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2398 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2399 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002400 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2401 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2402 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 {
2404 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2405 ssl->state++;
2406 return( 0 );
2407 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002408#endif
2409
2410#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2411 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2412 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2413 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2414 {
2415 ssl_get_ecdh_params_from_cert( ssl );
2416
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002417 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002418 ssl->state++;
2419 return( 0 );
2420 }
2421#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002423#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2424 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2425 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2426 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002427 {
2428 /* TODO: Support identity hints */
2429 *(p++) = 0x00;
2430 *(p++) = 0x00;
2431
2432 n += 2;
2433 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002434#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2435 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436
2437#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2438 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2439 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2440 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002441 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002442 /*
2443 * Ephemeral DH parameters:
2444 *
2445 * struct {
2446 * opaque dh_p<1..2^16-1>;
2447 * opaque dh_g<1..2^16-1>;
2448 * opaque dh_Ys<1..2^16-1>;
2449 * } ServerDHParams;
2450 */
2451 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2452 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2453 {
2454 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2455 return( ret );
2456 }
Paul Bakker48916f92012-09-16 19:57:18 +00002457
Paul Bakker41c83d32013-03-20 14:39:14 +01002458 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002459 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2460 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002461 {
2462 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2463 return( ret );
2464 }
2465
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002466 dig_signed = p;
2467 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002468
2469 p += len;
2470 n += len;
2471
Paul Bakker41c83d32013-03-20 14:39:14 +01002472 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2473 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2474 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2475 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2476 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002477#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2478 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002479
Gergely Budai987bfb52014-01-19 21:48:42 +01002480#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002481 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002482 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2483 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002485 /*
2486 * Ephemeral ECDH parameters:
2487 *
2488 * struct {
2489 * ECParameters curve_params;
2490 * ECPoint public;
2491 * } ServerECDHParams;
2492 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002493 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002494#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002495 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002496
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002497 /* Match our preference list against the offered curves */
2498 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2499 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2500 if( (*curve)->grp_id == *gid )
2501 goto curve_matching_done;
2502
2503curve_matching_done:
2504#else
2505 curve = ssl->handshake->curves;
2506#endif
2507
2508 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002509 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002510 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2511 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002512 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002513
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002514 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002515
Paul Bakker41c83d32013-03-20 14:39:14 +01002516 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002517 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002518 {
2519 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2520 return( ret );
2521 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002523 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2524 p, SSL_MAX_CONTENT_LEN - n,
2525 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002526 {
2527 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2528 return( ret );
2529 }
2530
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002531 dig_signed = p;
2532 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002533
2534 p += len;
2535 n += len;
2536
Paul Bakker41c83d32013-03-20 14:39:14 +01002537 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2538 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002539#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002541#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002542 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2543 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002544 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002545 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2546 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002547 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002548 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002549 unsigned int hashlen = 0;
2550 unsigned char hash[64];
2551 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002552
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002553 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002554 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2555 */
Paul Bakker577e0062013-08-28 11:57:20 +02002556#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002557 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2558 {
2559 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2560
2561 if( md_alg == POLARSSL_MD_NONE )
2562 {
2563 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002564 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002565 }
2566 }
Paul Bakker577e0062013-08-28 11:57:20 +02002567 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002568#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002569#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2570 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002571 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002572 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2573 {
2574 md_alg = POLARSSL_MD_SHA1;
2575 }
2576 else
Paul Bakker577e0062013-08-28 11:57:20 +02002577#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002578 {
2579 md_alg = POLARSSL_MD_NONE;
2580 }
2581
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002582 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002583 * Compute the hash to be signed
2584 */
Paul Bakker577e0062013-08-28 11:57:20 +02002585#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2586 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002587 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002588 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589 md5_context md5;
2590 sha1_context sha1;
2591
Paul Bakker5b4af392014-06-26 12:09:34 +02002592 md5_init( &md5 );
2593 sha1_init( &sha1 );
2594
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595 /*
2596 * digitally-signed struct {
2597 * opaque md5_hash[16];
2598 * opaque sha_hash[20];
2599 * };
2600 *
2601 * md5_hash
2602 * MD5(ClientHello.random + ServerHello.random
2603 * + ServerParams);
2604 * sha_hash
2605 * SHA(ClientHello.random + ServerHello.random
2606 * + ServerParams);
2607 */
2608 md5_starts( &md5 );
2609 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002610 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002611 md5_finish( &md5, hash );
2612
2613 sha1_starts( &sha1 );
2614 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002615 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002616 sha1_finish( &sha1, hash + 16 );
2617
2618 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002619
2620 md5_free( &md5 );
2621 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002622 }
2623 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002624#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2625 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002626#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2627 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002628 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002629 {
2630 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002631 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002632
Paul Bakker84bbeb52014-07-01 14:53:22 +02002633 md_init( &ctx );
2634
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002635 /* Info from md_alg will be used instead */
2636 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637
2638 /*
2639 * digitally-signed struct {
2640 * opaque client_random[32];
2641 * opaque server_random[32];
2642 * ServerDHParams params;
2643 * };
2644 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002645 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 {
2647 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2648 return( ret );
2649 }
2650
2651 md_starts( &ctx );
2652 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002653 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002654 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002655 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002656 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002657 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002658#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2659 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002660 {
2661 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002662 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002663 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002664
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002665 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2666 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002667
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002668 /*
2669 * Make the signature
2670 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002671 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002672 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002673 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2674 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002675 }
Paul Bakker23f36802012-09-28 14:15:14 +00002676
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002677#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002678 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2679 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002681 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002682
2683 n += 2;
2684 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002685#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002686
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002687 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002688 p + 2 , &signature_len,
2689 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002691 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002692 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002693 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002694
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002695 *(p++) = (unsigned char)( signature_len >> 8 );
2696 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002697 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002698
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002699 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002700
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002701 p += signature_len;
2702 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002703 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002704#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002705 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2706 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002707
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002708 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2710 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2711
2712 ssl->state++;
2713
2714 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2715 {
2716 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2717 return( ret );
2718 }
2719
2720 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2721
2722 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723}
2724
2725static int ssl_write_server_hello_done( ssl_context *ssl )
2726{
2727 int ret;
2728
2729 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2730
2731 ssl->out_msglen = 4;
2732 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2733 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2734
2735 ssl->state++;
2736
2737 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2738 {
2739 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2740 return( ret );
2741 }
2742
2743 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2744
2745 return( 0 );
2746}
2747
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002748#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2749 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2750static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2751 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002752{
2753 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002754 size_t n;
2755
2756 /*
2757 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2758 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002759 if( *p + 2 > end )
2760 {
2761 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2762 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2763 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002764
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002765 n = ( (*p)[0] << 8 ) | (*p)[1];
2766 *p += 2;
2767
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002768 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002769 {
2770 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2771 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2772 }
2773
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002774 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002775 {
2776 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2777 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2778 }
2779
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002780 *p += n;
2781
Paul Bakker70df2fb2013-04-17 17:19:09 +02002782 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2783
Paul Bakker70df2fb2013-04-17 17:19:09 +02002784 return( ret );
2785}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002786#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2787 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002788
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002789#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2790 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002791static int ssl_parse_encrypted_pms( ssl_context *ssl,
2792 const unsigned char *p,
2793 const unsigned char *end,
2794 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002795{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002796 int ret;
2797 size_t len = pk_get_len( ssl_own_key( ssl ) );
2798 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002799
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002800 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002801 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002802 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002803 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2804 }
2805
2806 /*
2807 * Decrypt the premaster using own private RSA key
2808 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002809#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2810 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002811 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2812 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002813 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2814 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002815 {
2816 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2817 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2818 }
2819 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002820#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002821
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002822 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002823 {
2824 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2826 }
2827
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002828 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2829 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002830 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002831 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002832
2833 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002834 pms[0] != ssl->handshake->max_major_ver ||
2835 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002836 {
2837 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2838
2839 /*
2840 * Protection against Bleichenbacher's attack:
2841 * invalid PKCS#1 v1.5 padding must not cause
2842 * the connection to end immediately; instead,
2843 * send a bad_record_mac later in the handshake.
2844 */
2845 ssl->handshake->pmslen = 48;
2846
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002847 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002848 if( ret != 0 )
2849 return( ret );
2850 }
2851
2852 return( ret );
2853}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002854#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2855 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002856
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002857#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002858static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2859 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002860{
Paul Bakker6db455e2013-09-18 17:29:31 +02002861 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002862 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002863
Paul Bakker6db455e2013-09-18 17:29:31 +02002864 if( ssl->f_psk == NULL &&
2865 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2866 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002867 {
2868 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2869 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2870 }
2871
2872 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002873 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002874 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002875 if( *p + 2 > end )
2876 {
2877 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2878 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2879 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002880
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002881 n = ( (*p)[0] << 8 ) | (*p)[1];
2882 *p += 2;
2883
2884 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002885 {
2886 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2887 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2888 }
2889
Paul Bakker6db455e2013-09-18 17:29:31 +02002890 if( ssl->f_psk != NULL )
2891 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002892 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002893 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2894 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002895 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002896 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002897 /* Identity is not a big secret since clients send it in the clear,
2898 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002899 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002900 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002901 {
2902 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2903 }
2904 }
2905
2906 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002907 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002908 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002909 if( ( ret = ssl_send_alert_message( ssl,
2910 SSL_ALERT_LEVEL_FATAL,
2911 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2912 {
2913 return( ret );
2914 }
2915
2916 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002917 }
2918
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002920
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002921 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002922}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002923#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002924
Paul Bakker5121ce52009-01-03 21:22:43 +00002925static int ssl_parse_client_key_exchange( ssl_context *ssl )
2926{
Paul Bakker23986e52011-04-24 08:57:21 +00002927 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002928 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002929
Paul Bakker41c83d32013-03-20 14:39:14 +01002930 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002931
2932 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2933
2934 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2935 {
2936 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2937 return( ret );
2938 }
2939
2940 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2941 {
2942 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002943 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002944 }
2945
2946 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2947 {
2948 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002949 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 }
2951
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002952#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002953 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002955 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002956 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002957
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002960 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2961 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002962 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002964 if( p != end )
2965 {
2966 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2967 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2968 }
2969
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002970 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002971
2972 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2973 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002974 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002975 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002976 {
2977 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2978 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2979 }
2980
2981 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002982 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002983 else
2984#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002985#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002986 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2987 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2988 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002989 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002990 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2991 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2992 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002993 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002994 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002995 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002996 {
2997 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2998 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2999 }
3000
3001 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3002
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003003 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3004 &ssl->handshake->pmslen,
3005 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003006 POLARSSL_MPI_MAX_SIZE,
3007 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003008 {
3009 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3010 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3011 }
3012
3013 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003014 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003015 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003016#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003017 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3018 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3019 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003020#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3021 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003022 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003023 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003024 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003025
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003026 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003027 {
3028 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3029 return( ret );
3030 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003032 if( p != end )
3033 {
3034 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3035 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3036 }
3037
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003038 if( ( ret = ssl_psk_derive_premaster( ssl,
3039 ciphersuite_info->key_exchange ) ) != 0 )
3040 {
3041 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3042 return( ret );
3043 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003044 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003046#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003047#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3048 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3049 {
3050 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003051 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003052
3053 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3054 {
3055 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3056 return( ret );
3057 }
3058
3059 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3060 {
3061 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3062 return( ret );
3063 }
3064
3065 if( ( ret = ssl_psk_derive_premaster( ssl,
3066 ciphersuite_info->key_exchange ) ) != 0 )
3067 {
3068 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3069 return( ret );
3070 }
3071 }
3072 else
3073#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003074#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3075 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3076 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003077 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003078 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003079
3080 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3081 {
3082 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3083 return( ret );
3084 }
3085 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3086 {
3087 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3088 return( ret );
3089 }
3090
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003091 if( p != end )
3092 {
3093 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3094 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3095 }
3096
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003097 if( ( ret = ssl_psk_derive_premaster( ssl,
3098 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003099 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003100 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3101 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003102 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003103 }
3104 else
3105#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003106#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3107 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3108 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003109 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003110 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003111
3112 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3113 {
3114 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3115 return( ret );
3116 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003117
3118 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3119 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003120 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003121 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3122 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003123 }
3124
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003125 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3126
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003127 if( ( ret = ssl_psk_derive_premaster( ssl,
3128 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003129 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003130 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003131 return( ret );
3132 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003133 }
3134 else
3135#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003136#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3137 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003138 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003139 if( ( ret = ssl_parse_encrypted_pms( ssl,
3140 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003141 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003142 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003143 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003144 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003145 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003146 }
3147 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003148 else
3149#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3150 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003151 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003152 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakkerff60ee62010-03-16 21:09:09 +00003155 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3156 {
3157 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3158 return( ret );
3159 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003160
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 ssl->state++;
3162
3163 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3164
3165 return( 0 );
3166}
3167
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003168#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3169 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003170 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3171 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003172static int ssl_parse_certificate_verify( ssl_context *ssl )
3173{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003174 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003175
3176 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3177
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003179 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003180 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003181 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003182 {
3183 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3184 ssl->state++;
3185 return( 0 );
3186 }
3187
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003188 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3189 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003190}
3191#else
3192static int ssl_parse_certificate_verify( ssl_context *ssl )
3193{
3194 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003195 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003196 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003197 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003198 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003199#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003200 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003201#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003202 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003203 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3204
3205 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3206
3207 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003208 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003209 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3211 {
3212 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3213 ssl->state++;
3214 return( 0 );
3215 }
3216
Paul Bakkered27a042013-04-18 22:46:23 +02003217 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003218 {
3219 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3220 ssl->state++;
3221 return( 0 );
3222 }
3223
Paul Bakker48916f92012-09-16 19:57:18 +00003224 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003225
3226 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3227 {
3228 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3229 return( ret );
3230 }
3231
3232 ssl->state++;
3233
3234 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3235 {
3236 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003237 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 }
3239
3240 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3241 {
3242 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003243 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003244 }
3245
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003246 /*
3247 * 0 . 0 handshake type
3248 * 1 . 3 handshake length
3249 * 4 . 5 sig alg (TLS 1.2 only)
3250 * 4+n . 5+n signature length (n = sa_len)
3251 * 6+n . 6+n+m signature (m = sig_len)
3252 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003253
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003254#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3255 defined(POLARSSL_SSL_PROTO_TLS1_1)
3256 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003257 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003258 sa_len = 0;
3259
Paul Bakkerc70b9822013-04-07 22:00:46 +02003260 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003261 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003262
3263 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3264 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3265 POLARSSL_PK_ECDSA ) )
3266 {
3267 hash_start += 16;
3268 hashlen -= 16;
3269 md_alg = POLARSSL_MD_SHA1;
3270 }
Paul Bakker926af752012-11-23 13:38:07 +01003271 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003272 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003273#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3274 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003275#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3276 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003277 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003278 sa_len = 2;
3279
Paul Bakker5121ce52009-01-03 21:22:43 +00003280 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003281 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003282 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003283 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003285 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3286 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003287 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3288 }
3289
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003290 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003291
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003292 /* Info from md_alg will be used instead */
3293 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003294
3295 /*
3296 * Signature
3297 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003298 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3299 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003300 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003301 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3302 " for verify message" ) );
3303 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003304 }
3305
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003306 /*
3307 * Check the certificate's key type matches the signature alg
3308 */
3309 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3310 {
3311 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3312 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3313 }
Paul Bakker577e0062013-08-28 11:57:20 +02003314 }
3315 else
3316#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3317 {
3318 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003319 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003320 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003321
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003322 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003323
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003324 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003325 {
3326 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003327 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003328 }
3329
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003330 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003331 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003332 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003334 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003335 return( ret );
3336 }
3337
3338 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3339
Paul Bakkered27a042013-04-18 22:46:23 +02003340 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003341}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003342#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3343 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3344 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003345
Paul Bakkera503a632013-08-14 13:48:06 +02003346#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003347static int ssl_write_new_session_ticket( ssl_context *ssl )
3348{
3349 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003350 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003351 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003352
3353 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3354
3355 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3356 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3357
3358 /*
3359 * struct {
3360 * uint32 ticket_lifetime_hint;
3361 * opaque ticket<0..2^16-1>;
3362 * } NewSessionTicket;
3363 *
3364 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3365 * 8 . 9 ticket_len (n)
3366 * 10 . 9+n ticket content
3367 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003368
3369 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3370 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3371 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3372 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003373
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003374 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3375 {
3376 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3377 tlen = 0;
3378 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003379
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003380 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3381 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003382
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003383 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003384
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003385 /*
3386 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3387 * ChangeCipherSpec share the same state.
3388 */
3389 ssl->handshake->new_session_ticket = 0;
3390
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003391 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3392 {
3393 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3394 return( ret );
3395 }
3396
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003397 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3398
3399 return( 0 );
3400}
Paul Bakkera503a632013-08-14 13:48:06 +02003401#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003402
Paul Bakker5121ce52009-01-03 21:22:43 +00003403/*
Paul Bakker1961b702013-01-25 14:49:24 +01003404 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003405 */
Paul Bakker1961b702013-01-25 14:49:24 +01003406int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003407{
3408 int ret = 0;
3409
Paul Bakker1961b702013-01-25 14:49:24 +01003410 if( ssl->state == SSL_HANDSHAKE_OVER )
3411 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003412
Paul Bakker1961b702013-01-25 14:49:24 +01003413 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3414
3415 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3416 return( ret );
3417
3418 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 {
Paul Bakker1961b702013-01-25 14:49:24 +01003420 case SSL_HELLO_REQUEST:
3421 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 break;
3423
Paul Bakker1961b702013-01-25 14:49:24 +01003424 /*
3425 * <== ClientHello
3426 */
3427 case SSL_CLIENT_HELLO:
3428 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003429 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003430
3431 /*
3432 * ==> ServerHello
3433 * Certificate
3434 * ( ServerKeyExchange )
3435 * ( CertificateRequest )
3436 * ServerHelloDone
3437 */
3438 case SSL_SERVER_HELLO:
3439 ret = ssl_write_server_hello( ssl );
3440 break;
3441
3442 case SSL_SERVER_CERTIFICATE:
3443 ret = ssl_write_certificate( ssl );
3444 break;
3445
3446 case SSL_SERVER_KEY_EXCHANGE:
3447 ret = ssl_write_server_key_exchange( ssl );
3448 break;
3449
3450 case SSL_CERTIFICATE_REQUEST:
3451 ret = ssl_write_certificate_request( ssl );
3452 break;
3453
3454 case SSL_SERVER_HELLO_DONE:
3455 ret = ssl_write_server_hello_done( ssl );
3456 break;
3457
3458 /*
3459 * <== ( Certificate/Alert )
3460 * ClientKeyExchange
3461 * ( CertificateVerify )
3462 * ChangeCipherSpec
3463 * Finished
3464 */
3465 case SSL_CLIENT_CERTIFICATE:
3466 ret = ssl_parse_certificate( ssl );
3467 break;
3468
3469 case SSL_CLIENT_KEY_EXCHANGE:
3470 ret = ssl_parse_client_key_exchange( ssl );
3471 break;
3472
3473 case SSL_CERTIFICATE_VERIFY:
3474 ret = ssl_parse_certificate_verify( ssl );
3475 break;
3476
3477 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3478 ret = ssl_parse_change_cipher_spec( ssl );
3479 break;
3480
3481 case SSL_CLIENT_FINISHED:
3482 ret = ssl_parse_finished( ssl );
3483 break;
3484
3485 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003486 * ==> ( NewSessionTicket )
3487 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003488 * Finished
3489 */
3490 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003491#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003492 if( ssl->handshake->new_session_ticket != 0 )
3493 ret = ssl_write_new_session_ticket( ssl );
3494 else
Paul Bakkera503a632013-08-14 13:48:06 +02003495#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003496 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003497 break;
3498
3499 case SSL_SERVER_FINISHED:
3500 ret = ssl_write_finished( ssl );
3501 break;
3502
3503 case SSL_FLUSH_BUFFERS:
3504 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3505 ssl->state = SSL_HANDSHAKE_WRAPUP;
3506 break;
3507
3508 case SSL_HANDSHAKE_WRAPUP:
3509 ssl_handshake_wrapup( ssl );
3510 break;
3511
3512 default:
3513 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3514 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 }
3516
Paul Bakker5121ce52009-01-03 21:22:43 +00003517 return( ret );
3518}
Paul Bakker9af723c2014-05-01 13:03:14 +02003519#endif /* POLARSSL_SSL_SRV_C */