blob: b7c0335c574a314d27f8093d156e66c7475d4031 [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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +0000463 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100464 {
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é-Gonnardc3f6b62c2014-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é-Gonnard852a6d32015-03-19 16:15:20 +0000736 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200737 {
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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001045 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001046 {
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é-Gonnard852a6d32015-03-19 16:15:20 +00001192 if( ssl->renego_status == 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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001332 if( ssl->renego_status == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001333#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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001395 && ssl->renego_status == SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001396#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é-Gonnard852a6d32015-03-19 16:15:20 +00001426 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001427 {
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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001502 if( ssl->renego_status == 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)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001651 && ssl->renego_status == SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001652#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
1681 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001682 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001683 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001684 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001685 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001686 else
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001687#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001688 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001689
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001690 ciph_len = ( buf[ciph_offset + 0] << 8 )
1691 | ( buf[ciph_offset + 1] );
1692
1693 if( ciph_len < 2 ||
1694 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1695 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001696 {
1697 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1698 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1699 }
1700
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001701 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1702 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001703
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001704 /*
1705 * Check the compression algorithms length and pick one
1706 */
1707 comp_offset = ciph_offset + 2 + ciph_len;
1708
1709 comp_len = buf[comp_offset];
1710
1711 if( comp_len < 1 ||
1712 comp_len > 16 ||
1713 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001714 {
1715 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1716 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1717 }
1718
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001719 SSL_DEBUG_BUF( 3, "client hello, compression",
1720 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001721
1722 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001723#if defined(POLARSSL_ZLIB_SUPPORT)
1724 for( i = 0; i < comp_len; ++i )
1725 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001726 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 {
Paul Bakker48916f92012-09-16 19:57:18 +00001728 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001729 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730 }
1731 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001732#endif
1733
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001734 /* See comments in ssl_write_client_hello() */
1735#if defined(POLARSSL_SSL_PROTO_DTLS)
1736 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1737 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1738#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001739
Paul Bakkerec636f32012-09-09 19:17:02 +00001740 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001741 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001742 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001743 ext_offset = comp_offset + 1 + comp_len;
1744 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001745 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001746 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001747 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001748 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1749 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1750 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001751
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001752 ext_len = ( buf[ext_offset + 0] << 8 )
1753 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001754
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001755 if( ( ext_len > 0 && ext_len < 4 ) ||
1756 msg_len != ext_offset + 2 + ext_len )
1757 {
1758 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1759 SSL_DEBUG_BUF( 3, "client hello extensions",
1760 buf + ext_offset + 2, ext_len );
1761 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001762 }
1763 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001764 else
1765 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001766
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001767 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001768
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001769 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001770 {
1771 unsigned int ext_id = ( ( ext[0] << 8 )
1772 | ( ext[1] ) );
1773 unsigned int ext_size = ( ( ext[2] << 8 )
1774 | ( ext[3] ) );
1775
1776 if( ext_size + 4 > ext_len )
1777 {
1778 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1779 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1780 }
1781 switch( ext_id )
1782 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001783#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001784 case TLS_EXT_SERVERNAME:
1785 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1786 if( ssl->f_sni == NULL )
1787 break;
1788
1789 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1790 if( ret != 0 )
1791 return( ret );
1792 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001793#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001794
Paul Bakker48916f92012-09-16 19:57:18 +00001795 case TLS_EXT_RENEGOTIATION_INFO:
1796 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001797#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001798 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001799#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001800
Paul Bakker23f36802012-09-28 14:15:14 +00001801 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1802 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001803 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001804 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001805
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001806#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1807 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001808 case TLS_EXT_SIG_ALG:
1809 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001810#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001811 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker23f36802012-09-28 14:15:14 +00001812 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001813#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001814
1815 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1816 if( ret != 0 )
1817 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001818 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001819#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1820 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001821
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001822#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001823 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1824 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1825
1826 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1827 if( ret != 0 )
1828 return( ret );
1829 break;
1830
1831 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1832 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001833 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001834
1835 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1836 if( ret != 0 )
1837 return( ret );
1838 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001839#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001840
Paul Bakker05decb22013-08-15 13:33:48 +02001841#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001842 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1843 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1844
1845 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1846 if( ret != 0 )
1847 return( ret );
1848 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001849#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001850
Paul Bakker1f2bc622013-08-15 13:45:55 +02001851#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001852 case TLS_EXT_TRUNCATED_HMAC:
1853 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1854
1855 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1856 if( ret != 0 )
1857 return( ret );
1858 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001859#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001860
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001861#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1862 case TLS_EXT_ENCRYPT_THEN_MAC:
1863 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1864
1865 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1866 if( ret != 0 )
1867 return( ret );
1868 break;
1869#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1870
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001871#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1872 case TLS_EXT_EXTENDED_MASTER_SECRET:
1873 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1874
1875 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1876 if( ret != 0 )
1877 return( ret );
1878 break;
1879#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1880
Paul Bakkera503a632013-08-14 13:48:06 +02001881#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001882 case TLS_EXT_SESSION_TICKET:
1883 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1884
1885 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1886 if( ret != 0 )
1887 return( ret );
1888 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001889#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001890
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001891#if defined(POLARSSL_SSL_ALPN)
1892 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001893 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001894
1895 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1896 if( ret != 0 )
1897 return( ret );
1898 break;
1899#endif /* POLARSSL_SSL_SESSION_TICKETS */
1900
Paul Bakker48916f92012-09-16 19:57:18 +00001901 default:
1902 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1903 ext_id ) );
1904 }
1905
1906 ext_len -= 4 + ext_size;
1907 ext += 4 + ext_size;
1908
1909 if( ext_len > 0 && ext_len < 4 )
1910 {
1911 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1912 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1913 }
1914 }
1915
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001916#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1917 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1918 {
Manuel Pégourié-Gonnarded99d702015-03-09 11:17:30 +00001919 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1920 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001921 {
1922 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1923
1924 if( ssl->minor_ver < ssl->max_minor_ver )
1925 {
1926 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1927
1928 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1929 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1930
1931 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1932 }
1933
1934 break;
1935 }
1936 }
1937#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1938
Paul Bakker48916f92012-09-16 19:57:18 +00001939 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001940 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1941 */
1942 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1943 {
1944 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1945 {
1946 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001947#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001948 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001949 {
1950 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1951
1952 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1953 return( ret );
1954
1955 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1956 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001957#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001958 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1959 break;
1960 }
1961 }
1962
1963 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001964 * Renegotiation security checks
1965 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001966 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001967 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1968 {
1969 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1970 handshake_failure = 1;
1971 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001972#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001973 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001974 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1975 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001976 {
1977 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001978 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001979 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001980 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001981 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1982 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001983 {
1984 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001985 handshake_failure = 1;
1986 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001987 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001988 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1989 renegotiation_info_seen == 1 )
1990 {
1991 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1992 handshake_failure = 1;
1993 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001994#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001995
1996 if( handshake_failure == 1 )
1997 {
1998 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1999 return( ret );
2000
Paul Bakker48916f92012-09-16 19:57:18 +00002001 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
2002 }
Paul Bakker380da532012-04-18 16:10:25 +00002003
Paul Bakker41c83d32013-03-20 14:39:14 +01002004 /*
2005 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002006 * (At the end because we need information from the EC-based extensions
2007 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002008 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002009 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002010 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002011 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002012#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002013 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002014 {
2015 for( i = 0; ciphersuites[i] != 0; i++ )
2016#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002017 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01002018 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002019 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002020#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002021 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002022 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2023 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2024 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002025
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002026 got_common_suite = 1;
2027
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002028 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2029 &ciphersuite_info ) ) != 0 )
2030 return( ret );
2031
2032 if( ciphersuite_info != NULL )
2033 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002034 }
2035 }
2036
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002037 if( got_common_suite )
2038 {
2039 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2040 "but none of them usable" ) );
2041 ssl_send_fatal_handshake_failure( ssl );
2042 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2043 }
2044 else
2045 {
2046 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2047 ssl_send_fatal_handshake_failure( ssl );
2048 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2049 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002050
2051have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002052 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2053
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002054 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002055 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2056 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2057
Paul Bakker5121ce52009-01-03 21:22:43 +00002058 ssl->state++;
2059
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002060#if defined(POLARSSL_SSL_PROTO_DTLS)
2061 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2062 ssl_recv_flight_completed( ssl );
2063#endif
2064
Paul Bakker5121ce52009-01-03 21:22:43 +00002065 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2066
2067 return( 0 );
2068}
2069
Paul Bakker1f2bc622013-08-15 13:45:55 +02002070#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002071static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2072 unsigned char *buf,
2073 size_t *olen )
2074{
2075 unsigned char *p = buf;
2076
2077 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2078 {
2079 *olen = 0;
2080 return;
2081 }
2082
2083 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2084
2085 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2086 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2087
2088 *p++ = 0x00;
2089 *p++ = 0x00;
2090
2091 *olen = 4;
2092}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002093#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002094
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002095#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2096static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2097 unsigned char *buf,
2098 size_t *olen )
2099{
2100 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002101 const ssl_ciphersuite_t *suite = NULL;
2102 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002103
2104 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2105 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2106 {
2107 *olen = 0;
2108 return;
2109 }
2110
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002111 /*
2112 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2113 * from a client and then selects a stream or Authenticated Encryption
2114 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2115 * encrypt-then-MAC response extension back to the client."
2116 */
2117 if( ( suite = ssl_ciphersuite_from_id(
2118 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2119 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2120 cipher->mode != POLARSSL_MODE_CBC )
2121 {
2122 *olen = 0;
2123 return;
2124 }
2125
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002126 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2127
2128 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2129 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2130
2131 *p++ = 0x00;
2132 *p++ = 0x00;
2133
2134 *olen = 4;
2135}
2136#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2137
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002138#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2139static void ssl_write_extended_ms_ext( ssl_context *ssl,
2140 unsigned char *buf,
2141 size_t *olen )
2142{
2143 unsigned char *p = buf;
2144
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002145 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2146 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002147 {
2148 *olen = 0;
2149 return;
2150 }
2151
2152 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2153 "extension" ) );
2154
2155 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2156 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2157
2158 *p++ = 0x00;
2159 *p++ = 0x00;
2160
2161 *olen = 4;
2162}
2163#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2164
Paul Bakkera503a632013-08-14 13:48:06 +02002165#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002166static void ssl_write_session_ticket_ext( ssl_context *ssl,
2167 unsigned char *buf,
2168 size_t *olen )
2169{
2170 unsigned char *p = buf;
2171
2172 if( ssl->handshake->new_session_ticket == 0 )
2173 {
2174 *olen = 0;
2175 return;
2176 }
2177
2178 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2179
2180 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2181 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2182
2183 *p++ = 0x00;
2184 *p++ = 0x00;
2185
2186 *olen = 4;
2187}
Paul Bakkera503a632013-08-14 13:48:06 +02002188#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002189
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002190static void ssl_write_renegotiation_ext( ssl_context *ssl,
2191 unsigned char *buf,
2192 size_t *olen )
2193{
2194 unsigned char *p = buf;
2195
2196 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2197 {
2198 *olen = 0;
2199 return;
2200 }
2201
2202 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2203
2204 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2205 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2206
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002207#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002208 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002209 {
2210 *p++ = 0x00;
2211 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2212 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002213
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002214 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2215 p += ssl->verify_data_len;
2216 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2217 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002218
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002219 *olen = 5 + ssl->verify_data_len * 2;
2220 }
2221 else
2222#endif /* POLARSSL_SSL_RENEGOTIATION */
2223 {
2224 *p++ = 0x00;
2225 *p++ = 0x01;
2226 *p++ = 0x00;
2227
2228 *olen = 5;
2229 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002230}
2231
Paul Bakker05decb22013-08-15 13:33:48 +02002232#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002233static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2234 unsigned char *buf,
2235 size_t *olen )
2236{
2237 unsigned char *p = buf;
2238
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002239 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2240 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002241 *olen = 0;
2242 return;
2243 }
2244
2245 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2246
2247 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2248 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2249
2250 *p++ = 0x00;
2251 *p++ = 1;
2252
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002253 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002254
2255 *olen = 5;
2256}
Paul Bakker05decb22013-08-15 13:33:48 +02002257#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002258
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002259#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002260static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2261 unsigned char *buf,
2262 size_t *olen )
2263{
2264 unsigned char *p = buf;
2265 ((void) ssl);
2266
Paul Bakker677377f2013-10-28 12:54:26 +01002267 if( ( ssl->handshake->cli_exts &
2268 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2269 {
2270 *olen = 0;
2271 return;
2272 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002273
2274 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2275
2276 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2277 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2278
2279 *p++ = 0x00;
2280 *p++ = 2;
2281
2282 *p++ = 1;
2283 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2284
2285 *olen = 6;
2286}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002287#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002288
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002289#if defined(POLARSSL_SSL_ALPN )
2290static void ssl_write_alpn_ext( ssl_context *ssl,
2291 unsigned char *buf, size_t *olen )
2292{
2293 if( ssl->alpn_chosen == NULL )
2294 {
2295 *olen = 0;
2296 return;
2297 }
2298
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002299 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002300
2301 /*
2302 * 0 . 1 ext identifier
2303 * 2 . 3 ext length
2304 * 4 . 5 protocol list length
2305 * 6 . 6 protocol name length
2306 * 7 . 7+n protocol name
2307 */
2308 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2309 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2310
2311 *olen = 7 + strlen( ssl->alpn_chosen );
2312
2313 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2314 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2315
2316 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2317 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2318
2319 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2320
2321 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2322}
2323#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2324
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002325#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002326static int ssl_write_hello_verify_request( ssl_context *ssl )
2327{
2328 int ret;
2329 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002330 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002331
2332 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2333
2334 /*
2335 * struct {
2336 * ProtocolVersion server_version;
2337 * opaque cookie<0..2^8-1>;
2338 * } HelloVerifyRequest;
2339 */
2340
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002341 /* The RFC is not clear on this point, but sending the actual negotiated
2342 * version looks like the most interoperable thing to do. */
2343 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002344 ssl->transport, p );
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +00002345 SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002346 p += 2;
2347
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002348 /* If we get here, f_cookie_check is not null */
2349 if( ssl->f_cookie_write == NULL )
2350 {
2351 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2352 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2353 }
2354
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002355 /* Skip length byte until we know the length */
2356 cookie_len_byte = p++;
2357
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002358 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2359 &p, ssl->out_buf + SSL_BUFFER_LEN,
2360 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002361 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002362 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002363 return( ret );
2364 }
2365
2366 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2367
2368 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002369
2370 ssl->out_msglen = p - ssl->out_msg;
2371 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2372 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2373
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002374 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002375
2376 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2377 {
2378 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2379 return( ret );
2380 }
2381
2382 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2383
2384 return( 0 );
2385}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002386#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002387
Paul Bakker5121ce52009-01-03 21:22:43 +00002388static int ssl_write_server_hello( ssl_context *ssl )
2389{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002390#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002392#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002393 int ret;
2394 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002395 unsigned char *buf, *p;
2396
2397 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2398
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002399#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002400 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002401 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002402 {
2403 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2404 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2405
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002406 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002407 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002408#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002409
Paul Bakkera9a028e2013-11-21 17:31:06 +01002410 if( ssl->f_rng == NULL )
2411 {
2412 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2413 return( POLARSSL_ERR_SSL_NO_RNG );
2414 }
2415
Paul Bakker5121ce52009-01-03 21:22:43 +00002416 /*
2417 * 0 . 0 handshake type
2418 * 1 . 3 handshake length
2419 * 4 . 5 protocol version
2420 * 6 . 9 UNIX time()
2421 * 10 . 37 random bytes
2422 */
2423 buf = ssl->out_msg;
2424 p = buf + 4;
2425
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002426 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2427 ssl->transport, p );
2428 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
2430 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002431 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002433#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002434 t = time( NULL );
2435 *p++ = (unsigned char)( t >> 24 );
2436 *p++ = (unsigned char)( t >> 16 );
2437 *p++ = (unsigned char)( t >> 8 );
2438 *p++ = (unsigned char)( t );
2439
2440 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002441#else
2442 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2443 return( ret );
2444
2445 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002446#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Paul Bakkera3d195c2011-11-27 21:07:34 +00002448 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2449 return( ret );
2450
2451 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakker48916f92012-09-16 19:57:18 +00002453 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
2455 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2456
2457 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002458 * Resume is 0 by default, see ssl_handshake_init().
2459 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2460 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002462 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002463#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002464 ssl->renego_status == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002465#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002466 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002467 ssl->f_get_cache != NULL &&
2468 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2469 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002470 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002471 ssl->handshake->resume = 1;
2472 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002473
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002474 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 {
2476 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002477 * New session, create a new session id,
2478 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002480 ssl->state++;
2481
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002482#if defined(POLARSSL_HAVE_TIME)
2483 ssl->session_negotiate->start = time( NULL );
2484#endif
2485
Paul Bakkera503a632013-08-14 13:48:06 +02002486#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002487 if( ssl->handshake->new_session_ticket != 0 )
2488 {
2489 ssl->session_negotiate->length = n = 0;
2490 memset( ssl->session_negotiate->id, 0, 32 );
2491 }
2492 else
2493#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002494 {
2495 ssl->session_negotiate->length = n = 32;
2496 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002497 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002498 return( ret );
2499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 }
2501 else
2502 {
2503 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002504 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002505 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002506 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002508
2509 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2510 {
2511 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2512 return( ret );
2513 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 }
2515
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002516 /*
2517 * 38 . 38 session id length
2518 * 39 . 38+n session id
2519 * 39+n . 40+n chosen ciphersuite
2520 * 41+n . 41+n chosen compression alg.
2521 * 42+n . 43+n extensions length
2522 * 44+n . 43+n+m extensions
2523 */
2524 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002525 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2526 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
2528 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2529 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2530 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002531 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532
Paul Bakker48916f92012-09-16 19:57:18 +00002533 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2534 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2535 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002537 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2538 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002539 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002540 ssl->session_negotiate->compression ) );
2541
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002542 /*
2543 * First write extensions, then the total length
2544 */
2545 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2546 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002547
Paul Bakker05decb22013-08-15 13:33:48 +02002548#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002549 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2550 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002551#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002552
Paul Bakker1f2bc622013-08-15 13:45:55 +02002553#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002554 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2555 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002556#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002557
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002558#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2559 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2560 ext_len += olen;
2561#endif
2562
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002563#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2564 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2565 ext_len += olen;
2566#endif
2567
Paul Bakkera503a632013-08-14 13:48:06 +02002568#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002569 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2570 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002571#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002572
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002573#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002574 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2575 ext_len += olen;
2576#endif
2577
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002578#if defined(POLARSSL_SSL_ALPN)
2579 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2580 ext_len += olen;
2581#endif
2582
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002583 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002584
Paul Bakkera7036632014-04-30 10:15:38 +02002585 if( ext_len > 0 )
2586 {
2587 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2588 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2589 p += ext_len;
2590 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
2592 ssl->out_msglen = p - buf;
2593 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2594 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2595
2596 ret = ssl_write_record( ssl );
2597
2598 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2599
2600 return( ret );
2601}
2602
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002603#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2604 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002605 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2606 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002607static int ssl_write_certificate_request( ssl_context *ssl )
2608{
Paul Bakkered27a042013-04-18 22:46:23 +02002609 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002610
2611 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2612
2613 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002614 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002615 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2616 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002617 {
2618 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2619 ssl->state++;
2620 return( 0 );
2621 }
2622
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002623 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2624 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002625}
2626#else
2627static int ssl_write_certificate_request( ssl_context *ssl )
2628{
2629 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2630 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002631 size_t dn_size, total_dn_size; /* excluding length bytes */
2632 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002633 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002634 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
2636 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2637
2638 ssl->state++;
2639
Paul Bakkerfbb17802013-04-17 19:10:21 +02002640 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002641 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002643 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002644 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002646 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002647 return( 0 );
2648 }
2649
2650 /*
2651 * 0 . 0 handshake type
2652 * 1 . 3 handshake length
2653 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002654 * 5 .. m-1 cert types
2655 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002656 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002657 * n .. n+1 length of all DNs
2658 * n+2 .. n+3 length of DN 1
2659 * n+4 .. ... Distinguished Name #1
2660 * ... .. ... length of DN 2, etc.
2661 */
2662 buf = ssl->out_msg;
2663 p = buf + 4;
2664
2665 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002666 * Supported certificate types
2667 *
2668 * ClientCertificateType certificate_types<1..2^8-1>;
2669 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002670 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002671 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002672
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002673#if defined(POLARSSL_RSA_C)
2674 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2675#endif
2676#if defined(POLARSSL_ECDSA_C)
2677 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2678#endif
2679
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002680 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002681 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002682
Paul Bakker577e0062013-08-28 11:57:20 +02002683 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002684#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002685 /*
2686 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002687 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002688 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2689 *
2690 * struct {
2691 * HashAlgorithm hash;
2692 * SignatureAlgorithm signature;
2693 * } SignatureAndHashAlgorithm;
2694 *
2695 * enum { (255) } HashAlgorithm;
2696 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002697 */
Paul Bakker21dca692013-01-03 11:41:08 +01002698 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002699 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002700 /*
2701 * Only use current running hash algorithm that is already required
2702 * for requested ciphersuite.
2703 */
Paul Bakker926af752012-11-23 13:38:07 +01002704 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2705
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002706 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2707 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002708 {
2709 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2710 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002711
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002712 /*
2713 * Supported signature algorithms
2714 */
2715#if defined(POLARSSL_RSA_C)
2716 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2717 p[2 + sa_len++] = SSL_SIG_RSA;
2718#endif
2719#if defined(POLARSSL_ECDSA_C)
2720 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2721 p[2 + sa_len++] = SSL_SIG_ECDSA;
2722#endif
Paul Bakker926af752012-11-23 13:38:07 +01002723
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002724 p[0] = (unsigned char)( sa_len >> 8 );
2725 p[1] = (unsigned char)( sa_len );
2726 sa_len += 2;
2727 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002728 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002729#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002730
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002731 /*
2732 * DistinguishedName certificate_authorities<0..2^16-1>;
2733 * opaque DistinguishedName<1..2^16-1>;
2734 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 p += 2;
2736 crt = ssl->ca_chain;
2737
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002738 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002739 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002740 {
2741 if( p - buf > 4096 )
2742 break;
2743
Paul Bakker926af752012-11-23 13:38:07 +01002744 dn_size = crt->subject_raw.len;
2745 *p++ = (unsigned char)( dn_size >> 8 );
2746 *p++ = (unsigned char)( dn_size );
2747 memcpy( p, crt->subject_raw.p, dn_size );
2748 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Paul Bakker926af752012-11-23 13:38:07 +01002750 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2751
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002752 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002753 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002754 }
2755
Paul Bakker926af752012-11-23 13:38:07 +01002756 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002757 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2758 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002759 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2760 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002761
2762 ret = ssl_write_record( ssl );
2763
2764 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2765
2766 return( ret );
2767}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002768#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2769 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002770 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2771 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002773#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2774 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2775static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2776{
2777 int ret;
2778
2779 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2780 {
2781 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2782 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2783 }
2784
2785 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2786 pk_ec( *ssl_own_key( ssl ) ),
2787 POLARSSL_ECDH_OURS ) ) != 0 )
2788 {
2789 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2790 return( ret );
2791 }
2792
2793 return( 0 );
2794}
2795#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2796 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2797
Paul Bakker41c83d32013-03-20 14:39:14 +01002798static int ssl_write_server_key_exchange( ssl_context *ssl )
2799{
Paul Bakker23986e52011-04-24 08:57:21 +00002800 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002801 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002802 const ssl_ciphersuite_t *ciphersuite_info =
2803 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002804
2805#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2806 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2807 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002808 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002809 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002810 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002811 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002812 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002813 ((void) dig_signed);
2814 ((void) dig_signed_len);
2815#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002816
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2818
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002819#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2820 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2821 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002822 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2823 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2824 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002825 {
2826 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2827 ssl->state++;
2828 return( 0 );
2829 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002830#endif
2831
2832#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2833 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2834 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2835 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2836 {
2837 ssl_get_ecdh_params_from_cert( ssl );
2838
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002839 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002840 ssl->state++;
2841 return( 0 );
2842 }
2843#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002844
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002845#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2846 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2847 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2848 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002849 {
2850 /* TODO: Support identity hints */
2851 *(p++) = 0x00;
2852 *(p++) = 0x00;
2853
2854 n += 2;
2855 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002856#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2857 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002858
2859#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2860 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2861 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2862 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002863 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002864 /*
2865 * Ephemeral DH parameters:
2866 *
2867 * struct {
2868 * opaque dh_p<1..2^16-1>;
2869 * opaque dh_g<1..2^16-1>;
2870 * opaque dh_Ys<1..2^16-1>;
2871 * } ServerDHParams;
2872 */
2873 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2874 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2875 {
2876 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2877 return( ret );
2878 }
Paul Bakker48916f92012-09-16 19:57:18 +00002879
Paul Bakker41c83d32013-03-20 14:39:14 +01002880 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002881 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2882 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002883 {
2884 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2885 return( ret );
2886 }
2887
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002888 dig_signed = p;
2889 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002890
2891 p += len;
2892 n += len;
2893
Paul Bakker41c83d32013-03-20 14:39:14 +01002894 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2895 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2896 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2897 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2898 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2900 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002901
Gergely Budai987bfb52014-01-19 21:48:42 +01002902#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002903 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002904 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2905 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002906 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002907 /*
2908 * Ephemeral ECDH parameters:
2909 *
2910 * struct {
2911 * ECParameters curve_params;
2912 * ECPoint public;
2913 * } ServerECDHParams;
2914 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002915 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002916#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002917 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002918
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002919 /* Match our preference list against the offered curves */
2920 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2921 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2922 if( (*curve)->grp_id == *gid )
2923 goto curve_matching_done;
2924
2925curve_matching_done:
2926#else
2927 curve = ssl->handshake->curves;
2928#endif
2929
2930 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002931 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002932 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2933 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002934 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002935
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002936 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002937
Paul Bakker41c83d32013-03-20 14:39:14 +01002938 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002939 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002940 {
2941 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2942 return( ret );
2943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002945 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2946 p, SSL_MAX_CONTENT_LEN - n,
2947 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002948 {
2949 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2950 return( ret );
2951 }
2952
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002953 dig_signed = p;
2954 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002955
2956 p += len;
2957 n += len;
2958
Paul Bakker41c83d32013-03-20 14:39:14 +01002959 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2960 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002961#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002963#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002964 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2965 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002966 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002967 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2968 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002969 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002970 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002971 unsigned int hashlen = 0;
2972 unsigned char hash[64];
2973 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002974
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002975 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002976 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2977 */
Paul Bakker577e0062013-08-28 11:57:20 +02002978#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002979 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2980 {
2981 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2982
2983 if( md_alg == POLARSSL_MD_NONE )
2984 {
2985 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002986 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002987 }
2988 }
Paul Bakker577e0062013-08-28 11:57:20 +02002989 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002990#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002991#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2992 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002993 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002994 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2995 {
2996 md_alg = POLARSSL_MD_SHA1;
2997 }
2998 else
Paul Bakker577e0062013-08-28 11:57:20 +02002999#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003000 {
3001 md_alg = POLARSSL_MD_NONE;
3002 }
3003
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003004 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003005 * Compute the hash to be signed
3006 */
Paul Bakker577e0062013-08-28 11:57:20 +02003007#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3008 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003009 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003010 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011 md5_context md5;
3012 sha1_context sha1;
3013
Paul Bakker5b4af392014-06-26 12:09:34 +02003014 md5_init( &md5 );
3015 sha1_init( &sha1 );
3016
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003017 /*
3018 * digitally-signed struct {
3019 * opaque md5_hash[16];
3020 * opaque sha_hash[20];
3021 * };
3022 *
3023 * md5_hash
3024 * MD5(ClientHello.random + ServerHello.random
3025 * + ServerParams);
3026 * sha_hash
3027 * SHA(ClientHello.random + ServerHello.random
3028 * + ServerParams);
3029 */
3030 md5_starts( &md5 );
3031 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003032 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033 md5_finish( &md5, hash );
3034
3035 sha1_starts( &sha1 );
3036 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003037 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003038 sha1_finish( &sha1, hash + 16 );
3039
3040 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02003041
3042 md5_free( &md5 );
3043 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003044 }
3045 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003046#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3047 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003048#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3049 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003050 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003051 {
3052 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003053 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054
Paul Bakker84bbeb52014-07-01 14:53:22 +02003055 md_init( &ctx );
3056
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003057 /* Info from md_alg will be used instead */
3058 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003059
3060 /*
3061 * digitally-signed struct {
3062 * opaque client_random[32];
3063 * opaque server_random[32];
3064 * ServerDHParams params;
3065 * };
3066 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003067 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003068 {
3069 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3070 return( ret );
3071 }
3072
3073 md_starts( &ctx );
3074 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003075 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003076 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003077 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003078 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003079 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003080#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3081 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003082 {
3083 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003084 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003085 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003086
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003087 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01003088 (unsigned int) ( md_get_size( md_info_from_type( md_alg ) ) ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003089
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003090 /*
3091 * Make the signature
3092 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003093 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003094 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003095 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3096 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003097 }
Paul Bakker23f36802012-09-28 14:15:14 +00003098
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003099#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003100 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3101 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003102 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003103 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003104
3105 n += 2;
3106 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003107#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003108
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003109 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003110 p + 2 , &signature_len,
3111 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003112 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003113 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003114 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003115 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003116
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003117 *(p++) = (unsigned char)( signature_len >> 8 );
3118 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003119 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003120
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003121 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003122
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003123 p += signature_len;
3124 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003125 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003126#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003127 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3128 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003129
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003130 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3132 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3133
3134 ssl->state++;
3135
3136 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3137 {
3138 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3139 return( ret );
3140 }
3141
3142 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3143
3144 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003145}
3146
3147static int ssl_write_server_hello_done( ssl_context *ssl )
3148{
3149 int ret;
3150
3151 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3152
3153 ssl->out_msglen = 4;
3154 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3155 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3156
3157 ssl->state++;
3158
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003159#if defined(POLARSSL_SSL_PROTO_DTLS)
3160 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3161 ssl_send_flight_completed( ssl );
3162#endif
3163
Paul Bakker5121ce52009-01-03 21:22:43 +00003164 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3165 {
3166 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3167 return( ret );
3168 }
3169
3170 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3171
3172 return( 0 );
3173}
3174
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003175#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3176 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3177static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3178 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003179{
3180 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003181 size_t n;
3182
3183 /*
3184 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3185 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003186 if( *p + 2 > end )
3187 {
3188 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3189 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3190 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003191
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003192 n = ( (*p)[0] << 8 ) | (*p)[1];
3193 *p += 2;
3194
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003195 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003196 {
3197 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3199 }
3200
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003201 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003202 {
3203 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3204 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3205 }
3206
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003207 *p += n;
3208
Paul Bakker70df2fb2013-04-17 17:19:09 +02003209 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3210
Paul Bakker70df2fb2013-04-17 17:19:09 +02003211 return( ret );
3212}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003213#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3214 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003215
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003216#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3217 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003218static int ssl_parse_encrypted_pms( ssl_context *ssl,
3219 const unsigned char *p,
3220 const unsigned char *end,
3221 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003222{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003223 int ret;
3224 size_t len = pk_get_len( ssl_own_key( ssl ) );
3225 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003226 unsigned char ver[2];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003227 unsigned char fake_pms[48], peer_pms[48];
3228 unsigned char mask;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003229 size_t i;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003230
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003231 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003232 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003233 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003234 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3235 }
3236
3237 /*
3238 * Decrypt the premaster using own private RSA key
3239 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003240#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3241 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003242 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3243 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003244 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3245 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003246 {
3247 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3248 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3249 }
3250 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003251#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003252
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003253 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003254 {
3255 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3256 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3257 }
3258
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003259 ssl_write_version( ssl->handshake->max_major_ver,
3260 ssl->handshake->max_minor_ver,
3261 ssl->transport, ver );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003262 /*
3263 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3264 * must not cause the connection to end immediately; instead, send a
3265 * bad_record_mac later in the handshake.
3266 * Also, avoid data-dependant branches here to protect against
3267 * timing-based variants.
3268 */
3269 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3270 if( ret != 0 )
3271 return( ret );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003272
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003273 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003274 peer_pms, &ssl->handshake->pmslen,
3275 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003276 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003277
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003278 ret |= ssl->handshake->pmslen - 48;
Manuel Pégourié-Gonnardf7d2bba2015-02-09 11:42:40 +00003279 ret |= peer_pms[0] - ver[0];
3280 ret |= peer_pms[1] - ver[1];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003281
3282#if defined(POLARSSL_SSL_DEBUG_ALL)
3283 if( ret != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003284 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003285#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003286
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003287 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3288 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3289 {
3290 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3291 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003292 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003293 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003294
Manuel Pégourié-Gonnard2ee8d242015-02-11 15:29:15 +00003295 mask = (unsigned char)( - ( ret != 0 ) ); /* ret ? 0xff : 0x00 */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003296 for( i = 0; i < ssl->handshake->pmslen; i++ )
3297 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3298
3299 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003300}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003301#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3302 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003303
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003304#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003305static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3306 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003307{
Paul Bakker6db455e2013-09-18 17:29:31 +02003308 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003309 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003310
Paul Bakker6db455e2013-09-18 17:29:31 +02003311 if( ssl->f_psk == NULL &&
3312 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3313 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003314 {
3315 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3316 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3317 }
3318
3319 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003320 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003321 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003322 if( *p + 2 > end )
3323 {
3324 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3325 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3326 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003327
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003328 n = ( (*p)[0] << 8 ) | (*p)[1];
3329 *p += 2;
3330
3331 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003332 {
3333 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3335 }
3336
Paul Bakker6db455e2013-09-18 17:29:31 +02003337 if( ssl->f_psk != NULL )
3338 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003339 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003340 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3341 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003342 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003343 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003344 /* Identity is not a big secret since clients send it in the clear,
3345 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003346 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003347 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003348 {
3349 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3350 }
3351 }
3352
3353 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003354 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003355 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003356 if( ( ret = ssl_send_alert_message( ssl,
3357 SSL_ALERT_LEVEL_FATAL,
3358 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3359 {
3360 return( ret );
3361 }
3362
3363 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003364 }
3365
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003366 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003367
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003368 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003369}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003370#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003371
Paul Bakker5121ce52009-01-03 21:22:43 +00003372static int ssl_parse_client_key_exchange( ssl_context *ssl )
3373{
Paul Bakker23986e52011-04-24 08:57:21 +00003374 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003375 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003376 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003377
Paul Bakker41c83d32013-03-20 14:39:14 +01003378 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003379
3380 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3381
3382 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3383 {
3384 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3385 return( ret );
3386 }
3387
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003388 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3389 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003390
Paul Bakker5121ce52009-01-03 21:22:43 +00003391 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3392 {
3393 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003395 }
3396
3397 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3398 {
3399 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003400 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003401 }
3402
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003403#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003404 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003405 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003407 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003408 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3409 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003411
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003412 if( p != end )
3413 {
3414 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3415 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3416 }
3417
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003418 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003419
3420 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3421 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003422 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003423 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003424 {
3425 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3427 }
3428
3429 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003430 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431 else
3432#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003433#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003434 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3435 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3436 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003437 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003438 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3439 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3440 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003441 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003442 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003443 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003444 {
3445 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3447 }
3448
3449 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3450
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003451 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3452 &ssl->handshake->pmslen,
3453 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003454 POLARSSL_MPI_MAX_SIZE,
3455 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003456 {
3457 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3458 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3459 }
3460
3461 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003462 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003463 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003464#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003465 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3466 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3467 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003468#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3469 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003470 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003471 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003472 {
3473 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3474 return( ret );
3475 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003476
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003477 if( p != end )
3478 {
3479 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3481 }
3482
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003483 if( ( ret = ssl_psk_derive_premaster( ssl,
3484 ciphersuite_info->key_exchange ) ) != 0 )
3485 {
3486 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3487 return( ret );
3488 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003489 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003490 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003491#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003492#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3493 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3494 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003495 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3496 {
3497 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3498 return( ret );
3499 }
3500
3501 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3502 {
3503 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3504 return( ret );
3505 }
3506
3507 if( ( ret = ssl_psk_derive_premaster( ssl,
3508 ciphersuite_info->key_exchange ) ) != 0 )
3509 {
3510 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3511 return( ret );
3512 }
3513 }
3514 else
3515#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003516#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3517 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3518 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003519 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3520 {
3521 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3522 return( ret );
3523 }
3524 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3525 {
3526 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3527 return( ret );
3528 }
3529
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003530 if( p != end )
3531 {
3532 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3533 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3534 }
3535
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003536 if( ( ret = ssl_psk_derive_premaster( ssl,
3537 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003538 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003539 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3540 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003541 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003542 }
3543 else
3544#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003545#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3546 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3547 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003548 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3549 {
3550 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3551 return( ret );
3552 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003553
3554 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3555 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003556 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003557 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3558 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003559 }
3560
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003561 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3562
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003563 if( ( ret = ssl_psk_derive_premaster( ssl,
3564 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003565 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003566 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003567 return( ret );
3568 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003569 }
3570 else
3571#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003572#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3573 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003574 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003575 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003576 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003577 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003578 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003579 }
3580 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003581 else
3582#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3583 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003584 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003585 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003586 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003587
Paul Bakkerff60ee62010-03-16 21:09:09 +00003588 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3589 {
3590 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3591 return( ret );
3592 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003593
Paul Bakker5121ce52009-01-03 21:22:43 +00003594 ssl->state++;
3595
3596 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3597
3598 return( 0 );
3599}
3600
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003601#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3602 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003603 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3604 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003605static int ssl_parse_certificate_verify( ssl_context *ssl )
3606{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003607 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
3609 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3610
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003611 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003612 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003613 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003614 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003615 {
3616 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3617 ssl->state++;
3618 return( 0 );
3619 }
3620
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003621 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3622 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003623}
3624#else
3625static int ssl_parse_certificate_verify( ssl_context *ssl )
3626{
3627 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003628 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003629 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003630 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003631 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003632#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003633 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003634#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003635 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003636 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3637
3638 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3639
3640 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003641 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003642 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003643 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3644 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645 {
3646 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3647 ssl->state++;
3648 return( 0 );
3649 }
3650
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003651 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003652 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003653
3654 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3655 {
3656 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3657 return( ret );
3658 }
3659
3660 ssl->state++;
3661
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003662 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3663 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 {
3665 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003666 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667 }
3668
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003669 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003670
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003671 /*
3672 * struct {
3673 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3674 * opaque signature<0..2^16-1>;
3675 * } DigitallySigned;
3676 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003677#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3678 defined(POLARSSL_SSL_PROTO_TLS1_1)
3679 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003680 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003681 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003682 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003683
3684 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3685 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3686 POLARSSL_PK_ECDSA ) )
3687 {
3688 hash_start += 16;
3689 hashlen -= 16;
3690 md_alg = POLARSSL_MD_SHA1;
3691 }
Paul Bakker926af752012-11-23 13:38:07 +01003692 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003693 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003694#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3695 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003696#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3697 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003698 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003699 if( i + 2 > ssl->in_hslen )
3700 {
3701 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3702 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3703 }
3704
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003706 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003708 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003709 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003710 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3711 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003712 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3713 }
3714
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003715 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003716
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003717 /* Info from md_alg will be used instead */
3718 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003719
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003720 i++;
3721
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003722 /*
3723 * Signature
3724 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003725 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003726 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003727 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003728 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3729 " for verify message" ) );
3730 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003731 }
3732
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003733 /*
3734 * Check the certificate's key type matches the signature alg
3735 */
3736 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3737 {
3738 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3739 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3740 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003741
3742 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003743 }
3744 else
3745#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3746 {
3747 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003748 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003749 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003750
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003751 if( i + 2 > ssl->in_hslen )
3752 {
3753 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3754 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3755 }
3756
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003757 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3758 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003759
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003760 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003761 {
3762 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003763 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 }
3765
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003766 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003767 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003768 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003770 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003771 return( ret );
3772 }
3773
3774 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3775
Paul Bakkered27a042013-04-18 22:46:23 +02003776 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003777}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003778#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3779 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3780 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003781
Paul Bakkera503a632013-08-14 13:48:06 +02003782#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003783static int ssl_write_new_session_ticket( ssl_context *ssl )
3784{
3785 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003786 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003787 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003788
3789 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3790
3791 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3792 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3793
3794 /*
3795 * struct {
3796 * uint32 ticket_lifetime_hint;
3797 * opaque ticket<0..2^16-1>;
3798 * } NewSessionTicket;
3799 *
3800 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3801 * 8 . 9 ticket_len (n)
3802 * 10 . 9+n ticket content
3803 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003804
3805 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3806 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3807 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3808 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003809
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003810 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3811 {
3812 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3813 tlen = 0;
3814 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003815
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003816 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3817 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003818
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003819 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003820
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003821 /*
3822 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3823 * ChangeCipherSpec share the same state.
3824 */
3825 ssl->handshake->new_session_ticket = 0;
3826
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003827 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3828 {
3829 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3830 return( ret );
3831 }
3832
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003833 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3834
3835 return( 0 );
3836}
Paul Bakkera503a632013-08-14 13:48:06 +02003837#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003838
Paul Bakker5121ce52009-01-03 21:22:43 +00003839/*
Paul Bakker1961b702013-01-25 14:49:24 +01003840 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003841 */
Paul Bakker1961b702013-01-25 14:49:24 +01003842int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003843{
3844 int ret = 0;
3845
Paul Bakker1961b702013-01-25 14:49:24 +01003846 if( ssl->state == SSL_HANDSHAKE_OVER )
3847 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003848
Paul Bakker1961b702013-01-25 14:49:24 +01003849 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3850
3851 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3852 return( ret );
3853
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003854#if defined(POLARSSL_SSL_PROTO_DTLS)
3855 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3856 ssl->handshake != NULL &&
3857 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3858 {
3859 if( ( ret = ssl_resend( ssl ) ) != 0 )
3860 return( ret );
3861 }
3862#endif
3863
Paul Bakker1961b702013-01-25 14:49:24 +01003864 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003865 {
Paul Bakker1961b702013-01-25 14:49:24 +01003866 case SSL_HELLO_REQUEST:
3867 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003868 break;
3869
Paul Bakker1961b702013-01-25 14:49:24 +01003870 /*
3871 * <== ClientHello
3872 */
3873 case SSL_CLIENT_HELLO:
3874 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003875 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003876
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003877#if defined(POLARSSL_SSL_PROTO_DTLS)
3878 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3879 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3880#endif
3881
Paul Bakker1961b702013-01-25 14:49:24 +01003882 /*
3883 * ==> ServerHello
3884 * Certificate
3885 * ( ServerKeyExchange )
3886 * ( CertificateRequest )
3887 * ServerHelloDone
3888 */
3889 case SSL_SERVER_HELLO:
3890 ret = ssl_write_server_hello( ssl );
3891 break;
3892
3893 case SSL_SERVER_CERTIFICATE:
3894 ret = ssl_write_certificate( ssl );
3895 break;
3896
3897 case SSL_SERVER_KEY_EXCHANGE:
3898 ret = ssl_write_server_key_exchange( ssl );
3899 break;
3900
3901 case SSL_CERTIFICATE_REQUEST:
3902 ret = ssl_write_certificate_request( ssl );
3903 break;
3904
3905 case SSL_SERVER_HELLO_DONE:
3906 ret = ssl_write_server_hello_done( ssl );
3907 break;
3908
3909 /*
3910 * <== ( Certificate/Alert )
3911 * ClientKeyExchange
3912 * ( CertificateVerify )
3913 * ChangeCipherSpec
3914 * Finished
3915 */
3916 case SSL_CLIENT_CERTIFICATE:
3917 ret = ssl_parse_certificate( ssl );
3918 break;
3919
3920 case SSL_CLIENT_KEY_EXCHANGE:
3921 ret = ssl_parse_client_key_exchange( ssl );
3922 break;
3923
3924 case SSL_CERTIFICATE_VERIFY:
3925 ret = ssl_parse_certificate_verify( ssl );
3926 break;
3927
3928 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3929 ret = ssl_parse_change_cipher_spec( ssl );
3930 break;
3931
3932 case SSL_CLIENT_FINISHED:
3933 ret = ssl_parse_finished( ssl );
3934 break;
3935
3936 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003937 * ==> ( NewSessionTicket )
3938 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003939 * Finished
3940 */
3941 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003942#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003943 if( ssl->handshake->new_session_ticket != 0 )
3944 ret = ssl_write_new_session_ticket( ssl );
3945 else
Paul Bakkera503a632013-08-14 13:48:06 +02003946#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003947 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003948 break;
3949
3950 case SSL_SERVER_FINISHED:
3951 ret = ssl_write_finished( ssl );
3952 break;
3953
3954 case SSL_FLUSH_BUFFERS:
3955 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3956 ssl->state = SSL_HANDSHAKE_WRAPUP;
3957 break;
3958
3959 case SSL_HANDSHAKE_WRAPUP:
3960 ssl_handshake_wrapup( ssl );
3961 break;
3962
3963 default:
3964 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3965 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003966 }
3967
Paul Bakker5121ce52009-01-03 21:22:43 +00003968 return( ret );
3969}
Paul Bakker9af723c2014-05-01 13:03:14 +02003970#endif /* POLARSSL_SSL_SRV_C */