blob: e6948ef7a145b0c288af9ec22eb1e4e70f93f432 [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
1297 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001298 {
1299 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1300 return( ret );
1301 }
1302
1303 buf = ssl->in_hdr;
1304
Paul Bakker78a8c712013-03-06 17:01:52 +01001305#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001306#if defined(POLARSSL_SSL_PROTO_DTLS)
1307 if( ssl->transport == SSL_TRANSPORT_STREAM )
1308#endif
1309 if( ( buf[0] & 0x80 ) != 0 )
1310 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001311#endif
1312
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001313 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001314
Paul Bakkerec636f32012-09-09 19:17:02 +00001315 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001316 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001317 *
1318 * Record layer:
1319 * 0 . 0 message type
1320 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001321 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001322 * 3 . 4 message length
1323 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001324 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1325 buf[0] ) );
1326
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001327 if( buf[0] != SSL_MSG_HANDSHAKE )
1328 {
1329 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1331 }
1332
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001333 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1334 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1335
1336 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1337 buf[1], buf[2] ) );
1338
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001339 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001340
1341 /* According to RFC 5246 Appendix E.1, the version here is typically
1342 * "{03,00}, the lowest version number supported by the client, [or] the
1343 * value of ClientHello.client_version", so the only meaningful check here
1344 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001345 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001347 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1348 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001351 /* For DTLS if this is the initial handshake, remember the client sequence
1352 * number to use it in our next message (RFC 6347 4.2.1) */
1353#if defined(POLARSSL_SSL_PROTO_DTLS)
1354 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1355 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1356 {
1357 /* Epoch should be 0 for initial handshakes */
1358 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1359 {
1360 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1361 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1362 }
1363
1364 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001365
1366#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1367 if( ssl_dtls_replay_check( ssl ) != 0 )
1368 {
1369 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1370 ssl->next_record_offset = 0;
1371 ssl->in_left = 0;
1372 goto read_record_header;
1373 }
1374
1375 /* No MAC to check yet, so we can update right now */
1376 ssl_dtls_replay_update( ssl );
1377#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001378 }
1379#endif /* POLARSSL_SSL_PROTO_DTLS */
1380
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001381 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001383#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardb89c4f32015-01-21 13:24:10 +00001384 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
1385 {
1386 /* Set by ssl_read_record() */
1387 msg_len = ssl->in_hslen;
1388 }
1389 else
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001390#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001391 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001392 if( msg_len > SSL_MAX_CONTENT_LEN )
1393 {
1394 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1395 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1396 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001397
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001398 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1399 {
1400 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1401 return( ret );
1402 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001403
1404 /* Done reading this record, get ready for the next one */
1405#if defined(POLARSSL_SSL_PROTO_DTLS)
1406 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1407 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1408 else
1409#endif
1410 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001411 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001412
1413 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001414
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001415 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001416
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001417 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001418
1419 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001420 * Handshake layer:
1421 * 0 . 0 handshake type
1422 * 1 . 3 handshake length
1423 * 4 . 5 DTLS only: message seqence number
1424 * 6 . 8 DTLS only: fragment offset
1425 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001426 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001427 if( msg_len < ssl_hs_hdr_len( ssl ) )
1428 {
1429 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1430 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1431 }
1432
1433 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1434
1435 if( buf[0] != SSL_HS_CLIENT_HELLO )
1436 {
1437 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1438 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1439 }
1440
1441 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1442 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1443
1444 /* We don't support fragmentation of ClientHello (yet?) */
1445 if( buf[1] != 0 ||
1446 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1447 {
1448 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1449 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1450 }
1451
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001452#if defined(POLARSSL_SSL_PROTO_DTLS)
1453 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1454 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001455 /*
1456 * Copy the client's handshake message_seq on initial handshakes
1457 */
1458 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001459 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001460 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1461 ssl->in_msg[5];
1462 ssl->handshake->out_msg_seq = cli_msg_seq;
1463 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001464 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001465 else
1466 {
1467 /* This couldn't be done in ssl_prepare_handshake_record() */
1468 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1469 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001470
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001471 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1472 {
1473 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1474 "%d (expected %d)", cli_msg_seq,
1475 ssl->handshake->in_msg_seq ) );
1476 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1477 }
1478
1479 ssl->handshake->in_msg_seq++;
1480 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001481
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001482 /*
1483 * For now we don't support fragmentation, so make sure
1484 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001485 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001486 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1487 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1488 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001489 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001490 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1491 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001492 }
1493#endif /* POLARSSL_SSL_PROTO_DTLS */
1494
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001495 buf += ssl_hs_hdr_len( ssl );
1496 msg_len -= ssl_hs_hdr_len( ssl );
1497
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001498 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001499 * ClientHello layer:
1500 * 0 . 1 protocol version
1501 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1502 * 34 . 35 session id length (1 byte)
1503 * 35 . 34+x session id
1504 * 35+x . 35+x DTLS only: cookie length (1 byte)
1505 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001506 * .. . .. ciphersuite list length (2 bytes)
1507 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001508 * .. . .. compression alg. list length (1 byte)
1509 * .. . .. compression alg. list
1510 * .. . .. extensions length (2 bytes, optional)
1511 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001512 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001513
1514 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001515 * Minimal length (with everything empty and extensions ommitted) is
1516 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1517 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001518 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001519 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001520 {
1521 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1522 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1523 }
1524
1525 /*
1526 * Check and save the protocol version
1527 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001528 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001529
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001530 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001531 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001532
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001533 ssl->handshake->max_major_ver = ssl->major_ver;
1534 ssl->handshake->max_minor_ver = ssl->minor_ver;
1535
1536 if( ssl->major_ver < ssl->min_major_ver ||
1537 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001538 {
1539 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001540 " [%d:%d] < [%d:%d]",
1541 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001542 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001543
1544 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1545 SSL_ALERT_MSG_PROTOCOL_VERSION );
1546
1547 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1548 }
1549
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001550 if( ssl->major_ver > ssl->max_major_ver )
1551 {
1552 ssl->major_ver = ssl->max_major_ver;
1553 ssl->minor_ver = ssl->max_minor_ver;
1554 }
1555 else if( ssl->minor_ver > ssl->max_minor_ver )
1556 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001557
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001558 /*
1559 * Save client random (inc. Unix time)
1560 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001561 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001562
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001563 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001564
1565 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001566 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001567 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001568 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001569
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001570 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001571 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001572 {
1573 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1574 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1575 }
1576
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001577 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001578
Paul Bakker48916f92012-09-16 19:57:18 +00001579 ssl->session_negotiate->length = sess_len;
1580 memset( ssl->session_negotiate->id, 0,
1581 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001582 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001583 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001584
1585 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001586 * Check the cookie length and content
1587 */
1588#if defined(POLARSSL_SSL_PROTO_DTLS)
1589 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1590 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001591 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001592 cookie_len = buf[cookie_offset];
1593
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001594 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001595 {
1596 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1597 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1598 }
1599
1600 SSL_DEBUG_BUF( 3, "client hello, cookie",
1601 buf + cookie_offset + 1, cookie_len );
1602
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001603#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001604 if( ssl->f_cookie_check != NULL &&
1605 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001606 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001607 if( ssl->f_cookie_check( ssl->p_cookie,
1608 buf + cookie_offset + 1, cookie_len,
1609 ssl->cli_id, ssl->cli_id_len ) != 0 )
1610 {
1611 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1612 ssl->handshake->verify_cookie_len = 1;
1613 }
1614 else
1615 {
1616 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1617 ssl->handshake->verify_cookie_len = 0;
1618 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001619 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001620 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001621#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001622 {
1623 /* We know we didn't send a cookie, so it should be empty */
1624 if( cookie_len != 0 )
1625 {
1626 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1628 }
1629
1630 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1631 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001632 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001633#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001634
1635 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001636 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001637 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001638#if defined(POLARSSL_SSL_PROTO_DTLS)
1639 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1640 ciph_offset = cookie_offset + 1 + cookie_len;
1641 else
1642#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001643 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001644
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001645 ciph_len = ( buf[ciph_offset + 0] << 8 )
1646 | ( buf[ciph_offset + 1] );
1647
1648 if( ciph_len < 2 ||
1649 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1650 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001651 {
1652 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1653 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1654 }
1655
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001656 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1657 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001658
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001659 /*
1660 * Check the compression algorithms length and pick one
1661 */
1662 comp_offset = ciph_offset + 2 + ciph_len;
1663
1664 comp_len = buf[comp_offset];
1665
1666 if( comp_len < 1 ||
1667 comp_len > 16 ||
1668 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001669 {
1670 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1671 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1672 }
1673
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001674 SSL_DEBUG_BUF( 3, "client hello, compression",
1675 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001676
1677 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001678#if defined(POLARSSL_ZLIB_SUPPORT)
1679 for( i = 0; i < comp_len; ++i )
1680 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001681 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682 {
Paul Bakker48916f92012-09-16 19:57:18 +00001683 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001684 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 }
1686 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001687#endif
1688
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001689 /* See comments in ssl_write_client_hello() */
1690#if defined(POLARSSL_SSL_PROTO_DTLS)
1691 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1692 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1693#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001694
Paul Bakkerec636f32012-09-09 19:17:02 +00001695 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001696 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001697 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001698 ext_offset = comp_offset + 1 + comp_len;
1699 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001700 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001701 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001702 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001703 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1705 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001706
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001707 ext_len = ( buf[ext_offset + 0] << 8 )
1708 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001709
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001710 if( ( ext_len > 0 && ext_len < 4 ) ||
1711 msg_len != ext_offset + 2 + ext_len )
1712 {
1713 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1714 SSL_DEBUG_BUF( 3, "client hello extensions",
1715 buf + ext_offset + 2, ext_len );
1716 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001717 }
1718 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001719 else
1720 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001721
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001722 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001723
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001724 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001725 {
1726 unsigned int ext_id = ( ( ext[0] << 8 )
1727 | ( ext[1] ) );
1728 unsigned int ext_size = ( ( ext[2] << 8 )
1729 | ( ext[3] ) );
1730
1731 if( ext_size + 4 > ext_len )
1732 {
1733 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1734 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1735 }
1736 switch( ext_id )
1737 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001738#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001739 case TLS_EXT_SERVERNAME:
1740 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1741 if( ssl->f_sni == NULL )
1742 break;
1743
1744 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1745 if( ret != 0 )
1746 return( ret );
1747 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001748#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001749
Paul Bakker48916f92012-09-16 19:57:18 +00001750 case TLS_EXT_RENEGOTIATION_INFO:
1751 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001752#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001753 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001754#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001755
Paul Bakker23f36802012-09-28 14:15:14 +00001756 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1757 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001758 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001759 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001760
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001761#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1762 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001763 case TLS_EXT_SIG_ALG:
1764 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001765#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001766 if( ssl->renegotiation == SSL_RENEGOTIATION )
1767 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001768#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001769
1770 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1771 if( ret != 0 )
1772 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001773 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001774#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1775 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001776
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001777#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001778 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1779 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1780
1781 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1782 if( ret != 0 )
1783 return( ret );
1784 break;
1785
1786 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1787 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001788 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001789
1790 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1791 if( ret != 0 )
1792 return( ret );
1793 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001794#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001795
Paul Bakker05decb22013-08-15 13:33:48 +02001796#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001797 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1798 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1799
1800 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1801 if( ret != 0 )
1802 return( ret );
1803 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001804#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001805
Paul Bakker1f2bc622013-08-15 13:45:55 +02001806#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001807 case TLS_EXT_TRUNCATED_HMAC:
1808 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1809
1810 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1811 if( ret != 0 )
1812 return( ret );
1813 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001814#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001815
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001816#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1817 case TLS_EXT_ENCRYPT_THEN_MAC:
1818 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1819
1820 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1821 if( ret != 0 )
1822 return( ret );
1823 break;
1824#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1825
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001826#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1827 case TLS_EXT_EXTENDED_MASTER_SECRET:
1828 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1829
1830 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1831 if( ret != 0 )
1832 return( ret );
1833 break;
1834#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1835
Paul Bakkera503a632013-08-14 13:48:06 +02001836#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001837 case TLS_EXT_SESSION_TICKET:
1838 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1839
1840 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1841 if( ret != 0 )
1842 return( ret );
1843 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001844#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001845
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001846#if defined(POLARSSL_SSL_ALPN)
1847 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001848 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001849
1850 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1851 if( ret != 0 )
1852 return( ret );
1853 break;
1854#endif /* POLARSSL_SSL_SESSION_TICKETS */
1855
Paul Bakker48916f92012-09-16 19:57:18 +00001856 default:
1857 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1858 ext_id ) );
1859 }
1860
1861 ext_len -= 4 + ext_size;
1862 ext += 4 + ext_size;
1863
1864 if( ext_len > 0 && ext_len < 4 )
1865 {
1866 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1867 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1868 }
1869 }
1870
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001871#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1872 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1873 {
1874 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1875 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1876 {
1877 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1878
1879 if( ssl->minor_ver < ssl->max_minor_ver )
1880 {
1881 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1882
1883 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1884 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1885
1886 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1887 }
1888
1889 break;
1890 }
1891 }
1892#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1893
Paul Bakker48916f92012-09-16 19:57:18 +00001894 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001895 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1896 */
1897 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1898 {
1899 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1900 {
1901 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1902 if( ssl->renegotiation == SSL_RENEGOTIATION )
1903 {
1904 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1905
1906 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1907 return( ret );
1908
1909 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1910 }
1911 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1912 break;
1913 }
1914 }
1915
1916 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001917 * Renegotiation security checks
1918 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001919 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001920 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1921 {
1922 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1923 handshake_failure = 1;
1924 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001925#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001926 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1927 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1928 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001929 {
1930 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001931 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001932 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001933 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1934 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1935 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001936 {
1937 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001938 handshake_failure = 1;
1939 }
1940 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1941 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1942 renegotiation_info_seen == 1 )
1943 {
1944 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1945 handshake_failure = 1;
1946 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001947#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001948
1949 if( handshake_failure == 1 )
1950 {
1951 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1952 return( ret );
1953
Paul Bakker48916f92012-09-16 19:57:18 +00001954 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1955 }
Paul Bakker380da532012-04-18 16:10:25 +00001956
Paul Bakker41c83d32013-03-20 14:39:14 +01001957 /*
1958 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001959 * (At the end because we need information from the EC-based extensions
1960 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001961 */
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001962 got_common_suite = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001963 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001964 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001965#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001966 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001967 {
1968 for( i = 0; ciphersuites[i] != 0; i++ )
1969#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001970 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001971 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001972 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001973#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001974 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001975 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1976 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1977 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001978
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001979 got_common_suite = 1;
1980
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001981 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1982 &ciphersuite_info ) ) != 0 )
1983 return( ret );
1984
1985 if( ciphersuite_info != NULL )
1986 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001987 }
1988 }
1989
Manuel Pégourié-Gonnardf01768c2015-01-08 17:06:16 +01001990 if( got_common_suite )
1991 {
1992 SSL_DEBUG_MSG( 1, ( "got ciphersuites in common, "
1993 "but none of them usable" ) );
1994 ssl_send_fatal_handshake_failure( ssl );
1995 return( POLARSSL_ERR_SSL_NO_USABLE_CIPHERSUITE );
1996 }
1997 else
1998 {
1999 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
2000 ssl_send_fatal_handshake_failure( ssl );
2001 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
2002 }
Paul Bakker41c83d32013-03-20 14:39:14 +01002003
2004have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02002005 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01002006 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
2007 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
2008
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 ssl->state++;
2010
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02002011#if defined(POLARSSL_SSL_PROTO_DTLS)
2012 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
2013 ssl_recv_flight_completed( ssl );
2014#endif
2015
Paul Bakker5121ce52009-01-03 21:22:43 +00002016 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
2017
2018 return( 0 );
2019}
2020
Paul Bakker1f2bc622013-08-15 13:45:55 +02002021#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002022static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
2023 unsigned char *buf,
2024 size_t *olen )
2025{
2026 unsigned char *p = buf;
2027
2028 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
2029 {
2030 *olen = 0;
2031 return;
2032 }
2033
2034 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
2035
2036 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
2037 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
2038
2039 *p++ = 0x00;
2040 *p++ = 0x00;
2041
2042 *olen = 4;
2043}
Paul Bakker1f2bc622013-08-15 13:45:55 +02002044#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002045
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002046#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2047static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2048 unsigned char *buf,
2049 size_t *olen )
2050{
2051 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002052 const ssl_ciphersuite_t *suite = NULL;
2053 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002054
2055 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2056 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2057 {
2058 *olen = 0;
2059 return;
2060 }
2061
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002062 /*
2063 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2064 * from a client and then selects a stream or Authenticated Encryption
2065 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2066 * encrypt-then-MAC response extension back to the client."
2067 */
2068 if( ( suite = ssl_ciphersuite_from_id(
2069 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2070 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2071 cipher->mode != POLARSSL_MODE_CBC )
2072 {
2073 *olen = 0;
2074 return;
2075 }
2076
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002077 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2078
2079 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2080 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2081
2082 *p++ = 0x00;
2083 *p++ = 0x00;
2084
2085 *olen = 4;
2086}
2087#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2088
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002089#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2090static void ssl_write_extended_ms_ext( ssl_context *ssl,
2091 unsigned char *buf,
2092 size_t *olen )
2093{
2094 unsigned char *p = buf;
2095
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002096 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2097 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002098 {
2099 *olen = 0;
2100 return;
2101 }
2102
2103 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2104 "extension" ) );
2105
2106 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2107 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2108
2109 *p++ = 0x00;
2110 *p++ = 0x00;
2111
2112 *olen = 4;
2113}
2114#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2115
Paul Bakkera503a632013-08-14 13:48:06 +02002116#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002117static void ssl_write_session_ticket_ext( ssl_context *ssl,
2118 unsigned char *buf,
2119 size_t *olen )
2120{
2121 unsigned char *p = buf;
2122
2123 if( ssl->handshake->new_session_ticket == 0 )
2124 {
2125 *olen = 0;
2126 return;
2127 }
2128
2129 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2130
2131 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2132 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2133
2134 *p++ = 0x00;
2135 *p++ = 0x00;
2136
2137 *olen = 4;
2138}
Paul Bakkera503a632013-08-14 13:48:06 +02002139#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002140
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002141static void ssl_write_renegotiation_ext( ssl_context *ssl,
2142 unsigned char *buf,
2143 size_t *olen )
2144{
2145 unsigned char *p = buf;
2146
2147 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2148 {
2149 *olen = 0;
2150 return;
2151 }
2152
2153 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2154
2155 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2156 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2157
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002158#if defined(POLARSSL_SSL_RENEGOTIATION)
2159 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2160 {
2161 *p++ = 0x00;
2162 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2163 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002164
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002165 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2166 p += ssl->verify_data_len;
2167 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2168 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002169
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002170 *olen = 5 + ssl->verify_data_len * 2;
2171 }
2172 else
2173#endif /* POLARSSL_SSL_RENEGOTIATION */
2174 {
2175 *p++ = 0x00;
2176 *p++ = 0x01;
2177 *p++ = 0x00;
2178
2179 *olen = 5;
2180 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002181}
2182
Paul Bakker05decb22013-08-15 13:33:48 +02002183#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002184static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2185 unsigned char *buf,
2186 size_t *olen )
2187{
2188 unsigned char *p = buf;
2189
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002190 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2191 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002192 *olen = 0;
2193 return;
2194 }
2195
2196 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2197
2198 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2199 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2200
2201 *p++ = 0x00;
2202 *p++ = 1;
2203
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002204 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002205
2206 *olen = 5;
2207}
Paul Bakker05decb22013-08-15 13:33:48 +02002208#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002209
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002210#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002211static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2212 unsigned char *buf,
2213 size_t *olen )
2214{
2215 unsigned char *p = buf;
2216 ((void) ssl);
2217
Paul Bakker677377f2013-10-28 12:54:26 +01002218 if( ( ssl->handshake->cli_exts &
2219 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2220 {
2221 *olen = 0;
2222 return;
2223 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002224
2225 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2226
2227 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2228 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2229
2230 *p++ = 0x00;
2231 *p++ = 2;
2232
2233 *p++ = 1;
2234 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2235
2236 *olen = 6;
2237}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002238#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002239
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002240#if defined(POLARSSL_SSL_ALPN )
2241static void ssl_write_alpn_ext( ssl_context *ssl,
2242 unsigned char *buf, size_t *olen )
2243{
2244 if( ssl->alpn_chosen == NULL )
2245 {
2246 *olen = 0;
2247 return;
2248 }
2249
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002250 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002251
2252 /*
2253 * 0 . 1 ext identifier
2254 * 2 . 3 ext length
2255 * 4 . 5 protocol list length
2256 * 6 . 6 protocol name length
2257 * 7 . 7+n protocol name
2258 */
2259 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2260 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2261
2262 *olen = 7 + strlen( ssl->alpn_chosen );
2263
2264 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2265 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2266
2267 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2268 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2269
2270 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2271
2272 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2273}
2274#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2275
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002276#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002277static int ssl_write_hello_verify_request( ssl_context *ssl )
2278{
2279 int ret;
2280 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002281 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002282
2283 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2284
2285 /*
2286 * struct {
2287 * ProtocolVersion server_version;
2288 * opaque cookie<0..2^8-1>;
2289 * } HelloVerifyRequest;
2290 */
2291
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002292 /* The RFC is not clear on this point, but sending the actual negotiated
2293 * version looks like the most interoperable thing to do. */
2294 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002295 ssl->transport, p );
2296 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2297 p += 2;
2298
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002299 /* If we get here, f_cookie_check is not null */
2300 if( ssl->f_cookie_write == NULL )
2301 {
2302 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2303 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2304 }
2305
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002306 /* Skip length byte until we know the length */
2307 cookie_len_byte = p++;
2308
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002309 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2310 &p, ssl->out_buf + SSL_BUFFER_LEN,
2311 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002312 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002313 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002314 return( ret );
2315 }
2316
2317 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2318
2319 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002320
2321 ssl->out_msglen = p - ssl->out_msg;
2322 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2323 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2324
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002325 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002326
2327 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2328 {
2329 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2330 return( ret );
2331 }
2332
2333 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2334
2335 return( 0 );
2336}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002337#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002338
Paul Bakker5121ce52009-01-03 21:22:43 +00002339static int ssl_write_server_hello( ssl_context *ssl )
2340{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002341#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002342 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002343#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002344 int ret;
2345 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002346 unsigned char *buf, *p;
2347
2348 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2349
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002350#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002351 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002352 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002353 {
2354 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2355 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2356
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002357 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002358 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002359#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002360
Paul Bakkera9a028e2013-11-21 17:31:06 +01002361 if( ssl->f_rng == NULL )
2362 {
2363 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2364 return( POLARSSL_ERR_SSL_NO_RNG );
2365 }
2366
Paul Bakker5121ce52009-01-03 21:22:43 +00002367 /*
2368 * 0 . 0 handshake type
2369 * 1 . 3 handshake length
2370 * 4 . 5 protocol version
2371 * 6 . 9 UNIX time()
2372 * 10 . 37 random bytes
2373 */
2374 buf = ssl->out_msg;
2375 p = buf + 4;
2376
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002377 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2378 ssl->transport, p );
2379 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002380
2381 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002382 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002383
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002384#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 t = time( NULL );
2386 *p++ = (unsigned char)( t >> 24 );
2387 *p++ = (unsigned char)( t >> 16 );
2388 *p++ = (unsigned char)( t >> 8 );
2389 *p++ = (unsigned char)( t );
2390
2391 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002392#else
2393 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2394 return( ret );
2395
2396 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002397#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002398
Paul Bakkera3d195c2011-11-27 21:07:34 +00002399 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2400 return( ret );
2401
2402 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002403
Paul Bakker48916f92012-09-16 19:57:18 +00002404 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
2406 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2407
2408 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002409 * Resume is 0 by default, see ssl_handshake_init().
2410 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2411 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002413 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002414#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002415 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002416#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002417 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002418 ssl->f_get_cache != NULL &&
2419 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2420 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002421 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002422 ssl->handshake->resume = 1;
2423 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002424
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002425 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002426 {
2427 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002428 * New session, create a new session id,
2429 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 ssl->state++;
2432
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002433#if defined(POLARSSL_HAVE_TIME)
2434 ssl->session_negotiate->start = time( NULL );
2435#endif
2436
Paul Bakkera503a632013-08-14 13:48:06 +02002437#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002438 if( ssl->handshake->new_session_ticket != 0 )
2439 {
2440 ssl->session_negotiate->length = n = 0;
2441 memset( ssl->session_negotiate->id, 0, 32 );
2442 }
2443 else
2444#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002445 {
2446 ssl->session_negotiate->length = n = 32;
2447 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002448 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002449 return( ret );
2450 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002451 }
2452 else
2453 {
2454 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002455 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002456 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002457 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002458 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002459
2460 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2461 {
2462 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2463 return( ret );
2464 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002465 }
2466
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002467 /*
2468 * 38 . 38 session id length
2469 * 39 . 38+n session id
2470 * 39+n . 40+n chosen ciphersuite
2471 * 41+n . 41+n chosen compression alg.
2472 * 42+n . 43+n extensions length
2473 * 44+n . 43+n+m extensions
2474 */
2475 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002476 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2477 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002478
2479 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2480 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2481 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002482 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002483
Paul Bakker48916f92012-09-16 19:57:18 +00002484 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2485 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2486 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002488 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2489 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002490 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002491 ssl->session_negotiate->compression ) );
2492
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002493 /*
2494 * First write extensions, then the total length
2495 */
2496 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2497 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002498
Paul Bakker05decb22013-08-15 13:33:48 +02002499#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002500 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2501 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002502#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002503
Paul Bakker1f2bc622013-08-15 13:45:55 +02002504#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002505 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2506 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002507#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002508
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002509#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2510 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2511 ext_len += olen;
2512#endif
2513
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002514#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2515 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2516 ext_len += olen;
2517#endif
2518
Paul Bakkera503a632013-08-14 13:48:06 +02002519#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002520 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2521 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002522#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002523
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002524#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002525 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2526 ext_len += olen;
2527#endif
2528
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002529#if defined(POLARSSL_SSL_ALPN)
2530 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2531 ext_len += olen;
2532#endif
2533
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002534 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002535
Paul Bakkera7036632014-04-30 10:15:38 +02002536 if( ext_len > 0 )
2537 {
2538 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2539 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2540 p += ext_len;
2541 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002542
2543 ssl->out_msglen = p - buf;
2544 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2545 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2546
2547 ret = ssl_write_record( ssl );
2548
2549 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2550
2551 return( ret );
2552}
2553
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002554#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2555 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002556 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2557 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002558static int ssl_write_certificate_request( ssl_context *ssl )
2559{
Paul Bakkered27a042013-04-18 22:46:23 +02002560 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561
2562 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2563
2564 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002566 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2567 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568 {
2569 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2570 ssl->state++;
2571 return( 0 );
2572 }
2573
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002574 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2575 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002576}
2577#else
2578static int ssl_write_certificate_request( ssl_context *ssl )
2579{
2580 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2581 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002582 size_t dn_size, total_dn_size; /* excluding length bytes */
2583 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002584 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002585 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
2587 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2588
2589 ssl->state++;
2590
Paul Bakkerfbb17802013-04-17 19:10:21 +02002591 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002592 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002593 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002594 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002595 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002596 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002597 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002598 return( 0 );
2599 }
2600
2601 /*
2602 * 0 . 0 handshake type
2603 * 1 . 3 handshake length
2604 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002605 * 5 .. m-1 cert types
2606 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002607 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 * n .. n+1 length of all DNs
2609 * n+2 .. n+3 length of DN 1
2610 * n+4 .. ... Distinguished Name #1
2611 * ... .. ... length of DN 2, etc.
2612 */
2613 buf = ssl->out_msg;
2614 p = buf + 4;
2615
2616 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002617 * Supported certificate types
2618 *
2619 * ClientCertificateType certificate_types<1..2^8-1>;
2620 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002621 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002622 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002623
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002624#if defined(POLARSSL_RSA_C)
2625 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2626#endif
2627#if defined(POLARSSL_ECDSA_C)
2628 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2629#endif
2630
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002631 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002632 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002633
Paul Bakker577e0062013-08-28 11:57:20 +02002634 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002635#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002636 /*
2637 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002638 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002639 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2640 *
2641 * struct {
2642 * HashAlgorithm hash;
2643 * SignatureAlgorithm signature;
2644 * } SignatureAndHashAlgorithm;
2645 *
2646 * enum { (255) } HashAlgorithm;
2647 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002648 */
Paul Bakker21dca692013-01-03 11:41:08 +01002649 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002650 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002651 /*
2652 * Only use current running hash algorithm that is already required
2653 * for requested ciphersuite.
2654 */
Paul Bakker926af752012-11-23 13:38:07 +01002655 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2656
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002657 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2658 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002659 {
2660 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2661 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002662
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002663 /*
2664 * Supported signature algorithms
2665 */
2666#if defined(POLARSSL_RSA_C)
2667 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2668 p[2 + sa_len++] = SSL_SIG_RSA;
2669#endif
2670#if defined(POLARSSL_ECDSA_C)
2671 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2672 p[2 + sa_len++] = SSL_SIG_ECDSA;
2673#endif
Paul Bakker926af752012-11-23 13:38:07 +01002674
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002675 p[0] = (unsigned char)( sa_len >> 8 );
2676 p[1] = (unsigned char)( sa_len );
2677 sa_len += 2;
2678 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002679 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002680#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002681
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002682 /*
2683 * DistinguishedName certificate_authorities<0..2^16-1>;
2684 * opaque DistinguishedName<1..2^16-1>;
2685 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002686 p += 2;
2687 crt = ssl->ca_chain;
2688
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002689 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002690 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002691 {
2692 if( p - buf > 4096 )
2693 break;
2694
Paul Bakker926af752012-11-23 13:38:07 +01002695 dn_size = crt->subject_raw.len;
2696 *p++ = (unsigned char)( dn_size >> 8 );
2697 *p++ = (unsigned char)( dn_size );
2698 memcpy( p, crt->subject_raw.p, dn_size );
2699 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002700
Paul Bakker926af752012-11-23 13:38:07 +01002701 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2702
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002703 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002704 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
Paul Bakker926af752012-11-23 13:38:07 +01002707 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2709 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002710 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2711 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002712
2713 ret = ssl_write_record( ssl );
2714
2715 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2716
2717 return( ret );
2718}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002719#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2720 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002721 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2722 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002723
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002724#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2725 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2726static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2727{
2728 int ret;
2729
2730 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2731 {
2732 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2733 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2734 }
2735
2736 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2737 pk_ec( *ssl_own_key( ssl ) ),
2738 POLARSSL_ECDH_OURS ) ) != 0 )
2739 {
2740 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2741 return( ret );
2742 }
2743
2744 return( 0 );
2745}
2746#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2747 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2748
Paul Bakker41c83d32013-03-20 14:39:14 +01002749static int ssl_write_server_key_exchange( ssl_context *ssl )
2750{
Paul Bakker23986e52011-04-24 08:57:21 +00002751 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002752 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002753 const ssl_ciphersuite_t *ciphersuite_info =
2754 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002755
2756#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2757 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2758 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002759 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002760 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002761 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002762 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002763 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002764 ((void) dig_signed);
2765 ((void) dig_signed_len);
2766#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002767
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2769
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002770#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2771 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2772 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002773 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2774 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2775 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002776 {
2777 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2778 ssl->state++;
2779 return( 0 );
2780 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002781#endif
2782
2783#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2784 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2785 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2786 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2787 {
2788 ssl_get_ecdh_params_from_cert( ssl );
2789
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002790 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002791 ssl->state++;
2792 return( 0 );
2793 }
2794#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002795
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002796#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2797 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2798 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2799 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002800 {
2801 /* TODO: Support identity hints */
2802 *(p++) = 0x00;
2803 *(p++) = 0x00;
2804
2805 n += 2;
2806 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002807#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2808 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002809
2810#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2811 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2812 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2813 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002814 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002815 /*
2816 * Ephemeral DH parameters:
2817 *
2818 * struct {
2819 * opaque dh_p<1..2^16-1>;
2820 * opaque dh_g<1..2^16-1>;
2821 * opaque dh_Ys<1..2^16-1>;
2822 * } ServerDHParams;
2823 */
2824 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2825 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2826 {
2827 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2828 return( ret );
2829 }
Paul Bakker48916f92012-09-16 19:57:18 +00002830
Paul Bakker41c83d32013-03-20 14:39:14 +01002831 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002832 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2833 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002834 {
2835 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2836 return( ret );
2837 }
2838
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002839 dig_signed = p;
2840 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002841
2842 p += len;
2843 n += len;
2844
Paul Bakker41c83d32013-03-20 14:39:14 +01002845 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2846 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2847 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2848 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2849 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002850#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2851 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002852
Gergely Budai987bfb52014-01-19 21:48:42 +01002853#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002854 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002855 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2856 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002857 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002858 /*
2859 * Ephemeral ECDH parameters:
2860 *
2861 * struct {
2862 * ECParameters curve_params;
2863 * ECPoint public;
2864 * } ServerECDHParams;
2865 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002866 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002867#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002868 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002869
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002870 /* Match our preference list against the offered curves */
2871 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2872 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2873 if( (*curve)->grp_id == *gid )
2874 goto curve_matching_done;
2875
2876curve_matching_done:
2877#else
2878 curve = ssl->handshake->curves;
2879#endif
2880
2881 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002882 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002883 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2884 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002885 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002886
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002887 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002888
Paul Bakker41c83d32013-03-20 14:39:14 +01002889 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002890 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002891 {
2892 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2893 return( ret );
2894 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002895
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002896 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2897 p, SSL_MAX_CONTENT_LEN - n,
2898 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002899 {
2900 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2901 return( ret );
2902 }
2903
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002904 dig_signed = p;
2905 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002906
2907 p += len;
2908 n += len;
2909
Paul Bakker41c83d32013-03-20 14:39:14 +01002910 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2911 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002912#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002913
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002915 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2916 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002917 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002918 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2919 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002920 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002921 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002922 unsigned int hashlen = 0;
2923 unsigned char hash[64];
2924 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002925
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002926 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002927 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2928 */
Paul Bakker577e0062013-08-28 11:57:20 +02002929#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002930 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2931 {
2932 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2933
2934 if( md_alg == POLARSSL_MD_NONE )
2935 {
2936 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002937 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002938 }
2939 }
Paul Bakker577e0062013-08-28 11:57:20 +02002940 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002941#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002942#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2943 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002944 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002945 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2946 {
2947 md_alg = POLARSSL_MD_SHA1;
2948 }
2949 else
Paul Bakker577e0062013-08-28 11:57:20 +02002950#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002951 {
2952 md_alg = POLARSSL_MD_NONE;
2953 }
2954
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002955 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002956 * Compute the hash to be signed
2957 */
Paul Bakker577e0062013-08-28 11:57:20 +02002958#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2959 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002960 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002961 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002962 md5_context md5;
2963 sha1_context sha1;
2964
Paul Bakker5b4af392014-06-26 12:09:34 +02002965 md5_init( &md5 );
2966 sha1_init( &sha1 );
2967
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002968 /*
2969 * digitally-signed struct {
2970 * opaque md5_hash[16];
2971 * opaque sha_hash[20];
2972 * };
2973 *
2974 * md5_hash
2975 * MD5(ClientHello.random + ServerHello.random
2976 * + ServerParams);
2977 * sha_hash
2978 * SHA(ClientHello.random + ServerHello.random
2979 * + ServerParams);
2980 */
2981 md5_starts( &md5 );
2982 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002983 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002984 md5_finish( &md5, hash );
2985
2986 sha1_starts( &sha1 );
2987 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002988 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002989 sha1_finish( &sha1, hash + 16 );
2990
2991 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002992
2993 md5_free( &md5 );
2994 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002995 }
2996 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002997#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2998 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002999#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3000 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02003001 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003002 {
3003 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02003004 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005
Paul Bakker84bbeb52014-07-01 14:53:22 +02003006 md_init( &ctx );
3007
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003008 /* Info from md_alg will be used instead */
3009 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003010
3011 /*
3012 * digitally-signed struct {
3013 * opaque client_random[32];
3014 * opaque server_random[32];
3015 * ServerDHParams params;
3016 * };
3017 */
Paul Bakker66d5d072014-06-17 16:39:18 +02003018 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003019 {
3020 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
3021 return( ret );
3022 }
3023
3024 md_starts( &ctx );
3025 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003026 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003027 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02003028 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00003029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003030 else
Paul Bakker9659dae2013-08-28 16:21:34 +02003031#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
3032 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003033 {
3034 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003035 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003036 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003037
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02003038 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
3039 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003040
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003041 /*
3042 * Make the signature
3043 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003044 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00003045 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003046 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3047 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003048 }
Paul Bakker23f36802012-09-28 14:15:14 +00003049
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003050#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003051 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3052 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003053 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003054 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003055
3056 n += 2;
3057 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003058#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003059
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003060 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003061 p + 2 , &signature_len,
3062 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003063 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003064 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003065 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003066 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003067
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003068 *(p++) = (unsigned char)( signature_len >> 8 );
3069 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003070 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003071
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003072 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003073
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003074 p += signature_len;
3075 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003076 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003077#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003078 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3079 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003080
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003081 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003082 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3083 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3084
3085 ssl->state++;
3086
3087 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3088 {
3089 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3090 return( ret );
3091 }
3092
3093 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3094
3095 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096}
3097
3098static int ssl_write_server_hello_done( ssl_context *ssl )
3099{
3100 int ret;
3101
3102 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3103
3104 ssl->out_msglen = 4;
3105 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3106 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3107
3108 ssl->state++;
3109
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003110#if defined(POLARSSL_SSL_PROTO_DTLS)
3111 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3112 ssl_send_flight_completed( ssl );
3113#endif
3114
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3116 {
3117 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3118 return( ret );
3119 }
3120
3121 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3122
3123 return( 0 );
3124}
3125
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003126#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3127 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3128static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3129 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003130{
3131 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003132 size_t n;
3133
3134 /*
3135 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3136 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003137 if( *p + 2 > end )
3138 {
3139 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3140 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3141 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003142
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003143 n = ( (*p)[0] << 8 ) | (*p)[1];
3144 *p += 2;
3145
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003146 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003147 {
3148 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3149 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3150 }
3151
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003152 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003153 {
3154 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3155 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3156 }
3157
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003158 *p += n;
3159
Paul Bakker70df2fb2013-04-17 17:19:09 +02003160 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3161
Paul Bakker70df2fb2013-04-17 17:19:09 +02003162 return( ret );
3163}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003164#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3165 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003166
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003167#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3168 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003169static int ssl_parse_encrypted_pms( ssl_context *ssl,
3170 const unsigned char *p,
3171 const unsigned char *end,
3172 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003173{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003174 int ret;
3175 size_t len = pk_get_len( ssl_own_key( ssl ) );
3176 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003177 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003178
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003179 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003180 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003181 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003182 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3183 }
3184
3185 /*
3186 * Decrypt the premaster using own private RSA key
3187 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003188#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3189 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003190 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3191 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003192 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3193 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003194 {
3195 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3196 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3197 }
3198 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003199#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003200
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003201 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003202 {
3203 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3204 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3205 }
3206
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003207 ssl_write_version( ssl->handshake->max_major_ver,
3208 ssl->handshake->max_minor_ver,
3209 ssl->transport, ver );
3210
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003211 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3212 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003213 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003214 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003215
3216 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003217 pms[0] != ver[0] ||
3218 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003219 {
3220 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3221
3222 /*
3223 * Protection against Bleichenbacher's attack:
3224 * invalid PKCS#1 v1.5 padding must not cause
3225 * the connection to end immediately; instead,
3226 * send a bad_record_mac later in the handshake.
3227 */
3228 ssl->handshake->pmslen = 48;
3229
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003230 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003231 if( ret != 0 )
3232 return( ret );
3233 }
3234
3235 return( ret );
3236}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003237#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3238 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003239
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003240#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003241static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3242 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003243{
Paul Bakker6db455e2013-09-18 17:29:31 +02003244 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003245 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003246
Paul Bakker6db455e2013-09-18 17:29:31 +02003247 if( ssl->f_psk == NULL &&
3248 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3249 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003250 {
3251 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3252 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3253 }
3254
3255 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003257 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003258 if( *p + 2 > end )
3259 {
3260 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3261 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3262 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003263
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003264 n = ( (*p)[0] << 8 ) | (*p)[1];
3265 *p += 2;
3266
3267 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003268 {
3269 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3270 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3271 }
3272
Paul Bakker6db455e2013-09-18 17:29:31 +02003273 if( ssl->f_psk != NULL )
3274 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003275 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003276 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3277 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003278 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003279 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003280 /* Identity is not a big secret since clients send it in the clear,
3281 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003282 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003283 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003284 {
3285 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3286 }
3287 }
3288
3289 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003290 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003291 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003292 if( ( ret = ssl_send_alert_message( ssl,
3293 SSL_ALERT_LEVEL_FATAL,
3294 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3295 {
3296 return( ret );
3297 }
3298
3299 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003300 }
3301
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003302 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003303
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003304 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003305}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003306#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003307
Paul Bakker5121ce52009-01-03 21:22:43 +00003308static int ssl_parse_client_key_exchange( ssl_context *ssl )
3309{
Paul Bakker23986e52011-04-24 08:57:21 +00003310 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003311 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003312 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003313
Paul Bakker41c83d32013-03-20 14:39:14 +01003314 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003315
3316 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3317
3318 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3319 {
3320 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3321 return( ret );
3322 }
3323
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003324 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3325 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003326
Paul Bakker5121ce52009-01-03 21:22:43 +00003327 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3328 {
3329 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003330 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003331 }
3332
3333 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3334 {
3335 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003336 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337 }
3338
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003339#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003340 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003341 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003342 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003343 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003344 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3345 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003346 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003347
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003348 if( p != end )
3349 {
3350 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3351 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3352 }
3353
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003354 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003355
3356 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3357 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003358 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003359 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003360 {
3361 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3362 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3363 }
3364
3365 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003366 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003367 else
3368#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003369#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003370 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3371 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3372 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003373 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003374 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3375 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3376 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003377 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003378 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003379 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003380 {
3381 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3382 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3383 }
3384
3385 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3386
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003387 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3388 &ssl->handshake->pmslen,
3389 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003390 POLARSSL_MPI_MAX_SIZE,
3391 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003392 {
3393 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3395 }
3396
3397 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003398 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003399 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003400#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003401 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3402 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3403 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003404#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3405 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003406 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003407 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003408 {
3409 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3410 return( ret );
3411 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003412
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003413 if( p != end )
3414 {
3415 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3416 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3417 }
3418
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003419 if( ( ret = ssl_psk_derive_premaster( ssl,
3420 ciphersuite_info->key_exchange ) ) != 0 )
3421 {
3422 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3423 return( ret );
3424 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003425 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003426 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003427#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003428#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3429 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3430 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003431 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3432 {
3433 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3434 return( ret );
3435 }
3436
3437 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3438 {
3439 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3440 return( ret );
3441 }
3442
3443 if( ( ret = ssl_psk_derive_premaster( ssl,
3444 ciphersuite_info->key_exchange ) ) != 0 )
3445 {
3446 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3447 return( ret );
3448 }
3449 }
3450 else
3451#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003452#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3453 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3454 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003455 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3456 {
3457 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3458 return( ret );
3459 }
3460 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3461 {
3462 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3463 return( ret );
3464 }
3465
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003466 if( p != end )
3467 {
3468 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3469 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3470 }
3471
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003472 if( ( ret = ssl_psk_derive_premaster( ssl,
3473 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003474 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003475 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3476 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003477 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003478 }
3479 else
3480#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003481#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3482 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3483 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003484 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3485 {
3486 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3487 return( ret );
3488 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003489
3490 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3491 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003492 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003493 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3494 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003495 }
3496
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003497 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3498
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003499 if( ( ret = ssl_psk_derive_premaster( ssl,
3500 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003501 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003502 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003503 return( ret );
3504 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003505 }
3506 else
3507#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003508#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3509 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003510 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003511 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003512 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003513 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515 }
3516 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003517 else
3518#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3519 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003520 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003521 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003522 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003523
Paul Bakkerff60ee62010-03-16 21:09:09 +00003524 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3525 {
3526 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3527 return( ret );
3528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 ssl->state++;
3531
3532 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3533
3534 return( 0 );
3535}
3536
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003537#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3538 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003539 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3540 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003541static int ssl_parse_certificate_verify( ssl_context *ssl )
3542{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003543 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003544
3545 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3546
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003547 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003548 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003549 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003550 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003551 {
3552 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3553 ssl->state++;
3554 return( 0 );
3555 }
3556
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003557 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3558 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003559}
3560#else
3561static int ssl_parse_certificate_verify( ssl_context *ssl )
3562{
3563 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003564 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003565 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003566 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003567 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003568#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003569 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003570#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003571 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003572 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3573
3574 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3575
3576 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003577 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003578 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003579 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3580 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003581 {
3582 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3583 ssl->state++;
3584 return( 0 );
3585 }
3586
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003587 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003588 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003589
3590 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3591 {
3592 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3593 return( ret );
3594 }
3595
3596 ssl->state++;
3597
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003598 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3599 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003600 {
3601 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003603 }
3604
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003605 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003606
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003607 /*
3608 * struct {
3609 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3610 * opaque signature<0..2^16-1>;
3611 * } DigitallySigned;
3612 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003613#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3614 defined(POLARSSL_SSL_PROTO_TLS1_1)
3615 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003616 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003617 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003618 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003619
3620 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3621 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3622 POLARSSL_PK_ECDSA ) )
3623 {
3624 hash_start += 16;
3625 hashlen -= 16;
3626 md_alg = POLARSSL_MD_SHA1;
3627 }
Paul Bakker926af752012-11-23 13:38:07 +01003628 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003629 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003630#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3631 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003632#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3633 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003634 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003635 if( i + 2 > ssl->in_hslen )
3636 {
3637 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3638 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3639 }
3640
Paul Bakker5121ce52009-01-03 21:22:43 +00003641 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003642 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003643 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003644 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003646 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3647 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003648 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3649 }
3650
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003651 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003652
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003653 /* Info from md_alg will be used instead */
3654 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003655
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003656 i++;
3657
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003658 /*
3659 * Signature
3660 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003661 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003662 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003663 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003664 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3665 " for verify message" ) );
3666 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003667 }
3668
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003669 /*
3670 * Check the certificate's key type matches the signature alg
3671 */
3672 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3673 {
3674 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3675 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3676 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003677
3678 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003679 }
3680 else
3681#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3682 {
3683 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003684 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003685 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003686
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003687 if( i + 2 > ssl->in_hslen )
3688 {
3689 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3690 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3691 }
3692
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003693 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3694 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003695
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003696 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003697 {
3698 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003699 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003700 }
3701
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003702 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003703 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003704 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003705 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003706 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003707 return( ret );
3708 }
3709
3710 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3711
Paul Bakkered27a042013-04-18 22:46:23 +02003712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003713}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003714#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3715 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3716 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003717
Paul Bakkera503a632013-08-14 13:48:06 +02003718#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003719static int ssl_write_new_session_ticket( ssl_context *ssl )
3720{
3721 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003722 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003723 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003724
3725 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3726
3727 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3728 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3729
3730 /*
3731 * struct {
3732 * uint32 ticket_lifetime_hint;
3733 * opaque ticket<0..2^16-1>;
3734 * } NewSessionTicket;
3735 *
3736 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3737 * 8 . 9 ticket_len (n)
3738 * 10 . 9+n ticket content
3739 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003740
3741 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3742 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3743 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3744 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003745
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003746 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3747 {
3748 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3749 tlen = 0;
3750 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003751
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003752 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3753 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003754
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003755 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003756
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003757 /*
3758 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3759 * ChangeCipherSpec share the same state.
3760 */
3761 ssl->handshake->new_session_ticket = 0;
3762
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003763 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3764 {
3765 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3766 return( ret );
3767 }
3768
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003769 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3770
3771 return( 0 );
3772}
Paul Bakkera503a632013-08-14 13:48:06 +02003773#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003774
Paul Bakker5121ce52009-01-03 21:22:43 +00003775/*
Paul Bakker1961b702013-01-25 14:49:24 +01003776 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003777 */
Paul Bakker1961b702013-01-25 14:49:24 +01003778int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003779{
3780 int ret = 0;
3781
Paul Bakker1961b702013-01-25 14:49:24 +01003782 if( ssl->state == SSL_HANDSHAKE_OVER )
3783 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
Paul Bakker1961b702013-01-25 14:49:24 +01003785 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3786
3787 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3788 return( ret );
3789
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003790#if defined(POLARSSL_SSL_PROTO_DTLS)
3791 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3792 ssl->handshake != NULL &&
3793 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3794 {
3795 if( ( ret = ssl_resend( ssl ) ) != 0 )
3796 return( ret );
3797 }
3798#endif
3799
Paul Bakker1961b702013-01-25 14:49:24 +01003800 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003801 {
Paul Bakker1961b702013-01-25 14:49:24 +01003802 case SSL_HELLO_REQUEST:
3803 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003804 break;
3805
Paul Bakker1961b702013-01-25 14:49:24 +01003806 /*
3807 * <== ClientHello
3808 */
3809 case SSL_CLIENT_HELLO:
3810 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003811 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003812
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003813#if defined(POLARSSL_SSL_PROTO_DTLS)
3814 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3815 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3816#endif
3817
Paul Bakker1961b702013-01-25 14:49:24 +01003818 /*
3819 * ==> ServerHello
3820 * Certificate
3821 * ( ServerKeyExchange )
3822 * ( CertificateRequest )
3823 * ServerHelloDone
3824 */
3825 case SSL_SERVER_HELLO:
3826 ret = ssl_write_server_hello( ssl );
3827 break;
3828
3829 case SSL_SERVER_CERTIFICATE:
3830 ret = ssl_write_certificate( ssl );
3831 break;
3832
3833 case SSL_SERVER_KEY_EXCHANGE:
3834 ret = ssl_write_server_key_exchange( ssl );
3835 break;
3836
3837 case SSL_CERTIFICATE_REQUEST:
3838 ret = ssl_write_certificate_request( ssl );
3839 break;
3840
3841 case SSL_SERVER_HELLO_DONE:
3842 ret = ssl_write_server_hello_done( ssl );
3843 break;
3844
3845 /*
3846 * <== ( Certificate/Alert )
3847 * ClientKeyExchange
3848 * ( CertificateVerify )
3849 * ChangeCipherSpec
3850 * Finished
3851 */
3852 case SSL_CLIENT_CERTIFICATE:
3853 ret = ssl_parse_certificate( ssl );
3854 break;
3855
3856 case SSL_CLIENT_KEY_EXCHANGE:
3857 ret = ssl_parse_client_key_exchange( ssl );
3858 break;
3859
3860 case SSL_CERTIFICATE_VERIFY:
3861 ret = ssl_parse_certificate_verify( ssl );
3862 break;
3863
3864 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3865 ret = ssl_parse_change_cipher_spec( ssl );
3866 break;
3867
3868 case SSL_CLIENT_FINISHED:
3869 ret = ssl_parse_finished( ssl );
3870 break;
3871
3872 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003873 * ==> ( NewSessionTicket )
3874 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003875 * Finished
3876 */
3877 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003878#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003879 if( ssl->handshake->new_session_ticket != 0 )
3880 ret = ssl_write_new_session_ticket( ssl );
3881 else
Paul Bakkera503a632013-08-14 13:48:06 +02003882#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003883 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003884 break;
3885
3886 case SSL_SERVER_FINISHED:
3887 ret = ssl_write_finished( ssl );
3888 break;
3889
3890 case SSL_FLUSH_BUFFERS:
3891 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3892 ssl->state = SSL_HANDSHAKE_WRAPUP;
3893 break;
3894
3895 case SSL_HANDSHAKE_WRAPUP:
3896 ssl_handshake_wrapup( ssl );
3897 break;
3898
3899 default:
3900 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3901 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003902 }
3903
Paul Bakker5121ce52009-01-03 21:22:43 +00003904 return( ret );
3905}
Paul Bakker9af723c2014-05-01 13:03:14 +02003906#endif /* POLARSSL_SSL_SRV_C */