blob: 59c1c1e775212e1b2c1e74932f2b95bb8310a4ab [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)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/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
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
32#include "polarssl/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)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <stdlib.h>
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020044#define polarssl_malloc malloc
45#define polarssl_free free
46#endif
47
Paul Bakkerfa9b1002013-07-03 15:31:03 +020048#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000049#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020050#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Paul Bakkera503a632013-08-14 13:48:06 +020052#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020053/* Implementation that should never be optimized out by the compiler */
54static void polarssl_zeroize( void *v, size_t n ) {
55 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
56}
57
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020058/*
59 * Serialize a session in the following format:
60 * 0 . n-1 session structure, n = sizeof(ssl_session)
61 * n . n+2 peer_cert length = m (0 if no certificate)
62 * n+3 . n+2+m peer cert ASN.1
63 *
64 * Assumes ticket is NULL (always true on server side).
65 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020066static int ssl_save_session( const ssl_session *session,
67 unsigned char *buf, size_t buf_len,
68 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020069{
70 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020071 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020073 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020076 if( left < sizeof( ssl_session ) )
77 return( -1 );
78
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020079 memcpy( p, session, sizeof( ssl_session ) );
80 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020081 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020082
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084 if( session->peer_cert == NULL )
85 cert_len = 0;
86 else
87 cert_len = session->peer_cert->raw.len;
88
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020089 if( left < 3 + cert_len )
90 return( -1 );
91
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020092 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
93 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
94 *p++ = (unsigned char)( cert_len & 0xFF );
95
96 if( session->peer_cert != NULL )
97 memcpy( p, session->peer_cert->raw.p, cert_len );
98
99 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200101
102 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200103
104 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200105}
106
107/*
108 * Unserialise session, see ssl_save_session()
109 */
110static int ssl_load_session( ssl_session *session,
111 const unsigned char *buf, size_t len )
112{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200113 const unsigned char *p = buf;
114 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200116 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118
119 if( p + sizeof( ssl_session ) > end )
120 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
121
122 memcpy( session, p, sizeof( ssl_session ) );
123 p += sizeof( ssl_session );
124
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200126 if( p + 3 > end )
127 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
128
129 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
130 p += 3;
131
132 if( cert_len == 0 )
133 {
134 session->peer_cert = NULL;
135 }
136 else
137 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200138 int ret;
139
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200140 if( p + cert_len > end )
141 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
142
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200143 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200144
145 if( session->peer_cert == NULL )
146 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
147
Paul Bakkerb6b09562013-09-18 14:17:41 +0200148 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200149
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200150 if( ( ret = x509_crt_parse_der( session->peer_cert,
151 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200152 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200155 session->peer_cert = NULL;
156 return( ret );
157 }
158
159 p += cert_len;
160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200162
163 if( p != end )
164 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
165
166 return( 0 );
167}
168
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200169/*
170 * Create session ticket, secured as recommended in RFC 5077 section 4:
171 *
172 * struct {
173 * opaque key_name[16];
174 * opaque iv[16];
175 * opaque encrypted_state<0..2^16-1>;
176 * opaque mac[32];
177 * } ticket;
178 *
179 * (the internal state structure differs, however).
180 */
181static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
182{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200183 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200184 unsigned char * const start = ssl->out_msg + 10;
185 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200186 unsigned char *state;
187 unsigned char iv[16];
188 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200189
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200190 *tlen = 0;
191
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200192 if( ssl->ticket_keys == NULL )
193 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
194
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200195 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200196 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200197 p += 16;
198
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200199 /* Generate and write IV (with a copy for aes_crypt) */
200 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
201 return( ret );
202 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200203 p += 16;
204
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200205 /*
206 * Dump session state
207 *
208 * After the session state itself, we still need room for 16 bytes of
209 * padding and 32 bytes of MAC, so there's only so much room left
210 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200211 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200212 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200213 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 &clear_len ) != 0 )
215 {
216 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
217 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200218 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200219
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 /* Apply PKCS padding */
221 pad_len = 16 - clear_len % 16;
222 enc_len = clear_len + pad_len;
223 for( i = clear_len; i < enc_len; i++ )
224 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200225
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200226 /* Encrypt */
227 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
228 enc_len, iv, state, state ) ) != 0 )
229 {
230 return( ret );
231 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200232
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200233 /* Write length */
234 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
235 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
236 p = state + enc_len;
237
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200238 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
239 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200240 p += 32;
241
242 *tlen = p - start;
243
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200244 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200245
246 return( 0 );
247}
248
249/*
250 * Load session ticket (see ssl_write_ticket for structure)
251 */
252static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200253 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200254 size_t len )
255{
256 int ret;
257 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200258 unsigned char *key_name = buf;
259 unsigned char *iv = buf + 16;
260 unsigned char *enc_len_p = iv + 16;
261 unsigned char *ticket = enc_len_p + 2;
262 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200263 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200264 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100265 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266
267 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200268
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200269 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
271
272 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
273 mac = ticket + enc_len;
274
275 if( len != enc_len + 66 )
276 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
277
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100278 /* Check name, in constant time though it's not a big secret */
279 diff = 0;
280 for( i = 0; i < 16; i++ )
281 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
282 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200283
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100284 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200285 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
286 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100287
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200288 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289 diff |= mac[i] ^ computed_mac[i];
290
291 /* Now return if ticket is not authentic, since we want to avoid
292 * decrypting arbitrary attacker-chosen data */
293 if( diff != 0 )
294 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200295
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200296 /* Decrypt */
297 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
298 enc_len, iv, ticket, ticket ) ) != 0 )
299 {
300 return( ret );
301 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200302
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200303 /* Check PKCS padding */
304 pad_len = ticket[enc_len - 1];
305
306 ret = 0;
307 for( i = 2; i < pad_len; i++ )
308 if( ticket[enc_len - i] != pad_len )
309 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
310 if( ret != 0 )
311 return( ret );
312
313 clear_len = enc_len - pad_len;
314
315 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
316
317 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200318 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
319 {
320 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100321 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200322 return( ret );
323 }
324
Paul Bakker606b4ba2013-08-14 16:52:14 +0200325#if defined(POLARSSL_HAVE_TIME)
326 /* Check if still valid */
327 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
328 {
329 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100330 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200331 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
332 }
333#endif
334
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200335 /*
336 * Keep the session ID sent by the client, since we MUST send it back to
337 * inform him we're accepting the ticket (RFC 5077 section 3.4)
338 */
339 session.length = ssl->session_negotiate->length;
340 memcpy( &session.id, ssl->session_negotiate->id, session.length );
341
342 ssl_session_free( ssl->session_negotiate );
343 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200344
345 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200346 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200347
348 return( 0 );
349}
Paul Bakkera503a632013-08-14 13:48:06 +0200350#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200351
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200352#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200353int ssl_set_client_transport_id( ssl_context *ssl,
354 const unsigned char *info,
355 size_t ilen )
356{
357 if( ssl->endpoint != SSL_IS_SERVER )
358 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
359
360 polarssl_free( ssl->cli_id );
361
362 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
363 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
364
365 memcpy( ssl->cli_id, info, ilen );
366 ssl->cli_id_len = ilen;
367
368 return( 0 );
369}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200370
371void ssl_set_dtls_cookies( ssl_context *ssl,
372 ssl_cookie_write_t *f_cookie_write,
373 ssl_cookie_check_t *f_cookie_check,
374 void *p_cookie )
375{
376 ssl->f_cookie_write = f_cookie_write;
377 ssl->f_cookie_check = f_cookie_check;
378 ssl->p_cookie = p_cookie;
379}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200380#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200381
Paul Bakker0be444a2013-08-27 21:55:01 +0200382#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200383/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200384 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100385 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200386 */
387static int ssl_sni_wrapper( ssl_context *ssl,
388 const unsigned char* name, size_t len )
389{
390 int ret;
391 ssl_key_cert *key_cert_ori = ssl->key_cert;
392
393 ssl->key_cert = NULL;
394 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200395 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200396
397 ssl->key_cert = key_cert_ori;
398
399 return( ret );
400}
401
Paul Bakker5701cdc2012-09-27 21:49:42 +0000402static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000403 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 size_t len )
405{
406 int ret;
407 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000408 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000409
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100410 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
411
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
413 if( servername_list_size + 2 != len )
414 {
415 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
416 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
417 }
418
419 p = buf + 2;
420 while( servername_list_size > 0 )
421 {
422 hostname_len = ( ( p[1] << 8 ) | p[2] );
423 if( hostname_len + 3 > servername_list_size )
424 {
425 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
426 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
427 }
428
429 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
430 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200431 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000432 if( ret != 0 )
433 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100434 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000435 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
436 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
438 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000439 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000440 }
441
442 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000443 p += hostname_len + 3;
444 }
445
446 if( servername_list_size != 0 )
447 {
448 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000450 }
451
452 return( 0 );
453}
Paul Bakker0be444a2013-08-27 21:55:01 +0200454#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000455
Paul Bakker48916f92012-09-16 19:57:18 +0000456static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000457 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000458 size_t len )
459{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000460 int ret;
461
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100462#if defined(POLARSSL_SSL_RENEGOTIATION)
463 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
464 {
465 /* Check verify-data in constant-time. The length OTOH is no secret */
466 if( len != 1 + ssl->verify_data_len ||
467 buf[0] != ssl->verify_data_len ||
468 safer_memcmp( buf + 1, ssl->peer_verify_data,
469 ssl->verify_data_len ) != 0 )
470 {
471 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
472
473 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
474 return( ret );
475
476 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
477 }
478 }
479 else
480#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000481 {
482 if( len != 1 || buf[0] != 0x0 )
483 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100484 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000485
486 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
487 return( ret );
488
Paul Bakker48916f92012-09-16 19:57:18 +0000489 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
490 }
491
492 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
493 }
Paul Bakker48916f92012-09-16 19:57:18 +0000494
495 return( 0 );
496}
497
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100498#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
499 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000500static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
501 const unsigned char *buf,
502 size_t len )
503{
504 size_t sig_alg_list_size;
505 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200506 const unsigned char *end = buf + len;
507 const int *md_cur;
508
Paul Bakker23f36802012-09-28 14:15:14 +0000509
510 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
511 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200512 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000513 {
514 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
515 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
516 }
517
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200518 /*
519 * For now, ignore the SignatureAlgorithm part and rely on offered
520 * ciphersuites only for that part. To be fixed later.
521 *
522 * So, just look at the HashAlgorithm part.
523 */
524 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
525 for( p = buf + 2; p < end; p += 2 ) {
526 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
527 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200528 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200529 }
Paul Bakker23f36802012-09-28 14:15:14 +0000530 }
Paul Bakker23f36802012-09-28 14:15:14 +0000531 }
532
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200533 /* Some key echanges do not need signatures at all */
534 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
535 return( 0 );
536
537have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000538 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
539 ssl->handshake->sig_alg ) );
540
541 return( 0 );
542}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100543#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
544 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000545
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200546#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200547static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
548 const unsigned char *buf,
549 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100550{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200551 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100552 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100554
555 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
556 if( list_size + 2 != len ||
557 list_size % 2 != 0 )
558 {
559 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
560 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
561 }
562
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200563 /* Should never happen unless client duplicates the extension */
564 if( ssl->handshake->curves != NULL )
565 {
566 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
567 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
568 }
569
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100570 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200571 * and leave room for a final 0 */
572 our_size = list_size / 2 + 1;
573 if( our_size > POLARSSL_ECP_DP_MAX )
574 our_size = POLARSSL_ECP_DP_MAX;
575
576 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
577 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
578
Paul Bakker9af723c2014-05-01 13:03:14 +0200579 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200580 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200581 ssl->handshake->curves = curves;
582
Paul Bakker41c83d32013-03-20 14:39:14 +0100583 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200584 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100585 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200586 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200587
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100589 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200590 *curves++ = curve_info;
591 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100592 }
593
594 list_size -= 2;
595 p += 2;
596 }
597
598 return( 0 );
599}
600
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200601static int ssl_parse_supported_point_formats( ssl_context *ssl,
602 const unsigned char *buf,
603 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100604{
605 size_t list_size;
606 const unsigned char *p;
607
608 list_size = buf[0];
609 if( list_size + 1 != len )
610 {
611 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
612 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
613 }
614
615 p = buf + 2;
616 while( list_size > 0 )
617 {
618 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
619 p[0] == POLARSSL_ECP_PF_COMPRESSED )
620 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200621 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200622 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100623 return( 0 );
624 }
625
626 list_size--;
627 p++;
628 }
629
630 return( 0 );
631}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200632#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100633
Paul Bakker05decb22013-08-15 13:33:48 +0200634#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200635static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
636 const unsigned char *buf,
637 size_t len )
638{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200639 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200640 {
641 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
642 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
643 }
644
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200645 ssl->session_negotiate->mfl_code = buf[0];
646
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200647 return( 0 );
648}
Paul Bakker05decb22013-08-15 13:33:48 +0200649#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200650
Paul Bakker1f2bc622013-08-15 13:45:55 +0200651#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200652static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
653 const unsigned char *buf,
654 size_t len )
655{
656 if( len != 0 )
657 {
658 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
659 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
660 }
661
662 ((void) buf);
663
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100664 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
665 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200666
667 return( 0 );
668}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200669#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200670
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100671#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
672static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
673 const unsigned char *buf,
674 size_t len )
675{
676 if( len != 0 )
677 {
678 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
679 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
680 }
681
682 ((void) buf);
683
684 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
685 ssl->minor_ver != SSL_MINOR_VERSION_0 )
686 {
687 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
688 }
689
690 return( 0 );
691}
692#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
693
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200694#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
695static int ssl_parse_extended_ms_ext( ssl_context *ssl,
696 const unsigned char *buf,
697 size_t len )
698{
699 if( len != 0 )
700 {
701 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
702 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
703 }
704
705 ((void) buf);
706
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200707 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
708 ssl->minor_ver != SSL_MINOR_VERSION_0 )
709 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200710 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200711 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200712
713 return( 0 );
714}
715#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
716
Paul Bakkera503a632013-08-14 13:48:06 +0200717#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200718static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200719 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720 size_t len )
721{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200722 int ret;
723
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200724 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
725 return( 0 );
726
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200727 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200728 ssl->handshake->new_session_ticket = 1;
729
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200730 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
731
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200732 if( len == 0 )
733 return( 0 );
734
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100735#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200736 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
737 {
738 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
739 return( 0 );
740 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100741#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200742
743 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744 * Failures are ok: just ignore the ticket and proceed.
745 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200746 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
747 {
748 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200749 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200750 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751
752 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
753
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200754 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200755
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200756 /* Don't send a new ticket after all, this one is OK */
757 ssl->handshake->new_session_ticket = 0;
758
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200759 return( 0 );
760}
Paul Bakkera503a632013-08-14 13:48:06 +0200761#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200762
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200763#if defined(POLARSSL_SSL_ALPN)
764static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200765 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200766{
Paul Bakker14b16c62014-05-28 11:33:54 +0200767 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768 const unsigned char *theirs, *start, *end;
769 const char **ours;
770
771 /* If ALPN not configured, just ignore the extension */
772 if( ssl->alpn_list == NULL )
773 return( 0 );
774
775 /*
776 * opaque ProtocolName<1..2^8-1>;
777 *
778 * struct {
779 * ProtocolName protocol_name_list<2..2^16-1>
780 * } ProtocolNameList;
781 */
782
783 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
784 if( len < 4 )
785 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
786
787 list_len = ( buf[0] << 8 ) | buf[1];
788 if( list_len != len - 2 )
789 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
790
791 /*
792 * Use our order of preference
793 */
794 start = buf + 2;
795 end = buf + len;
796 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
797 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200798 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200799 for( theirs = start; theirs != end; theirs += cur_len )
800 {
801 /* If the list is well formed, we should get equality first */
802 if( theirs > end )
803 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
804
805 cur_len = *theirs++;
806
807 /* Empty strings MUST NOT be included */
808 if( cur_len == 0 )
809 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
810
Paul Bakker14b16c62014-05-28 11:33:54 +0200811 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200812 memcmp( theirs, *ours, cur_len ) == 0 )
813 {
814 ssl->alpn_chosen = *ours;
815 return( 0 );
816 }
817 }
818 }
819
820 /* If we get there, no match was found */
821 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
822 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
823 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
824}
825#endif /* POLARSSL_SSL_ALPN */
826
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100827/*
828 * Auxiliary functions for ServerHello parsing and related actions
829 */
830
831#if defined(POLARSSL_X509_CRT_PARSE_C)
832/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100833 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100834 */
835#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100836static int ssl_check_key_curve( pk_context *pk,
837 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100838{
839 const ecp_curve_info **crv = curves;
840 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
841
842 while( *crv != NULL )
843 {
844 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100845 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100846 crv++;
847 }
848
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100849 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100850}
851#endif /* POLARSSL_ECDSA_C */
852
853/*
854 * Try picking a certificate for this ciphersuite,
855 * return 0 on success and -1 on failure.
856 */
857static int ssl_pick_cert( ssl_context *ssl,
858 const ssl_ciphersuite_t * ciphersuite_info )
859{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100860 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100861 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
862
863#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
864 if( ssl->handshake->sni_key_cert != NULL )
865 list = ssl->handshake->sni_key_cert;
866 else
867#endif
868 list = ssl->handshake->key_cert;
869
870 if( pk_alg == POLARSSL_PK_NONE )
871 return( 0 );
872
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000873 SSL_DEBUG_MSG( 3, ( "ciphersuite requires certificate" ) );
874
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100875 for( cur = list; cur != NULL; cur = cur->next )
876 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000877 SSL_DEBUG_CRT( 3, "candidate certificate chain, certificate",
878 cur->cert );
879
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100880 if( ! pk_can_do( cur->key, pk_alg ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000881 {
882 SSL_DEBUG_MSG( 3, ( "certificate mismatch: key type" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100883 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000884 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100885
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200886 /*
887 * This avoids sending the client a cert it'll reject based on
888 * keyUsage or other extensions.
889 *
890 * It also allows the user to provision different certificates for
891 * different uses based on keyUsage, eg if they want to avoid signing
892 * and decrypting with the same RSA key.
893 */
894 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
895 SSL_IS_SERVER ) != 0 )
896 {
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000897 SSL_DEBUG_MSG( 3, ( "certificate mismatch: "
898 "(extended) key usage extension" ) );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200899 continue;
900 }
901
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100902#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100903 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100904 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000905 {
906 SSL_DEBUG_MSG( 3, ( "certificate mismatch: elliptic curve" ) );
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100907 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000908 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100909#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100910
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100911 /*
912 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
913 * present them a SHA-higher cert rather than failing if it's the only
914 * one we got that satisfies the other conditions.
915 */
916 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
917 cur->cert->sig_md != POLARSSL_MD_SHA1 )
918 {
919 if( fallback == NULL )
920 fallback = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000921 {
922 SSL_DEBUG_MSG( 3, ( "certificate not preferred: "
923 "sha-2 with pre-TLS 1.2 client" ) );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100924 continue;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000925 }
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100926 }
927
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100928 /* If we get there, we got a winner */
929 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930 }
931
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000932 if( cur == NULL )
933 cur = fallback;
934
935
936 /* Do not update ssl->handshake->key_cert unless the is a match */
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100937 if( cur != NULL )
938 {
939 ssl->handshake->key_cert = cur;
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000940 SSL_DEBUG_CRT( 3, "selected certificate chain, certificate",
941 ssl->handshake->key_cert->cert );
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100942 return( 0 );
943 }
944
945 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100946}
947#endif /* POLARSSL_X509_CRT_PARSE_C */
948
949/*
950 * Check if a given ciphersuite is suitable for use with our config/keys/etc
951 * Sets ciphersuite_info only if the suite matches.
952 */
953static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
954 const ssl_ciphersuite_t **ciphersuite_info )
955{
956 const ssl_ciphersuite_t *suite_info;
957
958 suite_info = ssl_ciphersuite_from_id( suite_id );
959 if( suite_info == NULL )
960 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100961 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963 }
964
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000965 SSL_DEBUG_MSG( 3, ( "trying ciphersuite: %s", suite_info->name ) );
966
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100967 if( suite_info->min_minor_ver > ssl->minor_ver ||
968 suite_info->max_minor_ver < ssl->minor_ver )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000969 {
970 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: version" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100971 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000972 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100973
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100974#if defined(POLARSSL_SSL_PROTO_DTLS)
975 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
976 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
977 return( 0 );
978#endif
979
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100980 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
981 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000982 {
983 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: rc4" ) );
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100984 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000985 }
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100986
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100987#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
988 if( ssl_ciphersuite_uses_ec( suite_info ) &&
989 ( ssl->handshake->curves == NULL ||
990 ssl->handshake->curves[0] == NULL ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000991 {
992 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
993 "no common elliptic curve" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100994 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +0000995 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100996#endif
997
998#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
999 /* If the ciphersuite requires a pre-shared key and we don't
1000 * have one, skip it now rather than failing later */
1001 if( ssl_ciphersuite_uses_psk( suite_info ) &&
1002 ssl->f_psk == NULL &&
1003 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
1004 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001005 {
1006 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: no pre-shared key" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001007 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001008 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001009#endif
1010
1011#if defined(POLARSSL_X509_CRT_PARSE_C)
1012 /*
1013 * Final check: if ciphersuite requires us to have a
1014 * certificate/key of a particular type:
1015 * - select the appropriate certificate if we have one, or
1016 * - try the next ciphersuite if we don't
1017 * This must be done last since we modify the key_cert list.
1018 */
1019 if( ssl_pick_cert( ssl, suite_info ) != 0 )
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001020 {
1021 SSL_DEBUG_MSG( 3, ( "ciphersuite mismatch: "
1022 "no suitable certificate" ) );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001023 return( 0 );
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001024 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +01001025#endif
1026
1027 *ciphersuite_info = suite_info;
1028 return( 0 );
1029}
1030
Paul Bakker78a8c712013-03-06 17:01:52 +01001031#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1032static int ssl_parse_client_hello_v2( ssl_context *ssl )
1033{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001034 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001035 unsigned int i, j;
1036 size_t n;
1037 unsigned int ciph_len, sess_len, chal_len;
1038 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001039 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001040 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001041
1042 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1043
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001044#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001045 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1046 {
1047 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1048
1049 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1050 return( ret );
1051
1052 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1053 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001054#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001055
1056 buf = ssl->in_hdr;
1057
1058 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1059
1060 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1061 buf[2] ) );
1062 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1063 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1064 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1065 buf[3], buf[4] ) );
1066
1067 /*
1068 * SSLv2 Client Hello
1069 *
1070 * Record layer:
1071 * 0 . 1 message length
1072 *
1073 * SSL layer:
1074 * 2 . 2 message type
1075 * 3 . 4 protocol version
1076 */
1077 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1078 buf[3] != SSL_MAJOR_VERSION_3 )
1079 {
1080 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1081 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1082 }
1083
1084 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1085
1086 if( n < 17 || n > 512 )
1087 {
1088 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1089 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1090 }
1091
1092 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001093 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1094 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001095
1096 if( ssl->minor_ver < ssl->min_minor_ver )
1097 {
1098 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001099 " [%d:%d] < [%d:%d]",
1100 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001101 ssl->min_major_ver, ssl->min_minor_ver ) );
1102
1103 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1104 SSL_ALERT_MSG_PROTOCOL_VERSION );
1105 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1106 }
1107
Paul Bakker2fbefde2013-06-29 16:01:15 +02001108 ssl->handshake->max_major_ver = buf[3];
1109 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001110
1111 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1112 {
1113 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1114 return( ret );
1115 }
1116
1117 ssl->handshake->update_checksum( ssl, buf + 2, n );
1118
1119 buf = ssl->in_msg;
1120 n = ssl->in_left - 5;
1121
1122 /*
1123 * 0 . 1 ciphersuitelist length
1124 * 2 . 3 session id length
1125 * 4 . 5 challenge length
1126 * 6 . .. ciphersuitelist
1127 * .. . .. session id
1128 * .. . .. challenge
1129 */
1130 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1131
1132 ciph_len = ( buf[0] << 8 ) | buf[1];
1133 sess_len = ( buf[2] << 8 ) | buf[3];
1134 chal_len = ( buf[4] << 8 ) | buf[5];
1135
1136 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1137 ciph_len, sess_len, chal_len ) );
1138
1139 /*
1140 * Make sure each parameter length is valid
1141 */
1142 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1143 {
1144 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1145 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1146 }
1147
1148 if( sess_len > 32 )
1149 {
1150 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1151 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1152 }
1153
1154 if( chal_len < 8 || chal_len > 32 )
1155 {
1156 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1157 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1158 }
1159
1160 if( n != 6 + ciph_len + sess_len + chal_len )
1161 {
1162 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1163 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1164 }
1165
1166 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1167 buf + 6, ciph_len );
1168 SSL_DEBUG_BUF( 3, "client hello, session id",
1169 buf + 6 + ciph_len, sess_len );
1170 SSL_DEBUG_BUF( 3, "client hello, challenge",
1171 buf + 6 + ciph_len + sess_len, chal_len );
1172
1173 p = buf + 6 + ciph_len;
1174 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001175 memset( ssl->session_negotiate->id, 0,
1176 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001177 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1178
1179 p += sess_len;
1180 memset( ssl->handshake->randbytes, 0, 64 );
1181 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1182
1183 /*
1184 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1185 */
1186 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1187 {
1188 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1189 {
1190 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001191#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001192 if( ssl->renegotiation == SSL_RENEGOTIATION )
1193 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001194 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1195 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001196
1197 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1198 return( ret );
1199
1200 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1201 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001202#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001203 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1204 break;
1205 }
1206 }
1207
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001208#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1209 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1210 {
1211 if( p[0] == 0 &&
1212 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1213 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1214 {
1215 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1216
1217 if( ssl->minor_ver < ssl->max_minor_ver )
1218 {
1219 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1220
1221 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1222 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1223
1224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1225 }
1226
1227 break;
1228 }
1229 }
1230#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1231
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001232 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001233 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001234 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001235#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1236 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1237 {
1238 for( i = 0; ciphersuites[i] != 0; i++ )
1239#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001240 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001241 {
1242 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001243#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001244 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001245 if( p[0] != 0 ||
1246 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1247 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1248 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001249
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001250 got_common_suite = 1;
1251
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001252 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1253 &ciphersuite_info ) ) != 0 )
1254 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001255
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001256 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001257 goto have_ciphersuite_v2;
1258 }
1259 }
1260
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001261 if( got_common_suite )
1262 {
1263 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1264 "but none of them usable" ) );
1265 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1266 }
1267 else
1268 {
1269 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1270 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1271 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001272
1273have_ciphersuite_v2:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00001274 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
1275
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001276 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001277 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001278 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001279
1280 /*
1281 * SSLv2 Client Hello relevant renegotiation security checks
1282 */
1283 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1284 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1285 {
1286 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1287
1288 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1289 return( ret );
1290
1291 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1292 }
1293
1294 ssl->in_left = 0;
1295 ssl->state++;
1296
1297 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1298
1299 return( 0 );
1300}
1301#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303static int ssl_parse_client_hello( ssl_context *ssl )
1304{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001305 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001306 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001307 unsigned int ciph_offset, comp_offset, ext_offset;
1308 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001309#if defined(POLARSSL_SSL_PROTO_DTLS)
1310 unsigned int cookie_offset, cookie_len;
1311#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001312 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001313#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001314 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001315#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001316 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001317 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001318 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001319 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
1321 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1322
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001323#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1324read_record_header:
1325#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001326 /*
1327 * If renegotiating, then the input was read with ssl_read_record(),
1328 * otherwise read it ourselves manually in order to support SSLv2
1329 * ClientHello, which doesn't use the same record layer format.
1330 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001331#if defined(POLARSSL_SSL_RENEGOTIATION)
1332 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1333#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001334 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001335 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1336 {
1337 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1338 return( ret );
1339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
1342 buf = ssl->in_hdr;
1343
Paul Bakker78a8c712013-03-06 17:01:52 +01001344#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001345#if defined(POLARSSL_SSL_PROTO_DTLS)
1346 if( ssl->transport == SSL_TRANSPORT_STREAM )
1347#endif
1348 if( ( buf[0] & 0x80 ) != 0 )
1349 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001350#endif
1351
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001352 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001353
Paul Bakkerec636f32012-09-09 19:17:02 +00001354 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001355 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001356 *
1357 * Record layer:
1358 * 0 . 0 message type
1359 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001360 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001361 * 3 . 4 message length
1362 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001363 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1364 buf[0] ) );
1365
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001366 if( buf[0] != SSL_MSG_HANDSHAKE )
1367 {
1368 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1369 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1370 }
1371
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001372 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1373 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1374
1375 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1376 buf[1], buf[2] ) );
1377
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001378 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001379
1380 /* According to RFC 5246 Appendix E.1, the version here is typically
1381 * "{03,00}, the lowest version number supported by the client, [or] the
1382 * value of ClientHello.client_version", so the only meaningful check here
1383 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001384 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001386 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1387 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001390 /* For DTLS if this is the initial handshake, remember the client sequence
1391 * number to use it in our next message (RFC 6347 4.2.1) */
1392#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001393 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1394#if defined(POLARSSL_SSL_RENEGOTIATION)
1395 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1396#endif
1397 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001398 {
1399 /* Epoch should be 0 for initial handshakes */
1400 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1401 {
1402 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1403 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1404 }
1405
1406 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001407
1408#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1409 if( ssl_dtls_replay_check( ssl ) != 0 )
1410 {
1411 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1412 ssl->next_record_offset = 0;
1413 ssl->in_left = 0;
1414 goto read_record_header;
1415 }
1416
1417 /* No MAC to check yet, so we can update right now */
1418 ssl_dtls_replay_update( ssl );
1419#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001420 }
1421#endif /* POLARSSL_SSL_PROTO_DTLS */
1422
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001423 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001424
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001425#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001426 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1427 {
1428 /* Set by ssl_read_record() */
1429 msg_len = ssl->in_hslen;
1430 }
1431 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001432#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001433 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001434 if( msg_len > SSL_MAX_CONTENT_LEN )
1435 {
1436 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1437 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1438 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001439
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001440 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1441 {
1442 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1443 return( ret );
1444 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001445
1446 /* Done reading this record, get ready for the next one */
1447#if defined(POLARSSL_SSL_PROTO_DTLS)
1448 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1449 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1450 else
1451#endif
1452 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001453 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001454
1455 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001456
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001457 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001458
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001459 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001460
1461 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001462 * Handshake layer:
1463 * 0 . 0 handshake type
1464 * 1 . 3 handshake length
1465 * 4 . 5 DTLS only: message seqence number
1466 * 6 . 8 DTLS only: fragment offset
1467 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001468 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001469 if( msg_len < ssl_hs_hdr_len( ssl ) )
1470 {
1471 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1472 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1473 }
1474
1475 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1476
1477 if( buf[0] != SSL_HS_CLIENT_HELLO )
1478 {
1479 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1480 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1481 }
1482
1483 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1484 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1485
1486 /* We don't support fragmentation of ClientHello (yet?) */
1487 if( buf[1] != 0 ||
1488 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1489 {
1490 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1492 }
1493
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001494#if defined(POLARSSL_SSL_PROTO_DTLS)
1495 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1496 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001497 /*
1498 * Copy the client's handshake message_seq on initial handshakes
1499 */
1500 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001501 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001502 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1503 ssl->in_msg[5];
1504 ssl->handshake->out_msg_seq = cli_msg_seq;
1505 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001506 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001507 else
1508 {
1509 /* This couldn't be done in ssl_prepare_handshake_record() */
1510 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1511 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001512
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001513 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1514 {
1515 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1516 "%d (expected %d)", cli_msg_seq,
1517 ssl->handshake->in_msg_seq ) );
1518 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1519 }
1520
1521 ssl->handshake->in_msg_seq++;
1522 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001523
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001524 /*
1525 * For now we don't support fragmentation, so make sure
1526 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001527 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001528 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1529 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1530 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001531 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001532 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1533 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001534 }
1535#endif /* POLARSSL_SSL_PROTO_DTLS */
1536
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001537 buf += ssl_hs_hdr_len( ssl );
1538 msg_len -= ssl_hs_hdr_len( ssl );
1539
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001540 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001541 * ClientHello layer:
1542 * 0 . 1 protocol version
1543 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1544 * 34 . 35 session id length (1 byte)
1545 * 35 . 34+x session id
1546 * 35+x . 35+x DTLS only: cookie length (1 byte)
1547 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001548 * .. . .. ciphersuite list length (2 bytes)
1549 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001550 * .. . .. compression alg. list length (1 byte)
1551 * .. . .. compression alg. list
1552 * .. . .. extensions length (2 bytes, optional)
1553 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001554 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001555
1556 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001557 * Minimal length (with everything empty and extensions ommitted) is
1558 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1559 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001560 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001561 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001562 {
1563 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1564 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1565 }
1566
1567 /*
1568 * Check and save the protocol version
1569 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001570 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001571
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001572 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001574
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001575 ssl->handshake->max_major_ver = ssl->major_ver;
1576 ssl->handshake->max_minor_ver = ssl->minor_ver;
1577
1578 if( ssl->major_ver < ssl->min_major_ver ||
1579 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001580 {
1581 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001582 " [%d:%d] < [%d:%d]",
1583 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001584 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001585
1586 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1587 SSL_ALERT_MSG_PROTOCOL_VERSION );
1588
1589 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1590 }
1591
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001592 if( ssl->major_ver > ssl->max_major_ver )
1593 {
1594 ssl->major_ver = ssl->max_major_ver;
1595 ssl->minor_ver = ssl->max_minor_ver;
1596 }
1597 else if( ssl->minor_ver > ssl->max_minor_ver )
1598 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001599
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001600 /*
1601 * Save client random (inc. Unix time)
1602 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001603 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001604
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001605 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001606
1607 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001608 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001609 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001610 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001611
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001612 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001613 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001614 {
1615 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1616 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1617 }
1618
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001619 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001620
Paul Bakker48916f92012-09-16 19:57:18 +00001621 ssl->session_negotiate->length = sess_len;
1622 memset( ssl->session_negotiate->id, 0,
1623 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001624 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001625 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001626
1627 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001628 * Check the cookie length and content
1629 */
1630#if defined(POLARSSL_SSL_PROTO_DTLS)
1631 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1632 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001633 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001634 cookie_len = buf[cookie_offset];
1635
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001636 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001637 {
1638 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1639 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1640 }
1641
1642 SSL_DEBUG_BUF( 3, "client hello, cookie",
1643 buf + cookie_offset + 1, cookie_len );
1644
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001645#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001646 if( ssl->f_cookie_check != NULL &&
1647 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001648 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001649 if( ssl->f_cookie_check( ssl->p_cookie,
1650 buf + cookie_offset + 1, cookie_len,
1651 ssl->cli_id, ssl->cli_id_len ) != 0 )
1652 {
1653 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1654 ssl->handshake->verify_cookie_len = 1;
1655 }
1656 else
1657 {
1658 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1659 ssl->handshake->verify_cookie_len = 0;
1660 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001661 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001662 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001663#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001664 {
1665 /* We know we didn't send a cookie, so it should be empty */
1666 if( cookie_len != 0 )
1667 {
1668 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1669 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1670 }
1671
1672 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1673 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001674 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001675#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001676
1677 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001678 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001679 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001680#if defined(POLARSSL_SSL_PROTO_DTLS)
1681 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1682 ciph_offset = cookie_offset + 1 + cookie_len;
1683 else
1684#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001685 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001686
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001687 ciph_len = ( buf[ciph_offset + 0] << 8 )
1688 | ( buf[ciph_offset + 1] );
1689
1690 if( ciph_len < 2 ||
1691 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1692 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001693 {
1694 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1695 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1696 }
1697
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001698 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1699 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001700
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001701 /*
1702 * Check the compression algorithms length and pick one
1703 */
1704 comp_offset = ciph_offset + 2 + ciph_len;
1705
1706 comp_len = buf[comp_offset];
1707
1708 if( comp_len < 1 ||
1709 comp_len > 16 ||
1710 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001711 {
1712 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1713 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1714 }
1715
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001716 SSL_DEBUG_BUF( 3, "client hello, compression",
1717 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001718
1719 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001720#if defined(POLARSSL_ZLIB_SUPPORT)
1721 for( i = 0; i < comp_len; ++i )
1722 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001723 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 {
Paul Bakker48916f92012-09-16 19:57:18 +00001725 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001726 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 }
1728 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001729#endif
1730
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001731 /* See comments in ssl_write_client_hello() */
1732#if defined(POLARSSL_SSL_PROTO_DTLS)
1733 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1734 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1735#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001736
Paul Bakkerec636f32012-09-09 19:17:02 +00001737 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001738 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001739 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001740 ext_offset = comp_offset + 1 + comp_len;
1741 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001742 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001743 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001744 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001745 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1746 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1747 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001748
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001749 ext_len = ( buf[ext_offset + 0] << 8 )
1750 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001751
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001752 if( ( ext_len > 0 && ext_len < 4 ) ||
1753 msg_len != ext_offset + 2 + ext_len )
1754 {
1755 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1756 SSL_DEBUG_BUF( 3, "client hello extensions",
1757 buf + ext_offset + 2, ext_len );
1758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001759 }
1760 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001761 else
1762 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001763
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001764 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001765
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001766 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001767 {
1768 unsigned int ext_id = ( ( ext[0] << 8 )
1769 | ( ext[1] ) );
1770 unsigned int ext_size = ( ( ext[2] << 8 )
1771 | ( ext[3] ) );
1772
1773 if( ext_size + 4 > ext_len )
1774 {
1775 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1776 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1777 }
1778 switch( ext_id )
1779 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001780#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001781 case TLS_EXT_SERVERNAME:
1782 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1783 if( ssl->f_sni == NULL )
1784 break;
1785
1786 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1787 if( ret != 0 )
1788 return( ret );
1789 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001790#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001791
Paul Bakker48916f92012-09-16 19:57:18 +00001792 case TLS_EXT_RENEGOTIATION_INFO:
1793 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001794#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001795 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001796#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001797
Paul Bakker23f36802012-09-28 14:15:14 +00001798 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1799 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001800 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001801 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001802
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001803#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1804 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001805 case TLS_EXT_SIG_ALG:
1806 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001807#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001808 if( ssl->renegotiation == SSL_RENEGOTIATION )
1809 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001810#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001811
1812 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1813 if( ret != 0 )
1814 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001815 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001816#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1817 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001818
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001819#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001820 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1821 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1822
1823 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1824 if( ret != 0 )
1825 return( ret );
1826 break;
1827
1828 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1829 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001830 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001831
1832 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1833 if( ret != 0 )
1834 return( ret );
1835 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001836#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001837
Paul Bakker05decb22013-08-15 13:33:48 +02001838#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001839 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1840 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1841
1842 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1843 if( ret != 0 )
1844 return( ret );
1845 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001846#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001847
Paul Bakker1f2bc622013-08-15 13:45:55 +02001848#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001849 case TLS_EXT_TRUNCATED_HMAC:
1850 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1851
1852 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1853 if( ret != 0 )
1854 return( ret );
1855 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001856#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001857
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001858#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1859 case TLS_EXT_ENCRYPT_THEN_MAC:
1860 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1861
1862 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1863 if( ret != 0 )
1864 return( ret );
1865 break;
1866#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1867
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001868#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1869 case TLS_EXT_EXTENDED_MASTER_SECRET:
1870 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1871
1872 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1873 if( ret != 0 )
1874 return( ret );
1875 break;
1876#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1877
Paul Bakkera503a632013-08-14 13:48:06 +02001878#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001879 case TLS_EXT_SESSION_TICKET:
1880 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1881
1882 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1883 if( ret != 0 )
1884 return( ret );
1885 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001886#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001887
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001888#if defined(POLARSSL_SSL_ALPN)
1889 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001890 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001891
1892 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1893 if( ret != 0 )
1894 return( ret );
1895 break;
1896#endif /* POLARSSL_SSL_SESSION_TICKETS */
1897
Paul Bakker48916f92012-09-16 19:57:18 +00001898 default:
1899 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1900 ext_id ) );
1901 }
1902
1903 ext_len -= 4 + ext_size;
1904 ext += 4 + ext_size;
1905
1906 if( ext_len > 0 && ext_len < 4 )
1907 {
1908 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1909 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1910 }
1911 }
1912
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001913#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1914 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1915 {
1916 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1917 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1918 {
1919 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1920
1921 if( ssl->minor_ver < ssl->max_minor_ver )
1922 {
1923 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1924
1925 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1926 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1927
1928 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1929 }
1930
1931 break;
1932 }
1933 }
1934#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1935
Paul Bakker48916f92012-09-16 19:57:18 +00001936 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001937 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1938 */
1939 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1940 {
1941 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1942 {
1943 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1944 if( ssl->renegotiation == SSL_RENEGOTIATION )
1945 {
1946 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1947
1948 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1949 return( ret );
1950
1951 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1952 }
1953 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1954 break;
1955 }
1956 }
1957
1958 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001959 * Renegotiation security checks
1960 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001961 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001962 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1963 {
1964 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1965 handshake_failure = 1;
1966 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001967#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001968 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1969 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1970 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001971 {
1972 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001973 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001974 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001975 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1976 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1977 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001978 {
1979 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001980 handshake_failure = 1;
1981 }
1982 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1983 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1984 renegotiation_info_seen == 1 )
1985 {
1986 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1987 handshake_failure = 1;
1988 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001989#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001990
1991 if( handshake_failure == 1 )
1992 {
1993 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1994 return( ret );
1995
Paul Bakker48916f92012-09-16 19:57:18 +00001996 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1997 }
Paul Bakker380da532012-04-18 16:10:25 +00001998
Paul Bakker41c83d32013-03-20 14:39:14 +01001999 /*
2000 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02002001 * (At the end because we need information from the EC-based extensions
2002 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01002003 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002004 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002005 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01002006 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002007#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002008 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002009 {
2010 for( i = 0; ciphersuites[i] != 0; i++ )
2011#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002012 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01002013 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01002014 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01002015#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002016 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002017 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
2018 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
2019 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01002020
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002021 got_common_suite = 1;
2022
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01002023 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
2024 &ciphersuite_info ) ) != 0 )
2025 return( ret );
2026
2027 if( ciphersuite_info != NULL )
2028 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01002029 }
2030 }
2031
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01002032 if( got_common_suite )
2033 {
2034 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
2035 "but none of them usable" ) );
2036 ssl_send_fatal_handshake_failure( ssl );
2037 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2038 }
2039 else
2040 {
2041 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2042 ssl_send_fatal_handshake_failure( ssl );
2043 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2044 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002045
2046have_ciphersuite:
Manuel Pégourié-Gonnard607d6632015-01-26 11:17:20 +00002047 SSL_DEBUG_MSG( 2, ( "selected ciphersuite: %s", ciphersuite_info->name ) );
2048
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002049 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002050 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2051 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2052
Paul Bakker5121ce52009-01-03 21:22:43 +00002053 ssl->state++;
2054
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002055#if defined(POLARSSL_SSL_PROTO_DTLS)
2056 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2057 ssl_recv_flight_completed( ssl );
2058#endif
2059
Paul Bakker5121ce52009-01-03 21:22:43 +00002060 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2061
2062 return( 0 );
2063}
2064
Paul Bakker1f2bc622013-08-15 13:45:55 +02002065#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002066static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2067 unsigned char *buf,
2068 size_t *olen )
2069{
2070 unsigned char *p = buf;
2071
2072 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2073 {
2074 *olen = 0;
2075 return;
2076 }
2077
2078 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2079
2080 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2081 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2082
2083 *p++ = 0x00;
2084 *p++ = 0x00;
2085
2086 *olen = 4;
2087}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002088#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002089
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002090#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2091static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2092 unsigned char *buf,
2093 size_t *olen )
2094{
2095 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002096 const ssl_ciphersuite_t *suite = NULL;
2097 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002098
2099 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2100 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2101 {
2102 *olen = 0;
2103 return;
2104 }
2105
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002106 /*
2107 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2108 * from a client and then selects a stream or Authenticated Encryption
2109 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2110 * encrypt-then-MAC response extension back to the client."
2111 */
2112 if( ( suite = ssl_ciphersuite_from_id(
2113 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2114 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2115 cipher->mode != POLARSSL_MODE_CBC )
2116 {
2117 *olen = 0;
2118 return;
2119 }
2120
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002121 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2122
2123 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2124 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2125
2126 *p++ = 0x00;
2127 *p++ = 0x00;
2128
2129 *olen = 4;
2130}
2131#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2132
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002133#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2134static void ssl_write_extended_ms_ext( ssl_context *ssl,
2135 unsigned char *buf,
2136 size_t *olen )
2137{
2138 unsigned char *p = buf;
2139
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002140 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2141 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002142 {
2143 *olen = 0;
2144 return;
2145 }
2146
2147 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2148 "extension" ) );
2149
2150 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2151 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2152
2153 *p++ = 0x00;
2154 *p++ = 0x00;
2155
2156 *olen = 4;
2157}
2158#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2159
Paul Bakkera503a632013-08-14 13:48:06 +02002160#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002161static void ssl_write_session_ticket_ext( ssl_context *ssl,
2162 unsigned char *buf,
2163 size_t *olen )
2164{
2165 unsigned char *p = buf;
2166
2167 if( ssl->handshake->new_session_ticket == 0 )
2168 {
2169 *olen = 0;
2170 return;
2171 }
2172
2173 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2174
2175 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2176 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2177
2178 *p++ = 0x00;
2179 *p++ = 0x00;
2180
2181 *olen = 4;
2182}
Paul Bakkera503a632013-08-14 13:48:06 +02002183#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002184
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002185static void ssl_write_renegotiation_ext( ssl_context *ssl,
2186 unsigned char *buf,
2187 size_t *olen )
2188{
2189 unsigned char *p = buf;
2190
2191 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2192 {
2193 *olen = 0;
2194 return;
2195 }
2196
2197 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2198
2199 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2200 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2201
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002202#if defined(POLARSSL_SSL_RENEGOTIATION)
2203 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2204 {
2205 *p++ = 0x00;
2206 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2207 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002208
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002209 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2210 p += ssl->verify_data_len;
2211 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2212 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002213
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002214 *olen = 5 + ssl->verify_data_len * 2;
2215 }
2216 else
2217#endif /* POLARSSL_SSL_RENEGOTIATION */
2218 {
2219 *p++ = 0x00;
2220 *p++ = 0x01;
2221 *p++ = 0x00;
2222
2223 *olen = 5;
2224 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002225}
2226
Paul Bakker05decb22013-08-15 13:33:48 +02002227#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002228static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2229 unsigned char *buf,
2230 size_t *olen )
2231{
2232 unsigned char *p = buf;
2233
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002234 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2235 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002236 *olen = 0;
2237 return;
2238 }
2239
2240 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2241
2242 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2243 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2244
2245 *p++ = 0x00;
2246 *p++ = 1;
2247
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002248 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002249
2250 *olen = 5;
2251}
Paul Bakker05decb22013-08-15 13:33:48 +02002252#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002253
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002254#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002255static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2256 unsigned char *buf,
2257 size_t *olen )
2258{
2259 unsigned char *p = buf;
2260 ((void) ssl);
2261
Paul Bakker677377f2013-10-28 12:54:26 +01002262 if( ( ssl->handshake->cli_exts &
2263 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2264 {
2265 *olen = 0;
2266 return;
2267 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002268
2269 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2270
2271 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2272 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2273
2274 *p++ = 0x00;
2275 *p++ = 2;
2276
2277 *p++ = 1;
2278 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2279
2280 *olen = 6;
2281}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002282#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002283
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002284#if defined(POLARSSL_SSL_ALPN )
2285static void ssl_write_alpn_ext( ssl_context *ssl,
2286 unsigned char *buf, size_t *olen )
2287{
2288 if( ssl->alpn_chosen == NULL )
2289 {
2290 *olen = 0;
2291 return;
2292 }
2293
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002294 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002295
2296 /*
2297 * 0 . 1 ext identifier
2298 * 2 . 3 ext length
2299 * 4 . 5 protocol list length
2300 * 6 . 6 protocol name length
2301 * 7 . 7+n protocol name
2302 */
2303 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2304 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2305
2306 *olen = 7 + strlen( ssl->alpn_chosen );
2307
2308 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2309 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2310
2311 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2312 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2313
2314 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2315
2316 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2317}
2318#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2319
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002320#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002321static int ssl_write_hello_verify_request( ssl_context *ssl )
2322{
2323 int ret;
2324 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002325 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002326
2327 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2328
2329 /*
2330 * struct {
2331 * ProtocolVersion server_version;
2332 * opaque cookie<0..2^8-1>;
2333 * } HelloVerifyRequest;
2334 */
2335
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002336 /* The RFC is not clear on this point, but sending the actual negotiated
2337 * version looks like the most interoperable thing to do. */
2338 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002339 ssl->transport, p );
2340 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2341 p += 2;
2342
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002343 /* If we get here, f_cookie_check is not null */
2344 if( ssl->f_cookie_write == NULL )
2345 {
2346 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2347 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2348 }
2349
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002350 /* Skip length byte until we know the length */
2351 cookie_len_byte = p++;
2352
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002353 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2354 &p, ssl->out_buf + SSL_BUFFER_LEN,
2355 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002356 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002357 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002358 return( ret );
2359 }
2360
2361 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2362
2363 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002364
2365 ssl->out_msglen = p - ssl->out_msg;
2366 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2367 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2368
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002369 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002370
2371 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2372 {
2373 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2374 return( ret );
2375 }
2376
2377 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2378
2379 return( 0 );
2380}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002381#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002382
Paul Bakker5121ce52009-01-03 21:22:43 +00002383static int ssl_write_server_hello( ssl_context *ssl )
2384{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002385#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002386 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002387#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002388 int ret;
2389 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 unsigned char *buf, *p;
2391
2392 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2393
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002394#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002395 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002396 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002397 {
2398 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2399 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2400
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002401 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002402 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002403#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002404
Paul Bakkera9a028e2013-11-21 17:31:06 +01002405 if( ssl->f_rng == NULL )
2406 {
2407 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2408 return( POLARSSL_ERR_SSL_NO_RNG );
2409 }
2410
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 /*
2412 * 0 . 0 handshake type
2413 * 1 . 3 handshake length
2414 * 4 . 5 protocol version
2415 * 6 . 9 UNIX time()
2416 * 10 . 37 random bytes
2417 */
2418 buf = ssl->out_msg;
2419 p = buf + 4;
2420
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002421 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2422 ssl->transport, p );
2423 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
2425 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002426 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002427
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002428#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002429 t = time( NULL );
2430 *p++ = (unsigned char)( t >> 24 );
2431 *p++ = (unsigned char)( t >> 16 );
2432 *p++ = (unsigned char)( t >> 8 );
2433 *p++ = (unsigned char)( t );
2434
2435 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002436#else
2437 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2438 return( ret );
2439
2440 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002441#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Paul Bakkera3d195c2011-11-27 21:07:34 +00002443 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2444 return( ret );
2445
2446 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Paul Bakker48916f92012-09-16 19:57:18 +00002448 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
2450 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2451
2452 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002453 * Resume is 0 by default, see ssl_handshake_init().
2454 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2455 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002457 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002458#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002459 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002460#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002461 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002462 ssl->f_get_cache != NULL &&
2463 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2464 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002465 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002466 ssl->handshake->resume = 1;
2467 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002468
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002469 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 {
2471 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002472 * New session, create a new session id,
2473 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002475 ssl->state++;
2476
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002477#if defined(POLARSSL_HAVE_TIME)
2478 ssl->session_negotiate->start = time( NULL );
2479#endif
2480
Paul Bakkera503a632013-08-14 13:48:06 +02002481#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002482 if( ssl->handshake->new_session_ticket != 0 )
2483 {
2484 ssl->session_negotiate->length = n = 0;
2485 memset( ssl->session_negotiate->id, 0, 32 );
2486 }
2487 else
2488#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002489 {
2490 ssl->session_negotiate->length = n = 32;
2491 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002492 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002493 return( ret );
2494 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002495 }
2496 else
2497 {
2498 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002499 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002500 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002501 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002502 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002503
2504 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2505 {
2506 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2507 return( ret );
2508 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002509 }
2510
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002511 /*
2512 * 38 . 38 session id length
2513 * 39 . 38+n session id
2514 * 39+n . 40+n chosen ciphersuite
2515 * 41+n . 41+n chosen compression alg.
2516 * 42+n . 43+n extensions length
2517 * 44+n . 43+n+m extensions
2518 */
2519 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002520 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2521 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002522
2523 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2524 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2525 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002526 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Paul Bakker48916f92012-09-16 19:57:18 +00002528 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2529 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2530 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002532 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2533 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002534 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002535 ssl->session_negotiate->compression ) );
2536
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002537 /*
2538 * First write extensions, then the total length
2539 */
2540 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2541 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002542
Paul Bakker05decb22013-08-15 13:33:48 +02002543#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002544 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2545 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002546#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002547
Paul Bakker1f2bc622013-08-15 13:45:55 +02002548#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002549 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2550 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002551#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002552
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002553#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2554 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2555 ext_len += olen;
2556#endif
2557
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002558#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2559 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2560 ext_len += olen;
2561#endif
2562
Paul Bakkera503a632013-08-14 13:48:06 +02002563#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002564 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2565 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002566#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002567
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002568#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002569 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2570 ext_len += olen;
2571#endif
2572
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002573#if defined(POLARSSL_SSL_ALPN)
2574 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2575 ext_len += olen;
2576#endif
2577
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002578 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002579
Paul Bakkera7036632014-04-30 10:15:38 +02002580 if( ext_len > 0 )
2581 {
2582 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2583 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2584 p += ext_len;
2585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 ssl->out_msglen = p - buf;
2588 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2589 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2590
2591 ret = ssl_write_record( ssl );
2592
2593 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2594
2595 return( ret );
2596}
2597
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002598#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2599 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002600 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2601 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002602static int ssl_write_certificate_request( ssl_context *ssl )
2603{
Paul Bakkered27a042013-04-18 22:46:23 +02002604 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002605
2606 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2607
2608 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002609 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002610 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2611 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002612 {
2613 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2614 ssl->state++;
2615 return( 0 );
2616 }
2617
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002618 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2619 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002620}
2621#else
2622static int ssl_write_certificate_request( ssl_context *ssl )
2623{
2624 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2625 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002626 size_t dn_size, total_dn_size; /* excluding length bytes */
2627 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002628 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002629 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002630
2631 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2632
2633 ssl->state++;
2634
Paul Bakkerfbb17802013-04-17 19:10:21 +02002635 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002638 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002639 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002641 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002642 return( 0 );
2643 }
2644
2645 /*
2646 * 0 . 0 handshake type
2647 * 1 . 3 handshake length
2648 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002649 * 5 .. m-1 cert types
2650 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002651 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002652 * n .. n+1 length of all DNs
2653 * n+2 .. n+3 length of DN 1
2654 * n+4 .. ... Distinguished Name #1
2655 * ... .. ... length of DN 2, etc.
2656 */
2657 buf = ssl->out_msg;
2658 p = buf + 4;
2659
2660 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002661 * Supported certificate types
2662 *
2663 * ClientCertificateType certificate_types<1..2^8-1>;
2664 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002665 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002666 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002667
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002668#if defined(POLARSSL_RSA_C)
2669 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2670#endif
2671#if defined(POLARSSL_ECDSA_C)
2672 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2673#endif
2674
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002675 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002676 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002677
Paul Bakker577e0062013-08-28 11:57:20 +02002678 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002679#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002680 /*
2681 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002682 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002683 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2684 *
2685 * struct {
2686 * HashAlgorithm hash;
2687 * SignatureAlgorithm signature;
2688 * } SignatureAndHashAlgorithm;
2689 *
2690 * enum { (255) } HashAlgorithm;
2691 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002692 */
Paul Bakker21dca692013-01-03 11:41:08 +01002693 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002694 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002695 /*
2696 * Only use current running hash algorithm that is already required
2697 * for requested ciphersuite.
2698 */
Paul Bakker926af752012-11-23 13:38:07 +01002699 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2700
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002701 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2702 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002703 {
2704 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2705 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002706
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002707 /*
2708 * Supported signature algorithms
2709 */
2710#if defined(POLARSSL_RSA_C)
2711 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2712 p[2 + sa_len++] = SSL_SIG_RSA;
2713#endif
2714#if defined(POLARSSL_ECDSA_C)
2715 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2716 p[2 + sa_len++] = SSL_SIG_ECDSA;
2717#endif
Paul Bakker926af752012-11-23 13:38:07 +01002718
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002719 p[0] = (unsigned char)( sa_len >> 8 );
2720 p[1] = (unsigned char)( sa_len );
2721 sa_len += 2;
2722 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002723 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002724#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002725
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002726 /*
2727 * DistinguishedName certificate_authorities<0..2^16-1>;
2728 * opaque DistinguishedName<1..2^16-1>;
2729 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 p += 2;
2731 crt = ssl->ca_chain;
2732
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002733 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002734 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002735 {
2736 if( p - buf > 4096 )
2737 break;
2738
Paul Bakker926af752012-11-23 13:38:07 +01002739 dn_size = crt->subject_raw.len;
2740 *p++ = (unsigned char)( dn_size >> 8 );
2741 *p++ = (unsigned char)( dn_size );
2742 memcpy( p, crt->subject_raw.p, dn_size );
2743 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002744
Paul Bakker926af752012-11-23 13:38:07 +01002745 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2746
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002747 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002748 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002749 }
2750
Paul Bakker926af752012-11-23 13:38:07 +01002751 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002752 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2753 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002754 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2755 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002756
2757 ret = ssl_write_record( ssl );
2758
2759 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2760
2761 return( ret );
2762}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2764 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002765 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2766 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002767
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002768#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2769 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2770static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2771{
2772 int ret;
2773
2774 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2775 {
2776 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2777 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2778 }
2779
2780 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2781 pk_ec( *ssl_own_key( ssl ) ),
2782 POLARSSL_ECDH_OURS ) ) != 0 )
2783 {
2784 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2785 return( ret );
2786 }
2787
2788 return( 0 );
2789}
2790#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2791 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2792
Paul Bakker41c83d32013-03-20 14:39:14 +01002793static int ssl_write_server_key_exchange( ssl_context *ssl )
2794{
Paul Bakker23986e52011-04-24 08:57:21 +00002795 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002796 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002797 const ssl_ciphersuite_t *ciphersuite_info =
2798 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002799
2800#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2801 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2802 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002803 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002804 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002805 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002806 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002807 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002808 ((void) dig_signed);
2809 ((void) dig_signed_len);
2810#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002811
Paul Bakker5121ce52009-01-03 21:22:43 +00002812 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2813
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002814#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2815 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2816 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002817 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2818 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2819 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002820 {
2821 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2822 ssl->state++;
2823 return( 0 );
2824 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002825#endif
2826
2827#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2828 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2829 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2830 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2831 {
2832 ssl_get_ecdh_params_from_cert( ssl );
2833
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002834 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002835 ssl->state++;
2836 return( 0 );
2837 }
2838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002840#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2841 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2842 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2843 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002844 {
2845 /* TODO: Support identity hints */
2846 *(p++) = 0x00;
2847 *(p++) = 0x00;
2848
2849 n += 2;
2850 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002851#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2852 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002853
2854#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2855 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2856 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2857 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002858 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002859 /*
2860 * Ephemeral DH parameters:
2861 *
2862 * struct {
2863 * opaque dh_p<1..2^16-1>;
2864 * opaque dh_g<1..2^16-1>;
2865 * opaque dh_Ys<1..2^16-1>;
2866 * } ServerDHParams;
2867 */
2868 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2869 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2870 {
2871 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2872 return( ret );
2873 }
Paul Bakker48916f92012-09-16 19:57:18 +00002874
Paul Bakker41c83d32013-03-20 14:39:14 +01002875 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002876 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2877 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002878 {
2879 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2880 return( ret );
2881 }
2882
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002883 dig_signed = p;
2884 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002885
2886 p += len;
2887 n += len;
2888
Paul Bakker41c83d32013-03-20 14:39:14 +01002889 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2890 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2891 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2892 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2893 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002894#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2895 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002896
Gergely Budai987bfb52014-01-19 21:48:42 +01002897#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002898 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002899 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2900 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002901 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002902 /*
2903 * Ephemeral ECDH parameters:
2904 *
2905 * struct {
2906 * ECParameters curve_params;
2907 * ECPoint public;
2908 * } ServerECDHParams;
2909 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002910 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002911#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002912 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002913
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002914 /* Match our preference list against the offered curves */
2915 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2916 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2917 if( (*curve)->grp_id == *gid )
2918 goto curve_matching_done;
2919
2920curve_matching_done:
2921#else
2922 curve = ssl->handshake->curves;
2923#endif
2924
2925 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002926 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002927 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2928 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002929 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002930
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002931 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002932
Paul Bakker41c83d32013-03-20 14:39:14 +01002933 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002934 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002935 {
2936 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2937 return( ret );
2938 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002939
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002940 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2941 p, SSL_MAX_CONTENT_LEN - n,
2942 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002943 {
2944 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2945 return( ret );
2946 }
2947
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002948 dig_signed = p;
2949 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002950
2951 p += len;
2952 n += len;
2953
Paul Bakker41c83d32013-03-20 14:39:14 +01002954 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2955 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002956#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002958#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002959 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2960 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002961 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002962 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2963 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002964 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002965 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002966 unsigned int hashlen = 0;
2967 unsigned char hash[64];
2968 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002969
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002970 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002971 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2972 */
Paul Bakker577e0062013-08-28 11:57:20 +02002973#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002974 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2975 {
2976 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2977
2978 if( md_alg == POLARSSL_MD_NONE )
2979 {
2980 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002981 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002982 }
2983 }
Paul Bakker577e0062013-08-28 11:57:20 +02002984 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002986#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2987 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002988 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002989 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2990 {
2991 md_alg = POLARSSL_MD_SHA1;
2992 }
2993 else
Paul Bakker577e0062013-08-28 11:57:20 +02002994#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002995 {
2996 md_alg = POLARSSL_MD_NONE;
2997 }
2998
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002999 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003000 * Compute the hash to be signed
3001 */
Paul Bakker577e0062013-08-28 11:57:20 +02003002#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3003 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003004 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00003005 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003006 md5_context md5;
3007 sha1_context sha1;
3008
Paul Bakker5b4af392014-06-26 12:09:34 +02003009 md5_init( &md5 );
3010 sha1_init( &sha1 );
3011
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003012 /*
3013 * digitally-signed struct {
3014 * opaque md5_hash[16];
3015 * opaque sha_hash[20];
3016 * };
3017 *
3018 * md5_hash
3019 * MD5(ClientHello.random + ServerHello.random
3020 * + ServerParams);
3021 * sha_hash
3022 * SHA(ClientHello.random + ServerHello.random
3023 * + ServerParams);
3024 */
3025 md5_starts( &md5 );
3026 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003027 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003028 md5_finish( &md5, hash );
3029
3030 sha1_starts( &sha1 );
3031 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003032 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003033 sha1_finish( &sha1, hash + 16 );
3034
3035 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02003036
3037 md5_free( &md5 );
3038 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003039 }
3040 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003041#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3042 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003043#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3044 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003045 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003046 {
3047 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003048 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003049
Paul Bakker84bbeb52014-07-01 14:53:22 +02003050 md_init( &ctx );
3051
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003052 /* Info from md_alg will be used instead */
3053 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003054
3055 /*
3056 * digitally-signed struct {
3057 * opaque client_random[32];
3058 * opaque server_random[32];
3059 * ServerDHParams params;
3060 * };
3061 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003062 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063 {
3064 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3065 return( ret );
3066 }
3067
3068 md_starts( &ctx );
3069 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003070 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003071 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003072 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003073 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003074 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003075#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3076 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003077 {
3078 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003079 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003080 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003081
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003082 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3083 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003084
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003085 /*
3086 * Make the signature
3087 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003088 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003089 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003090 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3091 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003092 }
Paul Bakker23f36802012-09-28 14:15:14 +00003093
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003094#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003095 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3096 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003097 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003098 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003099
3100 n += 2;
3101 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003102#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003103
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003104 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003105 p + 2 , &signature_len,
3106 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003107 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003108 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003109 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003110 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003111
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003112 *(p++) = (unsigned char)( signature_len >> 8 );
3113 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003114 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003115
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003116 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003117
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003118 p += signature_len;
3119 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003120 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003121#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003122 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3123 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003124
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003125 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003126 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3127 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3128
3129 ssl->state++;
3130
3131 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3132 {
3133 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3134 return( ret );
3135 }
3136
3137 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3138
3139 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003140}
3141
3142static int ssl_write_server_hello_done( ssl_context *ssl )
3143{
3144 int ret;
3145
3146 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3147
3148 ssl->out_msglen = 4;
3149 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3150 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3151
3152 ssl->state++;
3153
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003154#if defined(POLARSSL_SSL_PROTO_DTLS)
3155 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3156 ssl_send_flight_completed( ssl );
3157#endif
3158
Paul Bakker5121ce52009-01-03 21:22:43 +00003159 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3160 {
3161 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3162 return( ret );
3163 }
3164
3165 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3166
3167 return( 0 );
3168}
3169
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003170#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3171 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3172static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3173 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003174{
3175 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003176 size_t n;
3177
3178 /*
3179 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3180 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003181 if( *p + 2 > end )
3182 {
3183 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3184 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3185 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003186
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003187 n = ( (*p)[0] << 8 ) | (*p)[1];
3188 *p += 2;
3189
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003190 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003191 {
3192 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3193 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3194 }
3195
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003196 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003197 {
3198 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3199 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3200 }
3201
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003202 *p += n;
3203
Paul Bakker70df2fb2013-04-17 17:19:09 +02003204 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3205
Paul Bakker70df2fb2013-04-17 17:19:09 +02003206 return( ret );
3207}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003208#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3209 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003210
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003211#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3212 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003213static int ssl_parse_encrypted_pms( ssl_context *ssl,
3214 const unsigned char *p,
3215 const unsigned char *end,
3216 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003217{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003218 int ret;
3219 size_t len = pk_get_len( ssl_own_key( ssl ) );
3220 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003221 unsigned char ver[2];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003222 unsigned char fake_pms[48], peer_pms[48];
3223 unsigned char mask;
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003224 size_t i;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003225
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003226 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003227 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003228 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003229 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3230 }
3231
3232 /*
3233 * Decrypt the premaster using own private RSA key
3234 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003235#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3236 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003237 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3238 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003239 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3240 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003241 {
3242 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3243 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3244 }
3245 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003246#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003247
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003248 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003249 {
3250 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3251 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3252 }
3253
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003254 ssl_write_version( ssl->handshake->max_major_ver,
3255 ssl->handshake->max_minor_ver,
3256 ssl->transport, ver );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003257 /*
3258 * Protection against Bleichenbacher's attack: invalid PKCS#1 v1.5 padding
3259 * must not cause the connection to end immediately; instead, send a
3260 * bad_record_mac later in the handshake.
3261 * Also, avoid data-dependant branches here to protect against
3262 * timing-based variants.
3263 */
3264 ret = ssl->f_rng( ssl->p_rng, fake_pms, sizeof( fake_pms ) );
3265 if( ret != 0 )
3266 return( ret );
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003267
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003268 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003269 peer_pms, &ssl->handshake->pmslen,
3270 sizeof( peer_pms ),
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003271 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003272
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003273 ret |= ssl->handshake->pmslen - 48;
Manuel Pégourié-Gonnardf7d2bba2015-02-09 11:42:40 +00003274 ret |= peer_pms[0] - ver[0];
3275 ret |= peer_pms[1] - ver[1];
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003276
3277#if defined(POLARSSL_SSL_DEBUG_ALL)
3278 if( ret != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003279 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003280#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003281
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003282 if( sizeof( ssl->handshake->premaster ) < pms_offset ||
3283 sizeof( ssl->handshake->premaster ) - pms_offset < 48 )
3284 {
3285 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3286 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003287 }
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003288 ssl->handshake->pmslen = 48;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003289
Manuel Pégourié-Gonnard2ee8d242015-02-11 15:29:15 +00003290 mask = (unsigned char)( - ( ret != 0 ) ); /* ret ? 0xff : 0x00 */
Manuel Pégourié-Gonnard6674cce2015-02-06 10:30:58 +00003291 for( i = 0; i < ssl->handshake->pmslen; i++ )
3292 pms[i] = ( mask & fake_pms[i] ) | ( (~mask) & peer_pms[i] );
3293
3294 return( 0 );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003295}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003296#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3297 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003298
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003299#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003300static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3301 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003302{
Paul Bakker6db455e2013-09-18 17:29:31 +02003303 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003304 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003305
Paul Bakker6db455e2013-09-18 17:29:31 +02003306 if( ssl->f_psk == NULL &&
3307 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3308 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003309 {
3310 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3311 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3312 }
3313
3314 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003315 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003316 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003317 if( *p + 2 > end )
3318 {
3319 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3321 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003322
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003323 n = ( (*p)[0] << 8 ) | (*p)[1];
3324 *p += 2;
3325
3326 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003327 {
3328 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3329 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3330 }
3331
Paul Bakker6db455e2013-09-18 17:29:31 +02003332 if( ssl->f_psk != NULL )
3333 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003334 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003335 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3336 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003337 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003338 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003339 /* Identity is not a big secret since clients send it in the clear,
3340 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003341 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003342 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003343 {
3344 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3345 }
3346 }
3347
3348 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003349 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003350 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003351 if( ( ret = ssl_send_alert_message( ssl,
3352 SSL_ALERT_LEVEL_FATAL,
3353 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3354 {
3355 return( ret );
3356 }
3357
3358 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003359 }
3360
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003361 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003362
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003363 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003364}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003365#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003366
Paul Bakker5121ce52009-01-03 21:22:43 +00003367static int ssl_parse_client_key_exchange( ssl_context *ssl )
3368{
Paul Bakker23986e52011-04-24 08:57:21 +00003369 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003370 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003371 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003372
Paul Bakker41c83d32013-03-20 14:39:14 +01003373 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003374
3375 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3376
3377 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3378 {
3379 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3380 return( ret );
3381 }
3382
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003383 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3384 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003385
Paul Bakker5121ce52009-01-03 21:22:43 +00003386 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3387 {
3388 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003389 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003390 }
3391
3392 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3393 {
3394 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003395 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003396 }
3397
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003398#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003399 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003400 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003401 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003402 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003403 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3404 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003405 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003407 if( p != end )
3408 {
3409 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3410 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3411 }
3412
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003413 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003414
3415 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3416 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003417 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003418 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003419 {
3420 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3422 }
3423
3424 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003425 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003426 else
3427#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003428#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003429 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3430 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3431 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003432 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003433 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3434 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3435 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003436 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003437 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003438 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003439 {
3440 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3442 }
3443
3444 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3445
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003446 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3447 &ssl->handshake->pmslen,
3448 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003449 POLARSSL_MPI_MAX_SIZE,
3450 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003451 {
3452 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3453 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3454 }
3455
3456 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003457 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003458 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003459#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003460 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3461 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3462 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003463#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3464 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003465 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003466 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003467 {
3468 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3469 return( ret );
3470 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003471
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003472 if( p != end )
3473 {
3474 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3475 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3476 }
3477
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003478 if( ( ret = ssl_psk_derive_premaster( ssl,
3479 ciphersuite_info->key_exchange ) ) != 0 )
3480 {
3481 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3482 return( ret );
3483 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003484 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003485 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003486#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003487#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3488 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3489 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003490 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3491 {
3492 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3493 return( ret );
3494 }
3495
3496 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3497 {
3498 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3499 return( ret );
3500 }
3501
3502 if( ( ret = ssl_psk_derive_premaster( ssl,
3503 ciphersuite_info->key_exchange ) ) != 0 )
3504 {
3505 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3506 return( ret );
3507 }
3508 }
3509 else
3510#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003511#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3512 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3513 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003514 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3515 {
3516 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3517 return( ret );
3518 }
3519 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3520 {
3521 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3522 return( ret );
3523 }
3524
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003525 if( p != end )
3526 {
3527 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3529 }
3530
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003531 if( ( ret = ssl_psk_derive_premaster( ssl,
3532 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003533 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003534 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3535 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003536 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003537 }
3538 else
3539#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003540#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3541 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3542 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003543 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3544 {
3545 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3546 return( ret );
3547 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003548
3549 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3550 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003551 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003552 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3553 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003554 }
3555
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003556 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3557
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003558 if( ( ret = ssl_psk_derive_premaster( ssl,
3559 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003560 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003561 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003562 return( ret );
3563 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003564 }
3565 else
3566#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003567#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3568 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003569 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003570 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003571 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003572 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003573 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574 }
3575 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003576 else
3577#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3578 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003579 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003580 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003581 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003582
Paul Bakkerff60ee62010-03-16 21:09:09 +00003583 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3584 {
3585 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3586 return( ret );
3587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003588
Paul Bakker5121ce52009-01-03 21:22:43 +00003589 ssl->state++;
3590
3591 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3592
3593 return( 0 );
3594}
3595
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003596#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3597 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003598 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3599 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003600static int ssl_parse_certificate_verify( ssl_context *ssl )
3601{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003602 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003603
3604 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3605
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003606 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003607 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003608 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003609 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003610 {
3611 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3612 ssl->state++;
3613 return( 0 );
3614 }
3615
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003616 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3617 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003618}
3619#else
3620static int ssl_parse_certificate_verify( ssl_context *ssl )
3621{
3622 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003623 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003624 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003625 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003626 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003627#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003628 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003629#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003630 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003631 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3632
3633 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3634
3635 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003636 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003637 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003638 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3639 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003640 {
3641 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3642 ssl->state++;
3643 return( 0 );
3644 }
3645
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003646 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003647 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003648
3649 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3650 {
3651 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3652 return( ret );
3653 }
3654
3655 ssl->state++;
3656
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003657 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3658 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 {
3660 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003661 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003662 }
3663
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003664 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003665
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003666 /*
3667 * struct {
3668 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3669 * opaque signature<0..2^16-1>;
3670 * } DigitallySigned;
3671 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003672#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3673 defined(POLARSSL_SSL_PROTO_TLS1_1)
3674 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003675 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003676 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003677 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003678
3679 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3680 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3681 POLARSSL_PK_ECDSA ) )
3682 {
3683 hash_start += 16;
3684 hashlen -= 16;
3685 md_alg = POLARSSL_MD_SHA1;
3686 }
Paul Bakker926af752012-11-23 13:38:07 +01003687 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003688 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003689#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3690 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003691#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3692 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003693 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003694 if( i + 2 > ssl->in_hslen )
3695 {
3696 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3697 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3698 }
3699
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003701 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003703 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003704 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003705 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3706 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003707 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3708 }
3709
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003710 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003711
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003712 /* Info from md_alg will be used instead */
3713 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003714
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003715 i++;
3716
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003717 /*
3718 * Signature
3719 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003720 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003721 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003722 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003723 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3724 " for verify message" ) );
3725 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003726 }
3727
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003728 /*
3729 * Check the certificate's key type matches the signature alg
3730 */
3731 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3732 {
3733 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3734 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3735 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003736
3737 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003738 }
3739 else
3740#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3741 {
3742 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003743 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003744 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003745
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003746 if( i + 2 > ssl->in_hslen )
3747 {
3748 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3749 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3750 }
3751
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003752 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3753 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003754
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003755 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003756 {
3757 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003758 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003759 }
3760
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003761 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003762 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003763 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003764 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003765 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003766 return( ret );
3767 }
3768
3769 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3770
Paul Bakkered27a042013-04-18 22:46:23 +02003771 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003772}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003773#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3774 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3775 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003776
Paul Bakkera503a632013-08-14 13:48:06 +02003777#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003778static int ssl_write_new_session_ticket( ssl_context *ssl )
3779{
3780 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003781 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003782 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003783
3784 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3785
3786 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3787 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3788
3789 /*
3790 * struct {
3791 * uint32 ticket_lifetime_hint;
3792 * opaque ticket<0..2^16-1>;
3793 * } NewSessionTicket;
3794 *
3795 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3796 * 8 . 9 ticket_len (n)
3797 * 10 . 9+n ticket content
3798 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003799
3800 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3801 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3802 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3803 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003804
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003805 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3806 {
3807 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3808 tlen = 0;
3809 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003810
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003811 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3812 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003813
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003814 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003815
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003816 /*
3817 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3818 * ChangeCipherSpec share the same state.
3819 */
3820 ssl->handshake->new_session_ticket = 0;
3821
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003822 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3823 {
3824 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3825 return( ret );
3826 }
3827
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003828 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3829
3830 return( 0 );
3831}
Paul Bakkera503a632013-08-14 13:48:06 +02003832#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003833
Paul Bakker5121ce52009-01-03 21:22:43 +00003834/*
Paul Bakker1961b702013-01-25 14:49:24 +01003835 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003836 */
Paul Bakker1961b702013-01-25 14:49:24 +01003837int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003838{
3839 int ret = 0;
3840
Paul Bakker1961b702013-01-25 14:49:24 +01003841 if( ssl->state == SSL_HANDSHAKE_OVER )
3842 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003843
Paul Bakker1961b702013-01-25 14:49:24 +01003844 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3845
3846 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3847 return( ret );
3848
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003849#if defined(POLARSSL_SSL_PROTO_DTLS)
3850 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3851 ssl->handshake != NULL &&
3852 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3853 {
3854 if( ( ret = ssl_resend( ssl ) ) != 0 )
3855 return( ret );
3856 }
3857#endif
3858
Paul Bakker1961b702013-01-25 14:49:24 +01003859 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003860 {
Paul Bakker1961b702013-01-25 14:49:24 +01003861 case SSL_HELLO_REQUEST:
3862 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003863 break;
3864
Paul Bakker1961b702013-01-25 14:49:24 +01003865 /*
3866 * <== ClientHello
3867 */
3868 case SSL_CLIENT_HELLO:
3869 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003870 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003871
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003872#if defined(POLARSSL_SSL_PROTO_DTLS)
3873 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3874 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3875#endif
3876
Paul Bakker1961b702013-01-25 14:49:24 +01003877 /*
3878 * ==> ServerHello
3879 * Certificate
3880 * ( ServerKeyExchange )
3881 * ( CertificateRequest )
3882 * ServerHelloDone
3883 */
3884 case SSL_SERVER_HELLO:
3885 ret = ssl_write_server_hello( ssl );
3886 break;
3887
3888 case SSL_SERVER_CERTIFICATE:
3889 ret = ssl_write_certificate( ssl );
3890 break;
3891
3892 case SSL_SERVER_KEY_EXCHANGE:
3893 ret = ssl_write_server_key_exchange( ssl );
3894 break;
3895
3896 case SSL_CERTIFICATE_REQUEST:
3897 ret = ssl_write_certificate_request( ssl );
3898 break;
3899
3900 case SSL_SERVER_HELLO_DONE:
3901 ret = ssl_write_server_hello_done( ssl );
3902 break;
3903
3904 /*
3905 * <== ( Certificate/Alert )
3906 * ClientKeyExchange
3907 * ( CertificateVerify )
3908 * ChangeCipherSpec
3909 * Finished
3910 */
3911 case SSL_CLIENT_CERTIFICATE:
3912 ret = ssl_parse_certificate( ssl );
3913 break;
3914
3915 case SSL_CLIENT_KEY_EXCHANGE:
3916 ret = ssl_parse_client_key_exchange( ssl );
3917 break;
3918
3919 case SSL_CERTIFICATE_VERIFY:
3920 ret = ssl_parse_certificate_verify( ssl );
3921 break;
3922
3923 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3924 ret = ssl_parse_change_cipher_spec( ssl );
3925 break;
3926
3927 case SSL_CLIENT_FINISHED:
3928 ret = ssl_parse_finished( ssl );
3929 break;
3930
3931 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003932 * ==> ( NewSessionTicket )
3933 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003934 * Finished
3935 */
3936 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003937#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003938 if( ssl->handshake->new_session_ticket != 0 )
3939 ret = ssl_write_new_session_ticket( ssl );
3940 else
Paul Bakkera503a632013-08-14 13:48:06 +02003941#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003942 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003943 break;
3944
3945 case SSL_SERVER_FINISHED:
3946 ret = ssl_write_finished( ssl );
3947 break;
3948
3949 case SSL_FLUSH_BUFFERS:
3950 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3951 ssl->state = SSL_HANDSHAKE_WRAPUP;
3952 break;
3953
3954 case SSL_HANDSHAKE_WRAPUP:
3955 ssl_handshake_wrapup( ssl );
3956 break;
3957
3958 default:
3959 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3960 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003961 }
3962
Paul Bakker5121ce52009-01-03 21:22:43 +00003963 return( ret );
3964}
Paul Bakker9af723c2014-05-01 13:03:14 +02003965#endif /* POLARSSL_SSL_SRV_C */