blob: 250f95fce844fb1fb2533f872e02ad45442b7b79 [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 ) */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100239 if( ( ret = md_hmac( md_info_from_type( POLARSSL_MD_SHA256 ),
240 ssl->ticket_keys->mac_key, 16,
241 start, p - start, p ) ) != 0 )
242 {
243 return( ret );
244 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200245 p += 32;
246
247 *tlen = p - start;
248
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200249 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200250
251 return( 0 );
252}
253
254/*
255 * Load session ticket (see ssl_write_ticket for structure)
256 */
257static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200259 size_t len )
260{
261 int ret;
262 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200263 unsigned char *key_name = buf;
264 unsigned char *iv = buf + 16;
265 unsigned char *enc_len_p = iv + 16;
266 unsigned char *ticket = enc_len_p + 2;
267 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200268 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200269 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100270 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200271
272 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200273
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200274 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200275 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
276
277 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
278 mac = ticket + enc_len;
279
280 if( len != enc_len + 66 )
281 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
282
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100283 /* Check name, in constant time though it's not a big secret */
284 diff = 0;
285 for( i = 0; i < 16; i++ )
286 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
287 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200288
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100290 if( ( ret = md_hmac( md_info_from_type( POLARSSL_MD_SHA256 ),
291 ssl->ticket_keys->mac_key, 16,
292 buf, len - 32, computed_mac ) ) != 0 )
293 {
294 return( ret );
295 }
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100296
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200297 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100298 diff |= mac[i] ^ computed_mac[i];
299
300 /* Now return if ticket is not authentic, since we want to avoid
301 * decrypting arbitrary attacker-chosen data */
302 if( diff != 0 )
303 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Decrypt */
306 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
307 enc_len, iv, ticket, ticket ) ) != 0 )
308 {
309 return( ret );
310 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200311
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200312 /* Check PKCS padding */
313 pad_len = ticket[enc_len - 1];
314
315 ret = 0;
316 for( i = 2; i < pad_len; i++ )
317 if( ticket[enc_len - i] != pad_len )
318 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
319 if( ret != 0 )
320 return( ret );
321
322 clear_len = enc_len - pad_len;
323
324 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
325
326 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200327 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
328 {
329 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100330 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200331 return( ret );
332 }
333
Paul Bakker606b4ba2013-08-14 16:52:14 +0200334#if defined(POLARSSL_HAVE_TIME)
335 /* Check if still valid */
336 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
337 {
338 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100339 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200340 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
341 }
342#endif
343
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200344 /*
345 * Keep the session ID sent by the client, since we MUST send it back to
346 * inform him we're accepting the ticket (RFC 5077 section 3.4)
347 */
348 session.length = ssl->session_negotiate->length;
349 memcpy( &session.id, ssl->session_negotiate->id, session.length );
350
351 ssl_session_free( ssl->session_negotiate );
352 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200353
354 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200355 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200356
357 return( 0 );
358}
Paul Bakkera503a632013-08-14 13:48:06 +0200359#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200360
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200361#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200362int ssl_set_client_transport_id( ssl_context *ssl,
363 const unsigned char *info,
364 size_t ilen )
365{
366 if( ssl->endpoint != SSL_IS_SERVER )
367 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
368
369 polarssl_free( ssl->cli_id );
370
371 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
372 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
373
374 memcpy( ssl->cli_id, info, ilen );
375 ssl->cli_id_len = ilen;
376
377 return( 0 );
378}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200379
380void ssl_set_dtls_cookies( ssl_context *ssl,
381 ssl_cookie_write_t *f_cookie_write,
382 ssl_cookie_check_t *f_cookie_check,
383 void *p_cookie )
384{
385 ssl->f_cookie_write = f_cookie_write;
386 ssl->f_cookie_check = f_cookie_check;
387 ssl->p_cookie = p_cookie;
388}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200389#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200390
Paul Bakker0be444a2013-08-27 21:55:01 +0200391#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200392/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200393 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100394 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200395 */
396static int ssl_sni_wrapper( ssl_context *ssl,
397 const unsigned char* name, size_t len )
398{
399 int ret;
400 ssl_key_cert *key_cert_ori = ssl->key_cert;
401
402 ssl->key_cert = NULL;
403 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200404 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200405
406 ssl->key_cert = key_cert_ori;
407
408 return( ret );
409}
410
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000412 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000413 size_t len )
414{
415 int ret;
416 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000417 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000418
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100419 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
420
Paul Bakker5701cdc2012-09-27 21:49:42 +0000421 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
422 if( servername_list_size + 2 != len )
423 {
424 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
425 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
426 }
427
428 p = buf + 2;
429 while( servername_list_size > 0 )
430 {
431 hostname_len = ( ( p[1] << 8 ) | p[2] );
432 if( hostname_len + 3 > servername_list_size )
433 {
434 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
435 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
436 }
437
438 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
439 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200440 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000441 if( ret != 0 )
442 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100443 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000444 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
445 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
447 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000448 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000449 }
450
451 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000452 p += hostname_len + 3;
453 }
454
455 if( servername_list_size != 0 )
456 {
457 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
458 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000459 }
460
461 return( 0 );
462}
Paul Bakker0be444a2013-08-27 21:55:01 +0200463#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000464
Paul Bakker48916f92012-09-16 19:57:18 +0000465static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000466 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000467 size_t len )
468{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000469 int ret;
470
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100471#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +0000472 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100473 {
474 /* Check verify-data in constant-time. The length OTOH is no secret */
475 if( len != 1 + ssl->verify_data_len ||
476 buf[0] != ssl->verify_data_len ||
477 safer_memcmp( buf + 1, ssl->peer_verify_data,
478 ssl->verify_data_len ) != 0 )
479 {
480 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
481
482 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
483 return( ret );
484
485 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
486 }
487 }
488 else
489#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000490 {
491 if( len != 1 || buf[0] != 0x0 )
492 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100493 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000494
495 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
496 return( ret );
497
Paul Bakker48916f92012-09-16 19:57:18 +0000498 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
499 }
500
501 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
502 }
Paul Bakker48916f92012-09-16 19:57:18 +0000503
504 return( 0 );
505}
506
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100507#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
508 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000509static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
510 const unsigned char *buf,
511 size_t len )
512{
513 size_t sig_alg_list_size;
514 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200515 const unsigned char *end = buf + len;
516 const int *md_cur;
517
Paul Bakker23f36802012-09-28 14:15:14 +0000518
519 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
520 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200521 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000522 {
523 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
524 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
525 }
526
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200527 /*
528 * For now, ignore the SignatureAlgorithm part and rely on offered
529 * ciphersuites only for that part. To be fixed later.
530 *
531 * So, just look at the HashAlgorithm part.
532 */
533 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
534 for( p = buf + 2; p < end; p += 2 ) {
535 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
536 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200537 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200538 }
Paul Bakker23f36802012-09-28 14:15:14 +0000539 }
Paul Bakker23f36802012-09-28 14:15:14 +0000540 }
541
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200542 /* Some key echanges do not need signatures at all */
543 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
544 return( 0 );
545
546have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000547 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
548 ssl->handshake->sig_alg ) );
549
550 return( 0 );
551}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100552#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
553 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000554
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200555#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200556static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
557 const unsigned char *buf,
558 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100559{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200560 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100561 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200562 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100563
564 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
565 if( list_size + 2 != len ||
566 list_size % 2 != 0 )
567 {
568 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
570 }
571
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200572 /* Should never happen unless client duplicates the extension */
573 if( ssl->handshake->curves != NULL )
574 {
575 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
576 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
577 }
578
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100579 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200580 * and leave room for a final 0 */
581 our_size = list_size / 2 + 1;
582 if( our_size > POLARSSL_ECP_DP_MAX )
583 our_size = POLARSSL_ECP_DP_MAX;
584
585 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
586 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
587
Paul Bakker9af723c2014-05-01 13:03:14 +0200588 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200589 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200590 ssl->handshake->curves = curves;
591
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200593 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100594 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200595 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200596
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200597 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100598 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200599 *curves++ = curve_info;
600 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100601 }
602
603 list_size -= 2;
604 p += 2;
605 }
606
607 return( 0 );
608}
609
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200610static int ssl_parse_supported_point_formats( ssl_context *ssl,
611 const unsigned char *buf,
612 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100613{
614 size_t list_size;
615 const unsigned char *p;
616
617 list_size = buf[0];
618 if( list_size + 1 != len )
619 {
620 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
621 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
622 }
623
624 p = buf + 2;
625 while( list_size > 0 )
626 {
627 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
628 p[0] == POLARSSL_ECP_PF_COMPRESSED )
629 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200630 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200631 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100632 return( 0 );
633 }
634
635 list_size--;
636 p++;
637 }
638
639 return( 0 );
640}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200641#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100642
Paul Bakker05decb22013-08-15 13:33:48 +0200643#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200644static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
645 const unsigned char *buf,
646 size_t len )
647{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200648 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200649 {
650 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
651 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
652 }
653
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200654 ssl->session_negotiate->mfl_code = buf[0];
655
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200656 return( 0 );
657}
Paul Bakker05decb22013-08-15 13:33:48 +0200658#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200659
Paul Bakker1f2bc622013-08-15 13:45:55 +0200660#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200661static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
662 const unsigned char *buf,
663 size_t len )
664{
665 if( len != 0 )
666 {
667 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
668 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
669 }
670
671 ((void) buf);
672
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100673 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
674 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200675
676 return( 0 );
677}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200678#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200679
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100680#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
681static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
682 const unsigned char *buf,
683 size_t len )
684{
685 if( len != 0 )
686 {
687 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
688 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
689 }
690
691 ((void) buf);
692
693 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
694 ssl->minor_ver != SSL_MINOR_VERSION_0 )
695 {
696 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
697 }
698
699 return( 0 );
700}
701#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
702
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200703#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
704static int ssl_parse_extended_ms_ext( ssl_context *ssl,
705 const unsigned char *buf,
706 size_t len )
707{
708 if( len != 0 )
709 {
710 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
711 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
712 }
713
714 ((void) buf);
715
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200716 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
717 ssl->minor_ver != SSL_MINOR_VERSION_0 )
718 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200719 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200720 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200721
722 return( 0 );
723}
724#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
725
Paul Bakkera503a632013-08-14 13:48:06 +0200726#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200727static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200728 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200729 size_t len )
730{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200731 int ret;
732
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200733 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
734 return( 0 );
735
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200736 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200737 ssl->handshake->new_session_ticket = 1;
738
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200739 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
740
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200741 if( len == 0 )
742 return( 0 );
743
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100744#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +0000745 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200746 {
747 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
748 return( 0 );
749 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100750#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751
752 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200753 * Failures are ok: just ignore the ticket and proceed.
754 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200755 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
756 {
757 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200758 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200759 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200760
761 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
762
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200763 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200764
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200765 /* Don't send a new ticket after all, this one is OK */
766 ssl->handshake->new_session_ticket = 0;
767
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200768 return( 0 );
769}
Paul Bakkera503a632013-08-14 13:48:06 +0200770#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200771
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200772#if defined(POLARSSL_SSL_ALPN)
773static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200774 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200775{
Paul Bakker14b16c62014-05-28 11:33:54 +0200776 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200777 const unsigned char *theirs, *start, *end;
778 const char **ours;
779
780 /* If ALPN not configured, just ignore the extension */
781 if( ssl->alpn_list == NULL )
782 return( 0 );
783
784 /*
785 * opaque ProtocolName<1..2^8-1>;
786 *
787 * struct {
788 * ProtocolName protocol_name_list<2..2^16-1>
789 * } ProtocolNameList;
790 */
791
792 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
793 if( len < 4 )
794 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
795
796 list_len = ( buf[0] << 8 ) | buf[1];
797 if( list_len != len - 2 )
798 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
799
800 /*
801 * Use our order of preference
802 */
803 start = buf + 2;
804 end = buf + len;
805 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
806 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200807 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200808 for( theirs = start; theirs != end; theirs += cur_len )
809 {
810 /* If the list is well formed, we should get equality first */
811 if( theirs > end )
812 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
813
814 cur_len = *theirs++;
815
816 /* Empty strings MUST NOT be included */
817 if( cur_len == 0 )
818 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
819
Paul Bakker14b16c62014-05-28 11:33:54 +0200820 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200821 memcmp( theirs, *ours, cur_len ) == 0 )
822 {
823 ssl->alpn_chosen = *ours;
824 return( 0 );
825 }
826 }
827 }
828
829 /* If we get there, no match was found */
830 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
831 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
832 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
833}
834#endif /* POLARSSL_SSL_ALPN */
835
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100836/*
837 * Auxiliary functions for ServerHello parsing and related actions
838 */
839
840#if defined(POLARSSL_X509_CRT_PARSE_C)
841/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100842 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100843 */
844#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100845static int ssl_check_key_curve( pk_context *pk,
846 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100847{
848 const ecp_curve_info **crv = curves;
849 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
850
851 while( *crv != NULL )
852 {
853 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100854 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100855 crv++;
856 }
857
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100858 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100859}
860#endif /* POLARSSL_ECDSA_C */
861
862/*
863 * Try picking a certificate for this ciphersuite,
864 * return 0 on success and -1 on failure.
865 */
866static int ssl_pick_cert( ssl_context *ssl,
867 const ssl_ciphersuite_t * ciphersuite_info )
868{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100869 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100870 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
871
872#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
873 if( ssl->handshake->sni_key_cert != NULL )
874 list = ssl->handshake->sni_key_cert;
875 else
876#endif
877 list = ssl->handshake->key_cert;
878
879 if( pk_alg == POLARSSL_PK_NONE )
880 return( 0 );
881
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000882 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
883
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100884 for( cur = list; cur != NULL; cur = cur->next )
885 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000886 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
887 cur->cert );
888
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100889 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000890 {
891 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100892 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000893 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200895 /*
896 * This avoids sending the client a cert it'll reject based on
897 * keyUsage or other extensions.
898 *
899 * It also allows the user to provision different certificates for
900 * different uses based on keyUsage, eg if they want to avoid signing
901 * and decrypting with the same RSA key.
902 */
903 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
904 SSL_IS_SERVER ) != 0 )
905 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000906 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
907 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200908 continue;
909 }
910
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100911#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100912 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100913 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000914 {
915 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100916 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000917 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100918#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100919
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100920 /*
921 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
922 * present them a SHA-higher cert rather than failing if it's the only
923 * one we got that satisfies the other conditions.
924 */
925 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
926 cur->cert->sig_md != POLARSSL_MD_SHA1 )
927 {
928 if( fallback == NULL )
929 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000930 {
931 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
932 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100933 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000934 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100935 }
936
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100937 /* If we get there, we got a winner */
938 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100939 }
940
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000941 if( cur == NULL )
942 cur = fallback;
943
944
945 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100946 if( cur != NULL )
947 {
948 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000949 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
950 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100951 return( 0 );
952 }
953
954 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100955}
956#endif /* POLARSSL_X509_CRT_PARSE_C */
957
958/*
959 * Check if a given ciphersuite is suitable for use with our config/keys/etc
960 * Sets ciphersuite_info only if the suite matches.
961 */
962static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
963 const ssl_ciphersuite_t **ciphersuite_info )
964{
965 const ssl_ciphersuite_t *suite_info;
966
967 suite_info = ssl_ciphersuite_from_id( suite_id );
968 if( suite_info == NULL )
969 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100970 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
971 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100972 }
973
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000974 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
975
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100976 if( suite_info->min_minor_ver > ssl->minor_ver ||
977 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000978 {
979 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100980 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000981 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100982
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100983#if defined(POLARSSL_SSL_PROTO_DTLS)
984 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
985 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
986 return( 0 );
987#endif
988
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100989 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
990 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
992 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100993 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000994 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100995
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
997 if( ssl_ciphersuite_uses_ec( suite_info ) &&
998 ( ssl->handshake->curves == NULL ||
999 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001000 {
1001 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1002 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001003 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001004 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001005#endif
1006
1007#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
1008 /* If the ciphersuite requires a pre-shared key and we don't
1009 * have one, skip it now rather than failing later */
1010 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1011 ssl->f_psk == NULL &&
1012 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1013 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001014 {
1015 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001016 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001017 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001018#endif
1019
1020#if defined(POLARSSL_X509_CRT_PARSE_C)
1021 /*
1022 * Final check: if ciphersuite requires us to have a
1023 * certificate/key of a particular type:
1024 * - select the appropriate certificate if we have one, or
1025 * - try the next ciphersuite if we don't
1026 * This must be done last since we modify the key_cert list.
1027 */
1028 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001029 {
1030 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1031 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001032 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001033 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001034#endif
1035
1036 *ciphersuite_info = suite_info;
1037 return( 0 );
1038}
1039
Paul Bakker78a8c712013-03-06 17:01:52 +01001040#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1041static int ssl_parse_client_hello_v2( ssl_context *ssl )
1042{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001043 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001044 unsigned int i, j;
1045 size_t n;
1046 unsigned int ciph_len, sess_len, chal_len;
1047 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001048 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001049 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001050
1051 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1052
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001053#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001054 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Paul Bakker78a8c712013-03-06 17:01:52 +01001055 {
1056 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1057
1058 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1059 return( ret );
1060
1061 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1062 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001063#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001064
1065 buf = ssl->in_hdr;
1066
1067 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1068
1069 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1070 buf[2] ) );
1071 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1072 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1073 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1074 buf[3], buf[4] ) );
1075
1076 /*
1077 * SSLv2 Client Hello
1078 *
1079 * Record layer:
1080 * 0 . 1 message length
1081 *
1082 * SSL layer:
1083 * 2 . 2 message type
1084 * 3 . 4 protocol version
1085 */
1086 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1087 buf[3] != SSL_MAJOR_VERSION_3 )
1088 {
1089 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1090 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1091 }
1092
1093 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1094
1095 if( n < 17 || n > 512 )
1096 {
1097 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1098 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1099 }
1100
1101 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001102 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1103 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001104
1105 if( ssl->minor_ver < ssl->min_minor_ver )
1106 {
1107 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001108 " [%d:%d] < [%d:%d]",
1109 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001110 ssl->min_major_ver, ssl->min_minor_ver ) );
1111
1112 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1113 SSL_ALERT_MSG_PROTOCOL_VERSION );
1114 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1115 }
1116
Paul Bakker2fbefde2013-06-29 16:01:15 +02001117 ssl->handshake->max_major_ver = buf[3];
1118 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001119
1120 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1121 {
1122 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1123 return( ret );
1124 }
1125
1126 ssl->handshake->update_checksum( ssl, buf + 2, n );
1127
1128 buf = ssl->in_msg;
1129 n = ssl->in_left - 5;
1130
1131 /*
1132 * 0 . 1 ciphersuitelist length
1133 * 2 . 3 session id length
1134 * 4 . 5 challenge length
1135 * 6 . .. ciphersuitelist
1136 * .. . .. session id
1137 * .. . .. challenge
1138 */
1139 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1140
1141 ciph_len = ( buf[0] << 8 ) | buf[1];
1142 sess_len = ( buf[2] << 8 ) | buf[3];
1143 chal_len = ( buf[4] << 8 ) | buf[5];
1144
1145 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1146 ciph_len, sess_len, chal_len ) );
1147
1148 /*
1149 * Make sure each parameter length is valid
1150 */
1151 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1152 {
1153 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1154 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1155 }
1156
1157 if( sess_len > 32 )
1158 {
1159 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1160 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1161 }
1162
1163 if( chal_len < 8 || chal_len > 32 )
1164 {
1165 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1166 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1167 }
1168
1169 if( n != 6 + ciph_len + sess_len + chal_len )
1170 {
1171 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1172 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1173 }
1174
1175 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1176 buf + 6, ciph_len );
1177 SSL_DEBUG_BUF( 3, "client hello, session id",
1178 buf + 6 + ciph_len, sess_len );
1179 SSL_DEBUG_BUF( 3, "client hello, challenge",
1180 buf + 6 + ciph_len + sess_len, chal_len );
1181
1182 p = buf + 6 + ciph_len;
1183 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001184 memset( ssl->session_negotiate->id, 0,
1185 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001186 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1187
1188 p += sess_len;
1189 memset( ssl->handshake->randbytes, 0, 64 );
1190 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1191
1192 /*
1193 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1194 */
1195 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1196 {
1197 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1198 {
1199 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001200#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001201 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker78a8c712013-03-06 17:01:52 +01001202 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001203 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1204 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001205
1206 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1207 return( ret );
1208
1209 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1210 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001211#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001212 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1213 break;
1214 }
1215 }
1216
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001217#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1218 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1219 {
1220 if( p[0] == 0 &&
Manuel Pégourié-Gonnarded99d702015-03-09 11:17:30 +00001221 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1222 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001223 {
1224 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1225
1226 if( ssl->minor_ver < ssl->max_minor_ver )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1229
1230 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1231 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1232
1233 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1234 }
1235
1236 break;
1237 }
1238 }
1239#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1240
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001241 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001242 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001243 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001244#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1245 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1246 {
1247 for( i = 0; ciphersuites[i] != 0; i++ )
1248#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001249 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001250 {
1251 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001252#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001253 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001254 if( p[0] != 0 ||
1255 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1256 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1257 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001258
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001259 got_common_suite = 1;
1260
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001261 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1262 &ciphersuite_info ) ) != 0 )
1263 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001264
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001265 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001266 goto have_ciphersuite_v2;
1267 }
1268 }
1269
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001270 if( got_common_suite )
1271 {
1272 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1273 "but none of them usable" ) );
1274 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1275 }
1276 else
1277 {
1278 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1279 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1280 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001281
1282have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001283 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1284
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001285 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001286 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001287 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001288
1289 /*
1290 * SSLv2 Client Hello relevant renegotiation security checks
1291 */
1292 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1293 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1294 {
1295 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1296
1297 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1298 return( ret );
1299
1300 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1301 }
1302
1303 ssl->in_left = 0;
1304 ssl->state++;
1305
1306 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1307
1308 return( 0 );
1309}
1310#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1311
Paul Bakker5121ce52009-01-03 21:22:43 +00001312static int ssl_parse_client_hello( ssl_context *ssl )
1313{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001314 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001315 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001316 unsigned int ciph_offset, comp_offset, ext_offset;
1317 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001318#if defined(POLARSSL_SSL_PROTO_DTLS)
1319 unsigned int cookie_offset, cookie_len;
1320#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001321 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001322#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001323 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001324#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001325 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001326 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001327 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001328 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001329
1330 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1331
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001332#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1333read_record_header:
1334#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001335 /*
1336 * If renegotiating, then the input was read with ssl_read_record(),
1337 * otherwise read it ourselves manually in order to support SSLv2
1338 * ClientHello, which doesn't use the same record layer format.
1339 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001340#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001341 if( ssl->renego_status == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001344 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1345 {
1346 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1347 return( ret );
1348 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 }
1350
1351 buf = ssl->in_hdr;
1352
Paul Bakker78a8c712013-03-06 17:01:52 +01001353#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001354#if defined(POLARSSL_SSL_PROTO_DTLS)
1355 if( ssl->transport == SSL_TRANSPORT_STREAM )
1356#endif
1357 if( ( buf[0] & 0x80 ) != 0 )
1358 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001359#endif
1360
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001361 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001362
Paul Bakkerec636f32012-09-09 19:17:02 +00001363 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001364 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001365 *
1366 * Record layer:
1367 * 0 . 0 message type
1368 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001369 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001370 * 3 . 4 message length
1371 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001372 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1373 buf[0] ) );
1374
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001375 if( buf[0] != SSL_MSG_HANDSHAKE )
1376 {
1377 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1378 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1379 }
1380
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001381 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1382 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1383
1384 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1385 buf[1], buf[2] ) );
1386
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001387 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001388
1389 /* According to RFC 5246 Appendix E.1, the version here is typically
1390 * "{03,00}, the lowest version number supported by the client, [or] the
1391 * value of ClientHello.client_version", so the only meaningful check here
1392 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001393 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001395 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1396 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1397 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001399 /* For DTLS if this is the initial handshake, remember the client sequence
1400 * number to use it in our next message (RFC 6347 4.2.1) */
1401#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001402 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1403#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001404 && ssl->renego_status == SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001405#endif
1406 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001407 {
1408 /* Epoch should be 0 for initial handshakes */
1409 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1412 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1413 }
1414
1415 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001416
1417#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1418 if( ssl_dtls_replay_check( ssl ) != 0 )
1419 {
1420 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1421 ssl->next_record_offset = 0;
1422 ssl->in_left = 0;
1423 goto read_record_header;
1424 }
1425
1426 /* No MAC to check yet, so we can update right now */
1427 ssl_dtls_replay_update( ssl );
1428#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001429 }
1430#endif /* POLARSSL_SSL_PROTO_DTLS */
1431
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001432 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001434#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001435 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001436 {
1437 /* Set by ssl_read_record() */
1438 msg_len = ssl->in_hslen;
1439 }
1440 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001441#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001442 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001443 if( msg_len > SSL_MAX_CONTENT_LEN )
1444 {
1445 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1446 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1447 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001448
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001449 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1450 {
1451 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1452 return( ret );
1453 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001454
1455 /* Done reading this record, get ready for the next one */
1456#if defined(POLARSSL_SSL_PROTO_DTLS)
1457 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1458 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1459 else
1460#endif
1461 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001462 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001463
1464 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001465
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001466 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001467
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001468 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001469
1470 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001471 * Handshake layer:
1472 * 0 . 0 handshake type
1473 * 1 . 3 handshake length
1474 * 4 . 5 DTLS only: message seqence number
1475 * 6 . 8 DTLS only: fragment offset
1476 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001477 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001478 if( msg_len < ssl_hs_hdr_len( ssl ) )
1479 {
1480 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1481 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1482 }
1483
1484 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1485
1486 if( buf[0] != SSL_HS_CLIENT_HELLO )
1487 {
1488 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1490 }
1491
1492 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1493 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1494
1495 /* We don't support fragmentation of ClientHello (yet?) */
1496 if( buf[1] != 0 ||
1497 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1498 {
1499 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1500 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1501 }
1502
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001503#if defined(POLARSSL_SSL_PROTO_DTLS)
1504 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1505 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001506 /*
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001507 * Copy the client's handshake message_seq on initial handshakes,
1508 * check sequence number on renego.
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001509 */
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001510#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001511 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001512 {
1513 /* This couldn't be done in ssl_prepare_handshake_record() */
1514 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1515 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001516
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001517 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1518 {
1519 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1520 "%d (expected %d)", cli_msg_seq,
1521 ssl->handshake->in_msg_seq ) );
1522 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1523 }
1524
1525 ssl->handshake->in_msg_seq++;
1526 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001527 else
1528#endif
1529 {
1530 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1531 ssl->in_msg[5];
1532 ssl->handshake->out_msg_seq = cli_msg_seq;
1533 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
1534 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001535
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001536 /*
1537 * For now we don't support fragmentation, so make sure
1538 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001539 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001540 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1541 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1542 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001543 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001544 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1545 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001546 }
1547#endif /* POLARSSL_SSL_PROTO_DTLS */
1548
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001549 buf += ssl_hs_hdr_len( ssl );
1550 msg_len -= ssl_hs_hdr_len( ssl );
1551
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001552 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001553 * ClientHello layer:
1554 * 0 . 1 protocol version
1555 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1556 * 34 . 35 session id length (1 byte)
1557 * 35 . 34+x session id
1558 * 35+x . 35+x DTLS only: cookie length (1 byte)
1559 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001560 * .. . .. ciphersuite list length (2 bytes)
1561 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001562 * .. . .. compression alg. list length (1 byte)
1563 * .. . .. compression alg. list
1564 * .. . .. extensions length (2 bytes, optional)
1565 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001566 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001567
1568 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001569 * Minimal length (with everything empty and extensions ommitted) is
1570 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1571 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001572 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001574 {
1575 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1576 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1577 }
1578
1579 /*
1580 * Check and save the protocol version
1581 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001582 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001583
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001584 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001585 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001586
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001587 ssl->handshake->max_major_ver = ssl->major_ver;
1588 ssl->handshake->max_minor_ver = ssl->minor_ver;
1589
1590 if( ssl->major_ver < ssl->min_major_ver ||
1591 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001592 {
1593 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001594 " [%d:%d] < [%d:%d]",
1595 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001596 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001597
1598 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1599 SSL_ALERT_MSG_PROTOCOL_VERSION );
1600
1601 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1602 }
1603
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001604 if( ssl->major_ver > ssl->max_major_ver )
1605 {
1606 ssl->major_ver = ssl->max_major_ver;
1607 ssl->minor_ver = ssl->max_minor_ver;
1608 }
1609 else if( ssl->minor_ver > ssl->max_minor_ver )
1610 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001611
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001612 /*
1613 * Save client random (inc. Unix time)
1614 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001615 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001616
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001617 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001618
1619 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001620 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001621 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001622 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001623
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001624 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001625 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001626 {
1627 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1628 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1629 }
1630
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001631 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001632
Paul Bakker48916f92012-09-16 19:57:18 +00001633 ssl->session_negotiate->length = sess_len;
1634 memset( ssl->session_negotiate->id, 0,
1635 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001636 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001637 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001638
1639 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001640 * Check the cookie length and content
1641 */
1642#if defined(POLARSSL_SSL_PROTO_DTLS)
1643 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1644 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001645 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001646 cookie_len = buf[cookie_offset];
1647
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001648 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001649 {
1650 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1651 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1652 }
1653
1654 SSL_DEBUG_BUF( 3, "client hello, cookie",
1655 buf + cookie_offset + 1, cookie_len );
1656
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001657#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001658 if( ssl->f_cookie_check != NULL
1659#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001660 && ssl->renego_status == SSL_INITIAL_HANDSHAKE
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001661#endif
1662 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001663 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001664 if( ssl->f_cookie_check( ssl->p_cookie,
1665 buf + cookie_offset + 1, cookie_len,
1666 ssl->cli_id, ssl->cli_id_len ) != 0 )
1667 {
1668 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1669 ssl->handshake->verify_cookie_len = 1;
1670 }
1671 else
1672 {
1673 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1674 ssl->handshake->verify_cookie_len = 0;
1675 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001676 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001677 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001678#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001679 {
1680 /* We know we didn't send a cookie, so it should be empty */
1681 if( cookie_len != 0 )
1682 {
1683 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1684 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1685 }
1686
1687 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1688 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001689
1690 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001691 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001692 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001693 ciph_offset = cookie_offset + 1 + cookie_len;
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001694 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001695 else
Manuel Pégourié-Gonnarda06d7fe2015-03-13 10:36:55 +00001696#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001697 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001698
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001699 ciph_len = ( buf[ciph_offset + 0] << 8 )
1700 | ( buf[ciph_offset + 1] );
1701
1702 if( ciph_len < 2 ||
1703 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1704 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001705 {
1706 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1707 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1708 }
1709
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001710 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1711 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001712
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001713 /*
1714 * Check the compression algorithms length and pick one
1715 */
1716 comp_offset = ciph_offset + 2 + ciph_len;
1717
1718 comp_len = buf[comp_offset];
1719
1720 if( comp_len < 1 ||
1721 comp_len > 16 ||
1722 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001723 {
1724 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1725 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1726 }
1727
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001728 SSL_DEBUG_BUF( 3, "client hello, compression",
1729 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001730
1731 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001732#if defined(POLARSSL_ZLIB_SUPPORT)
1733 for( i = 0; i < comp_len; ++i )
1734 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001735 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 {
Paul Bakker48916f92012-09-16 19:57:18 +00001737 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001738 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001739 }
1740 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001741#endif
1742
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001743 /* See comments in ssl_write_client_hello() */
1744#if defined(POLARSSL_SSL_PROTO_DTLS)
1745 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1746 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1747#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001748
Paul Bakkerec636f32012-09-09 19:17:02 +00001749 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001750 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001751 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001752 ext_offset = comp_offset + 1 + comp_len;
1753 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001754 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001755 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001756 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001757 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1759 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001760
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001761 ext_len = ( buf[ext_offset + 0] << 8 )
1762 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001763
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001764 if( ( ext_len > 0 && ext_len < 4 ) ||
1765 msg_len != ext_offset + 2 + ext_len )
1766 {
1767 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1768 SSL_DEBUG_BUF( 3, "client hello extensions",
1769 buf + ext_offset + 2, ext_len );
1770 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001771 }
1772 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001773 else
1774 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001775
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001776 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001777
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001778 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001779 {
1780 unsigned int ext_id = ( ( ext[0] << 8 )
1781 | ( ext[1] ) );
1782 unsigned int ext_size = ( ( ext[2] << 8 )
1783 | ( ext[3] ) );
1784
1785 if( ext_size + 4 > ext_len )
1786 {
1787 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1788 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1789 }
1790 switch( ext_id )
1791 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001792#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001793 case TLS_EXT_SERVERNAME:
1794 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1795 if( ssl->f_sni == NULL )
1796 break;
1797
1798 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1799 if( ret != 0 )
1800 return( ret );
1801 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001802#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 case TLS_EXT_RENEGOTIATION_INFO:
1805 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001806#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001807 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001808#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001809
Paul Bakker23f36802012-09-28 14:15:14 +00001810 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1811 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001812 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001813 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001814
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001815#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1816 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001817 case TLS_EXT_SIG_ALG:
1818 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001819#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001820 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Paul Bakker23f36802012-09-28 14:15:14 +00001821 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001822#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001823
1824 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1825 if( ret != 0 )
1826 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001827 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001828#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1829 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001830
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001831#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001832 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1833 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1834
1835 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1836 if( ret != 0 )
1837 return( ret );
1838 break;
1839
1840 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1841 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001842 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001843
1844 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1845 if( ret != 0 )
1846 return( ret );
1847 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001848#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001849
Paul Bakker05decb22013-08-15 13:33:48 +02001850#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001851 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1852 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1853
1854 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1855 if( ret != 0 )
1856 return( ret );
1857 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001858#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001859
Paul Bakker1f2bc622013-08-15 13:45:55 +02001860#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001861 case TLS_EXT_TRUNCATED_HMAC:
1862 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1863
1864 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1865 if( ret != 0 )
1866 return( ret );
1867 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001868#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001869
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001870#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1871 case TLS_EXT_ENCRYPT_THEN_MAC:
1872 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1873
1874 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1875 if( ret != 0 )
1876 return( ret );
1877 break;
1878#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1879
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001880#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1881 case TLS_EXT_EXTENDED_MASTER_SECRET:
1882 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1883
1884 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1885 if( ret != 0 )
1886 return( ret );
1887 break;
1888#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1889
Paul Bakkera503a632013-08-14 13:48:06 +02001890#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001891 case TLS_EXT_SESSION_TICKET:
1892 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1893
1894 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1895 if( ret != 0 )
1896 return( ret );
1897 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001898#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001899
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001900#if defined(POLARSSL_SSL_ALPN)
1901 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001902 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001903
1904 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1905 if( ret != 0 )
1906 return( ret );
1907 break;
1908#endif /* POLARSSL_SSL_SESSION_TICKETS */
1909
Paul Bakker48916f92012-09-16 19:57:18 +00001910 default:
1911 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1912 ext_id ) );
1913 }
1914
1915 ext_len -= 4 + ext_size;
1916 ext += 4 + ext_size;
1917
1918 if( ext_len > 0 && ext_len < 4 )
1919 {
1920 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1921 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1922 }
1923 }
1924
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001925#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1926 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1927 {
Manuel Pégourié-Gonnarded99d702015-03-09 11:17:30 +00001928 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE >> 8 ) & 0xff ) &&
1929 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV_VALUE ) & 0xff ) )
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001930 {
1931 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1932
1933 if( ssl->minor_ver < ssl->max_minor_ver )
1934 {
1935 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1936
1937 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1938 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1939
1940 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1941 }
1942
1943 break;
1944 }
1945 }
1946#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1947
Paul Bakker48916f92012-09-16 19:57:18 +00001948 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001949 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1950 */
1951 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1952 {
1953 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1954 {
1955 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001956#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001957 if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001958 {
1959 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1960
1961 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1962 return( ret );
1963
1964 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1965 }
Manuel Pégourié-Gonnard69849f82015-03-10 11:54:02 +00001966#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001967 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1968 break;
1969 }
1970 }
1971
1972 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001973 * Renegotiation security checks
1974 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001975 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001976 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1977 {
1978 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1979 handshake_failure = 1;
1980 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001981#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001982 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001983 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1984 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001985 {
1986 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001987 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001988 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001989 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001990 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1991 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001992 {
1993 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001994 handshake_failure = 1;
1995 }
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00001996 else if( ssl->renego_status == SSL_RENEGOTIATION_IN_PROGRESS &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001997 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1998 renegotiation_info_seen == 1 )
1999 {
2000 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
2001 handshake_failure = 1;
2002 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002003#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00002004
2005 if( handshake_failure == 1 )
2006 {
2007 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
2008 return( ret );
2009
Paul Bakker48916f92012-09-16 19:57:18 +00002010 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
2011 }
Paul Bakker380da532012-04-18 16:10:25 +00002012
Paul Bakker41c83d32013-03-20 14:39:14 +01002013 /*
2014 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002015 * (At the end because we need information from the EC-based extensions
2016 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002017 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002018 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002019 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002020 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002021#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002022 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002023 {
2024 for( i = 0; ciphersuites[i] != 0; i++ )
2025#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002026 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01002027 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002028 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002029#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002030 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002031 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2032 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2033 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002034
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002035 got_common_suite = 1;
2036
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002037 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2038 &ciphersuite_info ) ) != 0 )
2039 return( ret );
2040
2041 if( ciphersuite_info != NULL )
2042 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002043 }
2044 }
2045
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002046 if( got_common_suite )
2047 {
2048 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2049 "but none of them usable" ) );
2050 ssl_send_fatal_handshake_failure( ssl );
2051 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2052 }
2053 else
2054 {
2055 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2056 ssl_send_fatal_handshake_failure( ssl );
2057 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2058 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002059
2060have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002061 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2062
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002063 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002064 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2065 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2066
Paul Bakker5121ce52009-01-03 21:22:43 +00002067 ssl->state++;
2068
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002069#if defined(POLARSSL_SSL_PROTO_DTLS)
2070 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2071 ssl_recv_flight_completed( ssl );
2072#endif
2073
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2075
2076 return( 0 );
2077}
2078
Paul Bakker1f2bc622013-08-15 13:45:55 +02002079#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002080static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2081 unsigned char *buf,
2082 size_t *olen )
2083{
2084 unsigned char *p = buf;
2085
2086 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2087 {
2088 *olen = 0;
2089 return;
2090 }
2091
2092 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2093
2094 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2095 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2096
2097 *p++ = 0x00;
2098 *p++ = 0x00;
2099
2100 *olen = 4;
2101}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002102#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002103
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002104#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2105static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2106 unsigned char *buf,
2107 size_t *olen )
2108{
2109 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002110 const ssl_ciphersuite_t *suite = NULL;
2111 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002112
2113 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2114 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2115 {
2116 *olen = 0;
2117 return;
2118 }
2119
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002120 /*
2121 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2122 * from a client and then selects a stream or Authenticated Encryption
2123 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2124 * encrypt-then-MAC response extension back to the client."
2125 */
2126 if( ( suite = ssl_ciphersuite_from_id(
2127 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2128 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2129 cipher->mode != POLARSSL_MODE_CBC )
2130 {
2131 *olen = 0;
2132 return;
2133 }
2134
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002135 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2136
2137 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2138 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2139
2140 *p++ = 0x00;
2141 *p++ = 0x00;
2142
2143 *olen = 4;
2144}
2145#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2146
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002147#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2148static void ssl_write_extended_ms_ext( ssl_context *ssl,
2149 unsigned char *buf,
2150 size_t *olen )
2151{
2152 unsigned char *p = buf;
2153
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002154 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2155 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002156 {
2157 *olen = 0;
2158 return;
2159 }
2160
2161 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2162 "extension" ) );
2163
2164 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2165 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2166
2167 *p++ = 0x00;
2168 *p++ = 0x00;
2169
2170 *olen = 4;
2171}
2172#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2173
Paul Bakkera503a632013-08-14 13:48:06 +02002174#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002175static void ssl_write_session_ticket_ext( ssl_context *ssl,
2176 unsigned char *buf,
2177 size_t *olen )
2178{
2179 unsigned char *p = buf;
2180
2181 if( ssl->handshake->new_session_ticket == 0 )
2182 {
2183 *olen = 0;
2184 return;
2185 }
2186
2187 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2188
2189 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2190 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2191
2192 *p++ = 0x00;
2193 *p++ = 0x00;
2194
2195 *olen = 4;
2196}
Paul Bakkera503a632013-08-14 13:48:06 +02002197#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002198
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002199static void ssl_write_renegotiation_ext( ssl_context *ssl,
2200 unsigned char *buf,
2201 size_t *olen )
2202{
2203 unsigned char *p = buf;
2204
2205 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2206 {
2207 *olen = 0;
2208 return;
2209 }
2210
2211 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2212
2213 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2214 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2215
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002216#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002217 if( ssl->renego_status != SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002218 {
2219 *p++ = 0x00;
2220 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2221 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002222
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002223 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2224 p += ssl->verify_data_len;
2225 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2226 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002227
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002228 *olen = 5 + ssl->verify_data_len * 2;
2229 }
2230 else
2231#endif /* POLARSSL_SSL_RENEGOTIATION */
2232 {
2233 *p++ = 0x00;
2234 *p++ = 0x01;
2235 *p++ = 0x00;
2236
2237 *olen = 5;
2238 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002239}
2240
Paul Bakker05decb22013-08-15 13:33:48 +02002241#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002242static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2243 unsigned char *buf,
2244 size_t *olen )
2245{
2246 unsigned char *p = buf;
2247
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002248 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2249 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002250 *olen = 0;
2251 return;
2252 }
2253
2254 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2255
2256 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2257 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2258
2259 *p++ = 0x00;
2260 *p++ = 1;
2261
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002262 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002263
2264 *olen = 5;
2265}
Paul Bakker05decb22013-08-15 13:33:48 +02002266#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002267
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002268#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002269static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2270 unsigned char *buf,
2271 size_t *olen )
2272{
2273 unsigned char *p = buf;
2274 ((void) ssl);
2275
Paul Bakker677377f2013-10-28 12:54:26 +01002276 if( ( ssl->handshake->cli_exts &
2277 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2278 {
2279 *olen = 0;
2280 return;
2281 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002282
2283 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2284
2285 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2286 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2287
2288 *p++ = 0x00;
2289 *p++ = 2;
2290
2291 *p++ = 1;
2292 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2293
2294 *olen = 6;
2295}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002296#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002297
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002298#if defined(POLARSSL_SSL_ALPN )
2299static void ssl_write_alpn_ext( ssl_context *ssl,
2300 unsigned char *buf, size_t *olen )
2301{
2302 if( ssl->alpn_chosen == NULL )
2303 {
2304 *olen = 0;
2305 return;
2306 }
2307
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002308 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002309
2310 /*
2311 * 0 . 1 ext identifier
2312 * 2 . 3 ext length
2313 * 4 . 5 protocol list length
2314 * 6 . 6 protocol name length
2315 * 7 . 7+n protocol name
2316 */
2317 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2318 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2319
2320 *olen = 7 + strlen( ssl->alpn_chosen );
2321
2322 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2323 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2324
2325 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2326 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2327
2328 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2329
2330 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2331}
2332#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2333
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002334#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002335static int ssl_write_hello_verify_request( ssl_context *ssl )
2336{
2337 int ret;
2338 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002339 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002340
2341 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2342
2343 /*
2344 * struct {
2345 * ProtocolVersion server_version;
2346 * opaque cookie<0..2^8-1>;
2347 * } HelloVerifyRequest;
2348 */
2349
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002350 /* The RFC is not clear on this point, but sending the actual negotiated
2351 * version looks like the most interoperable thing to do. */
2352 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002353 ssl->transport, p );
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +00002354 SSL_DEBUG_BUF( 3, "server version", p, 2 );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002355 p += 2;
2356
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002357 /* If we get here, f_cookie_check is not null */
2358 if( ssl->f_cookie_write == NULL )
2359 {
2360 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2361 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2362 }
2363
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002364 /* Skip length byte until we know the length */
2365 cookie_len_byte = p++;
2366
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002367 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2368 &p, ssl->out_buf + SSL_BUFFER_LEN,
2369 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002370 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002371 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002372 return( ret );
2373 }
2374
2375 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2376
2377 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002378
2379 ssl->out_msglen = p - ssl->out_msg;
2380 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2381 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2382
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002383 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002384
2385 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2386 {
2387 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2388 return( ret );
2389 }
2390
2391 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2392
2393 return( 0 );
2394}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002395#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002396
Paul Bakker5121ce52009-01-03 21:22:43 +00002397static int ssl_write_server_hello( ssl_context *ssl )
2398{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002399#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002401#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002402 int ret;
2403 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002404 unsigned char *buf, *p;
2405
2406 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2407
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002408#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002409 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002410 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002411 {
2412 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2413 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2414
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002415 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002416 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002417#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002418
Paul Bakkera9a028e2013-11-21 17:31:06 +01002419 if( ssl->f_rng == NULL )
2420 {
2421 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2422 return( POLARSSL_ERR_SSL_NO_RNG );
2423 }
2424
Paul Bakker5121ce52009-01-03 21:22:43 +00002425 /*
2426 * 0 . 0 handshake type
2427 * 1 . 3 handshake length
2428 * 4 . 5 protocol version
2429 * 6 . 9 UNIX time()
2430 * 10 . 37 random bytes
2431 */
2432 buf = ssl->out_msg;
2433 p = buf + 4;
2434
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002435 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2436 ssl->transport, p );
2437 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002438
2439 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002440 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002442#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 t = time( NULL );
2444 *p++ = (unsigned char)( t >> 24 );
2445 *p++ = (unsigned char)( t >> 16 );
2446 *p++ = (unsigned char)( t >> 8 );
2447 *p++ = (unsigned char)( t );
2448
2449 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002450#else
2451 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2452 return( ret );
2453
2454 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002455#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002456
Paul Bakkera3d195c2011-11-27 21:07:34 +00002457 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2458 return( ret );
2459
2460 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Paul Bakker48916f92012-09-16 19:57:18 +00002462 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002463
2464 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2465
2466 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002467 * Resume is 0 by default, see ssl_handshake_init().
2468 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2469 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002471 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002472#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard852a6d32015-03-19 16:15:20 +00002473 ssl->renego_status == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002474#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002475 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002476 ssl->f_get_cache != NULL &&
2477 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2478 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002479 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002480 ssl->handshake->resume = 1;
2481 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002482
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002483 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 {
2485 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002486 * New session, create a new session id,
2487 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002488 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002489 ssl->state++;
2490
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002491#if defined(POLARSSL_HAVE_TIME)
2492 ssl->session_negotiate->start = time( NULL );
2493#endif
2494
Paul Bakkera503a632013-08-14 13:48:06 +02002495#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002496 if( ssl->handshake->new_session_ticket != 0 )
2497 {
2498 ssl->session_negotiate->length = n = 0;
2499 memset( ssl->session_negotiate->id, 0, 32 );
2500 }
2501 else
2502#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002503 {
2504 ssl->session_negotiate->length = n = 32;
2505 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002506 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002507 return( ret );
2508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
2510 else
2511 {
2512 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002513 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002514 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002515 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002517
2518 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2519 {
2520 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2521 return( ret );
2522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
2524
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002525 /*
2526 * 38 . 38 session id length
2527 * 39 . 38+n session id
2528 * 39+n . 40+n chosen ciphersuite
2529 * 41+n . 41+n chosen compression alg.
2530 * 42+n . 43+n extensions length
2531 * 44+n . 43+n+m extensions
2532 */
2533 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002534 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2535 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2538 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2539 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002540 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002541
Paul Bakker48916f92012-09-16 19:57:18 +00002542 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2543 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2544 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002546 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2547 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002548 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002549 ssl->session_negotiate->compression ) );
2550
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002551 /*
2552 * First write extensions, then the total length
2553 */
2554 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2555 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002556
Paul Bakker05decb22013-08-15 13:33:48 +02002557#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002558 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2559 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002560#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002561
Paul Bakker1f2bc622013-08-15 13:45:55 +02002562#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002563 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2564 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002565#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002566
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002567#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2568 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2569 ext_len += olen;
2570#endif
2571
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002572#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2573 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2574 ext_len += olen;
2575#endif
2576
Paul Bakkera503a632013-08-14 13:48:06 +02002577#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002578 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2579 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002580#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002581
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002582#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002583 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2584 ext_len += olen;
2585#endif
2586
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002587#if defined(POLARSSL_SSL_ALPN)
2588 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2589 ext_len += olen;
2590#endif
2591
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002592 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002593
Paul Bakkera7036632014-04-30 10:15:38 +02002594 if( ext_len > 0 )
2595 {
2596 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2597 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2598 p += ext_len;
2599 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002600
2601 ssl->out_msglen = p - buf;
2602 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2603 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2604
2605 ret = ssl_write_record( ssl );
2606
2607 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2608
2609 return( ret );
2610}
2611
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002612#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2613 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002614 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2615 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002616static int ssl_write_certificate_request( ssl_context *ssl )
2617{
Paul Bakkered27a042013-04-18 22:46:23 +02002618 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619
2620 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2621
2622 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002624 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2625 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002626 {
2627 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2628 ssl->state++;
2629 return( 0 );
2630 }
2631
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002632 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2633 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002634}
2635#else
2636static int ssl_write_certificate_request( ssl_context *ssl )
2637{
2638 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2639 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002640 size_t dn_size, total_dn_size; /* excluding length bytes */
2641 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002643 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002644
2645 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2646
2647 ssl->state++;
2648
Paul Bakkerfbb17802013-04-17 19:10:21 +02002649 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002650 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002652 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002653 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002654 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002655 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002656 return( 0 );
2657 }
2658
2659 /*
2660 * 0 . 0 handshake type
2661 * 1 . 3 handshake length
2662 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002663 * 5 .. m-1 cert types
2664 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002665 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002666 * n .. n+1 length of all DNs
2667 * n+2 .. n+3 length of DN 1
2668 * n+4 .. ... Distinguished Name #1
2669 * ... .. ... length of DN 2, etc.
2670 */
2671 buf = ssl->out_msg;
2672 p = buf + 4;
2673
2674 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002675 * Supported certificate types
2676 *
2677 * ClientCertificateType certificate_types<1..2^8-1>;
2678 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002679 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002680 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002681
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002682#if defined(POLARSSL_RSA_C)
2683 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2684#endif
2685#if defined(POLARSSL_ECDSA_C)
2686 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2687#endif
2688
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002689 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002690 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002691
Paul Bakker577e0062013-08-28 11:57:20 +02002692 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002693#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002694 /*
2695 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002696 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002697 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2698 *
2699 * struct {
2700 * HashAlgorithm hash;
2701 * SignatureAlgorithm signature;
2702 * } SignatureAndHashAlgorithm;
2703 *
2704 * enum { (255) } HashAlgorithm;
2705 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002706 */
Paul Bakker21dca692013-01-03 11:41:08 +01002707 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002708 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002709 /*
2710 * Only use current running hash algorithm that is already required
2711 * for requested ciphersuite.
2712 */
Paul Bakker926af752012-11-23 13:38:07 +01002713 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2714
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002715 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2716 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002717 {
2718 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2719 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002720
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002721 /*
2722 * Supported signature algorithms
2723 */
2724#if defined(POLARSSL_RSA_C)
2725 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2726 p[2 + sa_len++] = SSL_SIG_RSA;
2727#endif
2728#if defined(POLARSSL_ECDSA_C)
2729 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2730 p[2 + sa_len++] = SSL_SIG_ECDSA;
2731#endif
Paul Bakker926af752012-11-23 13:38:07 +01002732
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002733 p[0] = (unsigned char)( sa_len >> 8 );
2734 p[1] = (unsigned char)( sa_len );
2735 sa_len += 2;
2736 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002737 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002738#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002739
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002740 /*
2741 * DistinguishedName certificate_authorities<0..2^16-1>;
2742 * opaque DistinguishedName<1..2^16-1>;
2743 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002744 p += 2;
2745 crt = ssl->ca_chain;
2746
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002747 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002748 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 {
2750 if( p - buf > 4096 )
2751 break;
2752
Paul Bakker926af752012-11-23 13:38:07 +01002753 dn_size = crt->subject_raw.len;
2754 *p++ = (unsigned char)( dn_size >> 8 );
2755 *p++ = (unsigned char)( dn_size );
2756 memcpy( p, crt->subject_raw.p, dn_size );
2757 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002758
Paul Bakker926af752012-11-23 13:38:07 +01002759 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2760
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002761 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002762 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 }
2764
Paul Bakker926af752012-11-23 13:38:07 +01002765 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002766 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2767 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002768 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2769 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002770
2771 ret = ssl_write_record( ssl );
2772
2773 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2774
2775 return( ret );
2776}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002777#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2778 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002779 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2780 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002782#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2783 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2784static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2785{
2786 int ret;
2787
2788 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2789 {
2790 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2791 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2792 }
2793
2794 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2795 pk_ec( *ssl_own_key( ssl ) ),
2796 POLARSSL_ECDH_OURS ) ) != 0 )
2797 {
2798 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2799 return( ret );
2800 }
2801
2802 return( 0 );
2803}
2804#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2805 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2806
Paul Bakker41c83d32013-03-20 14:39:14 +01002807static int ssl_write_server_key_exchange( ssl_context *ssl )
2808{
Paul Bakker23986e52011-04-24 08:57:21 +00002809 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002810 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002811 const ssl_ciphersuite_t *ciphersuite_info =
2812 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002813
2814#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2815 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2816 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002817 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002818 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002819 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002820 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002821 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002822 ((void) dig_signed);
2823 ((void) dig_signed_len);
2824#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002825
Paul Bakker5121ce52009-01-03 21:22:43 +00002826 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2827
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002828#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2829 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2830 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002831 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2832 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2833 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 {
2835 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2836 ssl->state++;
2837 return( 0 );
2838 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002839#endif
2840
2841#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2842 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2843 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2844 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2845 {
2846 ssl_get_ecdh_params_from_cert( ssl );
2847
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002848 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002849 ssl->state++;
2850 return( 0 );
2851 }
2852#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002853
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002854#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2855 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2856 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2857 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002858 {
2859 /* TODO: Support identity hints */
2860 *(p++) = 0x00;
2861 *(p++) = 0x00;
2862
2863 n += 2;
2864 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002865#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2866 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002867
2868#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2869 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2870 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2871 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002872 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002873 /*
2874 * Ephemeral DH parameters:
2875 *
2876 * struct {
2877 * opaque dh_p<1..2^16-1>;
2878 * opaque dh_g<1..2^16-1>;
2879 * opaque dh_Ys<1..2^16-1>;
2880 * } ServerDHParams;
2881 */
2882 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2883 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2884 {
2885 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2886 return( ret );
2887 }
Paul Bakker48916f92012-09-16 19:57:18 +00002888
Paul Bakker41c83d32013-03-20 14:39:14 +01002889 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002890 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2891 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002892 {
2893 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2894 return( ret );
2895 }
2896
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002897 dig_signed = p;
2898 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002899
2900 p += len;
2901 n += len;
2902
Paul Bakker41c83d32013-03-20 14:39:14 +01002903 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2904 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2905 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2906 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2907 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002908#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2909 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002910
Gergely Budai987bfb52014-01-19 21:48:42 +01002911#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002912 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002913 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2914 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002915 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002916 /*
2917 * Ephemeral ECDH parameters:
2918 *
2919 * struct {
2920 * ECParameters curve_params;
2921 * ECPoint public;
2922 * } ServerECDHParams;
2923 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002924 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002925#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002926 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002927
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002928 /* Match our preference list against the offered curves */
2929 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2930 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2931 if( (*curve)->grp_id == *gid )
2932 goto curve_matching_done;
2933
2934curve_matching_done:
2935#else
2936 curve = ssl->handshake->curves;
2937#endif
2938
2939 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002940 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002941 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2942 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002943 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002944
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002945 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002946
Paul Bakker41c83d32013-03-20 14:39:14 +01002947 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002948 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002949 {
2950 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2951 return( ret );
2952 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002953
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002954 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2955 p, SSL_MAX_CONTENT_LEN - n,
2956 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002957 {
2958 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2959 return( ret );
2960 }
2961
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002962 dig_signed = p;
2963 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964
2965 p += len;
2966 n += len;
2967
Paul Bakker41c83d32013-03-20 14:39:14 +01002968 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2969 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002970#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002971
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002972#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002973 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2974 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002975 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002976 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2977 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002978 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002979 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002980 unsigned int hashlen = 0;
2981 unsigned char hash[64];
2982 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002983
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002984 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002985 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2986 */
Paul Bakker577e0062013-08-28 11:57:20 +02002987#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002988 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2989 {
2990 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2991
2992 if( md_alg == POLARSSL_MD_NONE )
2993 {
2994 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002995 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002996 }
2997 }
Paul Bakker577e0062013-08-28 11:57:20 +02002998 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002999#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003000#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3001 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02003002 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003003 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
3004 {
3005 md_alg = POLARSSL_MD_SHA1;
3006 }
3007 else
Paul Bakker577e0062013-08-28 11:57:20 +02003008#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003009 {
3010 md_alg = POLARSSL_MD_NONE;
3011 }
3012
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003013 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003014 * Compute the hash to be signed
3015 */
Paul Bakker577e0062013-08-28 11:57:20 +02003016#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3017 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003018 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003019 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003020 md5_context md5;
3021 sha1_context sha1;
3022
Paul Bakker5b4af392014-06-26 12:09:34 +02003023 md5_init( &md5 );
3024 sha1_init( &sha1 );
3025
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003026 /*
3027 * digitally-signed struct {
3028 * opaque md5_hash[16];
3029 * opaque sha_hash[20];
3030 * };
3031 *
3032 * md5_hash
3033 * MD5(ClientHello.random + ServerHello.random
3034 * + ServerParams);
3035 * sha_hash
3036 * SHA(ClientHello.random + ServerHello.random
3037 * + ServerParams);
3038 */
3039 md5_starts( &md5 );
3040 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003041 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003042 md5_finish( &md5, hash );
3043
3044 sha1_starts( &sha1 );
3045 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003046 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003047 sha1_finish( &sha1, hash + 16 );
3048
3049 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02003050
3051 md5_free( &md5 );
3052 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053 }
3054 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3056 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003057#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3058 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003059 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003060 {
3061 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003062 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063
Paul Bakker84bbeb52014-07-01 14:53:22 +02003064 md_init( &ctx );
3065
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003066 /* Info from md_alg will be used instead */
3067 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003068
3069 /*
3070 * digitally-signed struct {
3071 * opaque client_random[32];
3072 * opaque server_random[32];
3073 * ServerDHParams params;
3074 * };
3075 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003076 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077 {
3078 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3079 return( ret );
3080 }
3081
3082 md_starts( &ctx );
3083 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003084 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003085 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003086 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003087 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003088 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003089#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3090 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003091 {
3092 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003093 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003094 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003095
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003096 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
Manuel Pégourié-Gonnardca878db2015-03-24 12:13:30 +01003097 (unsigned int) ( md_get_size( md_info_from_type( md_alg ) ) ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003098
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003099 /*
3100 * Make the signature
3101 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003102 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003103 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003104 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3105 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003106 }
Paul Bakker23f36802012-09-28 14:15:14 +00003107
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003108#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003109 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3110 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003111 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003112 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003113
3114 n += 2;
3115 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003116#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003117
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003118 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003119 p + 2 , &signature_len,
3120 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003121 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003122 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003123 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003124 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003125
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003126 *(p++) = (unsigned char)( signature_len >> 8 );
3127 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003128 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003129
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003130 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003131
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003132 p += signature_len;
3133 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003134 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003135#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003136 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3137 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003138
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003139 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003140 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3141 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3142
3143 ssl->state++;
3144
3145 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3146 {
3147 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3148 return( ret );
3149 }
3150
3151 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3152
3153 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003154}
3155
3156static int ssl_write_server_hello_done( ssl_context *ssl )
3157{
3158 int ret;
3159
3160 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3161
3162 ssl->out_msglen = 4;
3163 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3164 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3165
3166 ssl->state++;
3167
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003168#if defined(POLARSSL_SSL_PROTO_DTLS)
3169 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3170 ssl_send_flight_completed( ssl );
3171#endif
3172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3174 {
3175 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3176 return( ret );
3177 }
3178
3179 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3180
3181 return( 0 );
3182}
3183
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003184#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3185 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3186static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3187 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003188{
3189 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003190 size_t n;
3191
3192 /*
3193 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3194 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195 if( *p + 2 > end )
3196 {
3197 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3198 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3199 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003200
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003201 n = ( (*p)[0] << 8 ) | (*p)[1];
3202 *p += 2;
3203
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003204 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003205 {
3206 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3207 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3208 }
3209
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003210 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003211 {
3212 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3213 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3214 }
3215
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003216 *p += n;
3217
Paul Bakker70df2fb2013-04-17 17:19:09 +02003218 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3219
Paul Bakker70df2fb2013-04-17 17:19:09 +02003220 return( ret );
3221}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003222#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3223 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003224
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003225#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3226 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003227static int ssl_parse_encrypted_pms( ssl_context *ssl,
3228 const unsigned char *p,
3229 const unsigned char *end,
3230 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003231{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003232 int ret;
3233 size_t len = pk_get_len( ssl_own_key( ssl ) );
3234 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003235 unsigned char ver[2];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003236 unsigned char fake_pms[48], peer_pms[48];
3237 unsigned char mask;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003238 size_t i;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003239
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003240 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003241 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003242 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003243 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3244 }
3245
3246 /*
3247 * Decrypt the premaster using own private RSA key
3248 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003249#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3250 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003251 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3252 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003253 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3254 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003255 {
3256 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3257 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3258 }
3259 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003260#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003261
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003262 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003263 {
3264 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3265 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3266 }
3267
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003268 ssl_write_version( ssl->handshake->max_major_ver,
3269 ssl->handshake->max_minor_ver,
3270 ssl->transport, ver );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003271 /*
3272 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3273 * must not cause the connection to end immediately; instead, send a
3274 * bad_record_mac later in the handshake.
3275 * Also, avoid data-dependant branches here to protect against
3276 * timing-based variants.
3277 */
3278 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3279 if( ret != 0 )
3280 return( ret );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003281
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003282 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003283 peer_pms, &ssl->handshake->pmslen,
3284 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003285 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003286
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003287 ret |= ssl->handshake->pmslen - 48;
Manuel Pégourié-Gonnardf7d2bba2015-02-09 11:42:40 +00003288 ret |= peer_pms[0] - ver[0];
3289 ret |= peer_pms[1] - ver[1];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003290
3291#if defined(POLARSSL_SSL_DEBUG_ALL)
3292 if( ret != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003293 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003294#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003295
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003296 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3297 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3298 {
3299 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3300 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003301 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003302 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003303
Manuel Pégourié-Gonnard2ee8d242015-02-11 15:29:15 +00003304 mask = (unsigned char)( - ( ret != 0 ) ); /* ret ? 0xff : 0x00 */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003305 for( i = 0; i < ssl->handshake->pmslen; i++ )
3306 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3307
3308 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003309}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003310#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3311 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003312
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003313#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003314static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3315 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003316{
Paul Bakker6db455e2013-09-18 17:29:31 +02003317 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003318 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003319
Paul Bakker6db455e2013-09-18 17:29:31 +02003320 if( ssl->f_psk == NULL &&
3321 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3322 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003323 {
3324 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3325 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3326 }
3327
3328 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003329 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003330 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003331 if( *p + 2 > end )
3332 {
3333 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3334 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3335 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003336
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003337 n = ( (*p)[0] << 8 ) | (*p)[1];
3338 *p += 2;
3339
3340 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003341 {
3342 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3343 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3344 }
3345
Paul Bakker6db455e2013-09-18 17:29:31 +02003346 if( ssl->f_psk != NULL )
3347 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003348 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003349 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3350 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003351 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003352 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003353 /* Identity is not a big secret since clients send it in the clear,
3354 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003355 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003356 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003357 {
3358 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3359 }
3360 }
3361
3362 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003363 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003364 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003365 if( ( ret = ssl_send_alert_message( ssl,
3366 SSL_ALERT_LEVEL_FATAL,
3367 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3368 {
3369 return( ret );
3370 }
3371
3372 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003373 }
3374
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003375 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003376
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003377 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003378}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003379#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003380
Paul Bakker5121ce52009-01-03 21:22:43 +00003381static int ssl_parse_client_key_exchange( ssl_context *ssl )
3382{
Paul Bakker23986e52011-04-24 08:57:21 +00003383 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003384 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003385 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003386
Paul Bakker41c83d32013-03-20 14:39:14 +01003387 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003388
3389 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3390
3391 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3392 {
3393 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3394 return( ret );
3395 }
3396
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003397 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3398 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003399
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3401 {
3402 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003404 }
3405
3406 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3407 {
3408 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 }
3411
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003413 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003415 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003416 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003417 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003419 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003420
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003421 if( p != end )
3422 {
3423 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3424 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3425 }
3426
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003427 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003428
3429 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3430 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003431 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003432 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003433 {
3434 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3435 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3436 }
3437
3438 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003439 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003440 else
3441#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003442#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003443 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3444 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3445 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003446 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003447 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3448 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3449 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003450 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003451 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003452 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003453 {
3454 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3455 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3456 }
3457
3458 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3459
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003460 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3461 &ssl->handshake->pmslen,
3462 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003463 POLARSSL_MPI_MAX_SIZE,
3464 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003465 {
3466 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3467 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3468 }
3469
3470 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003472 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003473#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003474 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3475 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3476 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003477#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3478 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003479 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003480 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003481 {
3482 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3483 return( ret );
3484 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003485
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003486 if( p != end )
3487 {
3488 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3490 }
3491
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003492 if( ( ret = ssl_psk_derive_premaster( ssl,
3493 ciphersuite_info->key_exchange ) ) != 0 )
3494 {
3495 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3496 return( ret );
3497 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003500#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003501#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3502 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3503 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003504 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3505 {
3506 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3507 return( ret );
3508 }
3509
3510 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3511 {
3512 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3513 return( ret );
3514 }
3515
3516 if( ( ret = ssl_psk_derive_premaster( ssl,
3517 ciphersuite_info->key_exchange ) ) != 0 )
3518 {
3519 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3520 return( ret );
3521 }
3522 }
3523 else
3524#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003525#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3526 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3527 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003528 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3529 {
3530 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3531 return( ret );
3532 }
3533 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3534 {
3535 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3536 return( ret );
3537 }
3538
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003539 if( p != end )
3540 {
3541 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3542 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3543 }
3544
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003545 if( ( ret = ssl_psk_derive_premaster( ssl,
3546 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003547 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003548 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3549 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003550 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003551 }
3552 else
3553#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003554#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3555 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3556 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003557 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3558 {
3559 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3560 return( ret );
3561 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003562
3563 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3564 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003565 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003566 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3567 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003568 }
3569
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003570 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3571
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003572 if( ( ret = ssl_psk_derive_premaster( ssl,
3573 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003574 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003575 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003576 return( ret );
3577 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003578 }
3579 else
3580#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003581#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3582 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003583 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003584 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003585 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003586 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003587 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003588 }
3589 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003590 else
3591#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3592 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003593 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003594 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003595 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003596
Paul Bakkerff60ee62010-03-16 21:09:09 +00003597 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3598 {
3599 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3600 return( ret );
3601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003602
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 ssl->state++;
3604
3605 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3606
3607 return( 0 );
3608}
3609
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003610#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3611 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003612 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3613 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003614static int ssl_parse_certificate_verify( ssl_context *ssl )
3615{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003616 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003617
3618 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3619
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003620 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003621 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003622 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003623 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003624 {
3625 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3626 ssl->state++;
3627 return( 0 );
3628 }
3629
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003630 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3631 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003632}
3633#else
3634static int ssl_parse_certificate_verify( ssl_context *ssl )
3635{
3636 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003637 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003638 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003639 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003640 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003641#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003642 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003643#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003644 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003645 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3646
3647 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3648
3649 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003650 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003651 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003652 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3653 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003654 {
3655 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3656 ssl->state++;
3657 return( 0 );
3658 }
3659
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003660 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003661 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662
3663 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3664 {
3665 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3666 return( ret );
3667 }
3668
3669 ssl->state++;
3670
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003671 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3672 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003673 {
3674 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003675 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003676 }
3677
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003678 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003679
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003680 /*
3681 * struct {
3682 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3683 * opaque signature<0..2^16-1>;
3684 * } DigitallySigned;
3685 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003686#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3687 defined(POLARSSL_SSL_PROTO_TLS1_1)
3688 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003689 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003690 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003691 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003692
3693 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3694 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3695 POLARSSL_PK_ECDSA ) )
3696 {
3697 hash_start += 16;
3698 hashlen -= 16;
3699 md_alg = POLARSSL_MD_SHA1;
3700 }
Paul Bakker926af752012-11-23 13:38:07 +01003701 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003702 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003703#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3704 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003705#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3706 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003707 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003708 if( i + 2 > ssl->in_hslen )
3709 {
3710 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3711 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3712 }
3713
Paul Bakker5121ce52009-01-03 21:22:43 +00003714 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003715 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003717 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003718 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003719 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3720 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003721 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3722 }
3723
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003724 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003725
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003726 /* Info from md_alg will be used instead */
3727 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003728
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003729 i++;
3730
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003731 /*
3732 * Signature
3733 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003734 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003735 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003736 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003737 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3738 " for verify message" ) );
3739 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003740 }
3741
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003742 /*
3743 * Check the certificate's key type matches the signature alg
3744 */
3745 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3746 {
3747 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3748 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3749 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003750
3751 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003752 }
3753 else
3754#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3755 {
3756 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003757 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003758 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003759
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003760 if( i + 2 > ssl->in_hslen )
3761 {
3762 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3763 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3764 }
3765
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003766 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3767 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003768
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003769 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 {
3771 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003772 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773 }
3774
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003775 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003776 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003777 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003779 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003780 return( ret );
3781 }
3782
3783 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3784
Paul Bakkered27a042013-04-18 22:46:23 +02003785 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003786}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003787#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3788 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3789 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003790
Paul Bakkera503a632013-08-14 13:48:06 +02003791#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003792static int ssl_write_new_session_ticket( ssl_context *ssl )
3793{
3794 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003795 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003796 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003797
3798 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3799
3800 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3801 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3802
3803 /*
3804 * struct {
3805 * uint32 ticket_lifetime_hint;
3806 * opaque ticket<0..2^16-1>;
3807 * } NewSessionTicket;
3808 *
3809 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3810 * 8 . 9 ticket_len (n)
3811 * 10 . 9+n ticket content
3812 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003813
3814 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3815 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3816 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3817 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003818
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003819 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3820 {
3821 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3822 tlen = 0;
3823 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003824
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003825 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3826 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003827
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003828 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003829
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003830 /*
3831 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3832 * ChangeCipherSpec share the same state.
3833 */
3834 ssl->handshake->new_session_ticket = 0;
3835
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003836 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3837 {
3838 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3839 return( ret );
3840 }
3841
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003842 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3843
3844 return( 0 );
3845}
Paul Bakkera503a632013-08-14 13:48:06 +02003846#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003847
Paul Bakker5121ce52009-01-03 21:22:43 +00003848/*
Paul Bakker1961b702013-01-25 14:49:24 +01003849 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003850 */
Paul Bakker1961b702013-01-25 14:49:24 +01003851int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003852{
3853 int ret = 0;
3854
Paul Bakker1961b702013-01-25 14:49:24 +01003855 if( ssl->state == SSL_HANDSHAKE_OVER )
3856 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003857
Paul Bakker1961b702013-01-25 14:49:24 +01003858 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3859
3860 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3861 return( ret );
3862
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003863#if defined(POLARSSL_SSL_PROTO_DTLS)
3864 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3865 ssl->handshake != NULL &&
3866 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3867 {
3868 if( ( ret = ssl_resend( ssl ) ) != 0 )
3869 return( ret );
3870 }
3871#endif
3872
Paul Bakker1961b702013-01-25 14:49:24 +01003873 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003874 {
Paul Bakker1961b702013-01-25 14:49:24 +01003875 case SSL_HELLO_REQUEST:
3876 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003877 break;
3878
Paul Bakker1961b702013-01-25 14:49:24 +01003879 /*
3880 * <== ClientHello
3881 */
3882 case SSL_CLIENT_HELLO:
3883 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003884 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003885
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003886#if defined(POLARSSL_SSL_PROTO_DTLS)
3887 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3888 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3889#endif
3890
Paul Bakker1961b702013-01-25 14:49:24 +01003891 /*
3892 * ==> ServerHello
3893 * Certificate
3894 * ( ServerKeyExchange )
3895 * ( CertificateRequest )
3896 * ServerHelloDone
3897 */
3898 case SSL_SERVER_HELLO:
3899 ret = ssl_write_server_hello( ssl );
3900 break;
3901
3902 case SSL_SERVER_CERTIFICATE:
3903 ret = ssl_write_certificate( ssl );
3904 break;
3905
3906 case SSL_SERVER_KEY_EXCHANGE:
3907 ret = ssl_write_server_key_exchange( ssl );
3908 break;
3909
3910 case SSL_CERTIFICATE_REQUEST:
3911 ret = ssl_write_certificate_request( ssl );
3912 break;
3913
3914 case SSL_SERVER_HELLO_DONE:
3915 ret = ssl_write_server_hello_done( ssl );
3916 break;
3917
3918 /*
3919 * <== ( Certificate/Alert )
3920 * ClientKeyExchange
3921 * ( CertificateVerify )
3922 * ChangeCipherSpec
3923 * Finished
3924 */
3925 case SSL_CLIENT_CERTIFICATE:
3926 ret = ssl_parse_certificate( ssl );
3927 break;
3928
3929 case SSL_CLIENT_KEY_EXCHANGE:
3930 ret = ssl_parse_client_key_exchange( ssl );
3931 break;
3932
3933 case SSL_CERTIFICATE_VERIFY:
3934 ret = ssl_parse_certificate_verify( ssl );
3935 break;
3936
3937 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3938 ret = ssl_parse_change_cipher_spec( ssl );
3939 break;
3940
3941 case SSL_CLIENT_FINISHED:
3942 ret = ssl_parse_finished( ssl );
3943 break;
3944
3945 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003946 * ==> ( NewSessionTicket )
3947 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003948 * Finished
3949 */
3950 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003951#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003952 if( ssl->handshake->new_session_ticket != 0 )
3953 ret = ssl_write_new_session_ticket( ssl );
3954 else
Paul Bakkera503a632013-08-14 13:48:06 +02003955#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003956 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003957 break;
3958
3959 case SSL_SERVER_FINISHED:
3960 ret = ssl_write_finished( ssl );
3961 break;
3962
3963 case SSL_FLUSH_BUFFERS:
3964 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3965 ssl->state = SSL_HANDSHAKE_WRAPUP;
3966 break;
3967
3968 case SSL_HANDSHAKE_WRAPUP:
3969 ssl_handshake_wrapup( ssl );
3970 break;
3971
3972 default:
3973 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3974 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003975 }
3976
Paul Bakker5121ce52009-01-03 21:22:43 +00003977 return( ret );
3978}
Paul Bakker9af723c2014-05-01 13:03:14 +02003979#endif /* POLARSSL_SSL_SRV_C */