blob: d83c5605fd3e351bbde4a83a5c87f5351b5d34b2 [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,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 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
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200354#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200355int ssl_set_client_transport_id( ssl_context *ssl,
356 const unsigned char *info,
357 size_t ilen )
358{
359 if( ssl->endpoint != SSL_IS_SERVER )
360 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
361
362 polarssl_free( ssl->cli_id );
363
364 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
365 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
366
367 memcpy( ssl->cli_id, info, ilen );
368 ssl->cli_id_len = ilen;
369
370 return( 0 );
371}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200372
373void ssl_set_dtls_cookies( ssl_context *ssl,
374 ssl_cookie_write_t *f_cookie_write,
375 ssl_cookie_check_t *f_cookie_check,
376 void *p_cookie )
377{
378 ssl->f_cookie_write = f_cookie_write;
379 ssl->f_cookie_check = f_cookie_check;
380 ssl->p_cookie = p_cookie;
381}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200382#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200383
Paul Bakker0be444a2013-08-27 21:55:01 +0200384#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200386 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100387 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388 */
389static int ssl_sni_wrapper( ssl_context *ssl,
390 const unsigned char* name, size_t len )
391{
392 int ret;
393 ssl_key_cert *key_cert_ori = ssl->key_cert;
394
395 ssl->key_cert = NULL;
396 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200397 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200398
399 ssl->key_cert = key_cert_ori;
400
401 return( ret );
402}
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000405 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000406 size_t len )
407{
408 int ret;
409 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000410 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100412 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
413
Paul Bakker5701cdc2012-09-27 21:49:42 +0000414 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
415 if( servername_list_size + 2 != len )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 p = buf + 2;
422 while( servername_list_size > 0 )
423 {
424 hostname_len = ( ( p[1] << 8 ) | p[2] );
425 if( hostname_len + 3 > servername_list_size )
426 {
427 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
429 }
430
431 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
432 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200433 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000434 if( ret != 0 )
435 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100436 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000437 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
438 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
440 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000441 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000445 p += hostname_len + 3;
446 }
447
448 if( servername_list_size != 0 )
449 {
450 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000452 }
453
454 return( 0 );
455}
Paul Bakker0be444a2013-08-27 21:55:01 +0200456#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000457
Paul Bakker48916f92012-09-16 19:57:18 +0000458static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000459 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000460 size_t len )
461{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000462 int ret;
463
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100464#if defined(POLARSSL_SSL_RENEGOTIATION)
465 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
466 {
467 /* Check verify-data in constant-time. The length OTOH is no secret */
468 if( len != 1 + ssl->verify_data_len ||
469 buf[0] != ssl->verify_data_len ||
470 safer_memcmp( buf + 1, ssl->peer_verify_data,
471 ssl->verify_data_len ) != 0 )
472 {
473 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
474
475 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
476 return( ret );
477
478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
479 }
480 }
481 else
482#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000483 {
484 if( len != 1 || buf[0] != 0x0 )
485 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100486 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000487
488 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
489 return( ret );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
492 }
493
494 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
495 }
Paul Bakker48916f92012-09-16 19:57:18 +0000496
497 return( 0 );
498}
499
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100500#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
501 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000502static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
503 const unsigned char *buf,
504 size_t len )
505{
506 size_t sig_alg_list_size;
507 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200508 const unsigned char *end = buf + len;
509 const int *md_cur;
510
Paul Bakker23f36802012-09-28 14:15:14 +0000511
512 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
513 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200514 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000515 {
516 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
517 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
518 }
519
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200520 /*
521 * For now, ignore the SignatureAlgorithm part and rely on offered
522 * ciphersuites only for that part. To be fixed later.
523 *
524 * So, just look at the HashAlgorithm part.
525 */
526 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
527 for( p = buf + 2; p < end; p += 2 ) {
528 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
529 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200530 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200531 }
Paul Bakker23f36802012-09-28 14:15:14 +0000532 }
Paul Bakker23f36802012-09-28 14:15:14 +0000533 }
534
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200535 /* Some key echanges do not need signatures at all */
536 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
537 return( 0 );
538
539have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000540 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
541 ssl->handshake->sig_alg ) );
542
543 return( 0 );
544}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100545#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
546 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000547
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200548#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200549static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
550 const unsigned char *buf,
551 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100552{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200555 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100556
557 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
558 if( list_size + 2 != len ||
559 list_size % 2 != 0 )
560 {
561 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
562 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
563 }
564
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200565 /* Should never happen unless client duplicates the extension */
566 if( ssl->handshake->curves != NULL )
567 {
568 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
570 }
571
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100572 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200573 * and leave room for a final 0 */
574 our_size = list_size / 2 + 1;
575 if( our_size > POLARSSL_ECP_DP_MAX )
576 our_size = POLARSSL_ECP_DP_MAX;
577
578 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
579 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
580
Paul Bakker9af723c2014-05-01 13:03:14 +0200581 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200582 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200583 ssl->handshake->curves = curves;
584
Paul Bakker41c83d32013-03-20 14:39:14 +0100585 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200586 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200589
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200590 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200592 *curves++ = curve_info;
593 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100594 }
595
596 list_size -= 2;
597 p += 2;
598 }
599
600 return( 0 );
601}
602
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200603static int ssl_parse_supported_point_formats( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100606{
607 size_t list_size;
608 const unsigned char *p;
609
610 list_size = buf[0];
611 if( list_size + 1 != len )
612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
617 p = buf + 2;
618 while( list_size > 0 )
619 {
620 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
621 p[0] == POLARSSL_ECP_PF_COMPRESSED )
622 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200623 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200624 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100625 return( 0 );
626 }
627
628 list_size--;
629 p++;
630 }
631
632 return( 0 );
633}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200634#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100635
Paul Bakker05decb22013-08-15 13:33:48 +0200636#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200637static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len )
640{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200641 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200642 {
643 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
644 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
645 }
646
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200647 ssl->session_negotiate->mfl_code = buf[0];
648
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200649 return( 0 );
650}
Paul Bakker05decb22013-08-15 13:33:48 +0200651#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200652
Paul Bakker1f2bc622013-08-15 13:45:55 +0200653#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200654static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
655 const unsigned char *buf,
656 size_t len )
657{
658 if( len != 0 )
659 {
660 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
661 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
662 }
663
664 ((void) buf);
665
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100666 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
667 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200668
669 return( 0 );
670}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200671#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200672
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100673#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
674static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
675 const unsigned char *buf,
676 size_t len )
677{
678 if( len != 0 )
679 {
680 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
682 }
683
684 ((void) buf);
685
686 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
687 ssl->minor_ver != SSL_MINOR_VERSION_0 )
688 {
689 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
690 }
691
692 return( 0 );
693}
694#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
695
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200696#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
697static int ssl_parse_extended_ms_ext( ssl_context *ssl,
698 const unsigned char *buf,
699 size_t len )
700{
701 if( len != 0 )
702 {
703 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705 }
706
707 ((void) buf);
708
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200709 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
710 ssl->minor_ver != SSL_MINOR_VERSION_0 )
711 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200712 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200713 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200714
715 return( 0 );
716}
717#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
718
Paul Bakkera503a632013-08-14 13:48:06 +0200719#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200721 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200722 size_t len )
723{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200724 int ret;
725
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200726 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
727 return( 0 );
728
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200729 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200730 ssl->handshake->new_session_ticket = 1;
731
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200732 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
733
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200734 if( len == 0 )
735 return( 0 );
736
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100737#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200738 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
739 {
740 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
741 return( 0 );
742 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100743#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744
745 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200746 * Failures are ok: just ignore the ticket and proceed.
747 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200748 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200752 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200753
754 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
755
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200756 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200757
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200758 /* Don't send a new ticket after all, this one is OK */
759 ssl->handshake->new_session_ticket = 0;
760
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200761 return( 0 );
762}
Paul Bakkera503a632013-08-14 13:48:06 +0200763#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200764
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200765#if defined(POLARSSL_SSL_ALPN)
766static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200767 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768{
Paul Bakker14b16c62014-05-28 11:33:54 +0200769 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200770 const unsigned char *theirs, *start, *end;
771 const char **ours;
772
773 /* If ALPN not configured, just ignore the extension */
774 if( ssl->alpn_list == NULL )
775 return( 0 );
776
777 /*
778 * opaque ProtocolName<1..2^8-1>;
779 *
780 * struct {
781 * ProtocolName protocol_name_list<2..2^16-1>
782 * } ProtocolNameList;
783 */
784
785 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
786 if( len < 4 )
787 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
788
789 list_len = ( buf[0] << 8 ) | buf[1];
790 if( list_len != len - 2 )
791 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
792
793 /*
794 * Use our order of preference
795 */
796 start = buf + 2;
797 end = buf + len;
798 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
799 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200800 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200801 for( theirs = start; theirs != end; theirs += cur_len )
802 {
803 /* If the list is well formed, we should get equality first */
804 if( theirs > end )
805 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
806
807 cur_len = *theirs++;
808
809 /* Empty strings MUST NOT be included */
810 if( cur_len == 0 )
811 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
812
Paul Bakker14b16c62014-05-28 11:33:54 +0200813 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200814 memcmp( theirs, *ours, cur_len ) == 0 )
815 {
816 ssl->alpn_chosen = *ours;
817 return( 0 );
818 }
819 }
820 }
821
822 /* If we get there, no match was found */
823 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
824 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
826}
827#endif /* POLARSSL_SSL_ALPN */
828
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100829/*
830 * Auxiliary functions for ServerHello parsing and related actions
831 */
832
833#if defined(POLARSSL_X509_CRT_PARSE_C)
834/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100835 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100836 */
837#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100838static int ssl_check_key_curve( pk_context *pk,
839 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100840{
841 const ecp_curve_info **crv = curves;
842 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
843
844 while( *crv != NULL )
845 {
846 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100847 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100848 crv++;
849 }
850
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100851 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100852}
853#endif /* POLARSSL_ECDSA_C */
854
855/*
856 * Try picking a certificate for this ciphersuite,
857 * return 0 on success and -1 on failure.
858 */
859static int ssl_pick_cert( ssl_context *ssl,
860 const ssl_ciphersuite_t * ciphersuite_info )
861{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100862 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
864
865#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
866 if( ssl->handshake->sni_key_cert != NULL )
867 list = ssl->handshake->sni_key_cert;
868 else
869#endif
870 list = ssl->handshake->key_cert;
871
872 if( pk_alg == POLARSSL_PK_NONE )
873 return( 0 );
874
875 for( cur = list; cur != NULL; cur = cur->next )
876 {
877 if( ! pk_can_do( cur->key, pk_alg ) )
878 continue;
879
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200880 /*
881 * This avoids sending the client a cert it'll reject based on
882 * keyUsage or other extensions.
883 *
884 * It also allows the user to provision different certificates for
885 * different uses based on keyUsage, eg if they want to avoid signing
886 * and decrypting with the same RSA key.
887 */
888 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
889 SSL_IS_SERVER ) != 0 )
890 {
891 continue;
892 }
893
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100895 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100896 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100897 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100898#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100899
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100900 /*
901 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
902 * present them a SHA-higher cert rather than failing if it's the only
903 * one we got that satisfies the other conditions.
904 */
905 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
906 cur->cert->sig_md != POLARSSL_MD_SHA1 )
907 {
908 if( fallback == NULL )
909 fallback = cur;
910 continue;
911 }
912
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100913 /* If we get there, we got a winner */
914 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915 }
916
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100917 if( cur != NULL )
918 {
919 ssl->handshake->key_cert = cur;
920 return( 0 );
921 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100923 if( fallback != NULL )
924 {
925 ssl->handshake->key_cert = fallback;
926 return( 0 );
927 }
928
929 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930}
931#endif /* POLARSSL_X509_CRT_PARSE_C */
932
933/*
934 * Check if a given ciphersuite is suitable for use with our config/keys/etc
935 * Sets ciphersuite_info only if the suite matches.
936 */
937static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
938 const ssl_ciphersuite_t **ciphersuite_info )
939{
940 const ssl_ciphersuite_t *suite_info;
941
942 suite_info = ssl_ciphersuite_from_id( suite_id );
943 if( suite_info == NULL )
944 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100945 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
946 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100947 }
948
949 if( suite_info->min_minor_ver > ssl->minor_ver ||
950 suite_info->max_minor_ver < ssl->minor_ver )
951 return( 0 );
952
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100953#if defined(POLARSSL_SSL_PROTO_DTLS)
954 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
955 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
956 return( 0 );
957#endif
958
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100959#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
960 if( ssl_ciphersuite_uses_ec( suite_info ) &&
961 ( ssl->handshake->curves == NULL ||
962 ssl->handshake->curves[0] == NULL ) )
963 return( 0 );
964#endif
965
966#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
967 /* If the ciphersuite requires a pre-shared key and we don't
968 * have one, skip it now rather than failing later */
969 if( ssl_ciphersuite_uses_psk( suite_info ) &&
970 ssl->f_psk == NULL &&
971 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
972 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
973 return( 0 );
974#endif
975
976#if defined(POLARSSL_X509_CRT_PARSE_C)
977 /*
978 * Final check: if ciphersuite requires us to have a
979 * certificate/key of a particular type:
980 * - select the appropriate certificate if we have one, or
981 * - try the next ciphersuite if we don't
982 * This must be done last since we modify the key_cert list.
983 */
984 if( ssl_pick_cert( ssl, suite_info ) != 0 )
985 return( 0 );
986#endif
987
988 *ciphersuite_info = suite_info;
989 return( 0 );
990}
991
Paul Bakker78a8c712013-03-06 17:01:52 +0100992#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
993static int ssl_parse_client_hello_v2( ssl_context *ssl )
994{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100995 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +0100996 unsigned int i, j;
997 size_t n;
998 unsigned int ciph_len, sess_len, chal_len;
999 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001000 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001001 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001002
1003 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1004
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001005#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001006 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1007 {
1008 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1009
1010 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1011 return( ret );
1012
1013 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1014 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001015#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001016
1017 buf = ssl->in_hdr;
1018
1019 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1020
1021 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1022 buf[2] ) );
1023 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1024 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1025 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1026 buf[3], buf[4] ) );
1027
1028 /*
1029 * SSLv2 Client Hello
1030 *
1031 * Record layer:
1032 * 0 . 1 message length
1033 *
1034 * SSL layer:
1035 * 2 . 2 message type
1036 * 3 . 4 protocol version
1037 */
1038 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1039 buf[3] != SSL_MAJOR_VERSION_3 )
1040 {
1041 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1042 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1043 }
1044
1045 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1046
1047 if( n < 17 || n > 512 )
1048 {
1049 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1050 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1051 }
1052
1053 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001054 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1055 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001056
1057 if( ssl->minor_ver < ssl->min_minor_ver )
1058 {
1059 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001060 " [%d:%d] < [%d:%d]",
1061 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001062 ssl->min_major_ver, ssl->min_minor_ver ) );
1063
1064 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1065 SSL_ALERT_MSG_PROTOCOL_VERSION );
1066 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1067 }
1068
Paul Bakker2fbefde2013-06-29 16:01:15 +02001069 ssl->handshake->max_major_ver = buf[3];
1070 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001071
1072 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1073 {
1074 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1075 return( ret );
1076 }
1077
1078 ssl->handshake->update_checksum( ssl, buf + 2, n );
1079
1080 buf = ssl->in_msg;
1081 n = ssl->in_left - 5;
1082
1083 /*
1084 * 0 . 1 ciphersuitelist length
1085 * 2 . 3 session id length
1086 * 4 . 5 challenge length
1087 * 6 . .. ciphersuitelist
1088 * .. . .. session id
1089 * .. . .. challenge
1090 */
1091 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1092
1093 ciph_len = ( buf[0] << 8 ) | buf[1];
1094 sess_len = ( buf[2] << 8 ) | buf[3];
1095 chal_len = ( buf[4] << 8 ) | buf[5];
1096
1097 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1098 ciph_len, sess_len, chal_len ) );
1099
1100 /*
1101 * Make sure each parameter length is valid
1102 */
1103 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1104 {
1105 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1106 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1107 }
1108
1109 if( sess_len > 32 )
1110 {
1111 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1112 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1113 }
1114
1115 if( chal_len < 8 || chal_len > 32 )
1116 {
1117 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1118 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1119 }
1120
1121 if( n != 6 + ciph_len + sess_len + chal_len )
1122 {
1123 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1124 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1125 }
1126
1127 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1128 buf + 6, ciph_len );
1129 SSL_DEBUG_BUF( 3, "client hello, session id",
1130 buf + 6 + ciph_len, sess_len );
1131 SSL_DEBUG_BUF( 3, "client hello, challenge",
1132 buf + 6 + ciph_len + sess_len, chal_len );
1133
1134 p = buf + 6 + ciph_len;
1135 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001136 memset( ssl->session_negotiate->id, 0,
1137 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001138 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1139
1140 p += sess_len;
1141 memset( ssl->handshake->randbytes, 0, 64 );
1142 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1143
1144 /*
1145 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1146 */
1147 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1148 {
1149 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1150 {
1151 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001152#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001153 if( ssl->renegotiation == SSL_RENEGOTIATION )
1154 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001155 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1156 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001157
1158 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1159 return( ret );
1160
1161 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1162 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001163#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001164 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1165 break;
1166 }
1167 }
1168
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001169#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1170 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1171 {
1172 if( p[0] == 0 &&
1173 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1174 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1175 {
1176 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1177
1178 if( ssl->minor_ver < ssl->max_minor_ver )
1179 {
1180 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1181
1182 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1183 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1184
1185 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1186 }
1187
1188 break;
1189 }
1190 }
1191#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1192
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001193 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001194 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001195 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001196#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1197 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1198 {
1199 for( i = 0; ciphersuites[i] != 0; i++ )
1200#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001201 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001202 {
1203 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001204#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001205 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001206 if( p[0] != 0 ||
1207 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1208 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1209 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001210
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001211 got_common_suite = 1;
1212
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001213 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1214 &ciphersuite_info ) ) != 0 )
1215 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001216
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001217 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001218 goto have_ciphersuite_v2;
1219 }
1220 }
1221
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001222 if( got_common_suite )
1223 {
1224 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1225 "but none of them usable" ) );
1226 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1227 }
1228 else
1229 {
1230 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1231 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1232 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001233
1234have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001235 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001236 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001237 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001238
1239 /*
1240 * SSLv2 Client Hello relevant renegotiation security checks
1241 */
1242 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1243 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1244 {
1245 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1246
1247 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1248 return( ret );
1249
1250 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1251 }
1252
1253 ssl->in_left = 0;
1254 ssl->state++;
1255
1256 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1257
1258 return( 0 );
1259}
1260#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262static int ssl_parse_client_hello( ssl_context *ssl )
1263{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001264 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001265 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001266 unsigned int ciph_offset, comp_offset, ext_offset;
1267 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001268#if defined(POLARSSL_SSL_PROTO_DTLS)
1269 unsigned int cookie_offset, cookie_len;
1270#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001271 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001272#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001273 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001274#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001275 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001276 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001277 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001278 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
1280 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1281
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001282#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1283read_record_header:
1284#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001285 /*
1286 * If renegotiating, then the input was read with ssl_read_record(),
1287 * otherwise read it ourselves manually in order to support SSLv2
1288 * ClientHello, which doesn't use the same record layer format.
1289 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001290#if defined(POLARSSL_SSL_RENEGOTIATION)
1291 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1292#endif
1293 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 {
1295 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1296 return( ret );
1297 }
1298
1299 buf = ssl->in_hdr;
1300
Paul Bakker78a8c712013-03-06 17:01:52 +01001301#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001302#if defined(POLARSSL_SSL_PROTO_DTLS)
1303 if( ssl->transport == SSL_TRANSPORT_STREAM )
1304#endif
1305 if( ( buf[0] & 0x80 ) != 0 )
1306 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001307#endif
1308
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001309 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001310
Paul Bakkerec636f32012-09-09 19:17:02 +00001311 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001312 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001313 *
1314 * Record layer:
1315 * 0 . 0 message type
1316 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001317 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001318 * 3 . 4 message length
1319 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001320 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1321 buf[0] ) );
1322
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001323 if( buf[0] != SSL_MSG_HANDSHAKE )
1324 {
1325 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1326 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1327 }
1328
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001329 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1330 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1331
1332 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1333 buf[1], buf[2] ) );
1334
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001335 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001336
1337 /* According to RFC 5246 Appendix E.1, the version here is typically
1338 * "{03,00}, the lowest version number supported by the client, [or] the
1339 * value of ClientHello.client_version", so the only meaningful check here
1340 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001341 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001343 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1344 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001347 /* For DTLS if this is the initial handshake, remember the client sequence
1348 * number to use it in our next message (RFC 6347 4.2.1) */
1349#if defined(POLARSSL_SSL_PROTO_DTLS)
1350 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1351 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1352 {
1353 /* Epoch should be 0 for initial handshakes */
1354 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1355 {
1356 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1357 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1358 }
1359
1360 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001361
1362#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1363 if( ssl_dtls_replay_check( ssl ) != 0 )
1364 {
1365 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1366 ssl->next_record_offset = 0;
1367 ssl->in_left = 0;
1368 goto read_record_header;
1369 }
1370
1371 /* No MAC to check yet, so we can update right now */
1372 ssl_dtls_replay_update( ssl );
1373#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001374 }
1375#endif /* POLARSSL_SSL_PROTO_DTLS */
1376
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001377 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001378
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001379#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001380 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1381 {
1382 /* Set by ssl_read_record() */
1383 msg_len = ssl->in_hslen;
1384 }
1385 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001386#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001387 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001388 if( msg_len > SSL_MAX_CONTENT_LEN )
1389 {
1390 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1391 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1392 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001393
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001394 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1395 {
1396 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1397 return( ret );
1398 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001399
1400 /* Done reading this record, get ready for the next one */
1401#if defined(POLARSSL_SSL_PROTO_DTLS)
1402 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1403 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1404 else
1405#endif
1406 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001407 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001408
1409 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001410
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001411 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001412
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001413 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001414
1415 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001416 * Handshake layer:
1417 * 0 . 0 handshake type
1418 * 1 . 3 handshake length
1419 * 4 . 5 DTLS only: message seqence number
1420 * 6 . 8 DTLS only: fragment offset
1421 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001422 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001423 if( msg_len < ssl_hs_hdr_len( ssl ) )
1424 {
1425 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1427 }
1428
1429 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1430
1431 if( buf[0] != SSL_HS_CLIENT_HELLO )
1432 {
1433 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1434 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1435 }
1436
1437 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1438 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1439
1440 /* We don't support fragmentation of ClientHello (yet?) */
1441 if( buf[1] != 0 ||
1442 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1443 {
1444 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1445 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1446 }
1447
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001448#if defined(POLARSSL_SSL_PROTO_DTLS)
1449 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1450 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001451 /*
1452 * Copy the client's handshake message_seq on initial handshakes
1453 */
1454 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001455 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001456 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1457 ssl->in_msg[5];
1458 ssl->handshake->out_msg_seq = cli_msg_seq;
1459 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001460 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001461 else
1462 {
1463 /* This couldn't be done in ssl_prepare_handshake_record() */
1464 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1465 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001466
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001467 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1468 {
1469 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1470 "%d (expected %d)", cli_msg_seq,
1471 ssl->handshake->in_msg_seq ) );
1472 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1473 }
1474
1475 ssl->handshake->in_msg_seq++;
1476 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001477
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001478 /*
1479 * For now we don't support fragmentation, so make sure
1480 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001481 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001482 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1483 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1484 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001485 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001486 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1487 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001488 }
1489#endif /* POLARSSL_SSL_PROTO_DTLS */
1490
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001491 buf += ssl_hs_hdr_len( ssl );
1492 msg_len -= ssl_hs_hdr_len( ssl );
1493
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001494 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001495 * ClientHello layer:
1496 * 0 . 1 protocol version
1497 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1498 * 34 . 35 session id length (1 byte)
1499 * 35 . 34+x session id
1500 * 35+x . 35+x DTLS only: cookie length (1 byte)
1501 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001502 * .. . .. ciphersuite list length (2 bytes)
1503 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001504 * .. . .. compression alg. list length (1 byte)
1505 * .. . .. compression alg. list
1506 * .. . .. extensions length (2 bytes, optional)
1507 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001508 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001509
1510 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001511 * Minimal length (with everything empty and extensions ommitted) is
1512 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1513 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001514 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001515 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001516 {
1517 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1518 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1519 }
1520
1521 /*
1522 * Check and save the protocol version
1523 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001524 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001525
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001526 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001527 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001528
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001529 ssl->handshake->max_major_ver = ssl->major_ver;
1530 ssl->handshake->max_minor_ver = ssl->minor_ver;
1531
1532 if( ssl->major_ver < ssl->min_major_ver ||
1533 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001534 {
1535 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001536 " [%d:%d] < [%d:%d]",
1537 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001538 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001539
1540 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1541 SSL_ALERT_MSG_PROTOCOL_VERSION );
1542
1543 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1544 }
1545
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001546 if( ssl->major_ver > ssl->max_major_ver )
1547 {
1548 ssl->major_ver = ssl->max_major_ver;
1549 ssl->minor_ver = ssl->max_minor_ver;
1550 }
1551 else if( ssl->minor_ver > ssl->max_minor_ver )
1552 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001553
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001554 /*
1555 * Save client random (inc. Unix time)
1556 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001557 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001558
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001559 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001560
1561 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001562 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001563 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001564 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001565
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001566 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001567 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001568 {
1569 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1570 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1571 }
1572
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001574
Paul Bakker48916f92012-09-16 19:57:18 +00001575 ssl->session_negotiate->length = sess_len;
1576 memset( ssl->session_negotiate->id, 0,
1577 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001578 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001579 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001580
1581 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001582 * Check the cookie length and content
1583 */
1584#if defined(POLARSSL_SSL_PROTO_DTLS)
1585 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1586 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001587 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001588 cookie_len = buf[cookie_offset];
1589
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001590 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001591 {
1592 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1593 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1594 }
1595
1596 SSL_DEBUG_BUF( 3, "client hello, cookie",
1597 buf + cookie_offset + 1, cookie_len );
1598
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001599#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001600 if( ssl->f_cookie_check != NULL &&
1601 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001602 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001603 if( ssl->f_cookie_check( ssl->p_cookie,
1604 buf + cookie_offset + 1, cookie_len,
1605 ssl->cli_id, ssl->cli_id_len ) != 0 )
1606 {
1607 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1608 ssl->handshake->verify_cookie_len = 1;
1609 }
1610 else
1611 {
1612 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1613 ssl->handshake->verify_cookie_len = 0;
1614 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001615 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001616 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001617#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001618 {
1619 /* We know we didn't send a cookie, so it should be empty */
1620 if( cookie_len != 0 )
1621 {
1622 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1623 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1624 }
1625
1626 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1627 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001628 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001629#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001630
1631 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001632 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001633 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001634#if defined(POLARSSL_SSL_PROTO_DTLS)
1635 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1636 ciph_offset = cookie_offset + 1 + cookie_len;
1637 else
1638#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001639 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001640
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 ciph_len = ( buf[ciph_offset + 0] << 8 )
1642 | ( buf[ciph_offset + 1] );
1643
1644 if( ciph_len < 2 ||
1645 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1646 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001647 {
1648 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1649 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1650 }
1651
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001652 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1653 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001654
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001655 /*
1656 * Check the compression algorithms length and pick one
1657 */
1658 comp_offset = ciph_offset + 2 + ciph_len;
1659
1660 comp_len = buf[comp_offset];
1661
1662 if( comp_len < 1 ||
1663 comp_len > 16 ||
1664 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001665 {
1666 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1667 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1668 }
1669
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001670 SSL_DEBUG_BUF( 3, "client hello, compression",
1671 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001672
1673 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001674#if defined(POLARSSL_ZLIB_SUPPORT)
1675 for( i = 0; i < comp_len; ++i )
1676 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001677 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 {
Paul Bakker48916f92012-09-16 19:57:18 +00001679 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001680 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 }
1682 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001683#endif
1684
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001685 /* See comments in ssl_write_client_hello() */
1686#if defined(POLARSSL_SSL_PROTO_DTLS)
1687 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1688 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1689#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001690
Paul Bakkerec636f32012-09-09 19:17:02 +00001691 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001692 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001693 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001694 ext_offset = comp_offset + 1 + comp_len;
1695 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001696 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001697 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001698 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001699 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1700 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1701 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001702
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001703 ext_len = ( buf[ext_offset + 0] << 8 )
1704 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001705
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001706 if( ( ext_len > 0 && ext_len < 4 ) ||
1707 msg_len != ext_offset + 2 + ext_len )
1708 {
1709 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1710 SSL_DEBUG_BUF( 3, "client hello extensions",
1711 buf + ext_offset + 2, ext_len );
1712 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001713 }
1714 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001715 else
1716 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001717
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001718 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001719
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001720 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001721 {
1722 unsigned int ext_id = ( ( ext[0] << 8 )
1723 | ( ext[1] ) );
1724 unsigned int ext_size = ( ( ext[2] << 8 )
1725 | ( ext[3] ) );
1726
1727 if( ext_size + 4 > ext_len )
1728 {
1729 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1730 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1731 }
1732 switch( ext_id )
1733 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001734#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001735 case TLS_EXT_SERVERNAME:
1736 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1737 if( ssl->f_sni == NULL )
1738 break;
1739
1740 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1741 if( ret != 0 )
1742 return( ret );
1743 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001744#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001745
Paul Bakker48916f92012-09-16 19:57:18 +00001746 case TLS_EXT_RENEGOTIATION_INFO:
1747 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001748#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001749 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001750#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001751
Paul Bakker23f36802012-09-28 14:15:14 +00001752 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1753 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001754 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001755 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001756
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001757#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1758 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001759 case TLS_EXT_SIG_ALG:
1760 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001761#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001762 if( ssl->renegotiation == SSL_RENEGOTIATION )
1763 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001764#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001765
1766 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1767 if( ret != 0 )
1768 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001769 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001770#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1771 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001772
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001773#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001774 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1775 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1776
1777 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1778 if( ret != 0 )
1779 return( ret );
1780 break;
1781
1782 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1783 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001784 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001785
1786 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1787 if( ret != 0 )
1788 return( ret );
1789 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001790#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001791
Paul Bakker05decb22013-08-15 13:33:48 +02001792#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001793 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1794 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1795
1796 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1797 if( ret != 0 )
1798 return( ret );
1799 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001800#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001801
Paul Bakker1f2bc622013-08-15 13:45:55 +02001802#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001803 case TLS_EXT_TRUNCATED_HMAC:
1804 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1805
1806 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1807 if( ret != 0 )
1808 return( ret );
1809 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001810#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001811
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001812#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1813 case TLS_EXT_ENCRYPT_THEN_MAC:
1814 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1815
1816 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1817 if( ret != 0 )
1818 return( ret );
1819 break;
1820#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1821
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001822#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1823 case TLS_EXT_EXTENDED_MASTER_SECRET:
1824 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1825
1826 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1827 if( ret != 0 )
1828 return( ret );
1829 break;
1830#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1831
Paul Bakkera503a632013-08-14 13:48:06 +02001832#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001833 case TLS_EXT_SESSION_TICKET:
1834 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1835
1836 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1837 if( ret != 0 )
1838 return( ret );
1839 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001840#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001841
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001842#if defined(POLARSSL_SSL_ALPN)
1843 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001844 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001845
1846 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1847 if( ret != 0 )
1848 return( ret );
1849 break;
1850#endif /* POLARSSL_SSL_SESSION_TICKETS */
1851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 default:
1853 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1854 ext_id ) );
1855 }
1856
1857 ext_len -= 4 + ext_size;
1858 ext += 4 + ext_size;
1859
1860 if( ext_len > 0 && ext_len < 4 )
1861 {
1862 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1863 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1864 }
1865 }
1866
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001867#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1868 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1869 {
1870 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1871 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1872 {
1873 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1874
1875 if( ssl->minor_ver < ssl->max_minor_ver )
1876 {
1877 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1878
1879 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1880 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1881
1882 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1883 }
1884
1885 break;
1886 }
1887 }
1888#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1889
Paul Bakker48916f92012-09-16 19:57:18 +00001890 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001891 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1892 */
1893 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1894 {
1895 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1896 {
1897 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1898 if( ssl->renegotiation == SSL_RENEGOTIATION )
1899 {
1900 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1901
1902 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1903 return( ret );
1904
1905 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1906 }
1907 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1908 break;
1909 }
1910 }
1911
1912 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001913 * Renegotiation security checks
1914 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001915 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001916 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1917 {
1918 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1919 handshake_failure = 1;
1920 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001921#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001922 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1923 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1924 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001925 {
1926 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001927 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001928 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001929 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1930 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1931 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001932 {
1933 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001934 handshake_failure = 1;
1935 }
1936 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1937 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1938 renegotiation_info_seen == 1 )
1939 {
1940 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1941 handshake_failure = 1;
1942 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001943#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001944
1945 if( handshake_failure == 1 )
1946 {
1947 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1948 return( ret );
1949
Paul Bakker48916f92012-09-16 19:57:18 +00001950 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1951 }
Paul Bakker380da532012-04-18 16:10:25 +00001952
Paul Bakker41c83d32013-03-20 14:39:14 +01001953 /*
1954 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001955 * (At the end because we need information from the EC-based extensions
1956 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001957 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001958 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001959 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001960 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001961#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001962 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001963 {
1964 for( i = 0; ciphersuites[i] != 0; i++ )
1965#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001966 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001967 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001968 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001969#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001970 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001971 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1972 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1973 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001974
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001975 got_common_suite = 1;
1976
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001977 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1978 &ciphersuite_info ) ) != 0 )
1979 return( ret );
1980
1981 if( ciphersuite_info != NULL )
1982 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001983 }
1984 }
1985
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001986 if( got_common_suite )
1987 {
1988 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1989 "but none of them usable" ) );
1990 ssl_send_fatal_handshake_failure( ssl );
1991 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1992 }
1993 else
1994 {
1995 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1996 ssl_send_fatal_handshake_failure( ssl );
1997 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1998 }
Paul Bakker41c83d32013-03-20 14:39:14 +01001999
2000have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002001 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002002 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2003 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2004
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 ssl->state++;
2006
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002007#if defined(POLARSSL_SSL_PROTO_DTLS)
2008 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2009 ssl_recv_flight_completed( ssl );
2010#endif
2011
Paul Bakker5121ce52009-01-03 21:22:43 +00002012 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2013
2014 return( 0 );
2015}
2016
Paul Bakker1f2bc622013-08-15 13:45:55 +02002017#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002018static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2019 unsigned char *buf,
2020 size_t *olen )
2021{
2022 unsigned char *p = buf;
2023
2024 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2025 {
2026 *olen = 0;
2027 return;
2028 }
2029
2030 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2031
2032 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2033 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2034
2035 *p++ = 0x00;
2036 *p++ = 0x00;
2037
2038 *olen = 4;
2039}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002040#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002041
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002042#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2043static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2044 unsigned char *buf,
2045 size_t *olen )
2046{
2047 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002048 const ssl_ciphersuite_t *suite = NULL;
2049 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002050
2051 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2052 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2053 {
2054 *olen = 0;
2055 return;
2056 }
2057
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002058 /*
2059 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2060 * from a client and then selects a stream or Authenticated Encryption
2061 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2062 * encrypt-then-MAC response extension back to the client."
2063 */
2064 if( ( suite = ssl_ciphersuite_from_id(
2065 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2066 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2067 cipher->mode != POLARSSL_MODE_CBC )
2068 {
2069 *olen = 0;
2070 return;
2071 }
2072
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002073 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2074
2075 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2076 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2077
2078 *p++ = 0x00;
2079 *p++ = 0x00;
2080
2081 *olen = 4;
2082}
2083#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2084
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002085#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2086static void ssl_write_extended_ms_ext( ssl_context *ssl,
2087 unsigned char *buf,
2088 size_t *olen )
2089{
2090 unsigned char *p = buf;
2091
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002092 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2093 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002094 {
2095 *olen = 0;
2096 return;
2097 }
2098
2099 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2100 "extension" ) );
2101
2102 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2103 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2104
2105 *p++ = 0x00;
2106 *p++ = 0x00;
2107
2108 *olen = 4;
2109}
2110#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2111
Paul Bakkera503a632013-08-14 13:48:06 +02002112#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002113static void ssl_write_session_ticket_ext( ssl_context *ssl,
2114 unsigned char *buf,
2115 size_t *olen )
2116{
2117 unsigned char *p = buf;
2118
2119 if( ssl->handshake->new_session_ticket == 0 )
2120 {
2121 *olen = 0;
2122 return;
2123 }
2124
2125 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2126
2127 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2128 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2129
2130 *p++ = 0x00;
2131 *p++ = 0x00;
2132
2133 *olen = 4;
2134}
Paul Bakkera503a632013-08-14 13:48:06 +02002135#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002136
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002137static void ssl_write_renegotiation_ext( ssl_context *ssl,
2138 unsigned char *buf,
2139 size_t *olen )
2140{
2141 unsigned char *p = buf;
2142
2143 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2144 {
2145 *olen = 0;
2146 return;
2147 }
2148
2149 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2150
2151 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2152 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2153
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002154#if defined(POLARSSL_SSL_RENEGOTIATION)
2155 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2156 {
2157 *p++ = 0x00;
2158 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2159 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002160
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002161 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2162 p += ssl->verify_data_len;
2163 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2164 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002165
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002166 *olen = 5 + ssl->verify_data_len * 2;
2167 }
2168 else
2169#endif /* POLARSSL_SSL_RENEGOTIATION */
2170 {
2171 *p++ = 0x00;
2172 *p++ = 0x01;
2173 *p++ = 0x00;
2174
2175 *olen = 5;
2176 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002177}
2178
Paul Bakker05decb22013-08-15 13:33:48 +02002179#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002180static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2181 unsigned char *buf,
2182 size_t *olen )
2183{
2184 unsigned char *p = buf;
2185
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002186 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2187 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002188 *olen = 0;
2189 return;
2190 }
2191
2192 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2193
2194 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2195 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2196
2197 *p++ = 0x00;
2198 *p++ = 1;
2199
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002200 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002201
2202 *olen = 5;
2203}
Paul Bakker05decb22013-08-15 13:33:48 +02002204#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002205
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002206#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002207static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2208 unsigned char *buf,
2209 size_t *olen )
2210{
2211 unsigned char *p = buf;
2212 ((void) ssl);
2213
Paul Bakker677377f2013-10-28 12:54:26 +01002214 if( ( ssl->handshake->cli_exts &
2215 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2216 {
2217 *olen = 0;
2218 return;
2219 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002220
2221 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2222
2223 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2224 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2225
2226 *p++ = 0x00;
2227 *p++ = 2;
2228
2229 *p++ = 1;
2230 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2231
2232 *olen = 6;
2233}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002234#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002235
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002236#if defined(POLARSSL_SSL_ALPN )
2237static void ssl_write_alpn_ext( ssl_context *ssl,
2238 unsigned char *buf, size_t *olen )
2239{
2240 if( ssl->alpn_chosen == NULL )
2241 {
2242 *olen = 0;
2243 return;
2244 }
2245
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002246 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002247
2248 /*
2249 * 0 . 1 ext identifier
2250 * 2 . 3 ext length
2251 * 4 . 5 protocol list length
2252 * 6 . 6 protocol name length
2253 * 7 . 7+n protocol name
2254 */
2255 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2256 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2257
2258 *olen = 7 + strlen( ssl->alpn_chosen );
2259
2260 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2261 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2262
2263 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2264 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2265
2266 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2267
2268 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2269}
2270#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2271
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002272#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002273static int ssl_write_hello_verify_request( ssl_context *ssl )
2274{
2275 int ret;
2276 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002277 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002278
2279 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2280
2281 /*
2282 * struct {
2283 * ProtocolVersion server_version;
2284 * opaque cookie<0..2^8-1>;
2285 * } HelloVerifyRequest;
2286 */
2287
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002288 /* The RFC is not clear on this point, but sending the actual negotiated
2289 * version looks like the most interoperable thing to do. */
2290 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002291 ssl->transport, p );
2292 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2293 p += 2;
2294
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002295 /* If we get here, f_cookie_check is not null */
2296 if( ssl->f_cookie_write == NULL )
2297 {
2298 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2299 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2300 }
2301
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002302 /* Skip length byte until we know the length */
2303 cookie_len_byte = p++;
2304
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002305 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2306 &p, ssl->out_buf + SSL_BUFFER_LEN,
2307 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002308 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002309 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002310 return( ret );
2311 }
2312
2313 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2314
2315 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002316
2317 ssl->out_msglen = p - ssl->out_msg;
2318 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2319 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2320
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002321 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002322
2323 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2324 {
2325 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2326 return( ret );
2327 }
2328
2329 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2330
2331 return( 0 );
2332}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002333#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002334
Paul Bakker5121ce52009-01-03 21:22:43 +00002335static int ssl_write_server_hello( ssl_context *ssl )
2336{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002337#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002338 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002339#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002340 int ret;
2341 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 unsigned char *buf, *p;
2343
2344 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2345
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002346#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002347 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002348 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002349 {
2350 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2351 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2352
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002353 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002354 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002355#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002356
Paul Bakkera9a028e2013-11-21 17:31:06 +01002357 if( ssl->f_rng == NULL )
2358 {
2359 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2360 return( POLARSSL_ERR_SSL_NO_RNG );
2361 }
2362
Paul Bakker5121ce52009-01-03 21:22:43 +00002363 /*
2364 * 0 . 0 handshake type
2365 * 1 . 3 handshake length
2366 * 4 . 5 protocol version
2367 * 6 . 9 UNIX time()
2368 * 10 . 37 random bytes
2369 */
2370 buf = ssl->out_msg;
2371 p = buf + 4;
2372
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002373 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2374 ssl->transport, p );
2375 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002376
2377 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002378 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002379
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002380#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002381 t = time( NULL );
2382 *p++ = (unsigned char)( t >> 24 );
2383 *p++ = (unsigned char)( t >> 16 );
2384 *p++ = (unsigned char)( t >> 8 );
2385 *p++ = (unsigned char)( t );
2386
2387 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002388#else
2389 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2390 return( ret );
2391
2392 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002393#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Paul Bakkera3d195c2011-11-27 21:07:34 +00002395 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2396 return( ret );
2397
2398 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002399
Paul Bakker48916f92012-09-16 19:57:18 +00002400 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002401
2402 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2403
2404 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002405 * Resume is 0 by default, see ssl_handshake_init().
2406 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2407 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002409 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002410#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002411 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002412#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002413 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002414 ssl->f_get_cache != NULL &&
2415 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2416 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002417 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002418 ssl->handshake->resume = 1;
2419 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002420
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002421 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 {
2423 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002424 * New session, create a new session id,
2425 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002427 ssl->state++;
2428
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002429#if defined(POLARSSL_HAVE_TIME)
2430 ssl->session_negotiate->start = time( NULL );
2431#endif
2432
Paul Bakkera503a632013-08-14 13:48:06 +02002433#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002434 if( ssl->handshake->new_session_ticket != 0 )
2435 {
2436 ssl->session_negotiate->length = n = 0;
2437 memset( ssl->session_negotiate->id, 0, 32 );
2438 }
2439 else
2440#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002441 {
2442 ssl->session_negotiate->length = n = 32;
2443 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002444 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002445 return( ret );
2446 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002447 }
2448 else
2449 {
2450 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002451 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002452 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002453 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002454 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002455
2456 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2457 {
2458 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2459 return( ret );
2460 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 }
2462
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002463 /*
2464 * 38 . 38 session id length
2465 * 39 . 38+n session id
2466 * 39+n . 40+n chosen ciphersuite
2467 * 41+n . 41+n chosen compression alg.
2468 * 42+n . 43+n extensions length
2469 * 44+n . 43+n+m extensions
2470 */
2471 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002472 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2473 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002474
2475 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2476 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2477 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002478 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Paul Bakker48916f92012-09-16 19:57:18 +00002480 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2481 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2482 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002484 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2485 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002486 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002487 ssl->session_negotiate->compression ) );
2488
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002489 /*
2490 * First write extensions, then the total length
2491 */
2492 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2493 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002494
Paul Bakker05decb22013-08-15 13:33:48 +02002495#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002496 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2497 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002498#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002499
Paul Bakker1f2bc622013-08-15 13:45:55 +02002500#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002501 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2502 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002503#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002504
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002505#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2506 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2507 ext_len += olen;
2508#endif
2509
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002510#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2511 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2512 ext_len += olen;
2513#endif
2514
Paul Bakkera503a632013-08-14 13:48:06 +02002515#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002516 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2517 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002518#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002519
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002520#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002521 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2522 ext_len += olen;
2523#endif
2524
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002525#if defined(POLARSSL_SSL_ALPN)
2526 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2527 ext_len += olen;
2528#endif
2529
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002530 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002531
Paul Bakkera7036632014-04-30 10:15:38 +02002532 if( ext_len > 0 )
2533 {
2534 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2535 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2536 p += ext_len;
2537 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
2539 ssl->out_msglen = p - buf;
2540 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2541 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2542
2543 ret = ssl_write_record( ssl );
2544
2545 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2546
2547 return( ret );
2548}
2549
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002550#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2551 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002552 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2553 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002554static int ssl_write_certificate_request( ssl_context *ssl )
2555{
Paul Bakkered27a042013-04-18 22:46:23 +02002556 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002557
2558 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2559
2560 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002561 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002562 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2563 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002564 {
2565 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2566 ssl->state++;
2567 return( 0 );
2568 }
2569
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002570 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2571 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002572}
2573#else
2574static int ssl_write_certificate_request( ssl_context *ssl )
2575{
2576 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2577 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002578 size_t dn_size, total_dn_size; /* excluding length bytes */
2579 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002580 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002581 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002582
2583 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2584
2585 ssl->state++;
2586
Paul Bakkerfbb17802013-04-17 19:10:21 +02002587 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002588 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002589 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002590 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002591 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002592 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002593 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 return( 0 );
2595 }
2596
2597 /*
2598 * 0 . 0 handshake type
2599 * 1 . 3 handshake length
2600 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002601 * 5 .. m-1 cert types
2602 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002603 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002604 * n .. n+1 length of all DNs
2605 * n+2 .. n+3 length of DN 1
2606 * n+4 .. ... Distinguished Name #1
2607 * ... .. ... length of DN 2, etc.
2608 */
2609 buf = ssl->out_msg;
2610 p = buf + 4;
2611
2612 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002613 * Supported certificate types
2614 *
2615 * ClientCertificateType certificate_types<1..2^8-1>;
2616 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002618 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002619
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002620#if defined(POLARSSL_RSA_C)
2621 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2622#endif
2623#if defined(POLARSSL_ECDSA_C)
2624 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2625#endif
2626
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002627 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002628 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002629
Paul Bakker577e0062013-08-28 11:57:20 +02002630 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002631#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002632 /*
2633 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002634 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002635 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2636 *
2637 * struct {
2638 * HashAlgorithm hash;
2639 * SignatureAlgorithm signature;
2640 * } SignatureAndHashAlgorithm;
2641 *
2642 * enum { (255) } HashAlgorithm;
2643 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002644 */
Paul Bakker21dca692013-01-03 11:41:08 +01002645 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002646 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002647 /*
2648 * Only use current running hash algorithm that is already required
2649 * for requested ciphersuite.
2650 */
Paul Bakker926af752012-11-23 13:38:07 +01002651 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2652
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002653 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2654 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002655 {
2656 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2657 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002658
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002659 /*
2660 * Supported signature algorithms
2661 */
2662#if defined(POLARSSL_RSA_C)
2663 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2664 p[2 + sa_len++] = SSL_SIG_RSA;
2665#endif
2666#if defined(POLARSSL_ECDSA_C)
2667 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2668 p[2 + sa_len++] = SSL_SIG_ECDSA;
2669#endif
Paul Bakker926af752012-11-23 13:38:07 +01002670
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002671 p[0] = (unsigned char)( sa_len >> 8 );
2672 p[1] = (unsigned char)( sa_len );
2673 sa_len += 2;
2674 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002675 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002676#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002678 /*
2679 * DistinguishedName certificate_authorities<0..2^16-1>;
2680 * opaque DistinguishedName<1..2^16-1>;
2681 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002682 p += 2;
2683 crt = ssl->ca_chain;
2684
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002685 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002686 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002687 {
2688 if( p - buf > 4096 )
2689 break;
2690
Paul Bakker926af752012-11-23 13:38:07 +01002691 dn_size = crt->subject_raw.len;
2692 *p++ = (unsigned char)( dn_size >> 8 );
2693 *p++ = (unsigned char)( dn_size );
2694 memcpy( p, crt->subject_raw.p, dn_size );
2695 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002696
Paul Bakker926af752012-11-23 13:38:07 +01002697 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2698
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002699 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002700 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 }
2702
Paul Bakker926af752012-11-23 13:38:07 +01002703 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2705 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002706 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2707 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002708
2709 ret = ssl_write_record( ssl );
2710
2711 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2712
2713 return( ret );
2714}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002715#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2716 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002717 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2718 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002720#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2721 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2722static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2723{
2724 int ret;
2725
2726 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2727 {
2728 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2729 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2730 }
2731
2732 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2733 pk_ec( *ssl_own_key( ssl ) ),
2734 POLARSSL_ECDH_OURS ) ) != 0 )
2735 {
2736 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2737 return( ret );
2738 }
2739
2740 return( 0 );
2741}
2742#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2743 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2744
Paul Bakker41c83d32013-03-20 14:39:14 +01002745static int ssl_write_server_key_exchange( ssl_context *ssl )
2746{
Paul Bakker23986e52011-04-24 08:57:21 +00002747 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002748 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002749 const ssl_ciphersuite_t *ciphersuite_info =
2750 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002751
2752#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2753 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2754 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002755 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002756 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002757 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002758 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002759 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002760 ((void) dig_signed);
2761 ((void) dig_signed_len);
2762#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002763
Paul Bakker5121ce52009-01-03 21:22:43 +00002764 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2765
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002766#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2767 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2768 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002769 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2770 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2771 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002772 {
2773 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2774 ssl->state++;
2775 return( 0 );
2776 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002777#endif
2778
2779#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2780 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2781 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2782 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2783 {
2784 ssl_get_ecdh_params_from_cert( ssl );
2785
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002786 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002787 ssl->state++;
2788 return( 0 );
2789 }
2790#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002791
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002792#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2793 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2794 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2795 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002796 {
2797 /* TODO: Support identity hints */
2798 *(p++) = 0x00;
2799 *(p++) = 0x00;
2800
2801 n += 2;
2802 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002803#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2804 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002805
2806#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2807 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2808 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2809 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002810 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002811 /*
2812 * Ephemeral DH parameters:
2813 *
2814 * struct {
2815 * opaque dh_p<1..2^16-1>;
2816 * opaque dh_g<1..2^16-1>;
2817 * opaque dh_Ys<1..2^16-1>;
2818 * } ServerDHParams;
2819 */
2820 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2821 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2822 {
2823 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2824 return( ret );
2825 }
Paul Bakker48916f92012-09-16 19:57:18 +00002826
Paul Bakker41c83d32013-03-20 14:39:14 +01002827 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002828 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2829 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002830 {
2831 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2832 return( ret );
2833 }
2834
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002835 dig_signed = p;
2836 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002837
2838 p += len;
2839 n += len;
2840
Paul Bakker41c83d32013-03-20 14:39:14 +01002841 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2842 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2843 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2844 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2845 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002846#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2847 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002848
Gergely Budai987bfb52014-01-19 21:48:42 +01002849#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002850 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002851 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2852 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002854 /*
2855 * Ephemeral ECDH parameters:
2856 *
2857 * struct {
2858 * ECParameters curve_params;
2859 * ECPoint public;
2860 * } ServerECDHParams;
2861 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002862 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002863#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002864 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002865
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002866 /* Match our preference list against the offered curves */
2867 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2868 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2869 if( (*curve)->grp_id == *gid )
2870 goto curve_matching_done;
2871
2872curve_matching_done:
2873#else
2874 curve = ssl->handshake->curves;
2875#endif
2876
2877 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002878 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002879 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2880 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002881 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002882
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002883 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002884
Paul Bakker41c83d32013-03-20 14:39:14 +01002885 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002886 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002887 {
2888 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2889 return( ret );
2890 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002891
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002892 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2893 p, SSL_MAX_CONTENT_LEN - n,
2894 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002895 {
2896 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2897 return( ret );
2898 }
2899
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002900 dig_signed = p;
2901 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002902
2903 p += len;
2904 n += len;
2905
Paul Bakker41c83d32013-03-20 14:39:14 +01002906 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2907 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002908#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002910#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002911 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2912 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002913 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002914 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2915 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002916 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002917 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002918 unsigned int hashlen = 0;
2919 unsigned char hash[64];
2920 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002921
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002922 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002923 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2924 */
Paul Bakker577e0062013-08-28 11:57:20 +02002925#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002926 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2927 {
2928 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2929
2930 if( md_alg == POLARSSL_MD_NONE )
2931 {
2932 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002933 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002934 }
2935 }
Paul Bakker577e0062013-08-28 11:57:20 +02002936 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002937#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002938#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2939 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002940 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002941 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2942 {
2943 md_alg = POLARSSL_MD_SHA1;
2944 }
2945 else
Paul Bakker577e0062013-08-28 11:57:20 +02002946#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002947 {
2948 md_alg = POLARSSL_MD_NONE;
2949 }
2950
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002951 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002952 * Compute the hash to be signed
2953 */
Paul Bakker577e0062013-08-28 11:57:20 +02002954#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2955 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002956 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002957 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958 md5_context md5;
2959 sha1_context sha1;
2960
Paul Bakker5b4af392014-06-26 12:09:34 +02002961 md5_init( &md5 );
2962 sha1_init( &sha1 );
2963
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964 /*
2965 * digitally-signed struct {
2966 * opaque md5_hash[16];
2967 * opaque sha_hash[20];
2968 * };
2969 *
2970 * md5_hash
2971 * MD5(ClientHello.random + ServerHello.random
2972 * + ServerParams);
2973 * sha_hash
2974 * SHA(ClientHello.random + ServerHello.random
2975 * + ServerParams);
2976 */
2977 md5_starts( &md5 );
2978 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002979 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002980 md5_finish( &md5, hash );
2981
2982 sha1_starts( &sha1 );
2983 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002984 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002985 sha1_finish( &sha1, hash + 16 );
2986
2987 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002988
2989 md5_free( &md5 );
2990 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002991 }
2992 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002993#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2994 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002995#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2996 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002997 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002998 {
2999 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003000 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003001
Paul Bakker84bbeb52014-07-01 14:53:22 +02003002 md_init( &ctx );
3003
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003004 /* Info from md_alg will be used instead */
3005 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003006
3007 /*
3008 * digitally-signed struct {
3009 * opaque client_random[32];
3010 * opaque server_random[32];
3011 * ServerDHParams params;
3012 * };
3013 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003014 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003015 {
3016 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3017 return( ret );
3018 }
3019
3020 md_starts( &ctx );
3021 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003022 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003023 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003024 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003025 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003026 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003027#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3028 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003029 {
3030 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003031 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003032 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003033
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003034 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3035 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003036
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003037 /*
3038 * Make the signature
3039 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003040 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003041 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003042 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3043 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003044 }
Paul Bakker23f36802012-09-28 14:15:14 +00003045
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003047 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3048 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003049 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003050 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051
3052 n += 2;
3053 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003054#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003055
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003056 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003057 p + 2 , &signature_len,
3058 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003059 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003060 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003061 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003062 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003063
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003064 *(p++) = (unsigned char)( signature_len >> 8 );
3065 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003066 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003067
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003068 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003069
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003070 p += signature_len;
3071 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003072 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003073#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003074 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3075 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3079 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3080
3081 ssl->state++;
3082
3083 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3084 {
3085 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3086 return( ret );
3087 }
3088
3089 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3090
3091 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092}
3093
3094static int ssl_write_server_hello_done( ssl_context *ssl )
3095{
3096 int ret;
3097
3098 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3099
3100 ssl->out_msglen = 4;
3101 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3102 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3103
3104 ssl->state++;
3105
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003106#if defined(POLARSSL_SSL_PROTO_DTLS)
3107 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3108 ssl_send_flight_completed( ssl );
3109#endif
3110
Paul Bakker5121ce52009-01-03 21:22:43 +00003111 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3112 {
3113 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3114 return( ret );
3115 }
3116
3117 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3118
3119 return( 0 );
3120}
3121
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003122#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3123 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3124static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3125 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003126{
3127 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003128 size_t n;
3129
3130 /*
3131 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3132 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003133 if( *p + 2 > end )
3134 {
3135 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3136 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3137 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003138
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 n = ( (*p)[0] << 8 ) | (*p)[1];
3140 *p += 2;
3141
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003142 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003143 {
3144 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3145 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3146 }
3147
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003148 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003149 {
3150 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3152 }
3153
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003154 *p += n;
3155
Paul Bakker70df2fb2013-04-17 17:19:09 +02003156 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3157
Paul Bakker70df2fb2013-04-17 17:19:09 +02003158 return( ret );
3159}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003160#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3161 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003162
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003163#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3164 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003165static int ssl_parse_encrypted_pms( ssl_context *ssl,
3166 const unsigned char *p,
3167 const unsigned char *end,
3168 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003169{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003170 int ret;
3171 size_t len = pk_get_len( ssl_own_key( ssl ) );
3172 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003173 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003174
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003175 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003176 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003177 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003178 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3179 }
3180
3181 /*
3182 * Decrypt the premaster using own private RSA key
3183 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003184#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3185 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003186 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3187 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003188 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3189 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003190 {
3191 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3192 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3193 }
3194 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003195#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003196
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003197 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003198 {
3199 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3200 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3201 }
3202
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003203 ssl_write_version( ssl->handshake->max_major_ver,
3204 ssl->handshake->max_minor_ver,
3205 ssl->transport, ver );
3206
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003207 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3208 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003209 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003210 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003211
3212 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003213 pms[0] != ver[0] ||
3214 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003215 {
3216 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3217
3218 /*
3219 * Protection against Bleichenbacher's attack:
3220 * invalid PKCS#1 v1.5 padding must not cause
3221 * the connection to end immediately; instead,
3222 * send a bad_record_mac later in the handshake.
3223 */
3224 ssl->handshake->pmslen = 48;
3225
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003226 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003227 if( ret != 0 )
3228 return( ret );
3229 }
3230
3231 return( ret );
3232}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003233#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3234 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003235
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003236#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003237static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3238 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003239{
Paul Bakker6db455e2013-09-18 17:29:31 +02003240 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003241 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003242
Paul Bakker6db455e2013-09-18 17:29:31 +02003243 if( ssl->f_psk == NULL &&
3244 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3245 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003246 {
3247 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3248 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3249 }
3250
3251 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003252 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003253 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003254 if( *p + 2 > end )
3255 {
3256 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3257 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3258 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003259
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003260 n = ( (*p)[0] << 8 ) | (*p)[1];
3261 *p += 2;
3262
3263 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003264 {
3265 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3266 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3267 }
3268
Paul Bakker6db455e2013-09-18 17:29:31 +02003269 if( ssl->f_psk != NULL )
3270 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003271 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003272 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3273 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003274 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003275 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003276 /* Identity is not a big secret since clients send it in the clear,
3277 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003278 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003279 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003280 {
3281 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3282 }
3283 }
3284
3285 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003286 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003287 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003288 if( ( ret = ssl_send_alert_message( ssl,
3289 SSL_ALERT_LEVEL_FATAL,
3290 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3291 {
3292 return( ret );
3293 }
3294
3295 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003296 }
3297
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003298 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003299
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003300 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003301}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003302#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003303
Paul Bakker5121ce52009-01-03 21:22:43 +00003304static int ssl_parse_client_key_exchange( ssl_context *ssl )
3305{
Paul Bakker23986e52011-04-24 08:57:21 +00003306 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003307 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003308 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003309
Paul Bakker41c83d32013-03-20 14:39:14 +01003310 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
3312 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3313
3314 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3315 {
3316 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3317 return( ret );
3318 }
3319
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003320 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3321 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003322
Paul Bakker5121ce52009-01-03 21:22:43 +00003323 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3324 {
3325 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003326 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 }
3328
3329 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3330 {
3331 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003332 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003333 }
3334
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003335#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003336 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003338 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003339 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003340 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3341 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003343
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003344 if( p != end )
3345 {
3346 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3347 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3348 }
3349
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003350 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003351
3352 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3353 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003354 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003355 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003356 {
3357 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3358 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3359 }
3360
3361 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003362 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003363 else
3364#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003365#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003366 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3367 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3368 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003369 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003370 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3371 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3372 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003373 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003374 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003375 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003376 {
3377 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3378 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3379 }
3380
3381 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3382
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003383 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3384 &ssl->handshake->pmslen,
3385 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003386 POLARSSL_MPI_MAX_SIZE,
3387 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003388 {
3389 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3390 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3391 }
3392
3393 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003395 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003396#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003397 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3398 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3399 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003400#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3401 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003402 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003403 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003404 {
3405 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3406 return( ret );
3407 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003408
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003409 if( p != end )
3410 {
3411 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3412 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3413 }
3414
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003415 if( ( ret = ssl_psk_derive_premaster( ssl,
3416 ciphersuite_info->key_exchange ) ) != 0 )
3417 {
3418 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3419 return( ret );
3420 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003421 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003422 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003423#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003424#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3425 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3426 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003427 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3428 {
3429 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3430 return( ret );
3431 }
3432
3433 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3434 {
3435 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3436 return( ret );
3437 }
3438
3439 if( ( ret = ssl_psk_derive_premaster( ssl,
3440 ciphersuite_info->key_exchange ) ) != 0 )
3441 {
3442 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3443 return( ret );
3444 }
3445 }
3446 else
3447#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003448#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3449 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3450 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003451 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3452 {
3453 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3454 return( ret );
3455 }
3456 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3457 {
3458 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3459 return( ret );
3460 }
3461
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003462 if( p != end )
3463 {
3464 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3465 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3466 }
3467
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003468 if( ( ret = ssl_psk_derive_premaster( ssl,
3469 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003470 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003471 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3472 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003473 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474 }
3475 else
3476#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003477#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3479 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003480 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3481 {
3482 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3483 return( ret );
3484 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003485
3486 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3487 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003488 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003489 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3490 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003491 }
3492
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003493 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3494
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003495 if( ( ret = ssl_psk_derive_premaster( ssl,
3496 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003497 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003498 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003499 return( ret );
3500 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003501 }
3502 else
3503#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003504#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3505 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003506 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003507 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003508 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003509 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003510 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003511 }
3512 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003513 else
3514#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3515 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003516 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003517 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003518 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003519
Paul Bakkerff60ee62010-03-16 21:09:09 +00003520 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3521 {
3522 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3523 return( ret );
3524 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003525
Paul Bakker5121ce52009-01-03 21:22:43 +00003526 ssl->state++;
3527
3528 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3529
3530 return( 0 );
3531}
3532
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003533#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3534 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003535 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3536 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003537static int ssl_parse_certificate_verify( ssl_context *ssl )
3538{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003539 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
3541 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3542
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003543 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003544 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003545 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003546 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003547 {
3548 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3549 ssl->state++;
3550 return( 0 );
3551 }
3552
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003553 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3554 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003555}
3556#else
3557static int ssl_parse_certificate_verify( ssl_context *ssl )
3558{
3559 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003560 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003561 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003562 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003563 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003564#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003565 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003566#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003567 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003568 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3569
3570 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3571
3572 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003573 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003574 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003575 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3576 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003577 {
3578 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3579 ssl->state++;
3580 return( 0 );
3581 }
3582
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003583 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003584 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003585
3586 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3587 {
3588 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3589 return( ret );
3590 }
3591
3592 ssl->state++;
3593
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003594 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3595 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 {
3597 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003598 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 }
3600
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003601 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003603 /*
3604 * struct {
3605 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3606 * opaque signature<0..2^16-1>;
3607 * } DigitallySigned;
3608 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003609#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3610 defined(POLARSSL_SSL_PROTO_TLS1_1)
3611 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003612 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003613 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003614 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003615
3616 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3617 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3618 POLARSSL_PK_ECDSA ) )
3619 {
3620 hash_start += 16;
3621 hashlen -= 16;
3622 md_alg = POLARSSL_MD_SHA1;
3623 }
Paul Bakker926af752012-11-23 13:38:07 +01003624 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003625 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003626#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3627 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003628#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3629 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003630 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003631 if( i + 2 > ssl->in_hslen )
3632 {
3633 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3634 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3635 }
3636
Paul Bakker5121ce52009-01-03 21:22:43 +00003637 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003638 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003639 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003640 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003642 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3643 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3645 }
3646
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003647 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003648
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003649 /* Info from md_alg will be used instead */
3650 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003651
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003652 i++;
3653
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003654 /*
3655 * Signature
3656 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003657 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003658 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003659 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003660 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3661 " for verify message" ) );
3662 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003663 }
3664
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003665 /*
3666 * Check the certificate's key type matches the signature alg
3667 */
3668 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3669 {
3670 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3671 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3672 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003673
3674 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003675 }
3676 else
3677#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3678 {
3679 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003680 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003681 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003682
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003683 if( i + 2 > ssl->in_hslen )
3684 {
3685 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3686 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3687 }
3688
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003689 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3690 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003691
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003692 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003693 {
3694 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003695 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 }
3697
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003698 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003699 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003700 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003701 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003702 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003703 return( ret );
3704 }
3705
3706 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3707
Paul Bakkered27a042013-04-18 22:46:23 +02003708 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003709}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003710#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3711 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3712 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003713
Paul Bakkera503a632013-08-14 13:48:06 +02003714#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003715static int ssl_write_new_session_ticket( ssl_context *ssl )
3716{
3717 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003718 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003719 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003720
3721 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3722
3723 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3724 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3725
3726 /*
3727 * struct {
3728 * uint32 ticket_lifetime_hint;
3729 * opaque ticket<0..2^16-1>;
3730 * } NewSessionTicket;
3731 *
3732 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3733 * 8 . 9 ticket_len (n)
3734 * 10 . 9+n ticket content
3735 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003736
3737 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3738 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3739 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3740 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003741
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003742 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3743 {
3744 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3745 tlen = 0;
3746 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003747
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003748 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3749 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003750
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003751 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003752
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003753 /*
3754 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3755 * ChangeCipherSpec share the same state.
3756 */
3757 ssl->handshake->new_session_ticket = 0;
3758
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003759 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3760 {
3761 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3762 return( ret );
3763 }
3764
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003765 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3766
3767 return( 0 );
3768}
Paul Bakkera503a632013-08-14 13:48:06 +02003769#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003770
Paul Bakker5121ce52009-01-03 21:22:43 +00003771/*
Paul Bakker1961b702013-01-25 14:49:24 +01003772 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 */
Paul Bakker1961b702013-01-25 14:49:24 +01003774int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003775{
3776 int ret = 0;
3777
Paul Bakker1961b702013-01-25 14:49:24 +01003778 if( ssl->state == SSL_HANDSHAKE_OVER )
3779 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780
Paul Bakker1961b702013-01-25 14:49:24 +01003781 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3782
3783 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3784 return( ret );
3785
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003786#if defined(POLARSSL_SSL_PROTO_DTLS)
3787 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3788 ssl->handshake != NULL &&
3789 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3790 {
3791 if( ( ret = ssl_resend( ssl ) ) != 0 )
3792 return( ret );
3793 }
3794#endif
3795
Paul Bakker1961b702013-01-25 14:49:24 +01003796 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003797 {
Paul Bakker1961b702013-01-25 14:49:24 +01003798 case SSL_HELLO_REQUEST:
3799 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003800 break;
3801
Paul Bakker1961b702013-01-25 14:49:24 +01003802 /*
3803 * <== ClientHello
3804 */
3805 case SSL_CLIENT_HELLO:
3806 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003808
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003809#if defined(POLARSSL_SSL_PROTO_DTLS)
3810 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3811 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3812#endif
3813
Paul Bakker1961b702013-01-25 14:49:24 +01003814 /*
3815 * ==> ServerHello
3816 * Certificate
3817 * ( ServerKeyExchange )
3818 * ( CertificateRequest )
3819 * ServerHelloDone
3820 */
3821 case SSL_SERVER_HELLO:
3822 ret = ssl_write_server_hello( ssl );
3823 break;
3824
3825 case SSL_SERVER_CERTIFICATE:
3826 ret = ssl_write_certificate( ssl );
3827 break;
3828
3829 case SSL_SERVER_KEY_EXCHANGE:
3830 ret = ssl_write_server_key_exchange( ssl );
3831 break;
3832
3833 case SSL_CERTIFICATE_REQUEST:
3834 ret = ssl_write_certificate_request( ssl );
3835 break;
3836
3837 case SSL_SERVER_HELLO_DONE:
3838 ret = ssl_write_server_hello_done( ssl );
3839 break;
3840
3841 /*
3842 * <== ( Certificate/Alert )
3843 * ClientKeyExchange
3844 * ( CertificateVerify )
3845 * ChangeCipherSpec
3846 * Finished
3847 */
3848 case SSL_CLIENT_CERTIFICATE:
3849 ret = ssl_parse_certificate( ssl );
3850 break;
3851
3852 case SSL_CLIENT_KEY_EXCHANGE:
3853 ret = ssl_parse_client_key_exchange( ssl );
3854 break;
3855
3856 case SSL_CERTIFICATE_VERIFY:
3857 ret = ssl_parse_certificate_verify( ssl );
3858 break;
3859
3860 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3861 ret = ssl_parse_change_cipher_spec( ssl );
3862 break;
3863
3864 case SSL_CLIENT_FINISHED:
3865 ret = ssl_parse_finished( ssl );
3866 break;
3867
3868 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003869 * ==> ( NewSessionTicket )
3870 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003871 * Finished
3872 */
3873 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003874#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003875 if( ssl->handshake->new_session_ticket != 0 )
3876 ret = ssl_write_new_session_ticket( ssl );
3877 else
Paul Bakkera503a632013-08-14 13:48:06 +02003878#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003879 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003880 break;
3881
3882 case SSL_SERVER_FINISHED:
3883 ret = ssl_write_finished( ssl );
3884 break;
3885
3886 case SSL_FLUSH_BUFFERS:
3887 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3888 ssl->state = SSL_HANDSHAKE_WRAPUP;
3889 break;
3890
3891 case SSL_HANDSHAKE_WRAPUP:
3892 ssl_handshake_wrapup( ssl );
3893 break;
3894
3895 default:
3896 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3897 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003898 }
3899
Paul Bakker5121ce52009-01-03 21:22:43 +00003900 return( ret );
3901}
Paul Bakker9af723c2014-05-01 13:03:14 +02003902#endif /* POLARSSL_SSL_SRV_C */