blob: ec806462dec9342b429f52f517cee62a487abec6 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/debug.h"
32#include "mbedtls/ssl.h"
Rich Evans00ab4702015-02-06 13:43:58 +000033
34#include <string.h>
35
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <stdlib.h>
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020044#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakkera503a632013-08-14 13:48:06 +020052#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020058/*
59 * Serialize a session in the following format:
60 * 0 . n-1 session structure, n = sizeof(ssl_session)
61 * n . n+2 peer_cert length = m (0 if no certificate)
62 * n+3 . n+2+m peer cert ASN.1
63 *
64 * Assumes ticket is NULL (always true on server side).
65 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020066static int ssl_save_session( const ssl_session *session,
67 unsigned char *buf, size_t buf_len,
68 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020069{
70 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020071 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020073 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020076 if( left < sizeof( ssl_session ) )
77 return( -1 );
78
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020079 memcpy( p, session, sizeof( ssl_session ) );
80 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020081 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020082
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084 if( session->peer_cert == NULL )
85 cert_len = 0;
86 else
87 cert_len = session->peer_cert->raw.len;
88
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020089 if( left < 3 + cert_len )
90 return( -1 );
91
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020092 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
93 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
94 *p++ = (unsigned char)( cert_len & 0xFF );
95
96 if( session->peer_cert != NULL )
97 memcpy( p, session->peer_cert->raw.p, cert_len );
98
99 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200101
102 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200103
104 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200105}
106
107/*
108 * Unserialise session, see ssl_save_session()
109 */
110static int ssl_load_session( ssl_session *session,
111 const unsigned char *buf, size_t len )
112{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113 const unsigned char *p = buf;
114 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200116 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118
119 if( p + sizeof( ssl_session ) > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 memcpy( session, p, sizeof( ssl_session ) );
123 p += sizeof( ssl_session );
124
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200126 if( p + 3 > end )
127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
128
129 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
130 p += 3;
131
132 if( cert_len == 0 )
133 {
134 session->peer_cert = NULL;
135 }
136 else
137 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200138 int ret;
139
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200140 if( p + cert_len > end )
141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
142
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200143 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
145 if( session->peer_cert == NULL )
146 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
147
Paul Bakkerb6b09562013-09-18 14:17:41 +0200148 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200150 if( ( ret = x509_crt_parse_der( session->peer_cert,
151 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200152 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200155 session->peer_cert = NULL;
156 return( ret );
157 }
158
159 p += cert_len;
160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200162
163 if( p != end )
164 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
165
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200169/*
170 * Create session ticket, secured as recommended in RFC 5077 section 4:
171 *
172 * struct {
173 * opaque key_name[16];
174 * opaque iv[16];
175 * opaque encrypted_state<0..2^16-1>;
176 * opaque mac[32];
177 * } ticket;
178 *
179 * (the internal state structure differs, however).
180 */
181static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
182{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200183 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200184 unsigned char * const start = ssl->out_msg + 10;
185 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200186 unsigned char *state;
187 unsigned char iv[16];
188 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200190 *tlen = 0;
191
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200192 if( ssl->ticket_keys == NULL )
193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
194
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200195 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200196 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200199 /* Generate and write IV (with a copy for aes_crypt) */
200 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
201 return( ret );
202 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200203 p += 16;
204
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 /*
206 * Dump session state
207 *
208 * After the session state itself, we still need room for 16 bytes of
209 * padding and 32 bytes of MAC, so there's only so much room left
210 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200212 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200213 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 &clear_len ) != 0 )
215 {
216 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
217 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Apply PKCS padding */
221 pad_len = 16 - clear_len % 16;
222 enc_len = clear_len + pad_len;
223 for( i = clear_len; i < enc_len; i++ )
224 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Encrypt */
227 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
228 enc_len, iv, state, state ) ) != 0 )
229 {
230 return( ret );
231 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200233 /* Write length */
234 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
236 p = state + enc_len;
237
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200238 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
239 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200240 p += 32;
241
242 *tlen = p - start;
243
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200244 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200245
246 return( 0 );
247}
248
249/*
250 * Load session ticket (see ssl_write_ticket for structure)
251 */
252static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200253 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200254 size_t len )
255{
256 int ret;
257 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 unsigned char *key_name = buf;
259 unsigned char *iv = buf + 16;
260 unsigned char *enc_len_p = iv + 16;
261 unsigned char *ticket = enc_len_p + 2;
262 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200263 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200264 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100265 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266
267 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200269 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
273 mac = ticket + enc_len;
274
275 if( len != enc_len + 66 )
276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100278 /* Check name, in constant time though it's not a big secret */
279 diff = 0;
280 for( i = 0; i < 16; i++ )
281 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
282 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100284 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200285 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
286 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100287
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200288 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289 diff |= mac[i] ^ computed_mac[i];
290
291 /* Now return if ticket is not authentic, since we want to avoid
292 * decrypting arbitrary attacker-chosen data */
293 if( diff != 0 )
294 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200295
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200296 /* Decrypt */
297 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
298 enc_len, iv, ticket, ticket ) ) != 0 )
299 {
300 return( ret );
301 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200302
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200303 /* Check PKCS padding */
304 pad_len = ticket[enc_len - 1];
305
306 ret = 0;
307 for( i = 2; i < pad_len; i++ )
308 if( ticket[enc_len - i] != pad_len )
309 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
310 if( ret != 0 )
311 return( ret );
312
313 clear_len = enc_len - pad_len;
314
315 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
316
317 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200318 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
319 {
320 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100321 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200322 return( ret );
323 }
324
Paul Bakker606b4ba2013-08-14 16:52:14 +0200325#if defined(POLARSSL_HAVE_TIME)
326 /* Check if still valid */
327 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
328 {
329 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100330 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200331 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
332 }
333#endif
334
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200335 /*
336 * Keep the session ID sent by the client, since we MUST send it back to
337 * inform him we're accepting the ticket (RFC 5077 section 3.4)
338 */
339 session.length = ssl->session_negotiate->length;
340 memcpy( &session.id, ssl->session_negotiate->id, session.length );
341
342 ssl_session_free( ssl->session_negotiate );
343 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200344
345 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200346 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200347
348 return( 0 );
349}
Paul Bakkera503a632013-08-14 13:48:06 +0200350#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200351
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200352#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200353int ssl_set_client_transport_id( ssl_context *ssl,
354 const unsigned char *info,
355 size_t ilen )
356{
357 if( ssl->endpoint != SSL_IS_SERVER )
358 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
359
360 polarssl_free( ssl->cli_id );
361
362 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
363 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
364
365 memcpy( ssl->cli_id, info, ilen );
366 ssl->cli_id_len = ilen;
367
368 return( 0 );
369}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200370
371void ssl_set_dtls_cookies( ssl_context *ssl,
372 ssl_cookie_write_t *f_cookie_write,
373 ssl_cookie_check_t *f_cookie_check,
374 void *p_cookie )
375{
376 ssl->f_cookie_write = f_cookie_write;
377 ssl->f_cookie_check = f_cookie_check;
378 ssl->p_cookie = p_cookie;
379}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200380#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200381
Paul Bakker0be444a2013-08-27 21:55:01 +0200382#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200383/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200384 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100385 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200386 */
387static int ssl_sni_wrapper( ssl_context *ssl,
388 const unsigned char* name, size_t len )
389{
390 int ret;
391 ssl_key_cert *key_cert_ori = ssl->key_cert;
392
393 ssl->key_cert = NULL;
394 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200395 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200396
397 ssl->key_cert = key_cert_ori;
398
399 return( ret );
400}
401
Paul Bakker5701cdc2012-09-27 21:49:42 +0000402static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000403 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 size_t len )
405{
406 int ret;
407 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000408 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000409
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100410 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
411
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
413 if( servername_list_size + 2 != len )
414 {
415 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
416 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
417 }
418
419 p = buf + 2;
420 while( servername_list_size > 0 )
421 {
422 hostname_len = ( ( p[1] << 8 ) | p[2] );
423 if( hostname_len + 3 > servername_list_size )
424 {
425 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
427 }
428
429 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
430 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200431 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000432 if( ret != 0 )
433 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100434 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000435 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
436 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
438 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000439 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000440 }
441
442 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000443 p += hostname_len + 3;
444 }
445
446 if( servername_list_size != 0 )
447 {
448 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000450 }
451
452 return( 0 );
453}
Paul Bakker0be444a2013-08-27 21:55:01 +0200454#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000457 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000458 size_t len )
459{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000460 int ret;
461
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100462#if defined(POLARSSL_SSL_RENEGOTIATION)
463 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
464 {
465 /* Check verify-data in constant-time. The length OTOH is no secret */
466 if( len != 1 + ssl->verify_data_len ||
467 buf[0] != ssl->verify_data_len ||
468 safer_memcmp( buf + 1, ssl->peer_verify_data,
469 ssl->verify_data_len ) != 0 )
470 {
471 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
472
473 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
474 return( ret );
475
476 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
477 }
478 }
479 else
480#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000481 {
482 if( len != 1 || buf[0] != 0x0 )
483 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100484 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000485
486 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
487 return( ret );
488
Paul Bakker48916f92012-09-16 19:57:18 +0000489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
490 }
491
492 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
493 }
Paul Bakker48916f92012-09-16 19:57:18 +0000494
495 return( 0 );
496}
497
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100498#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
499 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000500static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
501 const unsigned char *buf,
502 size_t len )
503{
504 size_t sig_alg_list_size;
505 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200506 const unsigned char *end = buf + len;
507 const int *md_cur;
508
Paul Bakker23f36802012-09-28 14:15:14 +0000509
510 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
511 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200512 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000513 {
514 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
515 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
516 }
517
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200518 /*
519 * For now, ignore the SignatureAlgorithm part and rely on offered
520 * ciphersuites only for that part. To be fixed later.
521 *
522 * So, just look at the HashAlgorithm part.
523 */
524 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
525 for( p = buf + 2; p < end; p += 2 ) {
526 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
527 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200528 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200529 }
Paul Bakker23f36802012-09-28 14:15:14 +0000530 }
Paul Bakker23f36802012-09-28 14:15:14 +0000531 }
532
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200533 /* Some key echanges do not need signatures at all */
534 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
535 return( 0 );
536
537have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000538 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
539 ssl->handshake->sig_alg ) );
540
541 return( 0 );
542}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100543#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
544 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000545
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200546#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200547static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
548 const unsigned char *buf,
549 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100550{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100552 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100554
555 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
556 if( list_size + 2 != len ||
557 list_size % 2 != 0 )
558 {
559 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
560 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
561 }
562
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200563 /* Should never happen unless client duplicates the extension */
564 if( ssl->handshake->curves != NULL )
565 {
566 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
567 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
568 }
569
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100570 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200571 * and leave room for a final 0 */
572 our_size = list_size / 2 + 1;
573 if( our_size > POLARSSL_ECP_DP_MAX )
574 our_size = POLARSSL_ECP_DP_MAX;
575
576 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
577 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
578
Paul Bakker9af723c2014-05-01 13:03:14 +0200579 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200580 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200581 ssl->handshake->curves = curves;
582
Paul Bakker41c83d32013-03-20 14:39:14 +0100583 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200584 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100585 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200586 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200587
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100589 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200590 *curves++ = curve_info;
591 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 }
593
594 list_size -= 2;
595 p += 2;
596 }
597
598 return( 0 );
599}
600
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200601static int ssl_parse_supported_point_formats( ssl_context *ssl,
602 const unsigned char *buf,
603 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100604{
605 size_t list_size;
606 const unsigned char *p;
607
608 list_size = buf[0];
609 if( list_size + 1 != len )
610 {
611 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
613 }
614
615 p = buf + 2;
616 while( list_size > 0 )
617 {
618 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
619 p[0] == POLARSSL_ECP_PF_COMPRESSED )
620 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200621 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200622 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100623 return( 0 );
624 }
625
626 list_size--;
627 p++;
628 }
629
630 return( 0 );
631}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200632#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100633
Paul Bakker05decb22013-08-15 13:33:48 +0200634#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200635static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
636 const unsigned char *buf,
637 size_t len )
638{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200639 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200640 {
641 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
642 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
643 }
644
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200645 ssl->session_negotiate->mfl_code = buf[0];
646
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200647 return( 0 );
648}
Paul Bakker05decb22013-08-15 13:33:48 +0200649#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200650
Paul Bakker1f2bc622013-08-15 13:45:55 +0200651#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200652static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
653 const unsigned char *buf,
654 size_t len )
655{
656 if( len != 0 )
657 {
658 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
659 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
660 }
661
662 ((void) buf);
663
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100664 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
665 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200666
667 return( 0 );
668}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200669#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200670
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100671#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
672static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
673 const unsigned char *buf,
674 size_t len )
675{
676 if( len != 0 )
677 {
678 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
679 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
680 }
681
682 ((void) buf);
683
684 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
685 ssl->minor_ver != SSL_MINOR_VERSION_0 )
686 {
687 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
688 }
689
690 return( 0 );
691}
692#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
693
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200694#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
695static int ssl_parse_extended_ms_ext( ssl_context *ssl,
696 const unsigned char *buf,
697 size_t len )
698{
699 if( len != 0 )
700 {
701 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
702 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
703 }
704
705 ((void) buf);
706
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200707 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
708 ssl->minor_ver != SSL_MINOR_VERSION_0 )
709 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200710 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200711 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200712
713 return( 0 );
714}
715#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
716
Paul Bakkera503a632013-08-14 13:48:06 +0200717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200718static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200719 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720 size_t len )
721{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200722 int ret;
723
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200724 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
725 return( 0 );
726
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200727 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200728 ssl->handshake->new_session_ticket = 1;
729
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200730 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
731
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200732 if( len == 0 )
733 return( 0 );
734
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100735#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200736 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
737 {
738 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
739 return( 0 );
740 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100741#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200742
743 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744 * Failures are ok: just ignore the ticket and proceed.
745 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200746 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200749 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200750 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751
752 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
753
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200754 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200755
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200756 /* Don't send a new ticket after all, this one is OK */
757 ssl->handshake->new_session_ticket = 0;
758
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200759 return( 0 );
760}
Paul Bakkera503a632013-08-14 13:48:06 +0200761#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200762
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200763#if defined(POLARSSL_SSL_ALPN)
764static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200765 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200766{
Paul Bakker14b16c62014-05-28 11:33:54 +0200767 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768 const unsigned char *theirs, *start, *end;
769 const char **ours;
770
771 /* If ALPN not configured, just ignore the extension */
772 if( ssl->alpn_list == NULL )
773 return( 0 );
774
775 /*
776 * opaque ProtocolName<1..2^8-1>;
777 *
778 * struct {
779 * ProtocolName protocol_name_list<2..2^16-1>
780 * } ProtocolNameList;
781 */
782
783 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
784 if( len < 4 )
785 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
786
787 list_len = ( buf[0] << 8 ) | buf[1];
788 if( list_len != len - 2 )
789 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
790
791 /*
792 * Use our order of preference
793 */
794 start = buf + 2;
795 end = buf + len;
796 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
797 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200798 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200799 for( theirs = start; theirs != end; theirs += cur_len )
800 {
801 /* If the list is well formed, we should get equality first */
802 if( theirs > end )
803 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
804
805 cur_len = *theirs++;
806
807 /* Empty strings MUST NOT be included */
808 if( cur_len == 0 )
809 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
810
Paul Bakker14b16c62014-05-28 11:33:54 +0200811 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200812 memcmp( theirs, *ours, cur_len ) == 0 )
813 {
814 ssl->alpn_chosen = *ours;
815 return( 0 );
816 }
817 }
818 }
819
820 /* If we get there, no match was found */
821 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
822 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
823 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
824}
825#endif /* POLARSSL_SSL_ALPN */
826
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100827/*
828 * Auxiliary functions for ServerHello parsing and related actions
829 */
830
831#if defined(POLARSSL_X509_CRT_PARSE_C)
832/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100833 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100834 */
835#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100836static int ssl_check_key_curve( pk_context *pk,
837 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100838{
839 const ecp_curve_info **crv = curves;
840 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
841
842 while( *crv != NULL )
843 {
844 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100845 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100846 crv++;
847 }
848
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100849 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100850}
851#endif /* POLARSSL_ECDSA_C */
852
853/*
854 * Try picking a certificate for this ciphersuite,
855 * return 0 on success and -1 on failure.
856 */
857static int ssl_pick_cert( ssl_context *ssl,
858 const ssl_ciphersuite_t * ciphersuite_info )
859{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100860 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100861 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
862
863#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
864 if( ssl->handshake->sni_key_cert != NULL )
865 list = ssl->handshake->sni_key_cert;
866 else
867#endif
868 list = ssl->handshake->key_cert;
869
870 if( pk_alg == POLARSSL_PK_NONE )
871 return( 0 );
872
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000873 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
874
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100875 for( cur = list; cur != NULL; cur = cur->next )
876 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000877 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
878 cur->cert );
879
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100880 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000881 {
882 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000884 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100885
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200886 /*
887 * This avoids sending the client a cert it'll reject based on
888 * keyUsage or other extensions.
889 *
890 * It also allows the user to provision different certificates for
891 * different uses based on keyUsage, eg if they want to avoid signing
892 * and decrypting with the same RSA key.
893 */
894 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
895 SSL_IS_SERVER ) != 0 )
896 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000897 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
898 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200899 continue;
900 }
901
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100903 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100904 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000905 {
906 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100907 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000908 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100909#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100910
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100911 /*
912 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
913 * present them a SHA-higher cert rather than failing if it's the only
914 * one we got that satisfies the other conditions.
915 */
916 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
917 cur->cert->sig_md != POLARSSL_MD_SHA1 )
918 {
919 if( fallback == NULL )
920 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000921 {
922 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
923 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100924 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000925 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100926 }
927
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100928 /* If we get there, we got a winner */
929 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930 }
931
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000932 if( cur == NULL )
933 cur = fallback;
934
935
936 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100937 if( cur != NULL )
938 {
939 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000940 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
941 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100942 return( 0 );
943 }
944
945 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100946}
947#endif /* POLARSSL_X509_CRT_PARSE_C */
948
949/*
950 * Check if a given ciphersuite is suitable for use with our config/keys/etc
951 * Sets ciphersuite_info only if the suite matches.
952 */
953static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
954 const ssl_ciphersuite_t **ciphersuite_info )
955{
956 const ssl_ciphersuite_t *suite_info;
957
958 suite_info = ssl_ciphersuite_from_id( suite_id );
959 if( suite_info == NULL )
960 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100961 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963 }
964
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
966
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967 if( suite_info->min_minor_ver > ssl->minor_ver ||
968 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000969 {
970 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100971 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000972 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100973
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100974#if defined(POLARSSL_SSL_PROTO_DTLS)
975 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
976 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
977 return( 0 );
978#endif
979
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100980 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
981 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000982 {
983 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100984 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100986
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100987#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
988 if( ssl_ciphersuite_uses_ec( suite_info ) &&
989 ( ssl->handshake->curves == NULL ||
990 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
992 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
993 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100994 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000995 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996#endif
997
998#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
999 /* If the ciphersuite requires a pre-shared key and we don't
1000 * have one, skip it now rather than failing later */
1001 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1002 ssl->f_psk == NULL &&
1003 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1004 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 {
1006 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001007 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001008 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001009#endif
1010
1011#if defined(POLARSSL_X509_CRT_PARSE_C)
1012 /*
1013 * Final check: if ciphersuite requires us to have a
1014 * certificate/key of a particular type:
1015 * - select the appropriate certificate if we have one, or
1016 * - try the next ciphersuite if we don't
1017 * This must be done last since we modify the key_cert list.
1018 */
1019 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001020 {
1021 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1022 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001023 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001024 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025#endif
1026
1027 *ciphersuite_info = suite_info;
1028 return( 0 );
1029}
1030
Paul Bakker78a8c712013-03-06 17:01:52 +01001031#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1032static int ssl_parse_client_hello_v2( ssl_context *ssl )
1033{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001034 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001035 unsigned int i, j;
1036 size_t n;
1037 unsigned int ciph_len, sess_len, chal_len;
1038 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001039 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001040 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001041
1042 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1043
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001044#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001045 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1046 {
1047 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1048
1049 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1050 return( ret );
1051
1052 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1053 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001054#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001055
1056 buf = ssl->in_hdr;
1057
1058 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1059
1060 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1061 buf[2] ) );
1062 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1063 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1064 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1065 buf[3], buf[4] ) );
1066
1067 /*
1068 * SSLv2 Client Hello
1069 *
1070 * Record layer:
1071 * 0 . 1 message length
1072 *
1073 * SSL layer:
1074 * 2 . 2 message type
1075 * 3 . 4 protocol version
1076 */
1077 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1078 buf[3] != SSL_MAJOR_VERSION_3 )
1079 {
1080 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1082 }
1083
1084 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1085
1086 if( n < 17 || n > 512 )
1087 {
1088 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1090 }
1091
1092 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001093 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1094 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001095
1096 if( ssl->minor_ver < ssl->min_minor_ver )
1097 {
1098 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001099 " [%d:%d] < [%d:%d]",
1100 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001101 ssl->min_major_ver, ssl->min_minor_ver ) );
1102
1103 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1104 SSL_ALERT_MSG_PROTOCOL_VERSION );
1105 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1106 }
1107
Paul Bakker2fbefde2013-06-29 16:01:15 +02001108 ssl->handshake->max_major_ver = buf[3];
1109 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001110
1111 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1112 {
1113 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1114 return( ret );
1115 }
1116
1117 ssl->handshake->update_checksum( ssl, buf + 2, n );
1118
1119 buf = ssl->in_msg;
1120 n = ssl->in_left - 5;
1121
1122 /*
1123 * 0 . 1 ciphersuitelist length
1124 * 2 . 3 session id length
1125 * 4 . 5 challenge length
1126 * 6 . .. ciphersuitelist
1127 * .. . .. session id
1128 * .. . .. challenge
1129 */
1130 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1131
1132 ciph_len = ( buf[0] << 8 ) | buf[1];
1133 sess_len = ( buf[2] << 8 ) | buf[3];
1134 chal_len = ( buf[4] << 8 ) | buf[5];
1135
1136 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1137 ciph_len, sess_len, chal_len ) );
1138
1139 /*
1140 * Make sure each parameter length is valid
1141 */
1142 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1143 {
1144 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1145 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1146 }
1147
1148 if( sess_len > 32 )
1149 {
1150 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1152 }
1153
1154 if( chal_len < 8 || chal_len > 32 )
1155 {
1156 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1157 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1158 }
1159
1160 if( n != 6 + ciph_len + sess_len + chal_len )
1161 {
1162 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1163 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1164 }
1165
1166 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1167 buf + 6, ciph_len );
1168 SSL_DEBUG_BUF( 3, "client hello, session id",
1169 buf + 6 + ciph_len, sess_len );
1170 SSL_DEBUG_BUF( 3, "client hello, challenge",
1171 buf + 6 + ciph_len + sess_len, chal_len );
1172
1173 p = buf + 6 + ciph_len;
1174 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001175 memset( ssl->session_negotiate->id, 0,
1176 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001177 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1178
1179 p += sess_len;
1180 memset( ssl->handshake->randbytes, 0, 64 );
1181 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1182
1183 /*
1184 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1185 */
1186 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1187 {
1188 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1189 {
1190 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001191#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001192 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001193 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001194 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1195 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001196
1197 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1198 return( ret );
1199
1200 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1201 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001202#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001203 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1204 break;
1205 }
1206 }
1207
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001208#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1209 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1210 {
1211 if( p[0] == 0 &&
Manuel Pégourié-Gonnarded99d702015-03-09 11:17:30 +00001212 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1213 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001214 {
1215 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1216
1217 if( ssl->minor_ver < ssl->max_minor_ver )
1218 {
1219 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1220
1221 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1222 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1223
1224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1225 }
1226
1227 break;
1228 }
1229 }
1230#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1231
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001232 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001233 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001234 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001235#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1236 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1237 {
1238 for( i = 0; ciphersuites[i] != 0; i++ )
1239#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001240 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001241 {
1242 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001243#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001244 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001245 if( p[0] != 0 ||
1246 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1247 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1248 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001249
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001250 got_common_suite = 1;
1251
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001252 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1253 &ciphersuite_info ) ) != 0 )
1254 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001255
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001256 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001257 goto have_ciphersuite_v2;
1258 }
1259 }
1260
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001261 if( got_common_suite )
1262 {
1263 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1264 "but none of them usable" ) );
1265 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1266 }
1267 else
1268 {
1269 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1270 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1271 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001272
1273have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001274 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1275
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001276 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001277 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001278 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001279
1280 /*
1281 * SSLv2 Client Hello relevant renegotiation security checks
1282 */
1283 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1284 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1285 {
1286 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1287
1288 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1289 return( ret );
1290
1291 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1292 }
1293
1294 ssl->in_left = 0;
1295 ssl->state++;
1296
1297 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1298
1299 return( 0 );
1300}
1301#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303static int ssl_parse_client_hello( ssl_context *ssl )
1304{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001305 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001306 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001307 unsigned int ciph_offset, comp_offset, ext_offset;
1308 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001309#if defined(POLARSSL_SSL_PROTO_DTLS)
1310 unsigned int cookie_offset, cookie_len;
1311#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001312 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001313#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001314 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001315#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001316 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001317 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001318 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001319 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1322
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001323#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1324read_record_header:
1325#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001326 /*
1327 * If renegotiating, then the input was read with ssl_read_record(),
1328 * otherwise read it ourselves manually in order to support SSLv2
1329 * ClientHello, which doesn't use the same record layer format.
1330 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001331#if defined(POLARSSL_SSL_RENEGOTIATION)
1332 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001335 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1336 {
1337 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1338 return( ret );
1339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
1342 buf = ssl->in_hdr;
1343
Paul Bakker78a8c712013-03-06 17:01:52 +01001344#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001345#if defined(POLARSSL_SSL_PROTO_DTLS)
1346 if( ssl->transport == SSL_TRANSPORT_STREAM )
1347#endif
1348 if( ( buf[0] & 0x80 ) != 0 )
1349 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001350#endif
1351
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001352 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001353
Paul Bakkerec636f32012-09-09 19:17:02 +00001354 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001355 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001356 *
1357 * Record layer:
1358 * 0 . 0 message type
1359 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001360 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001361 * 3 . 4 message length
1362 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001363 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1364 buf[0] ) );
1365
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001366 if( buf[0] != SSL_MSG_HANDSHAKE )
1367 {
1368 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1369 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1370 }
1371
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001372 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1373 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1374
1375 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1376 buf[1], buf[2] ) );
1377
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001378 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001379
1380 /* According to RFC 5246 Appendix E.1, the version here is typically
1381 * "{03,00}, the lowest version number supported by the client, [or] the
1382 * value of ClientHello.client_version", so the only meaningful check here
1383 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001384 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001386 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1387 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001390 /* For DTLS if this is the initial handshake, remember the client sequence
1391 * number to use it in our next message (RFC 6347 4.2.1) */
1392#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001393 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1394#if defined(POLARSSL_SSL_RENEGOTIATION)
1395 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1396#endif
1397 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001398 {
1399 /* Epoch should be 0 for initial handshakes */
1400 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1401 {
1402 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1404 }
1405
1406 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001407
1408#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1409 if( ssl_dtls_replay_check( ssl ) != 0 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1412 ssl->next_record_offset = 0;
1413 ssl->in_left = 0;
1414 goto read_record_header;
1415 }
1416
1417 /* No MAC to check yet, so we can update right now */
1418 ssl_dtls_replay_update( ssl );
1419#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001420 }
1421#endif /* POLARSSL_SSL_PROTO_DTLS */
1422
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001423 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001425#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001426 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1427 {
1428 /* Set by ssl_read_record() */
1429 msg_len = ssl->in_hslen;
1430 }
1431 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001432#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001433 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001434 if( msg_len > SSL_MAX_CONTENT_LEN )
1435 {
1436 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1438 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001439
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001440 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1441 {
1442 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1443 return( ret );
1444 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001445
1446 /* Done reading this record, get ready for the next one */
1447#if defined(POLARSSL_SSL_PROTO_DTLS)
1448 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1449 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1450 else
1451#endif
1452 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001453 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001454
1455 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001456
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001457 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001458
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001459 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001460
1461 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001462 * Handshake layer:
1463 * 0 . 0 handshake type
1464 * 1 . 3 handshake length
1465 * 4 . 5 DTLS only: message seqence number
1466 * 6 . 8 DTLS only: fragment offset
1467 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001468 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001469 if( msg_len < ssl_hs_hdr_len( ssl ) )
1470 {
1471 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1472 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1473 }
1474
1475 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1476
1477 if( buf[0] != SSL_HS_CLIENT_HELLO )
1478 {
1479 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1481 }
1482
1483 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1484 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1485
1486 /* We don't support fragmentation of ClientHello (yet?) */
1487 if( buf[1] != 0 ||
1488 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1489 {
1490 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1492 }
1493
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001494#if defined(POLARSSL_SSL_PROTO_DTLS)
1495 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1496 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001497 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001498 * Copy the client's handshake message_seq on initial handshakes,
1499 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001500 */
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001501#if defined(POLARSSL_SSL_RENEGOTIATION)
1502 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001503 {
1504 /* This couldn't be done in ssl_prepare_handshake_record() */
1505 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1506 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001507
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001508 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1509 {
1510 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1511 "%d (expected %d)", cli_msg_seq,
1512 ssl->handshake->in_msg_seq ) );
1513 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1514 }
1515
1516 ssl->handshake->in_msg_seq++;
1517 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001518 else
1519#endif
1520 {
1521 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1522 ssl->in_msg[5];
1523 ssl->handshake->out_msg_seq = cli_msg_seq;
1524 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1525 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001526
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001527 /*
1528 * For now we don't support fragmentation, so make sure
1529 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001530 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001531 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1532 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1533 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001534 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001535 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1536 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001537 }
1538#endif /* POLARSSL_SSL_PROTO_DTLS */
1539
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001540 buf += ssl_hs_hdr_len( ssl );
1541 msg_len -= ssl_hs_hdr_len( ssl );
1542
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001543 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001544 * ClientHello layer:
1545 * 0 . 1 protocol version
1546 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1547 * 34 . 35 session id length (1 byte)
1548 * 35 . 34+x session id
1549 * 35+x . 35+x DTLS only: cookie length (1 byte)
1550 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001551 * .. . .. ciphersuite list length (2 bytes)
1552 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001553 * .. . .. compression alg. list length (1 byte)
1554 * .. . .. compression alg. list
1555 * .. . .. extensions length (2 bytes, optional)
1556 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001557 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001558
1559 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001560 * Minimal length (with everything empty and extensions ommitted) is
1561 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1562 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001563 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001564 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001565 {
1566 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1567 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1568 }
1569
1570 /*
1571 * Check and save the protocol version
1572 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001574
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001575 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001576 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001577
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001578 ssl->handshake->max_major_ver = ssl->major_ver;
1579 ssl->handshake->max_minor_ver = ssl->minor_ver;
1580
1581 if( ssl->major_ver < ssl->min_major_ver ||
1582 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001583 {
1584 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001585 " [%d:%d] < [%d:%d]",
1586 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001587 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001588
1589 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1590 SSL_ALERT_MSG_PROTOCOL_VERSION );
1591
1592 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1593 }
1594
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001595 if( ssl->major_ver > ssl->max_major_ver )
1596 {
1597 ssl->major_ver = ssl->max_major_ver;
1598 ssl->minor_ver = ssl->max_minor_ver;
1599 }
1600 else if( ssl->minor_ver > ssl->max_minor_ver )
1601 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001602
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001603 /*
1604 * Save client random (inc. Unix time)
1605 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001606 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001607
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001608 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001609
1610 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001611 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001612 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001613 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001614
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001615 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001616 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001617 {
1618 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1619 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1620 }
1621
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001622 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001623
Paul Bakker48916f92012-09-16 19:57:18 +00001624 ssl->session_negotiate->length = sess_len;
1625 memset( ssl->session_negotiate->id, 0,
1626 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001627 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001628 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001629
1630 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001631 * Check the cookie length and content
1632 */
1633#if defined(POLARSSL_SSL_PROTO_DTLS)
1634 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1635 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001636 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001637 cookie_len = buf[cookie_offset];
1638
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001639 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001640 {
1641 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1642 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1643 }
1644
1645 SSL_DEBUG_BUF( 3, "client hello, cookie",
1646 buf + cookie_offset + 1, cookie_len );
1647
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001648#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001649 if( ssl->f_cookie_check != NULL
1650#if defined(POLARSSL_SSL_RENEGOTIATION)
1651 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1652#endif
1653 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001654 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001655 if( ssl->f_cookie_check( ssl->p_cookie,
1656 buf + cookie_offset + 1, cookie_len,
1657 ssl->cli_id, ssl->cli_id_len ) != 0 )
1658 {
1659 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1660 ssl->handshake->verify_cookie_len = 1;
1661 }
1662 else
1663 {
1664 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1665 ssl->handshake->verify_cookie_len = 0;
1666 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001667 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001668 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001669#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001670 {
1671 /* We know we didn't send a cookie, so it should be empty */
1672 if( cookie_len != 0 )
1673 {
1674 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1675 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1676 }
1677
1678 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1679 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001680 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001681#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001682
1683 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001684 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001685 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001686#if defined(POLARSSL_SSL_PROTO_DTLS)
1687 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1688 ciph_offset = cookie_offset + 1 + cookie_len;
1689 else
1690#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001691 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001692
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001693 ciph_len = ( buf[ciph_offset + 0] << 8 )
1694 | ( buf[ciph_offset + 1] );
1695
1696 if( ciph_len < 2 ||
1697 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1698 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001699 {
1700 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1701 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1702 }
1703
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001704 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1705 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001706
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001707 /*
1708 * Check the compression algorithms length and pick one
1709 */
1710 comp_offset = ciph_offset + 2 + ciph_len;
1711
1712 comp_len = buf[comp_offset];
1713
1714 if( comp_len < 1 ||
1715 comp_len > 16 ||
1716 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001717 {
1718 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1719 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1720 }
1721
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001722 SSL_DEBUG_BUF( 3, "client hello, compression",
1723 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001724
1725 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001726#if defined(POLARSSL_ZLIB_SUPPORT)
1727 for( i = 0; i < comp_len; ++i )
1728 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001729 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 {
Paul Bakker48916f92012-09-16 19:57:18 +00001731 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001732 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001733 }
1734 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001735#endif
1736
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001737 /* See comments in ssl_write_client_hello() */
1738#if defined(POLARSSL_SSL_PROTO_DTLS)
1739 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1740 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1741#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001742
Paul Bakkerec636f32012-09-09 19:17:02 +00001743 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001744 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001745 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001746 ext_offset = comp_offset + 1 + comp_len;
1747 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001748 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001749 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001750 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001751 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1752 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1753 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001754
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001755 ext_len = ( buf[ext_offset + 0] << 8 )
1756 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001757
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001758 if( ( ext_len > 0 && ext_len < 4 ) ||
1759 msg_len != ext_offset + 2 + ext_len )
1760 {
1761 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1762 SSL_DEBUG_BUF( 3, "client hello extensions",
1763 buf + ext_offset + 2, ext_len );
1764 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001765 }
1766 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001767 else
1768 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001769
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001770 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001771
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001772 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001773 {
1774 unsigned int ext_id = ( ( ext[0] << 8 )
1775 | ( ext[1] ) );
1776 unsigned int ext_size = ( ( ext[2] << 8 )
1777 | ( ext[3] ) );
1778
1779 if( ext_size + 4 > ext_len )
1780 {
1781 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1782 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1783 }
1784 switch( ext_id )
1785 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001786#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001787 case TLS_EXT_SERVERNAME:
1788 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1789 if( ssl->f_sni == NULL )
1790 break;
1791
1792 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1793 if( ret != 0 )
1794 return( ret );
1795 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001796#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001797
Paul Bakker48916f92012-09-16 19:57:18 +00001798 case TLS_EXT_RENEGOTIATION_INFO:
1799 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001800#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001801 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001802#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001803
Paul Bakker23f36802012-09-28 14:15:14 +00001804 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1805 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001806 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001807 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001808
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001809#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1810 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001811 case TLS_EXT_SIG_ALG:
1812 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001813#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001814 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker23f36802012-09-28 14:15:14 +00001815 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001816#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001817
1818 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1819 if( ret != 0 )
1820 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001821 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001822#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1823 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001824
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001825#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001826 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1827 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1828
1829 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1830 if( ret != 0 )
1831 return( ret );
1832 break;
1833
1834 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1835 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001836 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001837
1838 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1839 if( ret != 0 )
1840 return( ret );
1841 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001842#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001843
Paul Bakker05decb22013-08-15 13:33:48 +02001844#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001845 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1846 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1847
1848 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1849 if( ret != 0 )
1850 return( ret );
1851 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001852#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001853
Paul Bakker1f2bc622013-08-15 13:45:55 +02001854#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001855 case TLS_EXT_TRUNCATED_HMAC:
1856 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1857
1858 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1859 if( ret != 0 )
1860 return( ret );
1861 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001862#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001863
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001864#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1865 case TLS_EXT_ENCRYPT_THEN_MAC:
1866 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1867
1868 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1869 if( ret != 0 )
1870 return( ret );
1871 break;
1872#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1873
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001874#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1875 case TLS_EXT_EXTENDED_MASTER_SECRET:
1876 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1877
1878 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1879 if( ret != 0 )
1880 return( ret );
1881 break;
1882#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1883
Paul Bakkera503a632013-08-14 13:48:06 +02001884#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001885 case TLS_EXT_SESSION_TICKET:
1886 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1887
1888 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1889 if( ret != 0 )
1890 return( ret );
1891 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001892#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001893
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001894#if defined(POLARSSL_SSL_ALPN)
1895 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001896 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001897
1898 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1899 if( ret != 0 )
1900 return( ret );
1901 break;
1902#endif /* POLARSSL_SSL_SESSION_TICKETS */
1903
Paul Bakker48916f92012-09-16 19:57:18 +00001904 default:
1905 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1906 ext_id ) );
1907 }
1908
1909 ext_len -= 4 + ext_size;
1910 ext += 4 + ext_size;
1911
1912 if( ext_len > 0 && ext_len < 4 )
1913 {
1914 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1915 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1916 }
1917 }
1918
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001919#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1920 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1921 {
Manuel Pégourié-Gonnarded99d702015-03-09 11:17:30 +00001922 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1923 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001924 {
1925 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1926
1927 if( ssl->minor_ver < ssl->max_minor_ver )
1928 {
1929 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1930
1931 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1932 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1933
1934 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1935 }
1936
1937 break;
1938 }
1939 }
1940#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1941
Paul Bakker48916f92012-09-16 19:57:18 +00001942 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001943 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1944 */
1945 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1946 {
1947 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1948 {
1949 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001950#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001951 if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001952 {
1953 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1954
1955 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1956 return( ret );
1957
1958 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1959 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001960#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001961 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1962 break;
1963 }
1964 }
1965
1966 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001967 * Renegotiation security checks
1968 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001969 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001970 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1971 {
1972 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1973 handshake_failure = 1;
1974 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001975#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001976 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001977 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1978 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001979 {
1980 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001981 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001982 }
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001983 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001984 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1985 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001986 {
1987 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001988 handshake_failure = 1;
1989 }
Manuel Pégourié-Gonnard2f5a1b42015-03-09 11:12:32 +00001990 else if( ssl->renegotiation == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001991 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1992 renegotiation_info_seen == 1 )
1993 {
1994 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1995 handshake_failure = 1;
1996 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001997#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001998
1999 if( handshake_failure == 1 )
2000 {
2001 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
2002 return( ret );
2003
Paul Bakker48916f92012-09-16 19:57:18 +00002004 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
2005 }
Paul Bakker380da532012-04-18 16:10:25 +00002006
Paul Bakker41c83d32013-03-20 14:39:14 +01002007 /*
2008 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002009 * (At the end because we need information from the EC-based extensions
2010 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002011 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002012 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002013 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002014 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002015#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002016 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002017 {
2018 for( i = 0; ciphersuites[i] != 0; i++ )
2019#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002020 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01002021 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002022 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002023#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002024 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002025 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2026 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2027 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002028
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002029 got_common_suite = 1;
2030
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002031 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2032 &ciphersuite_info ) ) != 0 )
2033 return( ret );
2034
2035 if( ciphersuite_info != NULL )
2036 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002037 }
2038 }
2039
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002040 if( got_common_suite )
2041 {
2042 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2043 "but none of them usable" ) );
2044 ssl_send_fatal_handshake_failure( ssl );
2045 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2046 }
2047 else
2048 {
2049 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2050 ssl_send_fatal_handshake_failure( ssl );
2051 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2052 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002053
2054have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002055 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2056
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002057 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002058 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2059 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2060
Paul Bakker5121ce52009-01-03 21:22:43 +00002061 ssl->state++;
2062
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002063#if defined(POLARSSL_SSL_PROTO_DTLS)
2064 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2065 ssl_recv_flight_completed( ssl );
2066#endif
2067
Paul Bakker5121ce52009-01-03 21:22:43 +00002068 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2069
2070 return( 0 );
2071}
2072
Paul Bakker1f2bc622013-08-15 13:45:55 +02002073#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002074static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2075 unsigned char *buf,
2076 size_t *olen )
2077{
2078 unsigned char *p = buf;
2079
2080 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2081 {
2082 *olen = 0;
2083 return;
2084 }
2085
2086 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2087
2088 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2089 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2090
2091 *p++ = 0x00;
2092 *p++ = 0x00;
2093
2094 *olen = 4;
2095}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002096#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002097
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002098#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2099static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2100 unsigned char *buf,
2101 size_t *olen )
2102{
2103 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002104 const ssl_ciphersuite_t *suite = NULL;
2105 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002106
2107 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2108 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2109 {
2110 *olen = 0;
2111 return;
2112 }
2113
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002114 /*
2115 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2116 * from a client and then selects a stream or Authenticated Encryption
2117 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2118 * encrypt-then-MAC response extension back to the client."
2119 */
2120 if( ( suite = ssl_ciphersuite_from_id(
2121 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2122 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2123 cipher->mode != POLARSSL_MODE_CBC )
2124 {
2125 *olen = 0;
2126 return;
2127 }
2128
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002129 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2130
2131 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2132 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2133
2134 *p++ = 0x00;
2135 *p++ = 0x00;
2136
2137 *olen = 4;
2138}
2139#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2140
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002141#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2142static void ssl_write_extended_ms_ext( ssl_context *ssl,
2143 unsigned char *buf,
2144 size_t *olen )
2145{
2146 unsigned char *p = buf;
2147
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002148 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2149 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002150 {
2151 *olen = 0;
2152 return;
2153 }
2154
2155 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2156 "extension" ) );
2157
2158 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2159 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2160
2161 *p++ = 0x00;
2162 *p++ = 0x00;
2163
2164 *olen = 4;
2165}
2166#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2167
Paul Bakkera503a632013-08-14 13:48:06 +02002168#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002169static void ssl_write_session_ticket_ext( ssl_context *ssl,
2170 unsigned char *buf,
2171 size_t *olen )
2172{
2173 unsigned char *p = buf;
2174
2175 if( ssl->handshake->new_session_ticket == 0 )
2176 {
2177 *olen = 0;
2178 return;
2179 }
2180
2181 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2182
2183 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2184 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2185
2186 *p++ = 0x00;
2187 *p++ = 0x00;
2188
2189 *olen = 4;
2190}
Paul Bakkera503a632013-08-14 13:48:06 +02002191#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002192
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002193static void ssl_write_renegotiation_ext( ssl_context *ssl,
2194 unsigned char *buf,
2195 size_t *olen )
2196{
2197 unsigned char *p = buf;
2198
2199 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2200 {
2201 *olen = 0;
2202 return;
2203 }
2204
2205 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2206
2207 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2208 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2209
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002210#if defined(POLARSSL_SSL_RENEGOTIATION)
2211 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2212 {
2213 *p++ = 0x00;
2214 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2215 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002216
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002217 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2218 p += ssl->verify_data_len;
2219 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2220 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002221
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002222 *olen = 5 + ssl->verify_data_len * 2;
2223 }
2224 else
2225#endif /* POLARSSL_SSL_RENEGOTIATION */
2226 {
2227 *p++ = 0x00;
2228 *p++ = 0x01;
2229 *p++ = 0x00;
2230
2231 *olen = 5;
2232 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002233}
2234
Paul Bakker05decb22013-08-15 13:33:48 +02002235#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002236static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2237 unsigned char *buf,
2238 size_t *olen )
2239{
2240 unsigned char *p = buf;
2241
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002242 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2243 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002244 *olen = 0;
2245 return;
2246 }
2247
2248 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2249
2250 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2251 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2252
2253 *p++ = 0x00;
2254 *p++ = 1;
2255
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002256 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002257
2258 *olen = 5;
2259}
Paul Bakker05decb22013-08-15 13:33:48 +02002260#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002261
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002262#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002263static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2264 unsigned char *buf,
2265 size_t *olen )
2266{
2267 unsigned char *p = buf;
2268 ((void) ssl);
2269
Paul Bakker677377f2013-10-28 12:54:26 +01002270 if( ( ssl->handshake->cli_exts &
2271 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2272 {
2273 *olen = 0;
2274 return;
2275 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002276
2277 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2278
2279 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2280 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2281
2282 *p++ = 0x00;
2283 *p++ = 2;
2284
2285 *p++ = 1;
2286 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2287
2288 *olen = 6;
2289}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002290#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002291
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002292#if defined(POLARSSL_SSL_ALPN )
2293static void ssl_write_alpn_ext( ssl_context *ssl,
2294 unsigned char *buf, size_t *olen )
2295{
2296 if( ssl->alpn_chosen == NULL )
2297 {
2298 *olen = 0;
2299 return;
2300 }
2301
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002302 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002303
2304 /*
2305 * 0 . 1 ext identifier
2306 * 2 . 3 ext length
2307 * 4 . 5 protocol list length
2308 * 6 . 6 protocol name length
2309 * 7 . 7+n protocol name
2310 */
2311 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2312 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2313
2314 *olen = 7 + strlen( ssl->alpn_chosen );
2315
2316 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2317 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2318
2319 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2320 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2321
2322 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2323
2324 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2325}
2326#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2327
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002328#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002329static int ssl_write_hello_verify_request( ssl_context *ssl )
2330{
2331 int ret;
2332 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002333 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002334
2335 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2336
2337 /*
2338 * struct {
2339 * ProtocolVersion server_version;
2340 * opaque cookie<0..2^8-1>;
2341 * } HelloVerifyRequest;
2342 */
2343
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002344 /* The RFC is not clear on this point, but sending the actual negotiated
2345 * version looks like the most interoperable thing to do. */
2346 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002347 ssl->transport, p );
2348 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2349 p += 2;
2350
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002351 /* If we get here, f_cookie_check is not null */
2352 if( ssl->f_cookie_write == NULL )
2353 {
2354 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2355 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2356 }
2357
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002358 /* Skip length byte until we know the length */
2359 cookie_len_byte = p++;
2360
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002361 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2362 &p, ssl->out_buf + SSL_BUFFER_LEN,
2363 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002364 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002365 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002366 return( ret );
2367 }
2368
2369 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2370
2371 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002372
2373 ssl->out_msglen = p - ssl->out_msg;
2374 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2375 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2376
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002377 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002378
2379 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2380 {
2381 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2382 return( ret );
2383 }
2384
2385 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2386
2387 return( 0 );
2388}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002389#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002390
Paul Bakker5121ce52009-01-03 21:22:43 +00002391static int ssl_write_server_hello( ssl_context *ssl )
2392{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002393#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002395#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002396 int ret;
2397 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 unsigned char *buf, *p;
2399
2400 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2401
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002402#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002403 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002404 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002405 {
2406 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2407 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2408
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002409 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002410 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002411#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002412
Paul Bakkera9a028e2013-11-21 17:31:06 +01002413 if( ssl->f_rng == NULL )
2414 {
2415 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2416 return( POLARSSL_ERR_SSL_NO_RNG );
2417 }
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 /*
2420 * 0 . 0 handshake type
2421 * 1 . 3 handshake length
2422 * 4 . 5 protocol version
2423 * 6 . 9 UNIX time()
2424 * 10 . 37 random bytes
2425 */
2426 buf = ssl->out_msg;
2427 p = buf + 4;
2428
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002429 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2430 ssl->transport, p );
2431 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002434 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002435
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002436#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002437 t = time( NULL );
2438 *p++ = (unsigned char)( t >> 24 );
2439 *p++ = (unsigned char)( t >> 16 );
2440 *p++ = (unsigned char)( t >> 8 );
2441 *p++ = (unsigned char)( t );
2442
2443 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002444#else
2445 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2446 return( ret );
2447
2448 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002449#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002450
Paul Bakkera3d195c2011-11-27 21:07:34 +00002451 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2452 return( ret );
2453
2454 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002455
Paul Bakker48916f92012-09-16 19:57:18 +00002456 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
2458 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2459
2460 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002461 * Resume is 0 by default, see ssl_handshake_init().
2462 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2463 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002464 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002465 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002466#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002467 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002468#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002469 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002470 ssl->f_get_cache != NULL &&
2471 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2472 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002473 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002474 ssl->handshake->resume = 1;
2475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002476
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002477 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002478 {
2479 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002480 * New session, create a new session id,
2481 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002482 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002483 ssl->state++;
2484
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002485#if defined(POLARSSL_HAVE_TIME)
2486 ssl->session_negotiate->start = time( NULL );
2487#endif
2488
Paul Bakkera503a632013-08-14 13:48:06 +02002489#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002490 if( ssl->handshake->new_session_ticket != 0 )
2491 {
2492 ssl->session_negotiate->length = n = 0;
2493 memset( ssl->session_negotiate->id, 0, 32 );
2494 }
2495 else
2496#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002497 {
2498 ssl->session_negotiate->length = n = 32;
2499 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002500 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002501 return( ret );
2502 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002503 }
2504 else
2505 {
2506 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002507 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002509 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002510 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002511
2512 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2513 {
2514 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2515 return( ret );
2516 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002517 }
2518
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002519 /*
2520 * 38 . 38 session id length
2521 * 39 . 38+n session id
2522 * 39+n . 40+n chosen ciphersuite
2523 * 41+n . 41+n chosen compression alg.
2524 * 42+n . 43+n extensions length
2525 * 44+n . 43+n+m extensions
2526 */
2527 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002528 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2529 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
2531 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2532 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2533 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002534 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002535
Paul Bakker48916f92012-09-16 19:57:18 +00002536 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2537 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2538 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002540 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2541 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002542 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002543 ssl->session_negotiate->compression ) );
2544
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002545 /*
2546 * First write extensions, then the total length
2547 */
2548 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2549 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002550
Paul Bakker05decb22013-08-15 13:33:48 +02002551#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002552 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2553 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002554#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002555
Paul Bakker1f2bc622013-08-15 13:45:55 +02002556#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002557 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2558 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002559#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002560
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002561#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2562 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2563 ext_len += olen;
2564#endif
2565
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002566#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2567 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2568 ext_len += olen;
2569#endif
2570
Paul Bakkera503a632013-08-14 13:48:06 +02002571#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002572 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2573 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002574#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002575
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002576#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002577 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2578 ext_len += olen;
2579#endif
2580
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002581#if defined(POLARSSL_SSL_ALPN)
2582 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2583 ext_len += olen;
2584#endif
2585
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002586 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002587
Paul Bakkera7036632014-04-30 10:15:38 +02002588 if( ext_len > 0 )
2589 {
2590 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2591 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2592 p += ext_len;
2593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594
2595 ssl->out_msglen = p - buf;
2596 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2597 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2598
2599 ret = ssl_write_record( ssl );
2600
2601 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2602
2603 return( ret );
2604}
2605
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002606#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2607 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002608 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2609 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002610static int ssl_write_certificate_request( ssl_context *ssl )
2611{
Paul Bakkered27a042013-04-18 22:46:23 +02002612 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002613
2614 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2615
2616 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002617 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002618 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2619 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002620 {
2621 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2622 ssl->state++;
2623 return( 0 );
2624 }
2625
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002626 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2627 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002628}
2629#else
2630static int ssl_write_certificate_request( ssl_context *ssl )
2631{
2632 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2633 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002634 size_t dn_size, total_dn_size; /* excluding length bytes */
2635 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002636 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002637 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002638
2639 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2640
2641 ssl->state++;
2642
Paul Bakkerfbb17802013-04-17 19:10:21 +02002643 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002644 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002645 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002647 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002648 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002649 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002650 return( 0 );
2651 }
2652
2653 /*
2654 * 0 . 0 handshake type
2655 * 1 . 3 handshake length
2656 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002657 * 5 .. m-1 cert types
2658 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002659 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002660 * n .. n+1 length of all DNs
2661 * n+2 .. n+3 length of DN 1
2662 * n+4 .. ... Distinguished Name #1
2663 * ... .. ... length of DN 2, etc.
2664 */
2665 buf = ssl->out_msg;
2666 p = buf + 4;
2667
2668 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002669 * Supported certificate types
2670 *
2671 * ClientCertificateType certificate_types<1..2^8-1>;
2672 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002673 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002674 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002675
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002676#if defined(POLARSSL_RSA_C)
2677 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2678#endif
2679#if defined(POLARSSL_ECDSA_C)
2680 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2681#endif
2682
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002683 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002684 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002685
Paul Bakker577e0062013-08-28 11:57:20 +02002686 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002687#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002688 /*
2689 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002690 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002691 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2692 *
2693 * struct {
2694 * HashAlgorithm hash;
2695 * SignatureAlgorithm signature;
2696 * } SignatureAndHashAlgorithm;
2697 *
2698 * enum { (255) } HashAlgorithm;
2699 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002700 */
Paul Bakker21dca692013-01-03 11:41:08 +01002701 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002702 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002703 /*
2704 * Only use current running hash algorithm that is already required
2705 * for requested ciphersuite.
2706 */
Paul Bakker926af752012-11-23 13:38:07 +01002707 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2708
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002709 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2710 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002711 {
2712 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2713 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002714
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002715 /*
2716 * Supported signature algorithms
2717 */
2718#if defined(POLARSSL_RSA_C)
2719 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2720 p[2 + sa_len++] = SSL_SIG_RSA;
2721#endif
2722#if defined(POLARSSL_ECDSA_C)
2723 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2724 p[2 + sa_len++] = SSL_SIG_ECDSA;
2725#endif
Paul Bakker926af752012-11-23 13:38:07 +01002726
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002727 p[0] = (unsigned char)( sa_len >> 8 );
2728 p[1] = (unsigned char)( sa_len );
2729 sa_len += 2;
2730 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002733
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002734 /*
2735 * DistinguishedName certificate_authorities<0..2^16-1>;
2736 * opaque DistinguishedName<1..2^16-1>;
2737 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002738 p += 2;
2739 crt = ssl->ca_chain;
2740
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002741 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002742 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002743 {
2744 if( p - buf > 4096 )
2745 break;
2746
Paul Bakker926af752012-11-23 13:38:07 +01002747 dn_size = crt->subject_raw.len;
2748 *p++ = (unsigned char)( dn_size >> 8 );
2749 *p++ = (unsigned char)( dn_size );
2750 memcpy( p, crt->subject_raw.p, dn_size );
2751 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002752
Paul Bakker926af752012-11-23 13:38:07 +01002753 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2754
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002755 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002756 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 }
2758
Paul Bakker926af752012-11-23 13:38:07 +01002759 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002760 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2761 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002762 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2763 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002764
2765 ret = ssl_write_record( ssl );
2766
2767 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2768
2769 return( ret );
2770}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2772 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002773 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2774 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002775
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002776#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2777 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2778static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2779{
2780 int ret;
2781
2782 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2783 {
2784 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2785 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2786 }
2787
2788 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2789 pk_ec( *ssl_own_key( ssl ) ),
2790 POLARSSL_ECDH_OURS ) ) != 0 )
2791 {
2792 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2793 return( ret );
2794 }
2795
2796 return( 0 );
2797}
2798#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2799 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2800
Paul Bakker41c83d32013-03-20 14:39:14 +01002801static int ssl_write_server_key_exchange( ssl_context *ssl )
2802{
Paul Bakker23986e52011-04-24 08:57:21 +00002803 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002804 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002805 const ssl_ciphersuite_t *ciphersuite_info =
2806 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002807
2808#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2809 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2810 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002811 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002812 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002813 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002814 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002815 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002816 ((void) dig_signed);
2817 ((void) dig_signed_len);
2818#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002819
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2821
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002822#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2823 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2824 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002825 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2826 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2827 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002828 {
2829 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2830 ssl->state++;
2831 return( 0 );
2832 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002833#endif
2834
2835#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2836 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2837 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2838 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2839 {
2840 ssl_get_ecdh_params_from_cert( ssl );
2841
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002842 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002843 ssl->state++;
2844 return( 0 );
2845 }
2846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002848#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2849 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2850 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2851 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002852 {
2853 /* TODO: Support identity hints */
2854 *(p++) = 0x00;
2855 *(p++) = 0x00;
2856
2857 n += 2;
2858 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002859#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2860 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002861
2862#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2863 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2864 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2865 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002866 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002867 /*
2868 * Ephemeral DH parameters:
2869 *
2870 * struct {
2871 * opaque dh_p<1..2^16-1>;
2872 * opaque dh_g<1..2^16-1>;
2873 * opaque dh_Ys<1..2^16-1>;
2874 * } ServerDHParams;
2875 */
2876 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2877 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2878 {
2879 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2880 return( ret );
2881 }
Paul Bakker48916f92012-09-16 19:57:18 +00002882
Paul Bakker41c83d32013-03-20 14:39:14 +01002883 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002884 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2885 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002886 {
2887 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2888 return( ret );
2889 }
2890
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002891 dig_signed = p;
2892 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002893
2894 p += len;
2895 n += len;
2896
Paul Bakker41c83d32013-03-20 14:39:14 +01002897 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2898 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2899 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2900 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2901 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002902#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2903 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002904
Gergely Budai987bfb52014-01-19 21:48:42 +01002905#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002906 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002907 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2908 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002909 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002910 /*
2911 * Ephemeral ECDH parameters:
2912 *
2913 * struct {
2914 * ECParameters curve_params;
2915 * ECPoint public;
2916 * } ServerECDHParams;
2917 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002918 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002919#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002920 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002921
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002922 /* Match our preference list against the offered curves */
2923 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2924 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2925 if( (*curve)->grp_id == *gid )
2926 goto curve_matching_done;
2927
2928curve_matching_done:
2929#else
2930 curve = ssl->handshake->curves;
2931#endif
2932
2933 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002934 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002935 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2936 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002937 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002938
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002939 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002940
Paul Bakker41c83d32013-03-20 14:39:14 +01002941 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002942 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002943 {
2944 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2945 return( ret );
2946 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002947
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002948 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2949 p, SSL_MAX_CONTENT_LEN - n,
2950 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002951 {
2952 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2953 return( ret );
2954 }
2955
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002956 dig_signed = p;
2957 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958
2959 p += len;
2960 n += len;
2961
Paul Bakker41c83d32013-03-20 14:39:14 +01002962 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2963 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002964#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002965
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002967 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2968 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002969 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002972 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002973 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002974 unsigned int hashlen = 0;
2975 unsigned char hash[64];
2976 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002977
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002978 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002979 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2980 */
Paul Bakker577e0062013-08-28 11:57:20 +02002981#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002982 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2983 {
2984 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2985
2986 if( md_alg == POLARSSL_MD_NONE )
2987 {
2988 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002989 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002990 }
2991 }
Paul Bakker577e0062013-08-28 11:57:20 +02002992 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002993#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002994#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2995 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002996 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002997 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2998 {
2999 md_alg = POLARSSL_MD_SHA1;
3000 }
3001 else
Paul Bakker577e0062013-08-28 11:57:20 +02003002#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003003 {
3004 md_alg = POLARSSL_MD_NONE;
3005 }
3006
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003007 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003008 * Compute the hash to be signed
3009 */
Paul Bakker577e0062013-08-28 11:57:20 +02003010#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3011 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003012 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003013 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003014 md5_context md5;
3015 sha1_context sha1;
3016
Paul Bakker5b4af392014-06-26 12:09:34 +02003017 md5_init( &md5 );
3018 sha1_init( &sha1 );
3019
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003020 /*
3021 * digitally-signed struct {
3022 * opaque md5_hash[16];
3023 * opaque sha_hash[20];
3024 * };
3025 *
3026 * md5_hash
3027 * MD5(ClientHello.random + ServerHello.random
3028 * + ServerParams);
3029 * sha_hash
3030 * SHA(ClientHello.random + ServerHello.random
3031 * + ServerParams);
3032 */
3033 md5_starts( &md5 );
3034 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003035 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003036 md5_finish( &md5, hash );
3037
3038 sha1_starts( &sha1 );
3039 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003040 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003041 sha1_finish( &sha1, hash + 16 );
3042
3043 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02003044
3045 md5_free( &md5 );
3046 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003047 }
3048 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003049#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3050 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003051#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3052 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003053 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054 {
3055 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003056 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003057
Paul Bakker84bbeb52014-07-01 14:53:22 +02003058 md_init( &ctx );
3059
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003060 /* Info from md_alg will be used instead */
3061 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003062
3063 /*
3064 * digitally-signed struct {
3065 * opaque client_random[32];
3066 * opaque server_random[32];
3067 * ServerDHParams params;
3068 * };
3069 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003070 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003071 {
3072 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3073 return( ret );
3074 }
3075
3076 md_starts( &ctx );
3077 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003078 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003079 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003080 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003081 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003082 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003083#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3084 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003085 {
3086 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003087 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003088 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003089
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003090 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3091 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003092
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003093 /*
3094 * Make the signature
3095 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003096 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003097 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003098 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3099 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003100 }
Paul Bakker23f36802012-09-28 14:15:14 +00003101
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003102#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003103 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3104 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003105 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003106 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003107
3108 n += 2;
3109 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003110#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003111
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003112 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003113 p + 2 , &signature_len,
3114 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003115 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003116 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003117 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003118 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003119
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003120 *(p++) = (unsigned char)( signature_len >> 8 );
3121 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003122 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003123
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003124 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003125
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003126 p += signature_len;
3127 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003128 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003130 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3131 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003132
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003133 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3135 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3136
3137 ssl->state++;
3138
3139 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3140 {
3141 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3142 return( ret );
3143 }
3144
3145 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3146
3147 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003148}
3149
3150static int ssl_write_server_hello_done( ssl_context *ssl )
3151{
3152 int ret;
3153
3154 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3155
3156 ssl->out_msglen = 4;
3157 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3158 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3159
3160 ssl->state++;
3161
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003162#if defined(POLARSSL_SSL_PROTO_DTLS)
3163 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3164 ssl_send_flight_completed( ssl );
3165#endif
3166
Paul Bakker5121ce52009-01-03 21:22:43 +00003167 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3168 {
3169 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3170 return( ret );
3171 }
3172
3173 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3174
3175 return( 0 );
3176}
3177
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003178#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3179 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3180static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3181 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003182{
3183 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003184 size_t n;
3185
3186 /*
3187 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3188 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003189 if( *p + 2 > end )
3190 {
3191 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3192 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3193 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003194
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195 n = ( (*p)[0] << 8 ) | (*p)[1];
3196 *p += 2;
3197
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003198 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003199 {
3200 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3201 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3202 }
3203
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003204 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003205 {
3206 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3207 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3208 }
3209
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003210 *p += n;
3211
Paul Bakker70df2fb2013-04-17 17:19:09 +02003212 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3213
Paul Bakker70df2fb2013-04-17 17:19:09 +02003214 return( ret );
3215}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003216#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3217 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003218
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003219#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3220 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003221static int ssl_parse_encrypted_pms( ssl_context *ssl,
3222 const unsigned char *p,
3223 const unsigned char *end,
3224 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003225{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003226 int ret;
3227 size_t len = pk_get_len( ssl_own_key( ssl ) );
3228 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003229 unsigned char ver[2];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003230 unsigned char fake_pms[48], peer_pms[48];
3231 unsigned char mask;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003232 size_t i;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003233
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003234 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003235 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003236 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003237 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3238 }
3239
3240 /*
3241 * Decrypt the premaster using own private RSA key
3242 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003243#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3244 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003245 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3246 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003247 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3248 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003249 {
3250 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3252 }
3253 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003254#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003255
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003256 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003257 {
3258 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3259 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3260 }
3261
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003262 ssl_write_version( ssl->handshake->max_major_ver,
3263 ssl->handshake->max_minor_ver,
3264 ssl->transport, ver );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003265 /*
3266 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3267 * must not cause the connection to end immediately; instead, send a
3268 * bad_record_mac later in the handshake.
3269 * Also, avoid data-dependant branches here to protect against
3270 * timing-based variants.
3271 */
3272 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3273 if( ret != 0 )
3274 return( ret );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003275
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003276 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003277 peer_pms, &ssl->handshake->pmslen,
3278 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003279 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003280
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003281 ret |= ssl->handshake->pmslen - 48;
Manuel Pégourié-Gonnardf7d2bba2015-02-09 11:42:40 +00003282 ret |= peer_pms[0] - ver[0];
3283 ret |= peer_pms[1] - ver[1];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003284
3285#if defined(POLARSSL_SSL_DEBUG_ALL)
3286 if( ret != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003287 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003288#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003289
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003290 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3291 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3292 {
3293 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3294 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003295 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003296 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003297
Manuel Pégourié-Gonnard2ee8d242015-02-11 15:29:15 +00003298 mask = (unsigned char)( - ( ret != 0 ) ); /* ret ? 0xff : 0x00 */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003299 for( i = 0; i < ssl->handshake->pmslen; i++ )
3300 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3301
3302 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003303}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003304#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3305 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003306
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003307#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003308static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3309 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003310{
Paul Bakker6db455e2013-09-18 17:29:31 +02003311 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003312 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003313
Paul Bakker6db455e2013-09-18 17:29:31 +02003314 if( ssl->f_psk == NULL &&
3315 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3316 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003317 {
3318 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3319 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3320 }
3321
3322 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003323 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003324 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003325 if( *p + 2 > end )
3326 {
3327 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3328 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3329 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003330
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003331 n = ( (*p)[0] << 8 ) | (*p)[1];
3332 *p += 2;
3333
3334 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003335 {
3336 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3337 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3338 }
3339
Paul Bakker6db455e2013-09-18 17:29:31 +02003340 if( ssl->f_psk != NULL )
3341 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003342 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003343 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3344 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003345 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003346 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003347 /* Identity is not a big secret since clients send it in the clear,
3348 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003349 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003350 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003351 {
3352 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3353 }
3354 }
3355
3356 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003357 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003358 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003359 if( ( ret = ssl_send_alert_message( ssl,
3360 SSL_ALERT_LEVEL_FATAL,
3361 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3362 {
3363 return( ret );
3364 }
3365
3366 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003367 }
3368
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003369 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003370
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003371 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003372}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003373#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003374
Paul Bakker5121ce52009-01-03 21:22:43 +00003375static int ssl_parse_client_key_exchange( ssl_context *ssl )
3376{
Paul Bakker23986e52011-04-24 08:57:21 +00003377 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003378 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003379 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003380
Paul Bakker41c83d32013-03-20 14:39:14 +01003381 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003382
3383 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3384
3385 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3386 {
3387 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3388 return( ret );
3389 }
3390
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003391 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3392 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003393
Paul Bakker5121ce52009-01-03 21:22:43 +00003394 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3395 {
3396 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003397 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
3399
3400 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3401 {
3402 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003407 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003408 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003411 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3412 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003413 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003415 if( p != end )
3416 {
3417 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3419 }
3420
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003421 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003422
3423 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3424 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003425 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003426 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003427 {
3428 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3429 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3430 }
3431
3432 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003433 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003434 else
3435#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003436#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003437 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3438 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3439 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003440 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003441 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3442 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3443 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003444 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003445 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003446 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003447 {
3448 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3450 }
3451
3452 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3453
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003454 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3455 &ssl->handshake->pmslen,
3456 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003457 POLARSSL_MPI_MAX_SIZE,
3458 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003459 {
3460 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3462 }
3463
3464 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003466 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003467#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003468 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3469 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3470 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003471#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3472 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003473 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003475 {
3476 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3477 return( ret );
3478 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003480 if( p != end )
3481 {
3482 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3483 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3484 }
3485
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003486 if( ( ret = ssl_psk_derive_premaster( ssl,
3487 ciphersuite_info->key_exchange ) ) != 0 )
3488 {
3489 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3490 return( ret );
3491 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003492 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003493 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003494#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003495#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3496 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3497 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003498 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3499 {
3500 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3501 return( ret );
3502 }
3503
3504 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3505 {
3506 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3507 return( ret );
3508 }
3509
3510 if( ( ret = ssl_psk_derive_premaster( ssl,
3511 ciphersuite_info->key_exchange ) ) != 0 )
3512 {
3513 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3514 return( ret );
3515 }
3516 }
3517 else
3518#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003519#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3520 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3521 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003522 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3523 {
3524 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3525 return( ret );
3526 }
3527 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3528 {
3529 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3530 return( ret );
3531 }
3532
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003533 if( p != end )
3534 {
3535 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3536 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3537 }
3538
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003539 if( ( ret = ssl_psk_derive_premaster( ssl,
3540 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003541 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003542 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3543 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003544 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003545 }
3546 else
3547#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003548#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3549 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3550 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003551 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3552 {
3553 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3554 return( ret );
3555 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003556
3557 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3558 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003559 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003560 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3561 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003562 }
3563
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003564 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3565
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003566 if( ( ret = ssl_psk_derive_premaster( ssl,
3567 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003568 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003569 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003570 return( ret );
3571 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003572 }
3573 else
3574#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003575#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3576 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003577 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003578 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003579 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003580 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003581 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003582 }
3583 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003584 else
3585#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3586 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003587 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003588 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003590
Paul Bakkerff60ee62010-03-16 21:09:09 +00003591 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3592 {
3593 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3594 return( ret );
3595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 ssl->state++;
3598
3599 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3600
3601 return( 0 );
3602}
3603
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003604#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3605 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003606 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3607 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003608static int ssl_parse_certificate_verify( ssl_context *ssl )
3609{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003610 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
3612 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3613
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003614 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003615 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003616 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003617 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003618 {
3619 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3620 ssl->state++;
3621 return( 0 );
3622 }
3623
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003624 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3625 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003626}
3627#else
3628static int ssl_parse_certificate_verify( ssl_context *ssl )
3629{
3630 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003631 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003632 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003633 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003634 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003635#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003636 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003637#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003638 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003639 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3640
3641 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3642
3643 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003644 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003645 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003646 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3647 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003648 {
3649 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3650 ssl->state++;
3651 return( 0 );
3652 }
3653
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003654 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003655 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003656
3657 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3658 {
3659 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3660 return( ret );
3661 }
3662
3663 ssl->state++;
3664
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003665 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3666 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 {
3668 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003669 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670 }
3671
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003672 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003673
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003674 /*
3675 * struct {
3676 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3677 * opaque signature<0..2^16-1>;
3678 * } DigitallySigned;
3679 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003680#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3681 defined(POLARSSL_SSL_PROTO_TLS1_1)
3682 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003683 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003684 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003685 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003686
3687 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3688 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3689 POLARSSL_PK_ECDSA ) )
3690 {
3691 hash_start += 16;
3692 hashlen -= 16;
3693 md_alg = POLARSSL_MD_SHA1;
3694 }
Paul Bakker926af752012-11-23 13:38:07 +01003695 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003696 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003697#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3698 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003699#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3700 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003701 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003702 if( i + 2 > ssl->in_hslen )
3703 {
3704 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3705 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3706 }
3707
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003709 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003711 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003713 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3714 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003715 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3716 }
3717
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003718 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003719
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003720 /* Info from md_alg will be used instead */
3721 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003722
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003723 i++;
3724
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003725 /*
3726 * Signature
3727 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003728 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003729 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003730 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003731 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3732 " for verify message" ) );
3733 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003734 }
3735
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003736 /*
3737 * Check the certificate's key type matches the signature alg
3738 */
3739 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3740 {
3741 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3742 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3743 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003744
3745 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003746 }
3747 else
3748#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3749 {
3750 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003751 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003752 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003753
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003754 if( i + 2 > ssl->in_hslen )
3755 {
3756 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3757 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3758 }
3759
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003760 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3761 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003762
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003763 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 {
3765 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003766 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003767 }
3768
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003769 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003770 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003771 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003772 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003773 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774 return( ret );
3775 }
3776
3777 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3778
Paul Bakkered27a042013-04-18 22:46:23 +02003779 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003781#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3782 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3783 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
Paul Bakkera503a632013-08-14 13:48:06 +02003785#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003786static int ssl_write_new_session_ticket( ssl_context *ssl )
3787{
3788 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003789 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003790 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003791
3792 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3793
3794 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3795 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3796
3797 /*
3798 * struct {
3799 * uint32 ticket_lifetime_hint;
3800 * opaque ticket<0..2^16-1>;
3801 * } NewSessionTicket;
3802 *
3803 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3804 * 8 . 9 ticket_len (n)
3805 * 10 . 9+n ticket content
3806 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003807
3808 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3809 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3810 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3811 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003812
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003813 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3814 {
3815 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3816 tlen = 0;
3817 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003818
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003819 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3820 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003821
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003822 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003823
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003824 /*
3825 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3826 * ChangeCipherSpec share the same state.
3827 */
3828 ssl->handshake->new_session_ticket = 0;
3829
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003830 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3831 {
3832 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3833 return( ret );
3834 }
3835
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003836 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3837
3838 return( 0 );
3839}
Paul Bakkera503a632013-08-14 13:48:06 +02003840#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003841
Paul Bakker5121ce52009-01-03 21:22:43 +00003842/*
Paul Bakker1961b702013-01-25 14:49:24 +01003843 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003844 */
Paul Bakker1961b702013-01-25 14:49:24 +01003845int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003846{
3847 int ret = 0;
3848
Paul Bakker1961b702013-01-25 14:49:24 +01003849 if( ssl->state == SSL_HANDSHAKE_OVER )
3850 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003851
Paul Bakker1961b702013-01-25 14:49:24 +01003852 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3853
3854 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3855 return( ret );
3856
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003857#if defined(POLARSSL_SSL_PROTO_DTLS)
3858 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3859 ssl->handshake != NULL &&
3860 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3861 {
3862 if( ( ret = ssl_resend( ssl ) ) != 0 )
3863 return( ret );
3864 }
3865#endif
3866
Paul Bakker1961b702013-01-25 14:49:24 +01003867 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 {
Paul Bakker1961b702013-01-25 14:49:24 +01003869 case SSL_HELLO_REQUEST:
3870 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003871 break;
3872
Paul Bakker1961b702013-01-25 14:49:24 +01003873 /*
3874 * <== ClientHello
3875 */
3876 case SSL_CLIENT_HELLO:
3877 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003878 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003879
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003880#if defined(POLARSSL_SSL_PROTO_DTLS)
3881 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3882 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3883#endif
3884
Paul Bakker1961b702013-01-25 14:49:24 +01003885 /*
3886 * ==> ServerHello
3887 * Certificate
3888 * ( ServerKeyExchange )
3889 * ( CertificateRequest )
3890 * ServerHelloDone
3891 */
3892 case SSL_SERVER_HELLO:
3893 ret = ssl_write_server_hello( ssl );
3894 break;
3895
3896 case SSL_SERVER_CERTIFICATE:
3897 ret = ssl_write_certificate( ssl );
3898 break;
3899
3900 case SSL_SERVER_KEY_EXCHANGE:
3901 ret = ssl_write_server_key_exchange( ssl );
3902 break;
3903
3904 case SSL_CERTIFICATE_REQUEST:
3905 ret = ssl_write_certificate_request( ssl );
3906 break;
3907
3908 case SSL_SERVER_HELLO_DONE:
3909 ret = ssl_write_server_hello_done( ssl );
3910 break;
3911
3912 /*
3913 * <== ( Certificate/Alert )
3914 * ClientKeyExchange
3915 * ( CertificateVerify )
3916 * ChangeCipherSpec
3917 * Finished
3918 */
3919 case SSL_CLIENT_CERTIFICATE:
3920 ret = ssl_parse_certificate( ssl );
3921 break;
3922
3923 case SSL_CLIENT_KEY_EXCHANGE:
3924 ret = ssl_parse_client_key_exchange( ssl );
3925 break;
3926
3927 case SSL_CERTIFICATE_VERIFY:
3928 ret = ssl_parse_certificate_verify( ssl );
3929 break;
3930
3931 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3932 ret = ssl_parse_change_cipher_spec( ssl );
3933 break;
3934
3935 case SSL_CLIENT_FINISHED:
3936 ret = ssl_parse_finished( ssl );
3937 break;
3938
3939 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003940 * ==> ( NewSessionTicket )
3941 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003942 * Finished
3943 */
3944 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003945#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003946 if( ssl->handshake->new_session_ticket != 0 )
3947 ret = ssl_write_new_session_ticket( ssl );
3948 else
Paul Bakkera503a632013-08-14 13:48:06 +02003949#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003950 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003951 break;
3952
3953 case SSL_SERVER_FINISHED:
3954 ret = ssl_write_finished( ssl );
3955 break;
3956
3957 case SSL_FLUSH_BUFFERS:
3958 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3959 ssl->state = SSL_HANDSHAKE_WRAPUP;
3960 break;
3961
3962 case SSL_HANDSHAKE_WRAPUP:
3963 ssl_handshake_wrapup( ssl );
3964 break;
3965
3966 default:
3967 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3968 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003969 }
3970
Paul Bakker5121ce52009-01-03 21:22:43 +00003971 return( ret );
3972}
Paul Bakker9af723c2014-05-01 13:03:14 +02003973#endif /* POLARSSL_SSL_SRV_C */