blob: 6621faa6bbcf1d8a4eada136c46e0896ac7bf274 [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
666 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
667
668 return( 0 );
669}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200670#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200671
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +0100672#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
673static int ssl_parse_encrypt_then_mac_ext( ssl_context *ssl,
674 const unsigned char *buf,
675 size_t len )
676{
677 if( len != 0 )
678 {
679 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
680 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
681 }
682
683 ((void) buf);
684
685 if( ssl->encrypt_then_mac == SSL_ETM_ENABLED &&
686 ssl->minor_ver != SSL_MINOR_VERSION_0 )
687 {
688 ssl->session_negotiate->encrypt_then_mac = SSL_ETM_ENABLED;
689 }
690
691 return( 0 );
692}
693#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
694
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200695#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
696static int ssl_parse_extended_ms_ext( ssl_context *ssl,
697 const unsigned char *buf,
698 size_t len )
699{
700 if( len != 0 )
701 {
702 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
703 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
704 }
705
706 ((void) buf);
707
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200708 if( ssl->extended_ms == SSL_EXTENDED_MS_ENABLED &&
709 ssl->minor_ver != SSL_MINOR_VERSION_0 )
710 {
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200711 ssl->handshake->extended_ms = SSL_EXTENDED_MS_ENABLED;
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +0200712 }
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +0200713
714 return( 0 );
715}
716#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
717
Paul Bakkera503a632013-08-14 13:48:06 +0200718#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200719static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200720 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200721 size_t len )
722{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200723 int ret;
724
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200725 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
726 return( 0 );
727
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200728 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200729 ssl->handshake->new_session_ticket = 1;
730
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200731 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
732
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200733 if( len == 0 )
734 return( 0 );
735
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100736#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200737 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
738 {
739 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
740 return( 0 );
741 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100742#endif /* POLARSSL_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200743
744 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200745 * Failures are ok: just ignore the ticket and proceed.
746 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200747 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
748 {
749 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200750 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200751 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200752
753 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
754
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200755 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200756
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200757 /* Don't send a new ticket after all, this one is OK */
758 ssl->handshake->new_session_ticket = 0;
759
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200760 return( 0 );
761}
Paul Bakkera503a632013-08-14 13:48:06 +0200762#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200763
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200764#if defined(POLARSSL_SSL_ALPN)
765static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200766 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200767{
Paul Bakker14b16c62014-05-28 11:33:54 +0200768 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200769 const unsigned char *theirs, *start, *end;
770 const char **ours;
771
772 /* If ALPN not configured, just ignore the extension */
773 if( ssl->alpn_list == NULL )
774 return( 0 );
775
776 /*
777 * opaque ProtocolName<1..2^8-1>;
778 *
779 * struct {
780 * ProtocolName protocol_name_list<2..2^16-1>
781 * } ProtocolNameList;
782 */
783
784 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
785 if( len < 4 )
786 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
787
788 list_len = ( buf[0] << 8 ) | buf[1];
789 if( list_len != len - 2 )
790 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
791
792 /*
793 * Use our order of preference
794 */
795 start = buf + 2;
796 end = buf + len;
797 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
798 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200799 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200800 for( theirs = start; theirs != end; theirs += cur_len )
801 {
802 /* If the list is well formed, we should get equality first */
803 if( theirs > end )
804 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
805
806 cur_len = *theirs++;
807
808 /* Empty strings MUST NOT be included */
809 if( cur_len == 0 )
810 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
811
Paul Bakker14b16c62014-05-28 11:33:54 +0200812 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200813 memcmp( theirs, *ours, cur_len ) == 0 )
814 {
815 ssl->alpn_chosen = *ours;
816 return( 0 );
817 }
818 }
819 }
820
821 /* If we get there, no match was found */
822 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
823 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
824 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
825}
826#endif /* POLARSSL_SSL_ALPN */
827
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100828/*
829 * Auxiliary functions for ServerHello parsing and related actions
830 */
831
832#if defined(POLARSSL_X509_CRT_PARSE_C)
833/*
834 * Return 1 if the given EC key uses the given curve, 0 otherwise
835 */
836#if defined(POLARSSL_ECDSA_C)
837static int ssl_key_matches_curves( pk_context *pk,
838 const ecp_curve_info **curves )
839{
840 const ecp_curve_info **crv = curves;
841 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
842
843 while( *crv != NULL )
844 {
845 if( (*crv)->grp_id == grp_id )
846 return( 1 );
847 crv++;
848 }
849
850 return( 0 );
851}
852#endif /* POLARSSL_ECDSA_C */
853
854/*
855 * Try picking a certificate for this ciphersuite,
856 * return 0 on success and -1 on failure.
857 */
858static int ssl_pick_cert( ssl_context *ssl,
859 const ssl_ciphersuite_t * ciphersuite_info )
860{
861 ssl_key_cert *cur, *list;
862 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
863
864#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
865 if( ssl->handshake->sni_key_cert != NULL )
866 list = ssl->handshake->sni_key_cert;
867 else
868#endif
869 list = ssl->handshake->key_cert;
870
871 if( pk_alg == POLARSSL_PK_NONE )
872 return( 0 );
873
874 for( cur = list; cur != NULL; cur = cur->next )
875 {
876 if( ! pk_can_do( cur->key, pk_alg ) )
877 continue;
878
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200879 /*
880 * This avoids sending the client a cert it'll reject based on
881 * keyUsage or other extensions.
882 *
883 * It also allows the user to provision different certificates for
884 * different uses based on keyUsage, eg if they want to avoid signing
885 * and decrypting with the same RSA key.
886 */
887 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
888 SSL_IS_SERVER ) != 0 )
889 {
890 continue;
891 }
892
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100893#if defined(POLARSSL_ECDSA_C)
894 if( pk_alg == POLARSSL_PK_ECDSA )
895 {
896 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
897 break;
898 }
899 else
900#endif
901 break;
902 }
903
904 if( cur == NULL )
905 return( -1 );
906
907 ssl->handshake->key_cert = cur;
908 return( 0 );
909}
910#endif /* POLARSSL_X509_CRT_PARSE_C */
911
912/*
913 * Check if a given ciphersuite is suitable for use with our config/keys/etc
914 * Sets ciphersuite_info only if the suite matches.
915 */
916static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
917 const ssl_ciphersuite_t **ciphersuite_info )
918{
919 const ssl_ciphersuite_t *suite_info;
920
921 suite_info = ssl_ciphersuite_from_id( suite_id );
922 if( suite_info == NULL )
923 {
924 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
925 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
926 }
927
928 if( suite_info->min_minor_ver > ssl->minor_ver ||
929 suite_info->max_minor_ver < ssl->minor_ver )
930 return( 0 );
931
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100932#if defined(POLARSSL_SSL_PROTO_DTLS)
933 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
934 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
935 return( 0 );
936#endif
937
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100938#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
939 if( ssl_ciphersuite_uses_ec( suite_info ) &&
940 ( ssl->handshake->curves == NULL ||
941 ssl->handshake->curves[0] == NULL ) )
942 return( 0 );
943#endif
944
945#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
946 /* If the ciphersuite requires a pre-shared key and we don't
947 * have one, skip it now rather than failing later */
948 if( ssl_ciphersuite_uses_psk( suite_info ) &&
949 ssl->f_psk == NULL &&
950 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
951 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
952 return( 0 );
953#endif
954
955#if defined(POLARSSL_X509_CRT_PARSE_C)
956 /*
957 * Final check: if ciphersuite requires us to have a
958 * certificate/key of a particular type:
959 * - select the appropriate certificate if we have one, or
960 * - try the next ciphersuite if we don't
961 * This must be done last since we modify the key_cert list.
962 */
963 if( ssl_pick_cert( ssl, suite_info ) != 0 )
964 return( 0 );
965#endif
966
967 *ciphersuite_info = suite_info;
968 return( 0 );
969}
970
Paul Bakker78a8c712013-03-06 17:01:52 +0100971#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
972static int ssl_parse_client_hello_v2( ssl_context *ssl )
973{
974 int ret;
975 unsigned int i, j;
976 size_t n;
977 unsigned int ciph_len, sess_len, chal_len;
978 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200979 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200980 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100981
982 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
983
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100984#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +0100985 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
986 {
987 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
988
989 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
990 return( ret );
991
992 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
993 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +0100994#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +0100995
996 buf = ssl->in_hdr;
997
998 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
999
1000 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
1001 buf[2] ) );
1002 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
1003 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
1004 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
1005 buf[3], buf[4] ) );
1006
1007 /*
1008 * SSLv2 Client Hello
1009 *
1010 * Record layer:
1011 * 0 . 1 message length
1012 *
1013 * SSL layer:
1014 * 2 . 2 message type
1015 * 3 . 4 protocol version
1016 */
1017 if( buf[2] != SSL_HS_CLIENT_HELLO ||
1018 buf[3] != SSL_MAJOR_VERSION_3 )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
1025
1026 if( n < 17 || n > 512 )
1027 {
1028 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1029 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1030 }
1031
1032 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +02001033 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
1034 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +01001035
1036 if( ssl->minor_ver < ssl->min_minor_ver )
1037 {
1038 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001039 " [%d:%d] < [%d:%d]",
1040 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +01001041 ssl->min_major_ver, ssl->min_minor_ver ) );
1042
1043 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1044 SSL_ALERT_MSG_PROTOCOL_VERSION );
1045 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1046 }
1047
Paul Bakker2fbefde2013-06-29 16:01:15 +02001048 ssl->handshake->max_major_ver = buf[3];
1049 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +01001050
1051 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
1052 {
1053 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1054 return( ret );
1055 }
1056
1057 ssl->handshake->update_checksum( ssl, buf + 2, n );
1058
1059 buf = ssl->in_msg;
1060 n = ssl->in_left - 5;
1061
1062 /*
1063 * 0 . 1 ciphersuitelist length
1064 * 2 . 3 session id length
1065 * 4 . 5 challenge length
1066 * 6 . .. ciphersuitelist
1067 * .. . .. session id
1068 * .. . .. challenge
1069 */
1070 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1071
1072 ciph_len = ( buf[0] << 8 ) | buf[1];
1073 sess_len = ( buf[2] << 8 ) | buf[3];
1074 chal_len = ( buf[4] << 8 ) | buf[5];
1075
1076 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1077 ciph_len, sess_len, chal_len ) );
1078
1079 /*
1080 * Make sure each parameter length is valid
1081 */
1082 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1083 {
1084 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1085 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1086 }
1087
1088 if( sess_len > 32 )
1089 {
1090 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1091 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1092 }
1093
1094 if( chal_len < 8 || chal_len > 32 )
1095 {
1096 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1097 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1098 }
1099
1100 if( n != 6 + ciph_len + sess_len + chal_len )
1101 {
1102 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1104 }
1105
1106 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1107 buf + 6, ciph_len );
1108 SSL_DEBUG_BUF( 3, "client hello, session id",
1109 buf + 6 + ciph_len, sess_len );
1110 SSL_DEBUG_BUF( 3, "client hello, challenge",
1111 buf + 6 + ciph_len + sess_len, chal_len );
1112
1113 p = buf + 6 + ciph_len;
1114 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001115 memset( ssl->session_negotiate->id, 0,
1116 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001117 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1118
1119 p += sess_len;
1120 memset( ssl->handshake->randbytes, 0, 64 );
1121 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1122
1123 /*
1124 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1125 */
1126 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1127 {
1128 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1129 {
1130 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001131#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker78a8c712013-03-06 17:01:52 +01001132 if( ssl->renegotiation == SSL_RENEGOTIATION )
1133 {
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001134 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV "
1135 "during renegotiation" ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001136
1137 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1138 return( ret );
1139
1140 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1141 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001142#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakker78a8c712013-03-06 17:01:52 +01001143 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1144 break;
1145 }
1146 }
1147
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001148#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1149 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1150 {
1151 if( p[0] == 0 &&
1152 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1153 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1154 {
1155 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1156
1157 if( ssl->minor_ver < ssl->max_minor_ver )
1158 {
1159 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1160
1161 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1162 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1163
1164 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1165 }
1166
1167 break;
1168 }
1169 }
1170#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1171
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001172 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001173 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001174#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1175 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1176 {
1177 for( i = 0; ciphersuites[i] != 0; i++ )
1178#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001179 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001180 {
1181 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001182#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001183 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001184 if( p[0] != 0 ||
1185 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1186 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1187 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001188
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001189 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1190 &ciphersuite_info ) ) != 0 )
1191 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001192
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001193 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001194 goto have_ciphersuite_v2;
1195 }
1196 }
1197
1198 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1199
1200 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1201
1202have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001203 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001204 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001205 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001206
1207 /*
1208 * SSLv2 Client Hello relevant renegotiation security checks
1209 */
1210 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1211 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1212 {
1213 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1214
1215 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1216 return( ret );
1217
1218 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1219 }
1220
1221 ssl->in_left = 0;
1222 ssl->state++;
1223
1224 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1225
1226 return( 0 );
1227}
1228#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1229
Paul Bakker5121ce52009-01-03 21:22:43 +00001230static int ssl_parse_client_hello( ssl_context *ssl )
1231{
Paul Bakker23986e52011-04-24 08:57:21 +00001232 int ret;
1233 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001234 unsigned int ciph_offset, comp_offset, ext_offset;
1235 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001236#if defined(POLARSSL_SSL_PROTO_DTLS)
1237 unsigned int cookie_offset, cookie_len;
1238#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001239 unsigned char *buf, *p, *ext;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001240#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001241 int renegotiation_info_seen = 0;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001242#endif
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001243 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001244 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001245 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001246 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001247
1248 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1249
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001250#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1251read_record_header:
1252#endif
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001253 /*
1254 * If renegotiating, then the input was read with ssl_read_record(),
1255 * otherwise read it ourselves manually in order to support SSLv2
1256 * ClientHello, which doesn't use the same record layer format.
1257 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001258#if defined(POLARSSL_SSL_RENEGOTIATION)
1259 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1260#endif
1261 if( ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 {
1263 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1264 return( ret );
1265 }
1266
1267 buf = ssl->in_hdr;
1268
Paul Bakker78a8c712013-03-06 17:01:52 +01001269#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnard8a7cf252014-10-09 17:35:53 +02001270#if defined(POLARSSL_SSL_PROTO_DTLS)
1271 if( ssl->transport == SSL_TRANSPORT_STREAM )
1272#endif
1273 if( ( buf[0] & 0x80 ) != 0 )
1274 return ssl_parse_client_hello_v2( ssl );
Paul Bakker78a8c712013-03-06 17:01:52 +01001275#endif
1276
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001277 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001278
Paul Bakkerec636f32012-09-09 19:17:02 +00001279 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001280 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001281 *
1282 * Record layer:
1283 * 0 . 0 message type
1284 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001285 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001286 * 3 . 4 message length
1287 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001288 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1289 buf[0] ) );
1290
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001291 if( buf[0] != SSL_MSG_HANDSHAKE )
1292 {
1293 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1294 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1295 }
1296
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001297 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1298 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1299
1300 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1301 buf[1], buf[2] ) );
1302
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001303 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001304
1305 /* According to RFC 5246 Appendix E.1, the version here is typically
1306 * "{03,00}, the lowest version number supported by the client, [or] the
1307 * value of ClientHello.client_version", so the only meaningful check here
1308 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001309 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001311 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1312 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1313 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001314
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001315 /* For DTLS if this is the initial handshake, remember the client sequence
1316 * number to use it in our next message (RFC 6347 4.2.1) */
1317#if defined(POLARSSL_SSL_PROTO_DTLS)
1318 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1319 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1320 {
1321 /* Epoch should be 0 for initial handshakes */
1322 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1323 {
1324 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1325 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1326 }
1327
1328 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
Manuel Pégourié-Gonnardf03c7aa2014-09-24 14:54:06 +02001329
1330#if defined(POLARSSL_SSL_DTLS_ANTI_REPLAY)
1331 if( ssl_dtls_replay_check( ssl ) != 0 )
1332 {
1333 SSL_DEBUG_MSG( 1, ( "replayed record, discarding" ) );
1334 ssl->next_record_offset = 0;
1335 ssl->in_left = 0;
1336 goto read_record_header;
1337 }
1338
1339 /* No MAC to check yet, so we can update right now */
1340 ssl_dtls_replay_update( ssl );
1341#endif
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001342 }
1343#endif /* POLARSSL_SSL_PROTO_DTLS */
1344
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001345 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001347#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001348 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001349#endif
Paul Bakkerec636f32012-09-09 19:17:02 +00001350 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001351 if( msg_len > SSL_MAX_CONTENT_LEN )
1352 {
1353 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1354 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1355 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001356
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001357 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1358 {
1359 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1360 return( ret );
1361 }
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001362
1363 /* Done reading this record, get ready for the next one */
1364#if defined(POLARSSL_SSL_PROTO_DTLS)
1365 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1366 ssl->next_record_offset = msg_len + ssl_hdr_len( ssl );
1367 else
1368#endif
1369 ssl->in_left = 0;
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001370 }
1371 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001372 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001373 /* Set by ssl_read_record() */
1374 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001375 }
1376
1377 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001378
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001379 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001380
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001381 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001382
1383 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001384 * Handshake layer:
1385 * 0 . 0 handshake type
1386 * 1 . 3 handshake length
1387 * 4 . 5 DTLS only: message seqence number
1388 * 6 . 8 DTLS only: fragment offset
1389 * 9 . 11 DTLS only: fragment length
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001390 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001391 if( msg_len < ssl_hs_hdr_len( ssl ) )
1392 {
1393 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1394 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1395 }
1396
1397 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1398
1399 if( buf[0] != SSL_HS_CLIENT_HELLO )
1400 {
1401 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1402 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1403 }
1404
1405 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1406 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1407
1408 /* We don't support fragmentation of ClientHello (yet?) */
1409 if( buf[1] != 0 ||
1410 msg_len != ssl_hs_hdr_len( ssl ) + ( ( buf[2] << 8 ) | buf[3] ) )
1411 {
1412 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1413 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1414 }
1415
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001416#if defined(POLARSSL_SSL_PROTO_DTLS)
1417 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1418 {
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001419 /*
1420 * Copy the client's handshake message_seq on initial handshakes
1421 */
1422 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001423 {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001424 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1425 ssl->in_msg[5];
1426 ssl->handshake->out_msg_seq = cli_msg_seq;
1427 ssl->handshake->in_msg_seq = cli_msg_seq + 1;
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02001428 }
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001429 else
1430 {
1431 /* This couldn't be done in ssl_prepare_handshake_record() */
1432 unsigned int cli_msg_seq = ( ssl->in_msg[4] << 8 ) |
1433 ssl->in_msg[5];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001434
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02001435 if( cli_msg_seq != ssl->handshake->in_msg_seq )
1436 {
1437 SSL_DEBUG_MSG( 1, ( "bad client hello message_seq: "
1438 "%d (expected %d)", cli_msg_seq,
1439 ssl->handshake->in_msg_seq ) );
1440 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1441 }
1442
1443 ssl->handshake->in_msg_seq++;
1444 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001445
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001446 /*
1447 * For now we don't support fragmentation, so make sure
1448 * fragment_offset == 0 and fragment_length == length
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001449 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001450 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1451 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1452 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001453 SSL_DEBUG_MSG( 1, ( "ClientHello fragmentation not supported" ) );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001454 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1455 }
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001456 }
1457#endif /* POLARSSL_SSL_PROTO_DTLS */
1458
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001459 buf += ssl_hs_hdr_len( ssl );
1460 msg_len -= ssl_hs_hdr_len( ssl );
1461
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001462 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001463 * ClientHello layer:
1464 * 0 . 1 protocol version
1465 * 2 . 33 random bytes (starting with 4 bytes of Unix time)
1466 * 34 . 35 session id length (1 byte)
1467 * 35 . 34+x session id
1468 * 35+x . 35+x DTLS only: cookie length (1 byte)
1469 * 36+x . .. DTLS only: cookie
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001470 * .. . .. ciphersuite list length (2 bytes)
1471 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001472 * .. . .. compression alg. list length (1 byte)
1473 * .. . .. compression alg. list
1474 * .. . .. extensions length (2 bytes, optional)
1475 * .. . .. extensions (optional)
Paul Bakkerec636f32012-09-09 19:17:02 +00001476 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001477
1478 /*
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001479 * Minimal length (with everything empty and extensions ommitted) is
1480 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1481 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001482 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001483 if( msg_len < 38 )
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001484 {
1485 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1486 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1487 }
1488
1489 /*
1490 * Check and save the protocol version
1491 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001492 SSL_DEBUG_BUF( 3, "client hello, version", buf, 2 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001493
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001494 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001495 ssl->transport, buf );
Paul Bakkerec636f32012-09-09 19:17:02 +00001496
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001497 ssl->handshake->max_major_ver = ssl->major_ver;
1498 ssl->handshake->max_minor_ver = ssl->minor_ver;
1499
1500 if( ssl->major_ver < ssl->min_major_ver ||
1501 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001502 {
1503 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001504 " [%d:%d] < [%d:%d]",
1505 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001506 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001507
1508 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1509 SSL_ALERT_MSG_PROTOCOL_VERSION );
1510
1511 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1512 }
1513
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001514 if( ssl->major_ver > ssl->max_major_ver )
1515 {
1516 ssl->major_ver = ssl->max_major_ver;
1517 ssl->minor_ver = ssl->max_minor_ver;
1518 }
1519 else if( ssl->minor_ver > ssl->max_minor_ver )
1520 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001521
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001522 /*
1523 * Save client random (inc. Unix time)
1524 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001525 SSL_DEBUG_BUF( 3, "client hello, random bytes", buf + 2, 32 );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001526
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001527 memcpy( ssl->handshake->randbytes, buf + 2, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001528
1529 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001530 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001531 */
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001532 sess_len = buf[34];
Paul Bakkerec636f32012-09-09 19:17:02 +00001533
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001534 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001535 sess_len + 34 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001536 {
1537 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1538 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1539 }
1540
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001541 SSL_DEBUG_BUF( 3, "client hello, session id", buf + 35, sess_len );
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001542
Paul Bakker48916f92012-09-16 19:57:18 +00001543 ssl->session_negotiate->length = sess_len;
1544 memset( ssl->session_negotiate->id, 0,
1545 sizeof( ssl->session_negotiate->id ) );
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001546 memcpy( ssl->session_negotiate->id, buf + 35,
Paul Bakker48916f92012-09-16 19:57:18 +00001547 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001548
1549 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001550 * Check the cookie length and content
1551 */
1552#if defined(POLARSSL_SSL_PROTO_DTLS)
1553 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1554 {
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001555 cookie_offset = 35 + sess_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001556 cookie_len = buf[cookie_offset];
1557
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001558 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001559 {
1560 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1561 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1562 }
1563
1564 SSL_DEBUG_BUF( 3, "client hello, cookie",
1565 buf + cookie_offset + 1, cookie_len );
1566
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001567#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard30d16eb2014-08-19 17:43:50 +02001568 if( ssl->f_cookie_check != NULL &&
1569 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001570 {
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001571 if( ssl->f_cookie_check( ssl->p_cookie,
1572 buf + cookie_offset + 1, cookie_len,
1573 ssl->cli_id, ssl->cli_id_len ) != 0 )
1574 {
1575 SSL_DEBUG_MSG( 2, ( "cookie verification failed" ) );
1576 ssl->handshake->verify_cookie_len = 1;
1577 }
1578 else
1579 {
1580 SSL_DEBUG_MSG( 2, ( "cookie verification passed" ) );
1581 ssl->handshake->verify_cookie_len = 0;
1582 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001583 }
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02001584 else
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001585#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001586 {
1587 /* We know we didn't send a cookie, so it should be empty */
1588 if( cookie_len != 0 )
1589 {
1590 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1591 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1592 }
1593
1594 SSL_DEBUG_MSG( 2, ( "cookie verification skipped" ) );
1595 }
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001596 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001597#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001598
1599 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001600 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001601 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001602#if defined(POLARSSL_SSL_PROTO_DTLS)
1603 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1604 ciph_offset = cookie_offset + 1 + cookie_len;
1605 else
1606#endif
Manuel Pégourié-Gonnard19d438f2014-09-09 17:08:52 +02001607 ciph_offset = 35 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001608
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001609 ciph_len = ( buf[ciph_offset + 0] << 8 )
1610 | ( buf[ciph_offset + 1] );
1611
1612 if( ciph_len < 2 ||
1613 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1614 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001615 {
1616 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1617 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1618 }
1619
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001620 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1621 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001622
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001623 /*
1624 * Check the compression algorithms length and pick one
1625 */
1626 comp_offset = ciph_offset + 2 + ciph_len;
1627
1628 comp_len = buf[comp_offset];
1629
1630 if( comp_len < 1 ||
1631 comp_len > 16 ||
1632 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001633 {
1634 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1635 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1636 }
1637
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001638 SSL_DEBUG_BUF( 3, "client hello, compression",
1639 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001640
1641 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001642#if defined(POLARSSL_ZLIB_SUPPORT)
1643 for( i = 0; i < comp_len; ++i )
1644 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001645 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
Paul Bakker48916f92012-09-16 19:57:18 +00001647 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001648 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001649 }
1650 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001651#endif
1652
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001653 /* See comments in ssl_write_client_hello() */
1654#if defined(POLARSSL_SSL_PROTO_DTLS)
1655 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1656 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1657#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001658
Paul Bakkerec636f32012-09-09 19:17:02 +00001659 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001660 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001661 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001662 ext_offset = comp_offset + 1 + comp_len;
1663 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001664 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001665 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001666 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001667 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1668 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1669 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001670
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001671 ext_len = ( buf[ext_offset + 0] << 8 )
1672 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001673
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001674 if( ( ext_len > 0 && ext_len < 4 ) ||
1675 msg_len != ext_offset + 2 + ext_len )
1676 {
1677 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1678 SSL_DEBUG_BUF( 3, "client hello extensions",
1679 buf + ext_offset + 2, ext_len );
1680 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001681 }
1682 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001683 else
1684 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001685
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001686 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001687
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001688 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001689 {
1690 unsigned int ext_id = ( ( ext[0] << 8 )
1691 | ( ext[1] ) );
1692 unsigned int ext_size = ( ( ext[2] << 8 )
1693 | ( ext[3] ) );
1694
1695 if( ext_size + 4 > ext_len )
1696 {
1697 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1698 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1699 }
1700 switch( ext_id )
1701 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001702#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001703 case TLS_EXT_SERVERNAME:
1704 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1705 if( ssl->f_sni == NULL )
1706 break;
1707
1708 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1709 if( ret != 0 )
1710 return( ret );
1711 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001712#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001713
Paul Bakker48916f92012-09-16 19:57:18 +00001714 case TLS_EXT_RENEGOTIATION_INFO:
1715 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001716#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00001717 renegotiation_info_seen = 1;
Manuel Pégourié-Gonnardeaecbd32014-11-06 02:38:02 +01001718#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001719
Paul Bakker23f36802012-09-28 14:15:14 +00001720 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1721 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001722 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001723 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001724
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001725#if defined(POLARSSL_SSL_PROTO_TLS1_2) && \
1726 defined(POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED)
Paul Bakker23f36802012-09-28 14:15:14 +00001727 case TLS_EXT_SIG_ALG:
1728 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001729#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakker23f36802012-09-28 14:15:14 +00001730 if( ssl->renegotiation == SSL_RENEGOTIATION )
1731 break;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001732#endif
Paul Bakker23f36802012-09-28 14:15:14 +00001733
1734 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1735 if( ret != 0 )
1736 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001737 break;
Manuel Pégourié-Gonnardd9423232014-12-02 11:57:29 +01001738#endif /* POLARSSL_SSL_PROTO_TLS1_2 &&
1739 POLARSSL_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker48916f92012-09-16 19:57:18 +00001740
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001741#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001742 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1743 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1744
1745 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1746 if( ret != 0 )
1747 return( ret );
1748 break;
1749
1750 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1751 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001752 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001753
1754 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1755 if( ret != 0 )
1756 return( ret );
1757 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001758#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001759
Paul Bakker05decb22013-08-15 13:33:48 +02001760#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001761 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1762 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1763
1764 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1765 if( ret != 0 )
1766 return( ret );
1767 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001768#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001769
Paul Bakker1f2bc622013-08-15 13:45:55 +02001770#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001771 case TLS_EXT_TRUNCATED_HMAC:
1772 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1773
1774 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1775 if( ret != 0 )
1776 return( ret );
1777 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001778#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001779
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01001780#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
1781 case TLS_EXT_ENCRYPT_THEN_MAC:
1782 SSL_DEBUG_MSG( 3, ( "found encrypt then mac extension" ) );
1783
1784 ret = ssl_parse_encrypt_then_mac_ext( ssl, ext + 4, ext_size );
1785 if( ret != 0 )
1786 return( ret );
1787 break;
1788#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
1789
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02001790#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
1791 case TLS_EXT_EXTENDED_MASTER_SECRET:
1792 SSL_DEBUG_MSG( 3, ( "found extended master secret extension" ) );
1793
1794 ret = ssl_parse_extended_ms_ext( ssl, ext + 4, ext_size );
1795 if( ret != 0 )
1796 return( ret );
1797 break;
1798#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
1799
Paul Bakkera503a632013-08-14 13:48:06 +02001800#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001801 case TLS_EXT_SESSION_TICKET:
1802 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1803
1804 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1805 if( ret != 0 )
1806 return( ret );
1807 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001808#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001809
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001810#if defined(POLARSSL_SSL_ALPN)
1811 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001812 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001813
1814 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1815 if( ret != 0 )
1816 return( ret );
1817 break;
1818#endif /* POLARSSL_SSL_SESSION_TICKETS */
1819
Paul Bakker48916f92012-09-16 19:57:18 +00001820 default:
1821 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1822 ext_id ) );
1823 }
1824
1825 ext_len -= 4 + ext_size;
1826 ext += 4 + ext_size;
1827
1828 if( ext_len > 0 && ext_len < 4 )
1829 {
1830 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1831 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1832 }
1833 }
1834
Manuel Pégourié-Gonnardfedba982014-11-05 16:12:09 +01001835#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1836 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1837 {
1838 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1839 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1840 {
1841 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1842
1843 if( ssl->minor_ver < ssl->max_minor_ver )
1844 {
1845 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1846
1847 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1848 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1849
1850 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1851 }
1852
1853 break;
1854 }
1855 }
1856#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1857
Paul Bakker48916f92012-09-16 19:57:18 +00001858 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001859 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1860 */
1861 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1862 {
1863 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1864 {
1865 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1866 if( ssl->renegotiation == SSL_RENEGOTIATION )
1867 {
1868 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1869
1870 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1871 return( ret );
1872
1873 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1874 }
1875 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1876 break;
1877 }
1878 }
1879
1880 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001881 * Renegotiation security checks
1882 */
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001883 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION &&
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001884 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1885 {
1886 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1887 handshake_failure = 1;
1888 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001889#if defined(POLARSSL_SSL_RENEGOTIATION)
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001890 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1891 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1892 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001893 {
1894 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001895 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001896 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001897 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1898 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1899 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001900 {
1901 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001902 handshake_failure = 1;
1903 }
1904 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1905 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1906 renegotiation_info_seen == 1 )
1907 {
1908 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1909 handshake_failure = 1;
1910 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001911#endif /* POLARSSL_SSL_RENEGOTIATION */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001912
1913 if( handshake_failure == 1 )
1914 {
1915 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1916 return( ret );
1917
Paul Bakker48916f92012-09-16 19:57:18 +00001918 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1919 }
Paul Bakker380da532012-04-18 16:10:25 +00001920
Paul Bakker41c83d32013-03-20 14:39:14 +01001921 /*
1922 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001923 * (At the end because we need information from the EC-based extensions
1924 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001925 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001926 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001927 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001928#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001929 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001930 {
1931 for( i = 0; ciphersuites[i] != 0; i++ )
1932#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001933 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001934 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001935 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001936#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001937 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001938 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1939 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1940 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001941
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001942 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1943 &ciphersuite_info ) ) != 0 )
1944 return( ret );
1945
1946 if( ciphersuite_info != NULL )
1947 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001948 }
1949 }
1950
1951 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1952
1953 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1954 return( ret );
1955
1956 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1957
1958have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001959 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001960 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1961 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1962
Paul Bakker5121ce52009-01-03 21:22:43 +00001963 ssl->state++;
1964
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001965#if defined(POLARSSL_SSL_PROTO_DTLS)
1966 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1967 ssl_recv_flight_completed( ssl );
1968#endif
1969
Paul Bakker5121ce52009-01-03 21:22:43 +00001970 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1971
1972 return( 0 );
1973}
1974
Paul Bakker1f2bc622013-08-15 13:45:55 +02001975#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001976static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1977 unsigned char *buf,
1978 size_t *olen )
1979{
1980 unsigned char *p = buf;
1981
1982 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1983 {
1984 *olen = 0;
1985 return;
1986 }
1987
1988 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1989
1990 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1991 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1992
1993 *p++ = 0x00;
1994 *p++ = 0x00;
1995
1996 *olen = 4;
1997}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001998#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001999
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002000#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2001static void ssl_write_encrypt_then_mac_ext( ssl_context *ssl,
2002 unsigned char *buf,
2003 size_t *olen )
2004{
2005 unsigned char *p = buf;
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002006 const ssl_ciphersuite_t *suite = NULL;
2007 const cipher_info_t *cipher = NULL;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002008
2009 if( ssl->session_negotiate->encrypt_then_mac == SSL_EXTENDED_MS_DISABLED ||
2010 ssl->minor_ver == SSL_MINOR_VERSION_0 )
2011 {
2012 *olen = 0;
2013 return;
2014 }
2015
Manuel Pégourié-Gonnard78e745f2014-11-04 15:44:06 +01002016 /*
2017 * RFC 7366: "If a server receives an encrypt-then-MAC request extension
2018 * from a client and then selects a stream or Authenticated Encryption
2019 * with Associated Data (AEAD) ciphersuite, it MUST NOT send an
2020 * encrypt-then-MAC response extension back to the client."
2021 */
2022 if( ( suite = ssl_ciphersuite_from_id(
2023 ssl->session_negotiate->ciphersuite ) ) == NULL ||
2024 ( cipher = cipher_info_from_type( suite->cipher ) ) == NULL ||
2025 cipher->mode != POLARSSL_MODE_CBC )
2026 {
2027 *olen = 0;
2028 return;
2029 }
2030
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002031 SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) );
2032
2033 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF );
2034 *p++ = (unsigned char)( ( TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF );
2035
2036 *p++ = 0x00;
2037 *p++ = 0x00;
2038
2039 *olen = 4;
2040}
2041#endif /* POLARSSL_SSL_ENCRYPT_THEN_MAC */
2042
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002043#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2044static void ssl_write_extended_ms_ext( ssl_context *ssl,
2045 unsigned char *buf,
2046 size_t *olen )
2047{
2048 unsigned char *p = buf;
2049
Manuel Pégourié-Gonnardb575b542014-10-24 15:12:31 +02002050 if( ssl->handshake->extended_ms == SSL_EXTENDED_MS_DISABLED ||
2051 ssl->minor_ver == SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002052 {
2053 *olen = 0;
2054 return;
2055 }
2056
2057 SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret "
2058 "extension" ) );
2059
2060 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF );
2061 *p++ = (unsigned char)( ( TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF );
2062
2063 *p++ = 0x00;
2064 *p++ = 0x00;
2065
2066 *olen = 4;
2067}
2068#endif /* POLARSSL_SSL_EXTENDED_MASTER_SECRET */
2069
Paul Bakkera503a632013-08-14 13:48:06 +02002070#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002071static void ssl_write_session_ticket_ext( ssl_context *ssl,
2072 unsigned char *buf,
2073 size_t *olen )
2074{
2075 unsigned char *p = buf;
2076
2077 if( ssl->handshake->new_session_ticket == 0 )
2078 {
2079 *olen = 0;
2080 return;
2081 }
2082
2083 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
2084
2085 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
2086 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
2087
2088 *p++ = 0x00;
2089 *p++ = 0x00;
2090
2091 *olen = 4;
2092}
Paul Bakkera503a632013-08-14 13:48:06 +02002093#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002094
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002095static void ssl_write_renegotiation_ext( ssl_context *ssl,
2096 unsigned char *buf,
2097 size_t *olen )
2098{
2099 unsigned char *p = buf;
2100
2101 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
2102 {
2103 *olen = 0;
2104 return;
2105 }
2106
2107 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
2108
2109 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
2110 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
2111
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002112#if defined(POLARSSL_SSL_RENEGOTIATION)
2113 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
2114 {
2115 *p++ = 0x00;
2116 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
2117 *p++ = ssl->verify_data_len * 2 & 0xFF;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002118
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002119 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
2120 p += ssl->verify_data_len;
2121 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
2122 p += ssl->verify_data_len;
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002123
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002124 *olen = 5 + ssl->verify_data_len * 2;
2125 }
2126 else
2127#endif /* POLARSSL_SSL_RENEGOTIATION */
2128 {
2129 *p++ = 0x00;
2130 *p++ = 0x01;
2131 *p++ = 0x00;
2132
2133 *olen = 5;
2134 }
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002135}
2136
Paul Bakker05decb22013-08-15 13:33:48 +02002137#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002138static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
2139 unsigned char *buf,
2140 size_t *olen )
2141{
2142 unsigned char *p = buf;
2143
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02002144 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
2145 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002146 *olen = 0;
2147 return;
2148 }
2149
2150 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
2151
2152 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
2153 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
2154
2155 *p++ = 0x00;
2156 *p++ = 1;
2157
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02002158 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002159
2160 *olen = 5;
2161}
Paul Bakker05decb22013-08-15 13:33:48 +02002162#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002163
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002164#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002165static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
2166 unsigned char *buf,
2167 size_t *olen )
2168{
2169 unsigned char *p = buf;
2170 ((void) ssl);
2171
Paul Bakker677377f2013-10-28 12:54:26 +01002172 if( ( ssl->handshake->cli_exts &
2173 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
2174 {
2175 *olen = 0;
2176 return;
2177 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002178
2179 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2180
2181 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2182 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2183
2184 *p++ = 0x00;
2185 *p++ = 2;
2186
2187 *p++ = 1;
2188 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2189
2190 *olen = 6;
2191}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002192#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002193
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002194#if defined(POLARSSL_SSL_ALPN )
2195static void ssl_write_alpn_ext( ssl_context *ssl,
2196 unsigned char *buf, size_t *olen )
2197{
2198 if( ssl->alpn_chosen == NULL )
2199 {
2200 *olen = 0;
2201 return;
2202 }
2203
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002204 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002205
2206 /*
2207 * 0 . 1 ext identifier
2208 * 2 . 3 ext length
2209 * 4 . 5 protocol list length
2210 * 6 . 6 protocol name length
2211 * 7 . 7+n protocol name
2212 */
2213 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2214 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2215
2216 *olen = 7 + strlen( ssl->alpn_chosen );
2217
2218 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2219 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2220
2221 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2222 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2223
2224 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2225
2226 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2227}
2228#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2229
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002230#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002231static int ssl_write_hello_verify_request( ssl_context *ssl )
2232{
2233 int ret;
2234 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002235 unsigned char *cookie_len_byte;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002236
2237 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2238
2239 /*
2240 * struct {
2241 * ProtocolVersion server_version;
2242 * opaque cookie<0..2^8-1>;
2243 * } HelloVerifyRequest;
2244 */
2245
Manuel Pégourié-Gonnardb35fe562014-08-09 17:00:46 +02002246 /* The RFC is not clear on this point, but sending the actual negotiated
2247 * version looks like the most interoperable thing to do. */
2248 ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002249 ssl->transport, p );
2250 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2251 p += 2;
2252
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02002253 /* If we get here, f_cookie_check is not null */
2254 if( ssl->f_cookie_write == NULL )
2255 {
2256 SSL_DEBUG_MSG( 1, ( "inconsistent cookie callbacks" ) );
2257 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
2258 }
2259
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002260 /* Skip length byte until we know the length */
2261 cookie_len_byte = p++;
2262
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002263 if( ( ret = ssl->f_cookie_write( ssl->p_cookie,
2264 &p, ssl->out_buf + SSL_BUFFER_LEN,
2265 ssl->cli_id, ssl->cli_id_len ) ) != 0 )
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002266 {
Manuel Pégourié-Gonnardd485d192014-07-23 14:56:15 +02002267 SSL_DEBUG_RET( 1, "f_cookie_write", ret );
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002268 return( ret );
2269 }
2270
2271 *cookie_len_byte = (unsigned char)( p - ( cookie_len_byte + 1 ) );
2272
2273 SSL_DEBUG_BUF( 3, "cookie sent", cookie_len_byte + 1, *cookie_len_byte );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002274
2275 ssl->out_msglen = p - ssl->out_msg;
2276 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2277 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2278
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002279 ssl->state = SSL_SERVER_HELLO_VERIFY_REQUEST_SENT;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002280
2281 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2282 {
2283 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2284 return( ret );
2285 }
2286
2287 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2288
2289 return( 0 );
2290}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002291#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002292
Paul Bakker5121ce52009-01-03 21:22:43 +00002293static int ssl_write_server_hello( ssl_context *ssl )
2294{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002295#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002297#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002298 int ret;
2299 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002300 unsigned char *buf, *p;
2301
2302 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2303
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002304#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002305 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardd7f9bc52014-07-23 11:09:27 +02002306 ssl->handshake->verify_cookie_len != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002307 {
2308 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2309 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2310
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02002311 return( ssl_write_hello_verify_request( ssl ) );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002312 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002313#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002314
Paul Bakkera9a028e2013-11-21 17:31:06 +01002315 if( ssl->f_rng == NULL )
2316 {
2317 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2318 return( POLARSSL_ERR_SSL_NO_RNG );
2319 }
2320
Paul Bakker5121ce52009-01-03 21:22:43 +00002321 /*
2322 * 0 . 0 handshake type
2323 * 1 . 3 handshake length
2324 * 4 . 5 protocol version
2325 * 6 . 9 UNIX time()
2326 * 10 . 37 random bytes
2327 */
2328 buf = ssl->out_msg;
2329 p = buf + 4;
2330
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002331 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2332 ssl->transport, p );
2333 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002334
2335 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002336 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002337
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002338#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002339 t = time( NULL );
2340 *p++ = (unsigned char)( t >> 24 );
2341 *p++ = (unsigned char)( t >> 16 );
2342 *p++ = (unsigned char)( t >> 8 );
2343 *p++ = (unsigned char)( t );
2344
2345 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002346#else
2347 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2348 return( ret );
2349
2350 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002351#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002352
Paul Bakkera3d195c2011-11-27 21:07:34 +00002353 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2354 return( ret );
2355
2356 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002357
Paul Bakker48916f92012-09-16 19:57:18 +00002358 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002359
2360 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2361
2362 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002363 * Resume is 0 by default, see ssl_handshake_init().
2364 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2365 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002366 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002367 if( ssl->handshake->resume == 0 &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002368#if defined(POLARSSL_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002369 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01002370#endif
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002371 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002372 ssl->f_get_cache != NULL &&
2373 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2374 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002375 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002376 ssl->handshake->resume = 1;
2377 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002378
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002379 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 {
2381 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002382 * New session, create a new session id,
2383 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 ssl->state++;
2386
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002387#if defined(POLARSSL_HAVE_TIME)
2388 ssl->session_negotiate->start = time( NULL );
2389#endif
2390
Paul Bakkera503a632013-08-14 13:48:06 +02002391#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002392 if( ssl->handshake->new_session_ticket != 0 )
2393 {
2394 ssl->session_negotiate->length = n = 0;
2395 memset( ssl->session_negotiate->id, 0, 32 );
2396 }
2397 else
2398#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002399 {
2400 ssl->session_negotiate->length = n = 32;
2401 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002402 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002403 return( ret );
2404 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 }
2406 else
2407 {
2408 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002409 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002411 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002413
2414 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2415 {
2416 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2417 return( ret );
2418 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 }
2420
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002421 /*
2422 * 38 . 38 session id length
2423 * 39 . 38+n session id
2424 * 39+n . 40+n chosen ciphersuite
2425 * 41+n . 41+n chosen compression alg.
2426 * 42+n . 43+n extensions length
2427 * 44+n . 43+n+m extensions
2428 */
2429 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002430 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2431 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002432
2433 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2434 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2435 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002436 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002437
Paul Bakker48916f92012-09-16 19:57:18 +00002438 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2439 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2440 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002441
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002442 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2443 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002444 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002445 ssl->session_negotiate->compression ) );
2446
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002447 /*
2448 * First write extensions, then the total length
2449 */
2450 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2451 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002452
Paul Bakker05decb22013-08-15 13:33:48 +02002453#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002454 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2455 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002456#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002457
Paul Bakker1f2bc622013-08-15 13:45:55 +02002458#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002459 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2460 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002461#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002462
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01002463#if defined(POLARSSL_SSL_ENCRYPT_THEN_MAC)
2464 ssl_write_encrypt_then_mac_ext( ssl, p + 2 + ext_len, &olen );
2465 ext_len += olen;
2466#endif
2467
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02002468#if defined(POLARSSL_SSL_EXTENDED_MASTER_SECRET)
2469 ssl_write_extended_ms_ext( ssl, p + 2 + ext_len, &olen );
2470 ext_len += olen;
2471#endif
2472
Paul Bakkera503a632013-08-14 13:48:06 +02002473#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002474 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2475 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002476#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002477
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002478#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002479 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2480 ext_len += olen;
2481#endif
2482
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002483#if defined(POLARSSL_SSL_ALPN)
2484 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2485 ext_len += olen;
2486#endif
2487
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002488 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002489
Paul Bakkera7036632014-04-30 10:15:38 +02002490 if( ext_len > 0 )
2491 {
2492 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2493 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2494 p += ext_len;
2495 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002496
2497 ssl->out_msglen = p - buf;
2498 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2499 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2500
2501 ret = ssl_write_record( ssl );
2502
2503 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2504
2505 return( ret );
2506}
2507
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002508#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2509 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002510 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2511 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002512static int ssl_write_certificate_request( ssl_context *ssl )
2513{
Paul Bakkered27a042013-04-18 22:46:23 +02002514 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002515
2516 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2517
2518 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002519 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002520 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2521 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002522 {
2523 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2524 ssl->state++;
2525 return( 0 );
2526 }
2527
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002528 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002530}
2531#else
2532static int ssl_write_certificate_request( ssl_context *ssl )
2533{
2534 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2535 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002536 size_t dn_size, total_dn_size; /* excluding length bytes */
2537 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002538 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002539 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
2541 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2542
2543 ssl->state++;
2544
Paul Bakkerfbb17802013-04-17 19:10:21 +02002545 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002546 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002547 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002548 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002549 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002550 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002551 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552 return( 0 );
2553 }
2554
2555 /*
2556 * 0 . 0 handshake type
2557 * 1 . 3 handshake length
2558 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002559 * 5 .. m-1 cert types
2560 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002561 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002562 * n .. n+1 length of all DNs
2563 * n+2 .. n+3 length of DN 1
2564 * n+4 .. ... Distinguished Name #1
2565 * ... .. ... length of DN 2, etc.
2566 */
2567 buf = ssl->out_msg;
2568 p = buf + 4;
2569
2570 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002571 * Supported certificate types
2572 *
2573 * ClientCertificateType certificate_types<1..2^8-1>;
2574 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002575 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002576 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002577
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002578#if defined(POLARSSL_RSA_C)
2579 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2580#endif
2581#if defined(POLARSSL_ECDSA_C)
2582 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2583#endif
2584
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002585 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002586 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002587
Paul Bakker577e0062013-08-28 11:57:20 +02002588 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002589#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002590 /*
2591 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002592 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002593 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2594 *
2595 * struct {
2596 * HashAlgorithm hash;
2597 * SignatureAlgorithm signature;
2598 * } SignatureAndHashAlgorithm;
2599 *
2600 * enum { (255) } HashAlgorithm;
2601 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002602 */
Paul Bakker21dca692013-01-03 11:41:08 +01002603 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002604 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002605 /*
2606 * Only use current running hash algorithm that is already required
2607 * for requested ciphersuite.
2608 */
Paul Bakker926af752012-11-23 13:38:07 +01002609 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2610
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002611 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2612 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002613 {
2614 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2615 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002616
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002617 /*
2618 * Supported signature algorithms
2619 */
2620#if defined(POLARSSL_RSA_C)
2621 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2622 p[2 + sa_len++] = SSL_SIG_RSA;
2623#endif
2624#if defined(POLARSSL_ECDSA_C)
2625 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2626 p[2 + sa_len++] = SSL_SIG_ECDSA;
2627#endif
Paul Bakker926af752012-11-23 13:38:07 +01002628
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002629 p[0] = (unsigned char)( sa_len >> 8 );
2630 p[1] = (unsigned char)( sa_len );
2631 sa_len += 2;
2632 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002633 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002634#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002635
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002636 /*
2637 * DistinguishedName certificate_authorities<0..2^16-1>;
2638 * opaque DistinguishedName<1..2^16-1>;
2639 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002640 p += 2;
2641 crt = ssl->ca_chain;
2642
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002643 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002644 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002645 {
2646 if( p - buf > 4096 )
2647 break;
2648
Paul Bakker926af752012-11-23 13:38:07 +01002649 dn_size = crt->subject_raw.len;
2650 *p++ = (unsigned char)( dn_size >> 8 );
2651 *p++ = (unsigned char)( dn_size );
2652 memcpy( p, crt->subject_raw.p, dn_size );
2653 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002654
Paul Bakker926af752012-11-23 13:38:07 +01002655 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2656
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002657 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002658 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002659 }
2660
Paul Bakker926af752012-11-23 13:38:07 +01002661 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2663 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002664 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2665 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002666
2667 ret = ssl_write_record( ssl );
2668
2669 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2670
2671 return( ret );
2672}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002673#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2674 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002675 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2676 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002677
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002678#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2679 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2680static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2681{
2682 int ret;
2683
2684 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2687 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2688 }
2689
2690 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2691 pk_ec( *ssl_own_key( ssl ) ),
2692 POLARSSL_ECDH_OURS ) ) != 0 )
2693 {
2694 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2695 return( ret );
2696 }
2697
2698 return( 0 );
2699}
2700#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2701 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2702
Paul Bakker41c83d32013-03-20 14:39:14 +01002703static int ssl_write_server_key_exchange( ssl_context *ssl )
2704{
Paul Bakker23986e52011-04-24 08:57:21 +00002705 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002706 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002707 const ssl_ciphersuite_t *ciphersuite_info =
2708 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002709
2710#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2711 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2712 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002713 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002714 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002715 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002716 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002717 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002718 ((void) dig_signed);
2719 ((void) dig_signed_len);
2720#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002721
Paul Bakker5121ce52009-01-03 21:22:43 +00002722 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2723
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002724#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2725 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2726 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002727 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2728 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2729 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002730 {
2731 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2732 ssl->state++;
2733 return( 0 );
2734 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002735#endif
2736
2737#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2738 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2739 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2740 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2741 {
2742 ssl_get_ecdh_params_from_cert( ssl );
2743
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002744 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002745 ssl->state++;
2746 return( 0 );
2747 }
2748#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002749
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002750#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2751 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2752 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2753 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002754 {
2755 /* TODO: Support identity hints */
2756 *(p++) = 0x00;
2757 *(p++) = 0x00;
2758
2759 n += 2;
2760 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002761#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2762 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002763
2764#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2765 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2766 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2767 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002768 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002769 /*
2770 * Ephemeral DH parameters:
2771 *
2772 * struct {
2773 * opaque dh_p<1..2^16-1>;
2774 * opaque dh_g<1..2^16-1>;
2775 * opaque dh_Ys<1..2^16-1>;
2776 * } ServerDHParams;
2777 */
2778 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2779 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2780 {
2781 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2782 return( ret );
2783 }
Paul Bakker48916f92012-09-16 19:57:18 +00002784
Paul Bakker41c83d32013-03-20 14:39:14 +01002785 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002786 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2787 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002788 {
2789 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2790 return( ret );
2791 }
2792
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002793 dig_signed = p;
2794 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002795
2796 p += len;
2797 n += len;
2798
Paul Bakker41c83d32013-03-20 14:39:14 +01002799 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2800 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2801 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2802 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2803 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002804#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2805 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002806
Gergely Budai987bfb52014-01-19 21:48:42 +01002807#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002808 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002809 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2810 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002811 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002812 /*
2813 * Ephemeral ECDH parameters:
2814 *
2815 * struct {
2816 * ECParameters curve_params;
2817 * ECPoint public;
2818 * } ServerECDHParams;
2819 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002820 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002821#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002822 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002823
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002824 /* Match our preference list against the offered curves */
2825 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2826 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2827 if( (*curve)->grp_id == *gid )
2828 goto curve_matching_done;
2829
2830curve_matching_done:
2831#else
2832 curve = ssl->handshake->curves;
2833#endif
2834
2835 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002836 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002837 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2838 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002839 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002840
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002841 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002842
Paul Bakker41c83d32013-03-20 14:39:14 +01002843 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002844 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002845 {
2846 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2847 return( ret );
2848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002849
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002850 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2851 p, SSL_MAX_CONTENT_LEN - n,
2852 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002853 {
2854 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2855 return( ret );
2856 }
2857
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002858 dig_signed = p;
2859 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002860
2861 p += len;
2862 n += len;
2863
Paul Bakker41c83d32013-03-20 14:39:14 +01002864 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2865 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002866#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002868#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002869 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2870 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002871 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002872 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2873 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002874 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002875 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002876 unsigned int hashlen = 0;
2877 unsigned char hash[64];
2878 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002879
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002880 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002881 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2882 */
Paul Bakker577e0062013-08-28 11:57:20 +02002883#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002884 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2885 {
2886 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2887
2888 if( md_alg == POLARSSL_MD_NONE )
2889 {
2890 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002891 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002892 }
2893 }
Paul Bakker577e0062013-08-28 11:57:20 +02002894 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002895#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002896#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2897 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002898 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002899 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2900 {
2901 md_alg = POLARSSL_MD_SHA1;
2902 }
2903 else
Paul Bakker577e0062013-08-28 11:57:20 +02002904#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002905 {
2906 md_alg = POLARSSL_MD_NONE;
2907 }
2908
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002909 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002910 * Compute the hash to be signed
2911 */
Paul Bakker577e0062013-08-28 11:57:20 +02002912#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2913 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002914 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002915 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002916 md5_context md5;
2917 sha1_context sha1;
2918
Paul Bakker5b4af392014-06-26 12:09:34 +02002919 md5_init( &md5 );
2920 sha1_init( &sha1 );
2921
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002922 /*
2923 * digitally-signed struct {
2924 * opaque md5_hash[16];
2925 * opaque sha_hash[20];
2926 * };
2927 *
2928 * md5_hash
2929 * MD5(ClientHello.random + ServerHello.random
2930 * + ServerParams);
2931 * sha_hash
2932 * SHA(ClientHello.random + ServerHello.random
2933 * + ServerParams);
2934 */
2935 md5_starts( &md5 );
2936 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002937 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002938 md5_finish( &md5, hash );
2939
2940 sha1_starts( &sha1 );
2941 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002942 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002943 sha1_finish( &sha1, hash + 16 );
2944
2945 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002946
2947 md5_free( &md5 );
2948 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002949 }
2950 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002951#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2952 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002953#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2954 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002955 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002956 {
2957 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002958 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002959
Paul Bakker84bbeb52014-07-01 14:53:22 +02002960 md_init( &ctx );
2961
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002962 /* Info from md_alg will be used instead */
2963 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964
2965 /*
2966 * digitally-signed struct {
2967 * opaque client_random[32];
2968 * opaque server_random[32];
2969 * ServerDHParams params;
2970 * };
2971 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002972 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002973 {
2974 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2975 return( ret );
2976 }
2977
2978 md_starts( &ctx );
2979 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002980 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002981 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002982 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002983 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002984 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002985#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2986 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002987 {
2988 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002989 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002990 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002991
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002992 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2993 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002994
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002995 /*
2996 * Make the signature
2997 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002998 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002999 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003000 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
3001 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003002 }
Paul Bakker23f36802012-09-28 14:15:14 +00003003
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003004#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00003005 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
3006 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003007 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003008 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003009
3010 n += 2;
3011 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003012#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003013
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003014 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003015 p + 2 , &signature_len,
3016 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003017 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02003018 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02003019 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00003020 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02003021
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003022 *(p++) = (unsigned char)( signature_len >> 8 );
3023 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00003024 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003025
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003026 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00003027
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003028 p += signature_len;
3029 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00003030 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003031#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003032 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
3033 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00003034
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003035 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00003036 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3037 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
3038
3039 ssl->state++;
3040
3041 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3042 {
3043 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3044 return( ret );
3045 }
3046
3047 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
3048
3049 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00003050}
3051
3052static int ssl_write_server_hello_done( ssl_context *ssl )
3053{
3054 int ret;
3055
3056 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
3057
3058 ssl->out_msglen = 4;
3059 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3060 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
3061
3062 ssl->state++;
3063
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003064#if defined(POLARSSL_SSL_PROTO_DTLS)
3065 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
3066 ssl_send_flight_completed( ssl );
3067#endif
3068
Paul Bakker5121ce52009-01-03 21:22:43 +00003069 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3070 {
3071 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3072 return( ret );
3073 }
3074
3075 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
3076
3077 return( 0 );
3078}
3079
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003080#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
3081 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3082static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
3083 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003084{
3085 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003086 size_t n;
3087
3088 /*
3089 * Receive G^Y mod P, premaster = (G^Y)^X mod P
3090 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003091 if( *p + 2 > end )
3092 {
3093 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3094 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3095 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02003096
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003097 n = ( (*p)[0] << 8 ) | (*p)[1];
3098 *p += 2;
3099
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003100 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003101 {
3102 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3103 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3104 }
3105
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003106 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003107 {
3108 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
3109 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3110 }
3111
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003112 *p += n;
3113
Paul Bakker70df2fb2013-04-17 17:19:09 +02003114 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
3115
Paul Bakker70df2fb2013-04-17 17:19:09 +02003116 return( ret );
3117}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003118#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
3119 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003120
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003121#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
3122 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003123static int ssl_parse_encrypted_pms( ssl_context *ssl,
3124 const unsigned char *p,
3125 const unsigned char *end,
3126 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003127{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003128 int ret;
3129 size_t len = pk_get_len( ssl_own_key( ssl ) );
3130 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003131 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02003132
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02003133 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003134 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02003135 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003136 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3137 }
3138
3139 /*
3140 * Decrypt the premaster using own private RSA key
3141 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003142#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
3143 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02003144 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
3145 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003146 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
3147 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003148 {
3149 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3150 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3151 }
3152 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003153#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02003154
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003155 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003156 {
3157 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3158 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3159 }
3160
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003161 ssl_write_version( ssl->handshake->max_major_ver,
3162 ssl->handshake->max_minor_ver,
3163 ssl->transport, ver );
3164
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003165 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
3166 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01003167 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02003168 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003169
3170 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01003171 pms[0] != ver[0] ||
3172 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003173 {
3174 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3175
3176 /*
3177 * Protection against Bleichenbacher's attack:
3178 * invalid PKCS#1 v1.5 padding must not cause
3179 * the connection to end immediately; instead,
3180 * send a bad_record_mac later in the handshake.
3181 */
3182 ssl->handshake->pmslen = 48;
3183
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003184 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003185 if( ret != 0 )
3186 return( ret );
3187 }
3188
3189 return( ret );
3190}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02003191#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
3192 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02003193
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003194#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003195static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
3196 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003197{
Paul Bakker6db455e2013-09-18 17:29:31 +02003198 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003199 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003200
Paul Bakker6db455e2013-09-18 17:29:31 +02003201 if( ssl->f_psk == NULL &&
3202 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
3203 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003204 {
3205 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
3206 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3207 }
3208
3209 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003210 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003211 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003212 if( *p + 2 > end )
3213 {
3214 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3215 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3216 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003217
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003218 n = ( (*p)[0] << 8 ) | (*p)[1];
3219 *p += 2;
3220
3221 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003222 {
3223 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3225 }
3226
Paul Bakker6db455e2013-09-18 17:29:31 +02003227 if( ssl->f_psk != NULL )
3228 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003229 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003230 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3231 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003232 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003233 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003234 /* Identity is not a big secret since clients send it in the clear,
3235 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003236 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003237 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003238 {
3239 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3240 }
3241 }
3242
3243 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003244 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003245 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003246 if( ( ret = ssl_send_alert_message( ssl,
3247 SSL_ALERT_LEVEL_FATAL,
3248 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3249 {
3250 return( ret );
3251 }
3252
3253 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003254 }
3255
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003256 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003257
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003258 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003259}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003260#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003261
Paul Bakker5121ce52009-01-03 21:22:43 +00003262static int ssl_parse_client_key_exchange( ssl_context *ssl )
3263{
Paul Bakker23986e52011-04-24 08:57:21 +00003264 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003265 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003266 unsigned char *p, *end;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003267
Paul Bakker41c83d32013-03-20 14:39:14 +01003268 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003269
3270 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3271
3272 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3273 {
3274 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3275 return( ret );
3276 }
3277
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003278 p = ssl->in_msg + ssl_hs_hdr_len( ssl );
3279 end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnardf8995832014-09-10 08:25:12 +00003280
Paul Bakker5121ce52009-01-03 21:22:43 +00003281 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3282 {
3283 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003284 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003285 }
3286
3287 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3288 {
3289 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003290 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 }
3292
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003293#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003294 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003296 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003298 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3299 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003300 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003301
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003302 if( p != end )
3303 {
3304 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3305 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3306 }
3307
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003308 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003309
3310 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3311 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003312 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003313 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003314 {
3315 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3316 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3317 }
3318
3319 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003320 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003321 else
3322#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003323#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003324 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3325 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3326 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003327 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003328 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3329 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3330 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003331 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003332 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003333 p, end - p) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003334 {
3335 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3336 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3337 }
3338
3339 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3340
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003341 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3342 &ssl->handshake->pmslen,
3343 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003344 POLARSSL_MPI_MAX_SIZE,
3345 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003346 {
3347 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3348 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3349 }
3350
3351 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003352 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003353 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003354#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003355 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3356 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3357 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003358#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3359 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003360 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003361 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003362 {
3363 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3364 return( ret );
3365 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003366
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003367 if( p != end )
3368 {
3369 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3370 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3371 }
3372
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003373 if( ( ret = ssl_psk_derive_premaster( ssl,
3374 ciphersuite_info->key_exchange ) ) != 0 )
3375 {
3376 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3377 return( ret );
3378 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003380 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003381#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003382#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3383 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3384 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003385 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3386 {
3387 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3388 return( ret );
3389 }
3390
3391 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3392 {
3393 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3394 return( ret );
3395 }
3396
3397 if( ( ret = ssl_psk_derive_premaster( ssl,
3398 ciphersuite_info->key_exchange ) ) != 0 )
3399 {
3400 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3401 return( ret );
3402 }
3403 }
3404 else
3405#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003406#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3407 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3408 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003409 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3410 {
3411 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3412 return( ret );
3413 }
3414 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3415 {
3416 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3417 return( ret );
3418 }
3419
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003420 if( p != end )
3421 {
3422 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3423 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3424 }
3425
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003426 if( ( ret = ssl_psk_derive_premaster( ssl,
3427 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003428 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003429 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3430 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003431 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003432 }
3433 else
3434#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003435#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3436 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3437 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003438 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3439 {
3440 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3441 return( ret );
3442 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003443
3444 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3445 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003446 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003447 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3448 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003449 }
3450
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003451 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3452
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003453 if( ( ret = ssl_psk_derive_premaster( ssl,
3454 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003455 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003456 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003457 return( ret );
3458 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003459 }
3460 else
3461#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003462#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3463 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003464 {
Manuel Pégourié-Gonnard2114d722014-09-10 13:59:41 +00003465 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003466 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003467 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003468 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003469 }
3470 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003471 else
3472#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3473 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003474 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003475 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003476 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003477
Paul Bakkerff60ee62010-03-16 21:09:09 +00003478 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3479 {
3480 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3481 return( ret );
3482 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003483
Paul Bakker5121ce52009-01-03 21:22:43 +00003484 ssl->state++;
3485
3486 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3487
3488 return( 0 );
3489}
3490
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003491#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3492 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003493 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3494 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003495static int ssl_parse_certificate_verify( ssl_context *ssl )
3496{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003497 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003498
3499 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3500
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003501 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003502 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003503 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003504 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003505 {
3506 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3507 ssl->state++;
3508 return( 0 );
3509 }
3510
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003511 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3512 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003513}
3514#else
3515static int ssl_parse_certificate_verify( ssl_context *ssl )
3516{
3517 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003518 size_t i, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003519 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003520 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003521 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003522#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003523 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003524#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003525 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003526 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3527
3528 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3529
3530 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003531 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003532 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003533 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
3534 ssl->session_negotiate->peer_cert == NULL )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003535 {
3536 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3537 ssl->state++;
3538 return( 0 );
3539 }
3540
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003541 /* Needs to be done before read_record() to exclude current message */
Paul Bakker48916f92012-09-16 19:57:18 +00003542 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
3544 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3545 {
3546 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3547 return( ret );
3548 }
3549
3550 ssl->state++;
3551
Manuel Pégourié-Gonnard72226212014-09-10 14:23:38 +00003552 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE ||
3553 ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00003554 {
3555 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003556 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 }
3558
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003559 i = ssl_hs_hdr_len( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003560
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003561 /*
3562 * struct {
3563 * SignatureAndHashAlgorithm algorithm; -- TLS 1.2 only
3564 * opaque signature<0..2^16-1>;
3565 * } DigitallySigned;
3566 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003567#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3568 defined(POLARSSL_SSL_PROTO_TLS1_1)
3569 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003570 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02003571 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003572 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003573
3574 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3575 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3576 POLARSSL_PK_ECDSA ) )
3577 {
3578 hash_start += 16;
3579 hashlen -= 16;
3580 md_alg = POLARSSL_MD_SHA1;
3581 }
Paul Bakker926af752012-11-23 13:38:07 +01003582 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003583 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003584#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3585 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003586#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3587 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003588 {
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003589 if( i + 2 > ssl->in_hslen )
3590 {
3591 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3592 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3593 }
3594
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003596 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003597 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003598 if( ssl->in_msg[i] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003599 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003600 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3601 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003602 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3603 }
3604
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003605 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003606
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003607 /* Info from md_alg will be used instead */
3608 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003609
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003610 i++;
3611
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003612 /*
3613 * Signature
3614 */
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003615 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[i] ) )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003616 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003617 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003618 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3619 " for verify message" ) );
3620 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003621 }
3622
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003623 /*
3624 * Check the certificate's key type matches the signature alg
3625 */
3626 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3627 {
3628 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3629 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3630 }
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003631
3632 i++;
Paul Bakker577e0062013-08-28 11:57:20 +02003633 }
3634 else
3635#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3636 {
3637 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003638 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003639 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003640
Manuel Pégourié-Gonnard5ee96542014-09-10 14:27:21 +00003641 if( i + 2 > ssl->in_hslen )
3642 {
3643 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
3644 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3645 }
3646
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003647 sig_len = ( ssl->in_msg[i] << 8 ) | ssl->in_msg[i+1];
3648 i += 2;
Paul Bakker926af752012-11-23 13:38:07 +01003649
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003650 if( i + sig_len != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003651 {
3652 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003653 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003654 }
3655
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003656 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003657 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnard4528f3f2014-09-10 14:17:23 +00003658 ssl->in_msg + i, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003659 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003660 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003661 return( ret );
3662 }
3663
3664 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3665
Paul Bakkered27a042013-04-18 22:46:23 +02003666 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003667}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003668#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3669 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3670 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003671
Paul Bakkera503a632013-08-14 13:48:06 +02003672#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003673static int ssl_write_new_session_ticket( ssl_context *ssl )
3674{
3675 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003676 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003677 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003678
3679 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3680
3681 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3682 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3683
3684 /*
3685 * struct {
3686 * uint32 ticket_lifetime_hint;
3687 * opaque ticket<0..2^16-1>;
3688 * } NewSessionTicket;
3689 *
3690 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3691 * 8 . 9 ticket_len (n)
3692 * 10 . 9+n ticket content
3693 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003694
3695 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3696 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3697 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3698 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003699
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003700 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3701 {
3702 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3703 tlen = 0;
3704 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003705
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003706 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3707 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003708
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003709 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003710
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003711 /*
3712 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3713 * ChangeCipherSpec share the same state.
3714 */
3715 ssl->handshake->new_session_ticket = 0;
3716
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003717 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3718 {
3719 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3720 return( ret );
3721 }
3722
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003723 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3724
3725 return( 0 );
3726}
Paul Bakkera503a632013-08-14 13:48:06 +02003727#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003728
Paul Bakker5121ce52009-01-03 21:22:43 +00003729/*
Paul Bakker1961b702013-01-25 14:49:24 +01003730 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003731 */
Paul Bakker1961b702013-01-25 14:49:24 +01003732int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003733{
3734 int ret = 0;
3735
Paul Bakker1961b702013-01-25 14:49:24 +01003736 if( ssl->state == SSL_HANDSHAKE_OVER )
3737 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003738
Paul Bakker1961b702013-01-25 14:49:24 +01003739 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3740
3741 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3742 return( ret );
3743
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003744#if defined(POLARSSL_SSL_PROTO_DTLS)
3745 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
3746 ssl->handshake != NULL &&
3747 ssl->handshake->retransmit_state == SSL_RETRANS_SENDING )
3748 {
3749 if( ( ret = ssl_resend( ssl ) ) != 0 )
3750 return( ret );
3751 }
3752#endif
3753
Paul Bakker1961b702013-01-25 14:49:24 +01003754 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003755 {
Paul Bakker1961b702013-01-25 14:49:24 +01003756 case SSL_HELLO_REQUEST:
3757 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003758 break;
3759
Paul Bakker1961b702013-01-25 14:49:24 +01003760 /*
3761 * <== ClientHello
3762 */
3763 case SSL_CLIENT_HELLO:
3764 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003765 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003766
Manuel Pégourié-Gonnard579950c2014-09-29 17:47:33 +02003767#if defined(POLARSSL_SSL_PROTO_DTLS)
3768 case SSL_SERVER_HELLO_VERIFY_REQUEST_SENT:
3769 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
3770#endif
3771
Paul Bakker1961b702013-01-25 14:49:24 +01003772 /*
3773 * ==> ServerHello
3774 * Certificate
3775 * ( ServerKeyExchange )
3776 * ( CertificateRequest )
3777 * ServerHelloDone
3778 */
3779 case SSL_SERVER_HELLO:
3780 ret = ssl_write_server_hello( ssl );
3781 break;
3782
3783 case SSL_SERVER_CERTIFICATE:
3784 ret = ssl_write_certificate( ssl );
3785 break;
3786
3787 case SSL_SERVER_KEY_EXCHANGE:
3788 ret = ssl_write_server_key_exchange( ssl );
3789 break;
3790
3791 case SSL_CERTIFICATE_REQUEST:
3792 ret = ssl_write_certificate_request( ssl );
3793 break;
3794
3795 case SSL_SERVER_HELLO_DONE:
3796 ret = ssl_write_server_hello_done( ssl );
3797 break;
3798
3799 /*
3800 * <== ( Certificate/Alert )
3801 * ClientKeyExchange
3802 * ( CertificateVerify )
3803 * ChangeCipherSpec
3804 * Finished
3805 */
3806 case SSL_CLIENT_CERTIFICATE:
3807 ret = ssl_parse_certificate( ssl );
3808 break;
3809
3810 case SSL_CLIENT_KEY_EXCHANGE:
3811 ret = ssl_parse_client_key_exchange( ssl );
3812 break;
3813
3814 case SSL_CERTIFICATE_VERIFY:
3815 ret = ssl_parse_certificate_verify( ssl );
3816 break;
3817
3818 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3819 ret = ssl_parse_change_cipher_spec( ssl );
3820 break;
3821
3822 case SSL_CLIENT_FINISHED:
3823 ret = ssl_parse_finished( ssl );
3824 break;
3825
3826 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003827 * ==> ( NewSessionTicket )
3828 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003829 * Finished
3830 */
3831 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003832#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003833 if( ssl->handshake->new_session_ticket != 0 )
3834 ret = ssl_write_new_session_ticket( ssl );
3835 else
Paul Bakkera503a632013-08-14 13:48:06 +02003836#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003837 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003838 break;
3839
3840 case SSL_SERVER_FINISHED:
3841 ret = ssl_write_finished( ssl );
3842 break;
3843
3844 case SSL_FLUSH_BUFFERS:
3845 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3846 ssl->state = SSL_HANDSHAKE_WRAPUP;
3847 break;
3848
3849 case SSL_HANDSHAKE_WRAPUP:
3850 ssl_handshake_wrapup( ssl );
3851 break;
3852
3853 default:
3854 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3855 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003856 }
3857
Paul Bakker5121ce52009-01-03 21:22:43 +00003858 return( ret );
3859}
Paul Bakker9af723c2014-05-01 13:03:14 +02003860#endif /* POLARSSL_SSL_SRV_C */