blob: fc868096ab089e20aa60f57d6fdbf5e3056c58d7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 server-side functions
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
35#include "polarssl/ssl.h"
Paul Bakker41c83d32013-03-20 14:39:14 +010036#if defined(POLARSSL_ECP_C)
37#include "polarssl/ecp.h"
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Paul Bakker7dc4c442014-02-01 22:50:26 +010040#if defined(POLARSSL_PLATFORM_C)
41#include "polarssl/platform.h"
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020042#else
43#define polarssl_malloc malloc
44#define polarssl_free free
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047#include <stdlib.h>
48#include <stdio.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020049
50#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <time.h>
Paul Bakkerfa9b1002013-07-03 15:31:03 +020052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Paul Bakkera503a632013-08-14 13:48:06 +020054#if defined(POLARSSL_SSL_SESSION_TICKETS)
Paul Bakker34617722014-06-13 17:20:13 +020055/* Implementation that should never be optimized out by the compiler */
56static void polarssl_zeroize( void *v, size_t n ) {
57 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
58}
59
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020060/*
61 * Serialize a session in the following format:
62 * 0 . n-1 session structure, n = sizeof(ssl_session)
63 * n . n+2 peer_cert length = m (0 if no certificate)
64 * n+3 . n+2+m peer cert ASN.1
65 *
66 * Assumes ticket is NULL (always true on server side).
67 */
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020068static int ssl_save_session( const ssl_session *session,
69 unsigned char *buf, size_t buf_len,
70 size_t *olen )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020071{
72 unsigned char *p = buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020073 size_t left = buf_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020075 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020077
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020078 if( left < sizeof( ssl_session ) )
79 return( -1 );
80
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020081 memcpy( p, session, sizeof( ssl_session ) );
82 p += sizeof( ssl_session );
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020083 left -= sizeof( ssl_session );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020084
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020086 if( session->peer_cert == NULL )
87 cert_len = 0;
88 else
89 cert_len = session->peer_cert->raw.len;
90
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +020091 if( left < 3 + cert_len )
92 return( -1 );
93
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +020094 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
95 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
96 *p++ = (unsigned char)( cert_len & 0xFF );
97
98 if( session->peer_cert != NULL )
99 memcpy( p, session->peer_cert->raw.p, cert_len );
100
101 p += cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200103
104 *olen = p - buf;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200105
106 return( 0 );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200107}
108
109/*
110 * Unserialise session, see ssl_save_session()
111 */
112static int ssl_load_session( ssl_session *session,
113 const unsigned char *buf, size_t len )
114{
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200115 const unsigned char *p = buf;
116 const unsigned char * const end = buf + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200118 size_t cert_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200119#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200120
121 if( p + sizeof( ssl_session ) > end )
122 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
123
124 memcpy( session, p, sizeof( ssl_session ) );
125 p += sizeof( ssl_session );
126
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200128 if( p + 3 > end )
129 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
130
131 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
132 p += 3;
133
134 if( cert_len == 0 )
135 {
136 session->peer_cert = NULL;
137 }
138 else
139 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200140 int ret;
141
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200142 if( p + cert_len > end )
143 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
144
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200145 session->peer_cert = polarssl_malloc( sizeof( x509_crt ) );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200146
147 if( session->peer_cert == NULL )
148 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
149
Paul Bakkerb6b09562013-09-18 14:17:41 +0200150 x509_crt_init( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200151
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200152 if( ( ret = x509_crt_parse_der( session->peer_cert,
153 p, cert_len ) ) != 0 )
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200154 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155 x509_crt_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200156 polarssl_free( session->peer_cert );
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200157 session->peer_cert = NULL;
158 return( ret );
159 }
160
161 p += cert_len;
162 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163#endif /* POLARSSL_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard94f6a792013-08-01 14:33:49 +0200164
165 if( p != end )
166 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
167
168 return( 0 );
169}
170
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200171/*
172 * Create session ticket, secured as recommended in RFC 5077 section 4:
173 *
174 * struct {
175 * opaque key_name[16];
176 * opaque iv[16];
177 * opaque encrypted_state<0..2^16-1>;
178 * opaque mac[32];
179 * } ticket;
180 *
181 * (the internal state structure differs, however).
182 */
183static int ssl_write_ticket( ssl_context *ssl, size_t *tlen )
184{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200185 int ret;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200186 unsigned char * const start = ssl->out_msg + 10;
187 unsigned char *p = start;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200188 unsigned char *state;
189 unsigned char iv[16];
190 size_t clear_len, enc_len, pad_len, i;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200191
Manuel Pégourié-Gonnard0a201712013-08-23 16:25:16 +0200192 *tlen = 0;
193
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200194 if( ssl->ticket_keys == NULL )
195 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
196
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200197 /* Write key name */
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200198 memcpy( p, ssl->ticket_keys->key_name, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200199 p += 16;
200
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200201 /* Generate and write IV (with a copy for aes_crypt) */
202 if( ( ret = ssl->f_rng( ssl->p_rng, p, 16 ) ) != 0 )
203 return( ret );
204 memcpy( iv, p, 16 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200205 p += 16;
206
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200207 /*
208 * Dump session state
209 *
210 * After the session state itself, we still need room for 16 bytes of
211 * padding and 32 bytes of MAC, so there's only so much room left
212 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200213 state = p + 2;
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200214 if( ssl_save_session( ssl->session_negotiate, state,
Manuel Pégourié-Gonnard5d53cbe2014-07-14 13:51:41 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_msg ) - 48,
Manuel Pégourié-Gonnardc6554aa2013-08-23 11:10:28 +0200216 &clear_len ) != 0 )
217 {
218 return( POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE );
219 }
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200220 SSL_DEBUG_BUF( 3, "session ticket cleartext", state, clear_len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200221
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200222 /* Apply PKCS padding */
223 pad_len = 16 - clear_len % 16;
224 enc_len = clear_len + pad_len;
225 for( i = clear_len; i < enc_len; i++ )
226 state[i] = (unsigned char) pad_len;
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200227
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200228 /* Encrypt */
229 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->enc, AES_ENCRYPT,
230 enc_len, iv, state, state ) ) != 0 )
231 {
232 return( ret );
233 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200234
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200235 /* Write length */
236 *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF );
237 *p++ = (unsigned char)( ( enc_len ) & 0xFF );
238 p = state + enc_len;
239
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200240 /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */
241 sha256_hmac( ssl->ticket_keys->mac_key, 16, start, p - start, p, 0 );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200242 p += 32;
243
244 *tlen = p - start;
245
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200246 SSL_DEBUG_BUF( 3, "session ticket structure", start, *tlen );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200247
248 return( 0 );
249}
250
251/*
252 * Load session ticket (see ssl_write_ticket for structure)
253 */
254static int ssl_parse_ticket( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200255 unsigned char *buf,
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200256 size_t len )
257{
258 int ret;
259 ssl_session session;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200260 unsigned char *key_name = buf;
261 unsigned char *iv = buf + 16;
262 unsigned char *enc_len_p = iv + 16;
263 unsigned char *ticket = enc_len_p + 2;
264 unsigned char *mac;
Manuel Pégourié-Gonnard34ced2d2013-09-20 11:37:39 +0200265 unsigned char computed_mac[32];
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200266 size_t enc_len, clear_len, i;
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100267 unsigned char pad_len, diff;
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200268
269 SSL_DEBUG_BUF( 3, "session ticket structure", buf, len );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200270
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +0200271 if( len < 34 || ssl->ticket_keys == NULL )
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200272 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
273
274 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
275 mac = ticket + enc_len;
276
277 if( len != enc_len + 66 )
278 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
279
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100280 /* Check name, in constant time though it's not a big secret */
281 diff = 0;
282 for( i = 0; i < 16; i++ )
283 diff |= key_name[i] ^ ssl->ticket_keys->key_name[i];
284 /* don't return yet, check the MAC anyway */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200285
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100286 /* Check mac, with constant-time buffer comparison */
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200287 sha256_hmac( ssl->ticket_keys->mac_key, 16, buf, len - 32,
288 computed_mac, 0 );
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100289
Manuel Pégourié-Gonnard56dc9e82013-08-03 17:16:31 +0200290 for( i = 0; i < 32; i++ )
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100291 diff |= mac[i] ^ computed_mac[i];
292
293 /* Now return if ticket is not authentic, since we want to avoid
294 * decrypting arbitrary attacker-chosen data */
295 if( diff != 0 )
296 return( POLARSSL_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200297
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200298 /* Decrypt */
299 if( ( ret = aes_crypt_cbc( &ssl->ticket_keys->dec, AES_DECRYPT,
300 enc_len, iv, ticket, ticket ) ) != 0 )
301 {
302 return( ret );
303 }
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200304
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200305 /* Check PKCS padding */
306 pad_len = ticket[enc_len - 1];
307
308 ret = 0;
309 for( i = 2; i < pad_len; i++ )
310 if( ticket[enc_len - i] != pad_len )
311 ret = POLARSSL_ERR_SSL_BAD_INPUT_DATA;
312 if( ret != 0 )
313 return( ret );
314
315 clear_len = enc_len - pad_len;
316
317 SSL_DEBUG_BUF( 3, "session ticket cleartext", ticket, clear_len );
318
319 /* Actually load session */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200320 if( ( ret = ssl_load_session( &session, ticket, clear_len ) ) != 0 )
321 {
322 SSL_DEBUG_MSG( 1, ( "failed to parse ticket content" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100323 ssl_session_free( &session );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200324 return( ret );
325 }
326
Paul Bakker606b4ba2013-08-14 16:52:14 +0200327#if defined(POLARSSL_HAVE_TIME)
328 /* Check if still valid */
329 if( (int) ( time( NULL) - session.start ) > ssl->ticket_lifetime )
330 {
331 SSL_DEBUG_MSG( 1, ( "session ticket expired" ) );
Manuel Pégourié-Gonnardd701c9a2014-02-26 17:54:07 +0100332 ssl_session_free( &session );
Paul Bakker606b4ba2013-08-14 16:52:14 +0200333 return( POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED );
334 }
335#endif
336
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200337 /*
338 * Keep the session ID sent by the client, since we MUST send it back to
339 * inform him we're accepting the ticket (RFC 5077 section 3.4)
340 */
341 session.length = ssl->session_negotiate->length;
342 memcpy( &session.id, ssl->session_negotiate->id, session.length );
343
344 ssl_session_free( ssl->session_negotiate );
345 memcpy( ssl->session_negotiate, &session, sizeof( ssl_session ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200346
347 /* Zeroize instead of free as we copied the content */
Paul Bakker34617722014-06-13 17:20:13 +0200348 polarssl_zeroize( &session, sizeof( ssl_session ) );
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200349
350 return( 0 );
351}
Paul Bakkera503a632013-08-14 13:48:06 +0200352#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200353
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200354#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200355int ssl_set_client_transport_id( ssl_context *ssl,
356 const unsigned char *info,
357 size_t ilen )
358{
359 if( ssl->endpoint != SSL_IS_SERVER )
360 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
361
362 polarssl_free( ssl->cli_id );
363
364 if( ( ssl->cli_id = polarssl_malloc( ilen ) ) == NULL )
365 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
366
367 memcpy( ssl->cli_id, info, ilen );
368 ssl->cli_id_len = ilen;
369
370 return( 0 );
371}
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +0200372
373void ssl_set_dtls_cookies( ssl_context *ssl,
374 ssl_cookie_write_t *f_cookie_write,
375 ssl_cookie_check_t *f_cookie_check,
376 void *p_cookie )
377{
378 ssl->f_cookie_write = f_cookie_write;
379 ssl->f_cookie_check = f_cookie_check;
380 ssl->p_cookie = p_cookie;
381}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +0200382#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200383
Paul Bakker0be444a2013-08-27 21:55:01 +0200384#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200385/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200386 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100387 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388 */
389static int ssl_sni_wrapper( ssl_context *ssl,
390 const unsigned char* name, size_t len )
391{
392 int ret;
393 ssl_key_cert *key_cert_ori = ssl->key_cert;
394
395 ssl->key_cert = NULL;
396 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200397 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200398
399 ssl->key_cert = key_cert_ori;
400
401 return( ret );
402}
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000405 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000406 size_t len )
407{
408 int ret;
409 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000410 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000411
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100412 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
413
Paul Bakker5701cdc2012-09-27 21:49:42 +0000414 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
415 if( servername_list_size + 2 != len )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 p = buf + 2;
422 while( servername_list_size > 0 )
423 {
424 hostname_len = ( ( p[1] << 8 ) | p[2] );
425 if( hostname_len + 3 > servername_list_size )
426 {
427 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
428 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
429 }
430
431 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
432 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200433 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000434 if( ret != 0 )
435 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100436 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000437 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
438 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
439 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
440 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000441 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000445 p += hostname_len + 3;
446 }
447
448 if( servername_list_size != 0 )
449 {
450 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
451 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000452 }
453
454 return( 0 );
455}
Paul Bakker0be444a2013-08-27 21:55:01 +0200456#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000457
Paul Bakker48916f92012-09-16 19:57:18 +0000458static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000459 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000460 size_t len )
461{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000462 int ret;
463
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100464#if defined(POLARSSL_SSL_RENEGOTIATION)
465 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
466 {
467 /* Check verify-data in constant-time. The length OTOH is no secret */
468 if( len != 1 + ssl->verify_data_len ||
469 buf[0] != ssl->verify_data_len ||
470 safer_memcmp( buf + 1, ssl->peer_verify_data,
471 ssl->verify_data_len ) != 0 )
472 {
473 SSL_DEBUG_MSG( 1, ( "non-matching renegotiation info" ) );
474
475 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
476 return( ret );
477
478 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
479 }
480 }
481 else
482#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +0000483 {
484 if( len != 1 || buf[0] != 0x0 )
485 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100486 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiation info" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000487
488 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
489 return( ret );
490
Paul Bakker48916f92012-09-16 19:57:18 +0000491 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
492 }
493
494 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
495 }
Paul Bakker48916f92012-09-16 19:57:18 +0000496
497 return( 0 );
498}
499
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100500#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
501 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +0000502static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
503 const unsigned char *buf,
504 size_t len )
505{
506 size_t sig_alg_list_size;
507 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200508 const unsigned char *end = buf + len;
509 const int *md_cur;
510
Paul Bakker23f36802012-09-28 14:15:14 +0000511
512 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
513 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200514 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000515 {
516 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
517 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
518 }
519
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200520 /*
521 * For now, ignore the SignatureAlgorithm part and rely on offered
522 * ciphersuites only for that part. To be fixed later.
523 *
524 * So, just look at the HashAlgorithm part.
525 */
526 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
527 for( p = buf + 2; p < end; p += 2 ) {
528 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
529 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200530 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200531 }
Paul Bakker23f36802012-09-28 14:15:14 +0000532 }
Paul Bakker23f36802012-09-28 14:15:14 +0000533 }
534
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200535 /* Some key echanges do not need signatures at all */
536 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
537 return( 0 );
538
539have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000540 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
541 ssl->handshake->sig_alg ) );
542
543 return( 0 );
544}
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +0100545#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
546 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker23f36802012-09-28 14:15:14 +0000547
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200548#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200549static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
550 const unsigned char *buf,
551 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100552{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200553 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100554 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200555 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100556
557 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
558 if( list_size + 2 != len ||
559 list_size % 2 != 0 )
560 {
561 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
562 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
563 }
564
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200565 /* Should never happen unless client duplicates the extension */
566 if( ssl->handshake->curves != NULL )
567 {
568 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
569 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
570 }
571
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +0100572 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200573 * and leave room for a final 0 */
574 our_size = list_size / 2 + 1;
575 if( our_size > POLARSSL_ECP_DP_MAX )
576 our_size = POLARSSL_ECP_DP_MAX;
577
578 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
579 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
580
Paul Bakker9af723c2014-05-01 13:03:14 +0200581 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200582 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200583 ssl->handshake->curves = curves;
584
Paul Bakker41c83d32013-03-20 14:39:14 +0100585 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200586 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100587 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200589
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200590 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200592 *curves++ = curve_info;
593 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100594 }
595
596 list_size -= 2;
597 p += 2;
598 }
599
600 return( 0 );
601}
602
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200603static int ssl_parse_supported_point_formats( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100606{
607 size_t list_size;
608 const unsigned char *p;
609
610 list_size = buf[0];
611 if( list_size + 1 != len )
612 {
613 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
614 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
615 }
616
617 p = buf + 2;
618 while( list_size > 0 )
619 {
620 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
621 p[0] == POLARSSL_ECP_PF_COMPRESSED )
622 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200623 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200624 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100625 return( 0 );
626 }
627
628 list_size--;
629 p++;
630 }
631
632 return( 0 );
633}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200634#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100635
Paul Bakker05decb22013-08-15 13:33:48 +0200636#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200637static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
638 const unsigned char *buf,
639 size_t len )
640{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200641 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200642 {
643 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
644 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
645 }
646
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200647 ssl->session_negotiate->mfl_code = buf[0];
648
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200649 return( 0 );
650}
Paul Bakker05decb22013-08-15 13:33:48 +0200651#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200652
Paul Bakker1f2bc622013-08-15 13:45:55 +0200653#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200654static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
655 const unsigned char *buf,
656 size_t len )
657{
658 if( len != 0 )
659 {
660 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
661 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
662 }
663
664 ((void) buf);
665
Manuel Pégourié-Gonnarde117a8f2015-01-09 12:39:35 +0100666 if( ssl->trunc_hmac == SSL_TRUNC_HMAC_ENABLED )
667 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200668
669 return( 0 );
670}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200671#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200672
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100673#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
674static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
675 const unsigned char *buf,
676 size_t len )
677{
678 if( len != 0 )
679 {
680 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
681 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
682 }
683
684 ((void) buf);
685
686 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
687 ssl->minor_ver != SSL_MINOR_VERSION_0 )
688 {
689 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
690 }
691
692 return( 0 );
693}
694#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
695
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200696#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
697static int ssl_parse_extended_ms_ext( ssl_context *ssl,
698 const unsigned char *buf,
699 size_t len )
700{
701 if( len != 0 )
702 {
703 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705 }
706
707 ((void) buf);
708
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200709 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
710 ssl->minor_ver != SSL_MINOR_VERSION_0 )
711 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200712 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200713 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200714
715 return( 0 );
716}
717#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
718
Paul Bakkera503a632013-08-14 13:48:06 +0200719#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200720static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200721 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200722 size_t len )
723{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200724 int ret;
725
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200726 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
727 return( 0 );
728
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200729 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200730 ssl->handshake->new_session_ticket = 1;
731
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200732 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
733
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200734 if( len == 0 )
735 return( 0 );
736
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100737#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200738 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
739 {
740 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
741 return( 0 );
742 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100743#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200744
745 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200746 * Failures are ok: just ignore the ticket and proceed.
747 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200748 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
749 {
750 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200751 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200752 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200753
754 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
755
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200756 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200757
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200758 /* Don't send a new ticket after all, this one is OK */
759 ssl->handshake->new_session_ticket = 0;
760
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200761 return( 0 );
762}
Paul Bakkera503a632013-08-14 13:48:06 +0200763#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200764
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200765#if defined(POLARSSL_SSL_ALPN)
766static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200767 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200768{
Paul Bakker14b16c62014-05-28 11:33:54 +0200769 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200770 const unsigned char *theirs, *start, *end;
771 const char **ours;
772
773 /* If ALPN not configured, just ignore the extension */
774 if( ssl->alpn_list == NULL )
775 return( 0 );
776
777 /*
778 * opaque ProtocolName<1..2^8-1>;
779 *
780 * struct {
781 * ProtocolName protocol_name_list<2..2^16-1>
782 * } ProtocolNameList;
783 */
784
785 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
786 if( len < 4 )
787 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
788
789 list_len = ( buf[0] << 8 ) | buf[1];
790 if( list_len != len - 2 )
791 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
792
793 /*
794 * Use our order of preference
795 */
796 start = buf + 2;
797 end = buf + len;
798 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
799 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200800 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200801 for( theirs = start; theirs != end; theirs += cur_len )
802 {
803 /* If the list is well formed, we should get equality first */
804 if( theirs > end )
805 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
806
807 cur_len = *theirs++;
808
809 /* Empty strings MUST NOT be included */
810 if( cur_len == 0 )
811 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
812
Paul Bakker14b16c62014-05-28 11:33:54 +0200813 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200814 memcmp( theirs, *ours, cur_len ) == 0 )
815 {
816 ssl->alpn_chosen = *ours;
817 return( 0 );
818 }
819 }
820 }
821
822 /* If we get there, no match was found */
823 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
824 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
826}
827#endif /* POLARSSL_SSL_ALPN */
828
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100829/*
830 * Auxiliary functions for ServerHello parsing and related actions
831 */
832
833#if defined(POLARSSL_X509_CRT_PARSE_C)
834/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100835 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100836 */
837#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100838static int ssl_check_key_curve( pk_context *pk,
839 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100840{
841 const ecp_curve_info **crv = curves;
842 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
843
844 while( *crv != NULL )
845 {
846 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100847 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100848 crv++;
849 }
850
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100851 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100852}
853#endif /* POLARSSL_ECDSA_C */
854
855/*
856 * Try picking a certificate for this ciphersuite,
857 * return 0 on success and -1 on failure.
858 */
859static int ssl_pick_cert( ssl_context *ssl,
860 const ssl_ciphersuite_t * ciphersuite_info )
861{
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100862 ssl_key_cert *cur, *list, *fallback = NULL;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100863 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
864
865#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
866 if( ssl->handshake->sni_key_cert != NULL )
867 list = ssl->handshake->sni_key_cert;
868 else
869#endif
870 list = ssl->handshake->key_cert;
871
872 if( pk_alg == POLARSSL_PK_NONE )
873 return( 0 );
874
875 for( cur = list; cur != NULL; cur = cur->next )
876 {
877 if( ! pk_can_do( cur->key, pk_alg ) )
878 continue;
879
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200880 /*
881 * This avoids sending the client a cert it'll reject based on
882 * keyUsage or other extensions.
883 *
884 * It also allows the user to provision different certificates for
885 * different uses based on keyUsage, eg if they want to avoid signing
886 * and decrypting with the same RSA key.
887 */
888 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
889 SSL_IS_SERVER ) != 0 )
890 {
891 continue;
892 }
893
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100894#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100895 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100896 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100897 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100898#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100899
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100900 /*
901 * Try to select a SHA-1 certificate for pre-1.2 clients, but still
902 * present them a SHA-higher cert rather than failing if it's the only
903 * one we got that satisfies the other conditions.
904 */
905 if( ssl->minor_ver < SSL_MINOR_VERSION_3 &&
906 cur->cert->sig_md != POLARSSL_MD_SHA1 )
907 {
908 if( fallback == NULL )
909 fallback = cur;
910 continue;
911 }
912
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100913 /* If we get there, we got a winner */
914 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100915 }
916
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100917 if( cur != NULL )
918 {
919 ssl->handshake->key_cert = cur;
920 return( 0 );
921 }
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100922
Manuel Pégourié-Gonnarddf331a52015-01-08 16:43:07 +0100923 if( fallback != NULL )
924 {
925 ssl->handshake->key_cert = fallback;
926 return( 0 );
927 }
928
929 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100930}
931#endif /* POLARSSL_X509_CRT_PARSE_C */
932
933/*
934 * Check if a given ciphersuite is suitable for use with our config/keys/etc
935 * Sets ciphersuite_info only if the suite matches.
936 */
937static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
938 const ssl_ciphersuite_t **ciphersuite_info )
939{
940 const ssl_ciphersuite_t *suite_info;
941
942 suite_info = ssl_ciphersuite_from_id( suite_id );
943 if( suite_info == NULL )
944 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100945 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
946 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100947 }
948
949 if( suite_info->min_minor_ver > ssl->minor_ver ||
950 suite_info->max_minor_ver < ssl->minor_ver )
951 return( 0 );
952
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100953#if defined(POLARSSL_SSL_PROTO_DTLS)
954 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
955 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
956 return( 0 );
957#endif
958
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +0100959 if( ssl->arc4_disabled == SSL_ARC4_DISABLED &&
960 suite_info->cipher == POLARSSL_CIPHER_ARC4_128 )
961 return( 0 );
962
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100963#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
964 if( ssl_ciphersuite_uses_ec( suite_info ) &&
965 ( ssl->handshake->curves == NULL ||
966 ssl->handshake->curves[0] == NULL ) )
967 return( 0 );
968#endif
969
970#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
971 /* If the ciphersuite requires a pre-shared key and we don't
972 * have one, skip it now rather than failing later */
973 if( ssl_ciphersuite_uses_psk( suite_info ) &&
974 ssl->f_psk == NULL &&
975 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
976 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
977 return( 0 );
978#endif
979
980#if defined(POLARSSL_X509_CRT_PARSE_C)
981 /*
982 * Final check: if ciphersuite requires us to have a
983 * certificate/key of a particular type:
984 * - select the appropriate certificate if we have one, or
985 * - try the next ciphersuite if we don't
986 * This must be done last since we modify the key_cert list.
987 */
988 if( ssl_pick_cert( ssl, suite_info ) != 0 )
989 return( 0 );
990#endif
991
992 *ciphersuite_info = suite_info;
993 return( 0 );
994}
995
Paul Bakker78a8c712013-03-06 17:01:52 +0100996#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
997static int ssl_parse_client_hello_v2( ssl_context *ssl )
998{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +0100999 int ret, got_common_suite;
Paul Bakker78a8c712013-03-06 17:01:52 +01001000 unsigned int i, j;
1001 size_t n;
1002 unsigned int ciph_len, sess_len, chal_len;
1003 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001004 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +02001005 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +01001006
1007 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
1008
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001009#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001010 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
1013
1014 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1015 return( ret );
1016
1017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1018 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001019#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001020
1021 buf = ssl->in_hdr;
1022
1023 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1024
1025 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1026 buf[2] ) );
1027 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1028 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1029 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1030 buf[3], buf[4] ) );
1031
1032 /*
1033 * SSLv2 Client Hello
1034 *
1035 * Record layer:
1036 * 0 . 1 message length
1037 *
1038 * SSL layer:
1039 * 2 . 2 message type
1040 * 3 . 4 protocol version
1041 */
1042 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1043 buf[3] != SSL_MAJOR_VERSION_3 )
1044 {
1045 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1046 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1047 }
1048
1049 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1050
1051 if( n < 17 || n > 512 )
1052 {
1053 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1054 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1055 }
1056
1057 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001058 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1059 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001060
1061 if( ssl->minor_ver < ssl->min_minor_ver )
1062 {
1063 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001064 " [%d:%d] < [%d:%d]",
1065 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001066 ssl->min_major_ver, ssl->min_minor_ver ) );
1067
1068 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1069 SSL_ALERT_MSG_PROTOCOL_VERSION );
1070 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1071 }
1072
Paul Bakker2fbefde2013-06-29 16:01:15 +02001073 ssl->handshake->max_major_ver = buf[3];
1074 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001075
1076 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1077 {
1078 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1079 return( ret );
1080 }
1081
1082 ssl->handshake->update_checksum( ssl, buf + 2, n );
1083
1084 buf = ssl->in_msg;
1085 n = ssl->in_left - 5;
1086
1087 /*
1088 * 0 . 1 ciphersuitelist length
1089 * 2 . 3 session id length
1090 * 4 . 5 challenge length
1091 * 6 . .. ciphersuitelist
1092 * .. . .. session id
1093 * .. . .. challenge
1094 */
1095 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1096
1097 ciph_len = ( buf[0] << 8 ) | buf[1];
1098 sess_len = ( buf[2] << 8 ) | buf[3];
1099 chal_len = ( buf[4] << 8 ) | buf[5];
1100
1101 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1102 ciph_len, sess_len, chal_len ) );
1103
1104 /*
1105 * Make sure each parameter length is valid
1106 */
1107 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1108 {
1109 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1110 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1111 }
1112
1113 if( sess_len > 32 )
1114 {
1115 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1116 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1117 }
1118
1119 if( chal_len < 8 || chal_len > 32 )
1120 {
1121 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1122 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1123 }
1124
1125 if( n != 6 + ciph_len + sess_len + chal_len )
1126 {
1127 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1129 }
1130
1131 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1132 buf + 6, ciph_len );
1133 SSL_DEBUG_BUF( 3, "client hello, session id",
1134 buf + 6 + ciph_len, sess_len );
1135 SSL_DEBUG_BUF( 3, "client hello, challenge",
1136 buf + 6 + ciph_len + sess_len, chal_len );
1137
1138 p = buf + 6 + ciph_len;
1139 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001140 memset( ssl->session_negotiate->id, 0,
1141 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001142 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1143
1144 p += sess_len;
1145 memset( ssl->handshake->randbytes, 0, 64 );
1146 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1147
1148 /*
1149 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1150 */
1151 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1152 {
1153 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1154 {
1155 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001156#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001157 if( ssl->renegotiation == SSL_RENEGOTIATION )
1158 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001159 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1160 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001161
1162 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1163 return( ret );
1164
1165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1166 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001167#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001168 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1169 break;
1170 }
1171 }
1172
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001173#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1174 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1175 {
1176 if( p[0] == 0 &&
1177 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1178 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1179 {
1180 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1181
1182 if( ssl->minor_ver < ssl->max_minor_ver )
1183 {
1184 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1185
1186 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1187 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1188
1189 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1190 }
1191
1192 break;
1193 }
1194 }
1195#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1196
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001197 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001198 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001199 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001200#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1201 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1202 {
1203 for( i = 0; ciphersuites[i] != 0; i++ )
1204#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001205 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001206 {
1207 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001208#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001209 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001210 if( p[0] != 0 ||
1211 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1212 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1213 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001214
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001215 got_common_suite = 1;
1216
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001217 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1218 &ciphersuite_info ) ) != 0 )
1219 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001220
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001221 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001222 goto have_ciphersuite_v2;
1223 }
1224 }
1225
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001226 if( got_common_suite )
1227 {
1228 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1229 "but none of them usable" ) );
1230 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1231 }
1232 else
1233 {
1234 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1235 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1236 }
Paul Bakker78a8c712013-03-06 17:01:52 +01001237
1238have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001239 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001240 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001241 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001242
1243 /*
1244 * SSLv2 Client Hello relevant renegotiation security checks
1245 */
1246 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1247 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1248 {
1249 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1250
1251 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1252 return( ret );
1253
1254 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1255 }
1256
1257 ssl->in_left = 0;
1258 ssl->state++;
1259
1260 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1261
1262 return( 0 );
1263}
1264#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1265
Paul Bakker5121ce52009-01-03 21:22:43 +00001266static int ssl_parse_client_hello( ssl_context *ssl )
1267{
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001268 int ret, got_common_suite;
Paul Bakker23986e52011-04-24 08:57:21 +00001269 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001270 unsigned int ciph_offset, comp_offset, ext_offset;
1271 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001272#if defined(POLARSSL_SSL_PROTO_DTLS)
1273 unsigned int cookie_offset, cookie_len;
1274#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001275 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001276#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001277 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001278#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001279 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001280 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001281 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001282 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
1284 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1285
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001286#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1287read_record_header:
1288#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001289 /*
1290 * If renegotiating, then the input was read with ssl_read_record(),
1291 * otherwise read it ourselves manually in order to support SSLv2
1292 * ClientHello, which doesn't use the same record layer format.
1293 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001294#if defined(POLARSSL_SSL_RENEGOTIATION)
1295 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1296#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 {
Manuel Pégourié-Gonnard59c6f2e2015-01-22 11:06:40 +00001298 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
1299 {
1300 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1301 return( ret );
1302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
1304
1305 buf = ssl->in_hdr;
1306
Paul Bakker78a8c712013-03-06 17:01:52 +01001307#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001308#if defined(POLARSSL_SSL_PROTO_DTLS)
1309 if( ssl->transport == SSL_TRANSPORT_STREAM )
1310#endif
1311 if( ( buf[0] & 0x80 ) != 0 )
1312 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001313#endif
1314
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001315 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001316
Paul Bakkerec636f32012-09-09 19:17:02 +00001317 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001318 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001319 *
1320 * Record layer:
1321 * 0 . 0 message type
1322 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001323 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001324 * 3 . 4 message length
1325 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001326 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1327 buf[0] ) );
1328
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001329 if( buf[0] != SSL_MSG_HANDSHAKE )
1330 {
1331 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1332 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1333 }
1334
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001335 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1336 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1337
1338 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1339 buf[1], buf[2] ) );
1340
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001341 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001342
1343 /* According to RFC 5246 Appendix E.1, the version here is typically
1344 * "{03,00}, the lowest version number supported by the client, [or] the
1345 * value of ClientHello.client_version", so the only meaningful check here
1346 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001347 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001349 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1350 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1351 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001353 /* For DTLS if this is the initial handshake, remember the client sequence
1354 * number to use it in our next message (RFC 6347 4.2.1) */
1355#if defined(POLARSSL_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard3a173f42015-01-22 13:30:33 +00001356 if( ssl->transport == SSL_TRANSPORT_DATAGRAM
1357#if defined(POLARSSL_SSL_RENEGOTIATION)
1358 && ssl->renegotiation == SSL_INITIAL_HANDSHAKE
1359#endif
1360 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001361 {
1362 /* Epoch should be 0 for initial handshakes */
1363 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1364 {
1365 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1366 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1367 }
1368
1369 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001370
1371#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1372 if( ssl_dtls_replay_check( ssl ) != 0 )
1373 {
1374 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1375 ssl->next_record_offset = 0;
1376 ssl->in_left = 0;
1377 goto read_record_header;
1378 }
1379
1380 /* No MAC to check yet, so we can update right now */
1381 ssl_dtls_replay_update( ssl );
1382#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001383 }
1384#endif /* POLARSSL_SSL_PROTO_DTLS */
1385
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001386 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001388#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001389 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1390 {
1391 /* Set by ssl_read_record() */
1392 msg_len = ssl->in_hslen;
1393 }
1394 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001395#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001396 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001397 if( msg_len > SSL_MAX_CONTENT_LEN )
1398 {
1399 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1400 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1401 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001402
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001403 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1404 {
1405 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1406 return( ret );
1407 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001408
1409 /* Done reading this record, get ready for the next one */
1410#if defined(POLARSSL_SSL_PROTO_DTLS)
1411 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1412 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1413 else
1414#endif
1415 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001416 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001417
1418 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001419
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001420 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001421
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001422 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001423
1424 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001425 * Handshake layer:
1426 * 0 . 0 handshake type
1427 * 1 . 3 handshake length
1428 * 4 . 5 DTLS only: message seqence number
1429 * 6 . 8 DTLS only: fragment offset
1430 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001431 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001432 if( msg_len < ssl_hs_hdr_len( ssl ) )
1433 {
1434 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1435 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1436 }
1437
1438 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1439
1440 if( buf[0] != SSL_HS_CLIENT_HELLO )
1441 {
1442 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1444 }
1445
1446 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1447 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1448
1449 /* We don't support fragmentation of ClientHello (yet?) */
1450 if( buf[1] != 0 ||
1451 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1452 {
1453 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1454 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1455 }
1456
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001457#if defined(POLARSSL_SSL_PROTO_DTLS)
1458 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1459 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001460 /*
1461 * Copy the client's handshake message_seq on initial handshakes
1462 */
1463 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001464 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001465 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1466 ssl->in_msg[5];
1467 ssl->handshake->out_msg_seq = cli_msg_seq;
1468 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001469 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001470 else
1471 {
1472 /* This couldn't be done in ssl_prepare_handshake_record() */
1473 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1474 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001475
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001476 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1477 {
1478 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1479 "%d (expected %d)", cli_msg_seq,
1480 ssl->handshake->in_msg_seq ) );
1481 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1482 }
1483
1484 ssl->handshake->in_msg_seq++;
1485 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001486
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001487 /*
1488 * For now we don't support fragmentation, so make sure
1489 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001490 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001491 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1492 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1493 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001494 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001495 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1496 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001497 }
1498#endif /* POLARSSL_SSL_PROTO_DTLS */
1499
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001500 buf += ssl_hs_hdr_len( ssl );
1501 msg_len -= ssl_hs_hdr_len( ssl );
1502
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001503 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001504 * ClientHello layer:
1505 * 0 . 1 protocol version
1506 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1507 * 34 . 35 session id length (1 byte)
1508 * 35 . 34+x session id
1509 * 35+x . 35+x DTLS only: cookie length (1 byte)
1510 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001511 * .. . .. ciphersuite list length (2 bytes)
1512 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001513 * .. . .. compression alg. list length (1 byte)
1514 * .. . .. compression alg. list
1515 * .. . .. extensions length (2 bytes, optional)
1516 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001517 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001518
1519 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001520 * Minimal length (with everything empty and extensions ommitted) is
1521 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1522 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001523 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001524 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001525 {
1526 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1527 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1528 }
1529
1530 /*
1531 * Check and save the protocol version
1532 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001533 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001534
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001535 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001536 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001537
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001538 ssl->handshake->max_major_ver = ssl->major_ver;
1539 ssl->handshake->max_minor_ver = ssl->minor_ver;
1540
1541 if( ssl->major_ver < ssl->min_major_ver ||
1542 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001543 {
1544 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001545 " [%d:%d] < [%d:%d]",
1546 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001547 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001548
1549 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1550 SSL_ALERT_MSG_PROTOCOL_VERSION );
1551
1552 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1553 }
1554
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001555 if( ssl->major_ver > ssl->max_major_ver )
1556 {
1557 ssl->major_ver = ssl->max_major_ver;
1558 ssl->minor_ver = ssl->max_minor_ver;
1559 }
1560 else if( ssl->minor_ver > ssl->max_minor_ver )
1561 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001562
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001563 /*
1564 * Save client random (inc. Unix time)
1565 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001566 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001567
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001568 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001569
1570 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001571 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001572 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001573 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001574
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001575 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001576 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001577 {
1578 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1579 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1580 }
1581
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001582 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001583
Paul Bakker48916f92012-09-16 19:57:18 +00001584 ssl->session_negotiate->length = sess_len;
1585 memset( ssl->session_negotiate->id, 0,
1586 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001587 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001588 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001589
1590 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001591 * Check the cookie length and content
1592 */
1593#if defined(POLARSSL_SSL_PROTO_DTLS)
1594 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1595 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001596 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001597 cookie_len = buf[cookie_offset];
1598
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001599 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001600 {
1601 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1602 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1603 }
1604
1605 SSL_DEBUG_BUF( 3, "client hello, cookie",
1606 buf + cookie_offset + 1, cookie_len );
1607
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001608#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001609 if( ssl->f_cookie_check != NULL &&
1610 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001611 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001612 if( ssl->f_cookie_check( ssl->p_cookie,
1613 buf + cookie_offset + 1, cookie_len,
1614 ssl->cli_id, ssl->cli_id_len ) != 0 )
1615 {
1616 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1617 ssl->handshake->verify_cookie_len = 1;
1618 }
1619 else
1620 {
1621 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1622 ssl->handshake->verify_cookie_len = 0;
1623 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001624 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001625 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001626#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001627 {
1628 /* We know we didn't send a cookie, so it should be empty */
1629 if( cookie_len != 0 )
1630 {
1631 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1632 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1633 }
1634
1635 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1636 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001637 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001638#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001639
1640 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001642 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001643#if defined(POLARSSL_SSL_PROTO_DTLS)
1644 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1645 ciph_offset = cookie_offset + 1 + cookie_len;
1646 else
1647#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001648 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001649
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001650 ciph_len = ( buf[ciph_offset + 0] << 8 )
1651 | ( buf[ciph_offset + 1] );
1652
1653 if( ciph_len < 2 ||
1654 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1655 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001656 {
1657 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1658 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1659 }
1660
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001661 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1662 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001663
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001664 /*
1665 * Check the compression algorithms length and pick one
1666 */
1667 comp_offset = ciph_offset + 2 + ciph_len;
1668
1669 comp_len = buf[comp_offset];
1670
1671 if( comp_len < 1 ||
1672 comp_len > 16 ||
1673 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001674 {
1675 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1676 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1677 }
1678
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001679 SSL_DEBUG_BUF( 3, "client hello, compression",
1680 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001681
1682 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001683#if defined(POLARSSL_ZLIB_SUPPORT)
1684 for( i = 0; i < comp_len; ++i )
1685 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001686 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 {
Paul Bakker48916f92012-09-16 19:57:18 +00001688 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001689 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 }
1691 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001692#endif
1693
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001694 /* See comments in ssl_write_client_hello() */
1695#if defined(POLARSSL_SSL_PROTO_DTLS)
1696 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1697 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1698#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001699
Paul Bakkerec636f32012-09-09 19:17:02 +00001700 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001701 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001702 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001703 ext_offset = comp_offset + 1 + comp_len;
1704 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001705 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001706 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001707 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001708 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1709 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1710 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001711
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001712 ext_len = ( buf[ext_offset + 0] << 8 )
1713 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001714
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001715 if( ( ext_len > 0 && ext_len < 4 ) ||
1716 msg_len != ext_offset + 2 + ext_len )
1717 {
1718 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1719 SSL_DEBUG_BUF( 3, "client hello extensions",
1720 buf + ext_offset + 2, ext_len );
1721 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001722 }
1723 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001724 else
1725 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001726
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001727 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001728
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001729 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001730 {
1731 unsigned int ext_id = ( ( ext[0] << 8 )
1732 | ( ext[1] ) );
1733 unsigned int ext_size = ( ( ext[2] << 8 )
1734 | ( ext[3] ) );
1735
1736 if( ext_size + 4 > ext_len )
1737 {
1738 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1739 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1740 }
1741 switch( ext_id )
1742 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001743#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001744 case TLS_EXT_SERVERNAME:
1745 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1746 if( ssl->f_sni == NULL )
1747 break;
1748
1749 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1750 if( ret != 0 )
1751 return( ret );
1752 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001753#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001754
Paul Bakker48916f92012-09-16 19:57:18 +00001755 case TLS_EXT_RENEGOTIATION_INFO:
1756 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001757#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001758 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001759#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001760
Paul Bakker23f36802012-09-28 14:15:14 +00001761 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1762 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001763 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001764 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001765
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001766#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1767 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001768 case TLS_EXT_SIG_ALG:
1769 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001770#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001771 if( ssl->renegotiation == SSL_RENEGOTIATION )
1772 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001773#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001774
1775 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1776 if( ret != 0 )
1777 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001778 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001779#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1780 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001781
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001782#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001783 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1784 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1785
1786 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1787 if( ret != 0 )
1788 return( ret );
1789 break;
1790
1791 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1792 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001793 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001794
1795 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1796 if( ret != 0 )
1797 return( ret );
1798 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001799#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001800
Paul Bakker05decb22013-08-15 13:33:48 +02001801#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001802 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1803 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1804
1805 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1806 if( ret != 0 )
1807 return( ret );
1808 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001809#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001810
Paul Bakker1f2bc622013-08-15 13:45:55 +02001811#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001812 case TLS_EXT_TRUNCATED_HMAC:
1813 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1814
1815 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1816 if( ret != 0 )
1817 return( ret );
1818 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001819#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001820
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001821#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1822 case TLS_EXT_ENCRYPT_THEN_MAC:
1823 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1824
1825 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1826 if( ret != 0 )
1827 return( ret );
1828 break;
1829#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1830
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001831#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1832 case TLS_EXT_EXTENDED_MASTER_SECRET:
1833 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1834
1835 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1836 if( ret != 0 )
1837 return( ret );
1838 break;
1839#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1840
Paul Bakkera503a632013-08-14 13:48:06 +02001841#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001842 case TLS_EXT_SESSION_TICKET:
1843 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1844
1845 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1846 if( ret != 0 )
1847 return( ret );
1848 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001849#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001850
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001851#if defined(POLARSSL_SSL_ALPN)
1852 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001853 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001854
1855 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1856 if( ret != 0 )
1857 return( ret );
1858 break;
1859#endif /* POLARSSL_SSL_SESSION_TICKETS */
1860
Paul Bakker48916f92012-09-16 19:57:18 +00001861 default:
1862 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1863 ext_id ) );
1864 }
1865
1866 ext_len -= 4 + ext_size;
1867 ext += 4 + ext_size;
1868
1869 if( ext_len > 0 && ext_len < 4 )
1870 {
1871 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1872 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1873 }
1874 }
1875
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001876#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1877 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1878 {
1879 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1880 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1881 {
1882 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1883
1884 if( ssl->minor_ver < ssl->max_minor_ver )
1885 {
1886 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1887
1888 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1889 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1890
1891 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1892 }
1893
1894 break;
1895 }
1896 }
1897#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1898
Paul Bakker48916f92012-09-16 19:57:18 +00001899 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001900 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1901 */
1902 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1903 {
1904 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1905 {
1906 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1907 if( ssl->renegotiation == SSL_RENEGOTIATION )
1908 {
1909 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1910
1911 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1912 return( ret );
1913
1914 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1915 }
1916 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1917 break;
1918 }
1919 }
1920
1921 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001922 * Renegotiation security checks
1923 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001924 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001925 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1926 {
1927 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1928 handshake_failure = 1;
1929 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001930#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001931 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1932 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1933 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001934 {
1935 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001936 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001937 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001938 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1939 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1940 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001941 {
1942 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001943 handshake_failure = 1;
1944 }
1945 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1946 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1947 renegotiation_info_seen == 1 )
1948 {
1949 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1950 handshake_failure = 1;
1951 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001952#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001953
1954 if( handshake_failure == 1 )
1955 {
1956 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1957 return( ret );
1958
Paul Bakker48916f92012-09-16 19:57:18 +00001959 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1960 }
Paul Bakker380da532012-04-18 16:10:25 +00001961
Paul Bakker41c83d32013-03-20 14:39:14 +01001962 /*
1963 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001964 * (At the end because we need information from the EC-based extensions
1965 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001966 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001967 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001968 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001969 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001970#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001971 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001972 {
1973 for( i = 0; ciphersuites[i] != 0; i++ )
1974#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001975 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001976 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001977 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001978#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001979 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001980 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1981 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1982 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001983
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001984 got_common_suite = 1;
1985
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001986 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1987 &ciphersuite_info ) ) != 0 )
1988 return( ret );
1989
1990 if( ciphersuite_info != NULL )
1991 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001992 }
1993 }
1994
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001995 if( got_common_suite )
1996 {
1997 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1998 "but none of them usable" ) );
1999 ssl_send_fatal_handshake_failure( ssl );
2000 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
2001 }
2002 else
2003 {
2004 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2005 ssl_send_fatal_handshake_failure( ssl );
2006 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2007 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002008
2009have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002010 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002011 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2012 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2013
Paul Bakker5121ce52009-01-03 21:22:43 +00002014 ssl->state++;
2015
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002016#if defined(POLARSSL_SSL_PROTO_DTLS)
2017 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2018 ssl_recv_flight_completed( ssl );
2019#endif
2020
Paul Bakker5121ce52009-01-03 21:22:43 +00002021 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2022
2023 return( 0 );
2024}
2025
Paul Bakker1f2bc622013-08-15 13:45:55 +02002026#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002027static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2028 unsigned char *buf,
2029 size_t *olen )
2030{
2031 unsigned char *p = buf;
2032
2033 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2034 {
2035 *olen = 0;
2036 return;
2037 }
2038
2039 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2040
2041 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2042 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2043
2044 *p++ = 0x00;
2045 *p++ = 0x00;
2046
2047 *olen = 4;
2048}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002049#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002050
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002051#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2052static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2053 unsigned char *buf,
2054 size_t *olen )
2055{
2056 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002057 const ssl_ciphersuite_t *suite = NULL;
2058 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002059
2060 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2061 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2062 {
2063 *olen = 0;
2064 return;
2065 }
2066
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002067 /*
2068 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2069 * from a client and then selects a stream or Authenticated Encryption
2070 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2071 * encrypt-then-MAC response extension back to the client."
2072 */
2073 if( ( suite = ssl_ciphersuite_from_id(
2074 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2075 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2076 cipher->mode != POLARSSL_MODE_CBC )
2077 {
2078 *olen = 0;
2079 return;
2080 }
2081
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002082 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2083
2084 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2085 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2086
2087 *p++ = 0x00;
2088 *p++ = 0x00;
2089
2090 *olen = 4;
2091}
2092#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2093
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002094#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2095static void ssl_write_extended_ms_ext( ssl_context *ssl,
2096 unsigned char *buf,
2097 size_t *olen )
2098{
2099 unsigned char *p = buf;
2100
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002101 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2102 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002103 {
2104 *olen = 0;
2105 return;
2106 }
2107
2108 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2109 "extension" ) );
2110
2111 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2112 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2113
2114 *p++ = 0x00;
2115 *p++ = 0x00;
2116
2117 *olen = 4;
2118}
2119#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2120
Paul Bakkera503a632013-08-14 13:48:06 +02002121#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002122static void ssl_write_session_ticket_ext( ssl_context *ssl,
2123 unsigned char *buf,
2124 size_t *olen )
2125{
2126 unsigned char *p = buf;
2127
2128 if( ssl->handshake->new_session_ticket == 0 )
2129 {
2130 *olen = 0;
2131 return;
2132 }
2133
2134 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2135
2136 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2137 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2138
2139 *p++ = 0x00;
2140 *p++ = 0x00;
2141
2142 *olen = 4;
2143}
Paul Bakkera503a632013-08-14 13:48:06 +02002144#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002145
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002146static void ssl_write_renegotiation_ext( ssl_context *ssl,
2147 unsigned char *buf,
2148 size_t *olen )
2149{
2150 unsigned char *p = buf;
2151
2152 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2153 {
2154 *olen = 0;
2155 return;
2156 }
2157
2158 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2159
2160 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2161 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2162
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002163#if defined(POLARSSL_SSL_RENEGOTIATION)
2164 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2165 {
2166 *p++ = 0x00;
2167 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2168 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002169
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002170 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2171 p += ssl->verify_data_len;
2172 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2173 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002174
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002175 *olen = 5 + ssl->verify_data_len * 2;
2176 }
2177 else
2178#endif /* POLARSSL_SSL_RENEGOTIATION */
2179 {
2180 *p++ = 0x00;
2181 *p++ = 0x01;
2182 *p++ = 0x00;
2183
2184 *olen = 5;
2185 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002186}
2187
Paul Bakker05decb22013-08-15 13:33:48 +02002188#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002189static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2190 unsigned char *buf,
2191 size_t *olen )
2192{
2193 unsigned char *p = buf;
2194
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002195 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2196 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002197 *olen = 0;
2198 return;
2199 }
2200
2201 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2202
2203 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2204 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2205
2206 *p++ = 0x00;
2207 *p++ = 1;
2208
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002209 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002210
2211 *olen = 5;
2212}
Paul Bakker05decb22013-08-15 13:33:48 +02002213#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002214
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002215#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002216static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2217 unsigned char *buf,
2218 size_t *olen )
2219{
2220 unsigned char *p = buf;
2221 ((void) ssl);
2222
Paul Bakker677377f2013-10-28 12:54:26 +01002223 if( ( ssl->handshake->cli_exts &
2224 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2225 {
2226 *olen = 0;
2227 return;
2228 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002229
2230 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2231
2232 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2233 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2234
2235 *p++ = 0x00;
2236 *p++ = 2;
2237
2238 *p++ = 1;
2239 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2240
2241 *olen = 6;
2242}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002243#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002244
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002245#if defined(POLARSSL_SSL_ALPN )
2246static void ssl_write_alpn_ext( ssl_context *ssl,
2247 unsigned char *buf, size_t *olen )
2248{
2249 if( ssl->alpn_chosen == NULL )
2250 {
2251 *olen = 0;
2252 return;
2253 }
2254
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002255 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002256
2257 /*
2258 * 0 . 1 ext identifier
2259 * 2 . 3 ext length
2260 * 4 . 5 protocol list length
2261 * 6 . 6 protocol name length
2262 * 7 . 7+n protocol name
2263 */
2264 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2265 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2266
2267 *olen = 7 + strlen( ssl->alpn_chosen );
2268
2269 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2270 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2271
2272 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2273 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2274
2275 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2276
2277 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2278}
2279#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2280
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002281#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002282static int ssl_write_hello_verify_request( ssl_context *ssl )
2283{
2284 int ret;
2285 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002286 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002287
2288 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2289
2290 /*
2291 * struct {
2292 * ProtocolVersion server_version;
2293 * opaque cookie<0..2^8-1>;
2294 * } HelloVerifyRequest;
2295 */
2296
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002297 /* The RFC is not clear on this point, but sending the actual negotiated
2298 * version looks like the most interoperable thing to do. */
2299 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002300 ssl->transport, p );
2301 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2302 p += 2;
2303
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002304 /* If we get here, f_cookie_check is not null */
2305 if( ssl->f_cookie_write == NULL )
2306 {
2307 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2308 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2309 }
2310
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002311 /* Skip length byte until we know the length */
2312 cookie_len_byte = p++;
2313
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002314 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2315 &p, ssl->out_buf + SSL_BUFFER_LEN,
2316 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002317 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002318 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002319 return( ret );
2320 }
2321
2322 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2323
2324 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002325
2326 ssl->out_msglen = p - ssl->out_msg;
2327 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2328 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2329
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002330 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002331
2332 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2333 {
2334 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2335 return( ret );
2336 }
2337
2338 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2339
2340 return( 0 );
2341}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002342#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002343
Paul Bakker5121ce52009-01-03 21:22:43 +00002344static int ssl_write_server_hello( ssl_context *ssl )
2345{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002346#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002347 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002348#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002349 int ret;
2350 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002351 unsigned char *buf, *p;
2352
2353 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2354
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002355#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002356 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002357 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002358 {
2359 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2360 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2361
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002362 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002363 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002364#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002365
Paul Bakkera9a028e2013-11-21 17:31:06 +01002366 if( ssl->f_rng == NULL )
2367 {
2368 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2369 return( POLARSSL_ERR_SSL_NO_RNG );
2370 }
2371
Paul Bakker5121ce52009-01-03 21:22:43 +00002372 /*
2373 * 0 . 0 handshake type
2374 * 1 . 3 handshake length
2375 * 4 . 5 protocol version
2376 * 6 . 9 UNIX time()
2377 * 10 . 37 random bytes
2378 */
2379 buf = ssl->out_msg;
2380 p = buf + 4;
2381
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002382 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2383 ssl->transport, p );
2384 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002385
2386 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002387 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002388
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002389#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002390 t = time( NULL );
2391 *p++ = (unsigned char)( t >> 24 );
2392 *p++ = (unsigned char)( t >> 16 );
2393 *p++ = (unsigned char)( t >> 8 );
2394 *p++ = (unsigned char)( t );
2395
2396 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002397#else
2398 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2399 return( ret );
2400
2401 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002402#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Paul Bakkera3d195c2011-11-27 21:07:34 +00002404 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2405 return( ret );
2406
2407 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Paul Bakker48916f92012-09-16 19:57:18 +00002409 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002410
2411 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2412
2413 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002414 * Resume is 0 by default, see ssl_handshake_init().
2415 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2416 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002417 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002418 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002419#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002420 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002421#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002422 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002423 ssl->f_get_cache != NULL &&
2424 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2425 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002426 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002427 ssl->handshake->resume = 1;
2428 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002429
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002430 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
2432 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002433 * New session, create a new session id,
2434 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002435 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002436 ssl->state++;
2437
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002438#if defined(POLARSSL_HAVE_TIME)
2439 ssl->session_negotiate->start = time( NULL );
2440#endif
2441
Paul Bakkera503a632013-08-14 13:48:06 +02002442#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002443 if( ssl->handshake->new_session_ticket != 0 )
2444 {
2445 ssl->session_negotiate->length = n = 0;
2446 memset( ssl->session_negotiate->id, 0, 32 );
2447 }
2448 else
2449#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002450 {
2451 ssl->session_negotiate->length = n = 32;
2452 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002453 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002454 return( ret );
2455 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 }
2457 else
2458 {
2459 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002460 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002461 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002462 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002464
2465 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2466 {
2467 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2468 return( ret );
2469 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
2471
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002472 /*
2473 * 38 . 38 session id length
2474 * 39 . 38+n session id
2475 * 39+n . 40+n chosen ciphersuite
2476 * 41+n . 41+n chosen compression alg.
2477 * 42+n . 43+n extensions length
2478 * 44+n . 43+n+m extensions
2479 */
2480 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002481 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2482 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
2484 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2485 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2486 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002487 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002488
Paul Bakker48916f92012-09-16 19:57:18 +00002489 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2490 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2491 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002493 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2494 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002495 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002496 ssl->session_negotiate->compression ) );
2497
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002498 /*
2499 * First write extensions, then the total length
2500 */
2501 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2502 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002503
Paul Bakker05decb22013-08-15 13:33:48 +02002504#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002505 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2506 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002507#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002508
Paul Bakker1f2bc622013-08-15 13:45:55 +02002509#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002510 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2511 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002512#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002513
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002514#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2515 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2516 ext_len += olen;
2517#endif
2518
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002519#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2520 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2521 ext_len += olen;
2522#endif
2523
Paul Bakkera503a632013-08-14 13:48:06 +02002524#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002525 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2526 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002527#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002528
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002529#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002530 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2531 ext_len += olen;
2532#endif
2533
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002534#if defined(POLARSSL_SSL_ALPN)
2535 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2536 ext_len += olen;
2537#endif
2538
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002539 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002540
Paul Bakkera7036632014-04-30 10:15:38 +02002541 if( ext_len > 0 )
2542 {
2543 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2544 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2545 p += ext_len;
2546 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
2548 ssl->out_msglen = p - buf;
2549 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2550 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2551
2552 ret = ssl_write_record( ssl );
2553
2554 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2555
2556 return( ret );
2557}
2558
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002559#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2560 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002561 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2562 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002563static int ssl_write_certificate_request( ssl_context *ssl )
2564{
Paul Bakkered27a042013-04-18 22:46:23 +02002565 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002566
2567 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2568
2569 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002570 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002571 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2572 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002573 {
2574 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2575 ssl->state++;
2576 return( 0 );
2577 }
2578
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002579 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2580 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002581}
2582#else
2583static int ssl_write_certificate_request( ssl_context *ssl )
2584{
2585 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2586 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002587 size_t dn_size, total_dn_size; /* excluding length bytes */
2588 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002589 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002590 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
2592 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2593
2594 ssl->state++;
2595
Paul Bakkerfbb17802013-04-17 19:10:21 +02002596 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002597 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002598 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002599 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002600 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002601 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 return( 0 );
2604 }
2605
2606 /*
2607 * 0 . 0 handshake type
2608 * 1 . 3 handshake length
2609 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002610 * 5 .. m-1 cert types
2611 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002612 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002613 * n .. n+1 length of all DNs
2614 * n+2 .. n+3 length of DN 1
2615 * n+4 .. ... Distinguished Name #1
2616 * ... .. ... length of DN 2, etc.
2617 */
2618 buf = ssl->out_msg;
2619 p = buf + 4;
2620
2621 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002622 * Supported certificate types
2623 *
2624 * ClientCertificateType certificate_types<1..2^8-1>;
2625 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002626 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002627 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002628
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629#if defined(POLARSSL_RSA_C)
2630 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2631#endif
2632#if defined(POLARSSL_ECDSA_C)
2633 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2634#endif
2635
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002636 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002637 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002638
Paul Bakker577e0062013-08-28 11:57:20 +02002639 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002640#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002641 /*
2642 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002643 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002644 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2645 *
2646 * struct {
2647 * HashAlgorithm hash;
2648 * SignatureAlgorithm signature;
2649 * } SignatureAndHashAlgorithm;
2650 *
2651 * enum { (255) } HashAlgorithm;
2652 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002653 */
Paul Bakker21dca692013-01-03 11:41:08 +01002654 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002655 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002656 /*
2657 * Only use current running hash algorithm that is already required
2658 * for requested ciphersuite.
2659 */
Paul Bakker926af752012-11-23 13:38:07 +01002660 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2661
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002662 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2663 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002664 {
2665 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2666 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002667
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002668 /*
2669 * Supported signature algorithms
2670 */
2671#if defined(POLARSSL_RSA_C)
2672 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2673 p[2 + sa_len++] = SSL_SIG_RSA;
2674#endif
2675#if defined(POLARSSL_ECDSA_C)
2676 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2677 p[2 + sa_len++] = SSL_SIG_ECDSA;
2678#endif
Paul Bakker926af752012-11-23 13:38:07 +01002679
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002680 p[0] = (unsigned char)( sa_len >> 8 );
2681 p[1] = (unsigned char)( sa_len );
2682 sa_len += 2;
2683 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002684 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002685#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002686
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002687 /*
2688 * DistinguishedName certificate_authorities<0..2^16-1>;
2689 * opaque DistinguishedName<1..2^16-1>;
2690 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 p += 2;
2692 crt = ssl->ca_chain;
2693
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002694 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002695 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 {
2697 if( p - buf > 4096 )
2698 break;
2699
Paul Bakker926af752012-11-23 13:38:07 +01002700 dn_size = crt->subject_raw.len;
2701 *p++ = (unsigned char)( dn_size >> 8 );
2702 *p++ = (unsigned char)( dn_size );
2703 memcpy( p, crt->subject_raw.p, dn_size );
2704 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705
Paul Bakker926af752012-11-23 13:38:07 +01002706 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2707
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002708 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002709 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 }
2711
Paul Bakker926af752012-11-23 13:38:07 +01002712 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2714 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002715 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2716 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002717
2718 ret = ssl_write_record( ssl );
2719
2720 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2721
2722 return( ret );
2723}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2725 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002726 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2727 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002729#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2730 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2731static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2732{
2733 int ret;
2734
2735 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2736 {
2737 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2738 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2739 }
2740
2741 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2742 pk_ec( *ssl_own_key( ssl ) ),
2743 POLARSSL_ECDH_OURS ) ) != 0 )
2744 {
2745 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2746 return( ret );
2747 }
2748
2749 return( 0 );
2750}
2751#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2752 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2753
Paul Bakker41c83d32013-03-20 14:39:14 +01002754static int ssl_write_server_key_exchange( ssl_context *ssl )
2755{
Paul Bakker23986e52011-04-24 08:57:21 +00002756 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002757 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002758 const ssl_ciphersuite_t *ciphersuite_info =
2759 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002760
2761#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2762 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2763 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002764 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002765 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002766 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002767 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002768 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002769 ((void) dig_signed);
2770 ((void) dig_signed_len);
2771#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002772
Paul Bakker5121ce52009-01-03 21:22:43 +00002773 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2774
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002775#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2776 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2777 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002778 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2779 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2780 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002781 {
2782 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2783 ssl->state++;
2784 return( 0 );
2785 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002786#endif
2787
2788#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2789 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2790 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2791 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2792 {
2793 ssl_get_ecdh_params_from_cert( ssl );
2794
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002795 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002796 ssl->state++;
2797 return( 0 );
2798 }
2799#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002800
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002801#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2802 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2803 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2804 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002805 {
2806 /* TODO: Support identity hints */
2807 *(p++) = 0x00;
2808 *(p++) = 0x00;
2809
2810 n += 2;
2811 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002812#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2813 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002814
2815#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2816 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2817 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2818 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002819 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002820 /*
2821 * Ephemeral DH parameters:
2822 *
2823 * struct {
2824 * opaque dh_p<1..2^16-1>;
2825 * opaque dh_g<1..2^16-1>;
2826 * opaque dh_Ys<1..2^16-1>;
2827 * } ServerDHParams;
2828 */
2829 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2830 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2831 {
2832 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2833 return( ret );
2834 }
Paul Bakker48916f92012-09-16 19:57:18 +00002835
Paul Bakker41c83d32013-03-20 14:39:14 +01002836 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002837 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2838 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002839 {
2840 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2841 return( ret );
2842 }
2843
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002844 dig_signed = p;
2845 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002846
2847 p += len;
2848 n += len;
2849
Paul Bakker41c83d32013-03-20 14:39:14 +01002850 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2851 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2852 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2853 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2854 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002855#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2856 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002857
Gergely Budai987bfb52014-01-19 21:48:42 +01002858#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002859 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002860 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2861 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002862 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002863 /*
2864 * Ephemeral ECDH parameters:
2865 *
2866 * struct {
2867 * ECParameters curve_params;
2868 * ECPoint public;
2869 * } ServerECDHParams;
2870 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002871 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002872#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002873 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002874
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002875 /* Match our preference list against the offered curves */
2876 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2877 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2878 if( (*curve)->grp_id == *gid )
2879 goto curve_matching_done;
2880
2881curve_matching_done:
2882#else
2883 curve = ssl->handshake->curves;
2884#endif
2885
2886 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002887 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002888 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2889 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002890 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002891
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002892 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002893
Paul Bakker41c83d32013-03-20 14:39:14 +01002894 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002895 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002896 {
2897 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2898 return( ret );
2899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002900
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002901 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2902 p, SSL_MAX_CONTENT_LEN - n,
2903 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002904 {
2905 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2906 return( ret );
2907 }
2908
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002909 dig_signed = p;
2910 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911
2912 p += len;
2913 n += len;
2914
Paul Bakker41c83d32013-03-20 14:39:14 +01002915 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2916 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002917#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002919#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002920 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2921 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002923 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2924 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002925 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002926 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002927 unsigned int hashlen = 0;
2928 unsigned char hash[64];
2929 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002930
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002931 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002932 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2933 */
Paul Bakker577e0062013-08-28 11:57:20 +02002934#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002935 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2936 {
2937 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2938
2939 if( md_alg == POLARSSL_MD_NONE )
2940 {
2941 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002942 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002943 }
2944 }
Paul Bakker577e0062013-08-28 11:57:20 +02002945 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002946#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002947#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2948 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002949 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002950 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2951 {
2952 md_alg = POLARSSL_MD_SHA1;
2953 }
2954 else
Paul Bakker577e0062013-08-28 11:57:20 +02002955#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002956 {
2957 md_alg = POLARSSL_MD_NONE;
2958 }
2959
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002960 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002961 * Compute the hash to be signed
2962 */
Paul Bakker577e0062013-08-28 11:57:20 +02002963#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2964 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002965 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002966 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002967 md5_context md5;
2968 sha1_context sha1;
2969
Paul Bakker5b4af392014-06-26 12:09:34 +02002970 md5_init( &md5 );
2971 sha1_init( &sha1 );
2972
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973 /*
2974 * digitally-signed struct {
2975 * opaque md5_hash[16];
2976 * opaque sha_hash[20];
2977 * };
2978 *
2979 * md5_hash
2980 * MD5(ClientHello.random + ServerHello.random
2981 * + ServerParams);
2982 * sha_hash
2983 * SHA(ClientHello.random + ServerHello.random
2984 * + ServerParams);
2985 */
2986 md5_starts( &md5 );
2987 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002988 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002989 md5_finish( &md5, hash );
2990
2991 sha1_starts( &sha1 );
2992 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002993 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002994 sha1_finish( &sha1, hash + 16 );
2995
2996 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002997
2998 md5_free( &md5 );
2999 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003000 }
3001 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003002#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
3003 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02003004#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3005 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003006 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003007 {
3008 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003009 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003010
Paul Bakker84bbeb52014-07-01 14:53:22 +02003011 md_init( &ctx );
3012
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003013 /* Info from md_alg will be used instead */
3014 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003015
3016 /*
3017 * digitally-signed struct {
3018 * opaque client_random[32];
3019 * opaque server_random[32];
3020 * ServerDHParams params;
3021 * };
3022 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003023 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003024 {
3025 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3026 return( ret );
3027 }
3028
3029 md_starts( &ctx );
3030 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003031 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003032 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003033 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003034 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003035 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003036#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3037 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003038 {
3039 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003040 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003041 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003042
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003043 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3044 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003045
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003046 /*
3047 * Make the signature
3048 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003049 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003050 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003051 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3052 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053 }
Paul Bakker23f36802012-09-28 14:15:14 +00003054
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003055#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003056 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3057 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003058 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003059 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003060
3061 n += 2;
3062 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003063#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003064
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003065 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003066 p + 2 , &signature_len,
3067 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003068 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003069 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003070 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003071 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003072
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003073 *(p++) = (unsigned char)( signature_len >> 8 );
3074 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003075 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003076
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003077 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003078
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003079 p += signature_len;
3080 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003081 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003083 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3084 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003085
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003086 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003087 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3088 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3089
3090 ssl->state++;
3091
3092 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3093 {
3094 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3095 return( ret );
3096 }
3097
3098 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3099
3100 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003101}
3102
3103static int ssl_write_server_hello_done( ssl_context *ssl )
3104{
3105 int ret;
3106
3107 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3108
3109 ssl->out_msglen = 4;
3110 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3111 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3112
3113 ssl->state++;
3114
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003115#if defined(POLARSSL_SSL_PROTO_DTLS)
3116 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3117 ssl_send_flight_completed( ssl );
3118#endif
3119
Paul Bakker5121ce52009-01-03 21:22:43 +00003120 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3121 {
3122 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3123 return( ret );
3124 }
3125
3126 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3127
3128 return( 0 );
3129}
3130
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003131#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3132 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3133static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3134 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003135{
3136 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003137 size_t n;
3138
3139 /*
3140 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3141 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003142 if( *p + 2 > end )
3143 {
3144 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3145 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3146 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003147
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003148 n = ( (*p)[0] << 8 ) | (*p)[1];
3149 *p += 2;
3150
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003151 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003152 {
3153 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3154 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3155 }
3156
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003157 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003158 {
3159 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3160 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3161 }
3162
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003163 *p += n;
3164
Paul Bakker70df2fb2013-04-17 17:19:09 +02003165 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3166
Paul Bakker70df2fb2013-04-17 17:19:09 +02003167 return( ret );
3168}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003169#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3170 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003171
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003172#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3173 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003174static int ssl_parse_encrypted_pms( ssl_context *ssl,
3175 const unsigned char *p,
3176 const unsigned char *end,
3177 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003178{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003179 int ret;
3180 size_t len = pk_get_len( ssl_own_key( ssl ) );
3181 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003182 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003183
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003184 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003185 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003186 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003187 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3188 }
3189
3190 /*
3191 * Decrypt the premaster using own private RSA key
3192 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003193#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3194 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003195 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3196 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003197 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3198 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003199 {
3200 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3201 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3202 }
3203 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003204#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003205
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003206 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003207 {
3208 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3209 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3210 }
3211
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003212 ssl_write_version( ssl->handshake->max_major_ver,
3213 ssl->handshake->max_minor_ver,
3214 ssl->transport, ver );
3215
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003216 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3217 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003218 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003219 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003220
3221 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003222 pms[0] != ver[0] ||
3223 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003224 {
3225 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3226
3227 /*
3228 * Protection against Bleichenbacher's attack:
3229 * invalid PKCS#1 v1.5 padding must not cause
3230 * the connection to end immediately; instead,
3231 * send a bad_record_mac later in the handshake.
3232 */
3233 ssl->handshake->pmslen = 48;
3234
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003235 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003236 if( ret != 0 )
3237 return( ret );
3238 }
3239
3240 return( ret );
3241}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003242#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3243 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003244
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003245#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003246static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3247 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003248{
Paul Bakker6db455e2013-09-18 17:29:31 +02003249 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003250 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003251
Paul Bakker6db455e2013-09-18 17:29:31 +02003252 if( ssl->f_psk == NULL &&
3253 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3254 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003255 {
3256 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3257 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3258 }
3259
3260 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003261 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003262 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003263 if( *p + 2 > end )
3264 {
3265 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3266 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3267 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003268
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003269 n = ( (*p)[0] << 8 ) | (*p)[1];
3270 *p += 2;
3271
3272 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003273 {
3274 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3275 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3276 }
3277
Paul Bakker6db455e2013-09-18 17:29:31 +02003278 if( ssl->f_psk != NULL )
3279 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003280 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003281 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3282 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003283 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003284 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003285 /* Identity is not a big secret since clients send it in the clear,
3286 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003287 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003288 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003289 {
3290 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3291 }
3292 }
3293
3294 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003295 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003296 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003297 if( ( ret = ssl_send_alert_message( ssl,
3298 SSL_ALERT_LEVEL_FATAL,
3299 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3300 {
3301 return( ret );
3302 }
3303
3304 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003305 }
3306
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003307 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003308
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003309 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003310}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003311#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003312
Paul Bakker5121ce52009-01-03 21:22:43 +00003313static int ssl_parse_client_key_exchange( ssl_context *ssl )
3314{
Paul Bakker23986e52011-04-24 08:57:21 +00003315 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003316 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003317 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003318
Paul Bakker41c83d32013-03-20 14:39:14 +01003319 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003320
3321 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3322
3323 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3324 {
3325 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3326 return( ret );
3327 }
3328
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003329 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3330 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003331
Paul Bakker5121ce52009-01-03 21:22:43 +00003332 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3333 {
3334 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003335 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003336 }
3337
3338 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3339 {
3340 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003341 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003342 }
3343
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003344#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003345 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003347 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003349 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3350 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003352
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003353 if( p != end )
3354 {
3355 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3356 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3357 }
3358
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003359 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003360
3361 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3362 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003363 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003364 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003365 {
3366 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3367 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3368 }
3369
3370 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003371 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003372 else
3373#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003374#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003375 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3376 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3377 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003378 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003379 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3380 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3381 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003382 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003383 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003384 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003385 {
3386 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3387 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3388 }
3389
3390 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3391
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003392 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3393 &ssl->handshake->pmslen,
3394 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003395 POLARSSL_MPI_MAX_SIZE,
3396 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003397 {
3398 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3399 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3400 }
3401
3402 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003403 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003404 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003405#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003406 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3407 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3408 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3410 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003411 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003413 {
3414 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3415 return( ret );
3416 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003417
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003418 if( p != end )
3419 {
3420 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3422 }
3423
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003424 if( ( ret = ssl_psk_derive_premaster( ssl,
3425 ciphersuite_info->key_exchange ) ) != 0 )
3426 {
3427 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3428 return( ret );
3429 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003430 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003431 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003432#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003433#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3434 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3435 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003436 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3437 {
3438 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3439 return( ret );
3440 }
3441
3442 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3443 {
3444 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3445 return( ret );
3446 }
3447
3448 if( ( ret = ssl_psk_derive_premaster( ssl,
3449 ciphersuite_info->key_exchange ) ) != 0 )
3450 {
3451 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3452 return( ret );
3453 }
3454 }
3455 else
3456#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003457#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3458 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3459 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003460 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3461 {
3462 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3463 return( ret );
3464 }
3465 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3466 {
3467 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3468 return( ret );
3469 }
3470
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003471 if( p != end )
3472 {
3473 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3474 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3475 }
3476
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003477 if( ( ret = ssl_psk_derive_premaster( ssl,
3478 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003479 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003480 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3481 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003482 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003483 }
3484 else
3485#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003486#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3487 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3488 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003489 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3490 {
3491 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3492 return( ret );
3493 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003494
3495 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3496 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003497 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003498 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3499 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003500 }
3501
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003502 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3503
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003504 if( ( ret = ssl_psk_derive_premaster( ssl,
3505 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003506 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003507 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003508 return( ret );
3509 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003510 }
3511 else
3512#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003513#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3514 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003515 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003516 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003517 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003518 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003519 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003520 }
3521 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003522 else
3523#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3524 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003525 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003526 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003527 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003528
Paul Bakkerff60ee62010-03-16 21:09:09 +00003529 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3530 {
3531 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3532 return( ret );
3533 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 ssl->state++;
3536
3537 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3538
3539 return( 0 );
3540}
3541
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003542#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3543 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003544 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3545 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003546static int ssl_parse_certificate_verify( ssl_context *ssl )
3547{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003548 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003549
3550 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3551
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003552 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003553 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003554 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003555 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003556 {
3557 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3558 ssl->state++;
3559 return( 0 );
3560 }
3561
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003562 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3563 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003564}
3565#else
3566static int ssl_parse_certificate_verify( ssl_context *ssl )
3567{
3568 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003569 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003570 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003571 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003572 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003573#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003574 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003575#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003576 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003577 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3578
3579 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3580
3581 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003582 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003583 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003584 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3585 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003586 {
3587 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3588 ssl->state++;
3589 return( 0 );
3590 }
3591
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003592 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003593 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003594
3595 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3596 {
3597 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3598 return( ret );
3599 }
3600
3601 ssl->state++;
3602
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003603 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3604 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003605 {
3606 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003607 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608 }
3609
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003610 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003611
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003612 /*
3613 * struct {
3614 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3615 * opaque signature<0..2^16-1>;
3616 * } DigitallySigned;
3617 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003618#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3619 defined(POLARSSL_SSL_PROTO_TLS1_1)
3620 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003621 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003622 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003623 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003624
3625 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3626 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3627 POLARSSL_PK_ECDSA ) )
3628 {
3629 hash_start += 16;
3630 hashlen -= 16;
3631 md_alg = POLARSSL_MD_SHA1;
3632 }
Paul Bakker926af752012-11-23 13:38:07 +01003633 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003634 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003635#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3636 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003637#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3638 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003639 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003640 if( i + 2 > ssl->in_hslen )
3641 {
3642 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3643 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3644 }
3645
Paul Bakker5121ce52009-01-03 21:22:43 +00003646 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003647 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003648 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003649 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003650 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003651 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3652 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003653 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3654 }
3655
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003656 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003657
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003658 /* Info from md_alg will be used instead */
3659 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003660
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003661 i++;
3662
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003663 /*
3664 * Signature
3665 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003666 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003667 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003668 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003669 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3670 " for verify message" ) );
3671 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003672 }
3673
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003674 /*
3675 * Check the certificate's key type matches the signature alg
3676 */
3677 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3678 {
3679 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3680 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3681 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003682
3683 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003684 }
3685 else
3686#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3687 {
3688 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003689 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003690 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003691
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003692 if( i + 2 > ssl->in_hslen )
3693 {
3694 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3695 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3696 }
3697
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003698 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3699 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003700
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003701 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003702 {
3703 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003704 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 }
3706
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003707 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003708 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003709 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003710 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003711 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003712 return( ret );
3713 }
3714
3715 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3716
Paul Bakkered27a042013-04-18 22:46:23 +02003717 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003718}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003719#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3720 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3721 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003722
Paul Bakkera503a632013-08-14 13:48:06 +02003723#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003724static int ssl_write_new_session_ticket( ssl_context *ssl )
3725{
3726 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003727 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003728 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003729
3730 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3731
3732 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3733 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3734
3735 /*
3736 * struct {
3737 * uint32 ticket_lifetime_hint;
3738 * opaque ticket<0..2^16-1>;
3739 * } NewSessionTicket;
3740 *
3741 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3742 * 8 . 9 ticket_len (n)
3743 * 10 . 9+n ticket content
3744 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003745
3746 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3747 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3748 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3749 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003750
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003751 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3752 {
3753 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3754 tlen = 0;
3755 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003756
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003757 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3758 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003759
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003760 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003761
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003762 /*
3763 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3764 * ChangeCipherSpec share the same state.
3765 */
3766 ssl->handshake->new_session_ticket = 0;
3767
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003768 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3769 {
3770 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3771 return( ret );
3772 }
3773
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003774 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3775
3776 return( 0 );
3777}
Paul Bakkera503a632013-08-14 13:48:06 +02003778#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003779
Paul Bakker5121ce52009-01-03 21:22:43 +00003780/*
Paul Bakker1961b702013-01-25 14:49:24 +01003781 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003782 */
Paul Bakker1961b702013-01-25 14:49:24 +01003783int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003784{
3785 int ret = 0;
3786
Paul Bakker1961b702013-01-25 14:49:24 +01003787 if( ssl->state == SSL_HANDSHAKE_OVER )
3788 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003789
Paul Bakker1961b702013-01-25 14:49:24 +01003790 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3791
3792 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3793 return( ret );
3794
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003795#if defined(POLARSSL_SSL_PROTO_DTLS)
3796 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3797 ssl->handshake != NULL &&
3798 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3799 {
3800 if( ( ret = ssl_resend( ssl ) ) != 0 )
3801 return( ret );
3802 }
3803#endif
3804
Paul Bakker1961b702013-01-25 14:49:24 +01003805 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003806 {
Paul Bakker1961b702013-01-25 14:49:24 +01003807 case SSL_HELLO_REQUEST:
3808 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003809 break;
3810
Paul Bakker1961b702013-01-25 14:49:24 +01003811 /*
3812 * <== ClientHello
3813 */
3814 case SSL_CLIENT_HELLO:
3815 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003816 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003817
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003818#if defined(POLARSSL_SSL_PROTO_DTLS)
3819 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3820 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3821#endif
3822
Paul Bakker1961b702013-01-25 14:49:24 +01003823 /*
3824 * ==> ServerHello
3825 * Certificate
3826 * ( ServerKeyExchange )
3827 * ( CertificateRequest )
3828 * ServerHelloDone
3829 */
3830 case SSL_SERVER_HELLO:
3831 ret = ssl_write_server_hello( ssl );
3832 break;
3833
3834 case SSL_SERVER_CERTIFICATE:
3835 ret = ssl_write_certificate( ssl );
3836 break;
3837
3838 case SSL_SERVER_KEY_EXCHANGE:
3839 ret = ssl_write_server_key_exchange( ssl );
3840 break;
3841
3842 case SSL_CERTIFICATE_REQUEST:
3843 ret = ssl_write_certificate_request( ssl );
3844 break;
3845
3846 case SSL_SERVER_HELLO_DONE:
3847 ret = ssl_write_server_hello_done( ssl );
3848 break;
3849
3850 /*
3851 * <== ( Certificate/Alert )
3852 * ClientKeyExchange
3853 * ( CertificateVerify )
3854 * ChangeCipherSpec
3855 * Finished
3856 */
3857 case SSL_CLIENT_CERTIFICATE:
3858 ret = ssl_parse_certificate( ssl );
3859 break;
3860
3861 case SSL_CLIENT_KEY_EXCHANGE:
3862 ret = ssl_parse_client_key_exchange( ssl );
3863 break;
3864
3865 case SSL_CERTIFICATE_VERIFY:
3866 ret = ssl_parse_certificate_verify( ssl );
3867 break;
3868
3869 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3870 ret = ssl_parse_change_cipher_spec( ssl );
3871 break;
3872
3873 case SSL_CLIENT_FINISHED:
3874 ret = ssl_parse_finished( ssl );
3875 break;
3876
3877 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003878 * ==> ( NewSessionTicket )
3879 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003880 * Finished
3881 */
3882 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003883#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003884 if( ssl->handshake->new_session_ticket != 0 )
3885 ret = ssl_write_new_session_ticket( ssl );
3886 else
Paul Bakkera503a632013-08-14 13:48:06 +02003887#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003888 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003889 break;
3890
3891 case SSL_SERVER_FINISHED:
3892 ret = ssl_write_finished( ssl );
3893 break;
3894
3895 case SSL_FLUSH_BUFFERS:
3896 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3897 ssl->state = SSL_HANDSHAKE_WRAPUP;
3898 break;
3899
3900 case SSL_HANDSHAKE_WRAPUP:
3901 ssl_handshake_wrapup( ssl );
3902 break;
3903
3904 default:
3905 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3906 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003907 }
3908
Paul Bakker5121ce52009-01-03 21:22:43 +00003909 return( ret );
3910}
Paul Bakker9af723c2014-05-01 13:03:14 +02003911#endif /* POLARSSL_SSL_SRV_C */