blob: 2b06f7e8a42b2d87146b2f6952bf659660e25397 [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é-Gonnard82202f02014-07-23 00:28:58 +0200372#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +0200373
Paul Bakker0be444a2013-08-27 21:55:01 +0200374#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200375/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200376 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +0100377 * making it act on ssl->handshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200378 */
379static int ssl_sni_wrapper( ssl_context *ssl,
380 const unsigned char* name, size_t len )
381{
382 int ret;
383 ssl_key_cert *key_cert_ori = ssl->key_cert;
384
385 ssl->key_cert = NULL;
386 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200387 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200388
389 ssl->key_cert = key_cert_ori;
390
391 return( ret );
392}
393
Paul Bakker5701cdc2012-09-27 21:49:42 +0000394static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000395 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000396 size_t len )
397{
398 int ret;
399 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000400 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000401
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100402 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
403
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
405 if( servername_list_size + 2 != len )
406 {
407 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
408 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
409 }
410
411 p = buf + 2;
412 while( servername_list_size > 0 )
413 {
414 hostname_len = ( ( p[1] << 8 ) | p[2] );
415 if( hostname_len + 3 > servername_list_size )
416 {
417 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
418 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
419 }
420
421 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
422 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200423 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000424 if( ret != 0 )
425 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100426 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
428 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
429 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
430 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000431 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000432 }
433
434 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000435 p += hostname_len + 3;
436 }
437
438 if( servername_list_size != 0 )
439 {
440 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
441 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000442 }
443
444 return( 0 );
445}
Paul Bakker0be444a2013-08-27 21:55:01 +0200446#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000447
Paul Bakker48916f92012-09-16 19:57:18 +0000448static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000449 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000450 size_t len )
451{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000452 int ret;
453
Paul Bakker48916f92012-09-16 19:57:18 +0000454 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
455 {
456 if( len != 1 || buf[0] != 0x0 )
457 {
458 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000459
460 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
461 return( ret );
462
Paul Bakker48916f92012-09-16 19:57:18 +0000463 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
464 }
465
466 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
467 }
468 else
469 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100470 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000471 if( len != 1 + ssl->verify_data_len ||
472 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100473 safer_memcmp( buf + 1, ssl->peer_verify_data,
474 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000475 {
476 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000477
478 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
479 return( ret );
480
Paul Bakker48916f92012-09-16 19:57:18 +0000481 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
482 }
483 }
484
485 return( 0 );
486}
487
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200488#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000489static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
490 const unsigned char *buf,
491 size_t len )
492{
493 size_t sig_alg_list_size;
494 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200495 const unsigned char *end = buf + len;
496 const int *md_cur;
497
Paul Bakker23f36802012-09-28 14:15:14 +0000498
499 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
500 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200501 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000502 {
503 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
504 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
505 }
506
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200507 /*
508 * For now, ignore the SignatureAlgorithm part and rely on offered
509 * ciphersuites only for that part. To be fixed later.
510 *
511 * So, just look at the HashAlgorithm part.
512 */
513 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
514 for( p = buf + 2; p < end; p += 2 ) {
515 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
516 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200517 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200518 }
Paul Bakker23f36802012-09-28 14:15:14 +0000519 }
Paul Bakker23f36802012-09-28 14:15:14 +0000520 }
521
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200522 /* Some key echanges do not need signatures at all */
523 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
524 return( 0 );
525
526have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000527 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
528 ssl->handshake->sig_alg ) );
529
530 return( 0 );
531}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200532#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000533
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200534#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200535static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
536 const unsigned char *buf,
537 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100538{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100540 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200541 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100542
543 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
544 if( list_size + 2 != len ||
545 list_size % 2 != 0 )
546 {
547 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
548 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
549 }
550
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200551 /* Should never happen unless client duplicates the extension */
552 if( ssl->handshake->curves != NULL )
553 {
554 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
555 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
556 }
557
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100558 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200559 * and leave room for a final 0 */
560 our_size = list_size / 2 + 1;
561 if( our_size > POLARSSL_ECP_DP_MAX )
562 our_size = POLARSSL_ECP_DP_MAX;
563
564 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
565 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
566
Paul Bakker9af723c2014-05-01 13:03:14 +0200567 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200568 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200569 ssl->handshake->curves = curves;
570
Paul Bakker41c83d32013-03-20 14:39:14 +0100571 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200572 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100573 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200574 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200575
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200576 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100577 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200578 *curves++ = curve_info;
579 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100580 }
581
582 list_size -= 2;
583 p += 2;
584 }
585
586 return( 0 );
587}
588
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200589static int ssl_parse_supported_point_formats( ssl_context *ssl,
590 const unsigned char *buf,
591 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100592{
593 size_t list_size;
594 const unsigned char *p;
595
596 list_size = buf[0];
597 if( list_size + 1 != len )
598 {
599 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
600 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
601 }
602
603 p = buf + 2;
604 while( list_size > 0 )
605 {
606 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
607 p[0] == POLARSSL_ECP_PF_COMPRESSED )
608 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200609 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200610 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100611 return( 0 );
612 }
613
614 list_size--;
615 p++;
616 }
617
618 return( 0 );
619}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200620#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100621
Paul Bakker05decb22013-08-15 13:33:48 +0200622#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200623static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
624 const unsigned char *buf,
625 size_t len )
626{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200627 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200628 {
629 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
630 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
631 }
632
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200633 ssl->session_negotiate->mfl_code = buf[0];
634
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200635 return( 0 );
636}
Paul Bakker05decb22013-08-15 13:33:48 +0200637#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200638
Paul Bakker1f2bc622013-08-15 13:45:55 +0200639#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200640static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
641 const unsigned char *buf,
642 size_t len )
643{
644 if( len != 0 )
645 {
646 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
647 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
648 }
649
650 ((void) buf);
651
652 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
653
654 return( 0 );
655}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200656#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200657
Paul Bakkera503a632013-08-14 13:48:06 +0200658#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200659static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200660 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200661 size_t len )
662{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200663 int ret;
664
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200665 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
666 return( 0 );
667
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200668 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200669 ssl->handshake->new_session_ticket = 1;
670
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200671 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
672
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200673 if( len == 0 )
674 return( 0 );
675
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200676 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
677 {
678 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
679 return( 0 );
680 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200681
682 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200683 * Failures are ok: just ignore the ticket and proceed.
684 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200685 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
686 {
687 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200688 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200689 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200690
691 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
692
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200693 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200694
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200695 /* Don't send a new ticket after all, this one is OK */
696 ssl->handshake->new_session_ticket = 0;
697
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200698 return( 0 );
699}
Paul Bakkera503a632013-08-14 13:48:06 +0200700#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200701
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200702#if defined(POLARSSL_SSL_ALPN)
703static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200704 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200705{
Paul Bakker14b16c62014-05-28 11:33:54 +0200706 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200707 const unsigned char *theirs, *start, *end;
708 const char **ours;
709
710 /* If ALPN not configured, just ignore the extension */
711 if( ssl->alpn_list == NULL )
712 return( 0 );
713
714 /*
715 * opaque ProtocolName<1..2^8-1>;
716 *
717 * struct {
718 * ProtocolName protocol_name_list<2..2^16-1>
719 * } ProtocolNameList;
720 */
721
722 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
723 if( len < 4 )
724 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
725
726 list_len = ( buf[0] << 8 ) | buf[1];
727 if( list_len != len - 2 )
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729
730 /*
731 * Use our order of preference
732 */
733 start = buf + 2;
734 end = buf + len;
735 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
736 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200737 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200738 for( theirs = start; theirs != end; theirs += cur_len )
739 {
740 /* If the list is well formed, we should get equality first */
741 if( theirs > end )
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743
744 cur_len = *theirs++;
745
746 /* Empty strings MUST NOT be included */
747 if( cur_len == 0 )
748 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
749
Paul Bakker14b16c62014-05-28 11:33:54 +0200750 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200751 memcmp( theirs, *ours, cur_len ) == 0 )
752 {
753 ssl->alpn_chosen = *ours;
754 return( 0 );
755 }
756 }
757 }
758
759 /* If we get there, no match was found */
760 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
761 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
762 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
763}
764#endif /* POLARSSL_SSL_ALPN */
765
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100766/*
767 * Auxiliary functions for ServerHello parsing and related actions
768 */
769
770#if defined(POLARSSL_X509_CRT_PARSE_C)
771/*
772 * Return 1 if the given EC key uses the given curve, 0 otherwise
773 */
774#if defined(POLARSSL_ECDSA_C)
775static int ssl_key_matches_curves( pk_context *pk,
776 const ecp_curve_info **curves )
777{
778 const ecp_curve_info **crv = curves;
779 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
780
781 while( *crv != NULL )
782 {
783 if( (*crv)->grp_id == grp_id )
784 return( 1 );
785 crv++;
786 }
787
788 return( 0 );
789}
790#endif /* POLARSSL_ECDSA_C */
791
792/*
793 * Try picking a certificate for this ciphersuite,
794 * return 0 on success and -1 on failure.
795 */
796static int ssl_pick_cert( ssl_context *ssl,
797 const ssl_ciphersuite_t * ciphersuite_info )
798{
799 ssl_key_cert *cur, *list;
800 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
801
802#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
803 if( ssl->handshake->sni_key_cert != NULL )
804 list = ssl->handshake->sni_key_cert;
805 else
806#endif
807 list = ssl->handshake->key_cert;
808
809 if( pk_alg == POLARSSL_PK_NONE )
810 return( 0 );
811
812 for( cur = list; cur != NULL; cur = cur->next )
813 {
814 if( ! pk_can_do( cur->key, pk_alg ) )
815 continue;
816
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200817 /*
818 * This avoids sending the client a cert it'll reject based on
819 * keyUsage or other extensions.
820 *
821 * It also allows the user to provision different certificates for
822 * different uses based on keyUsage, eg if they want to avoid signing
823 * and decrypting with the same RSA key.
824 */
825 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
826 SSL_IS_SERVER ) != 0 )
827 {
828 continue;
829 }
830
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100831#if defined(POLARSSL_ECDSA_C)
832 if( pk_alg == POLARSSL_PK_ECDSA )
833 {
834 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
835 break;
836 }
837 else
838#endif
839 break;
840 }
841
842 if( cur == NULL )
843 return( -1 );
844
845 ssl->handshake->key_cert = cur;
846 return( 0 );
847}
848#endif /* POLARSSL_X509_CRT_PARSE_C */
849
850/*
851 * Check if a given ciphersuite is suitable for use with our config/keys/etc
852 * Sets ciphersuite_info only if the suite matches.
853 */
854static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
855 const ssl_ciphersuite_t **ciphersuite_info )
856{
857 const ssl_ciphersuite_t *suite_info;
858
859 suite_info = ssl_ciphersuite_from_id( suite_id );
860 if( suite_info == NULL )
861 {
862 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
863 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
864 }
865
866 if( suite_info->min_minor_ver > ssl->minor_ver ||
867 suite_info->max_minor_ver < ssl->minor_ver )
868 return( 0 );
869
Manuel Pégourié-Gonnardd6664512014-02-06 13:26:57 +0100870#if defined(POLARSSL_SSL_PROTO_DTLS)
871 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
872 ( suite_info->flags & POLARSSL_CIPHERSUITE_NODTLS ) )
873 return( 0 );
874#endif
875
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100876#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
877 if( ssl_ciphersuite_uses_ec( suite_info ) &&
878 ( ssl->handshake->curves == NULL ||
879 ssl->handshake->curves[0] == NULL ) )
880 return( 0 );
881#endif
882
883#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
884 /* If the ciphersuite requires a pre-shared key and we don't
885 * have one, skip it now rather than failing later */
886 if( ssl_ciphersuite_uses_psk( suite_info ) &&
887 ssl->f_psk == NULL &&
888 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
889 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
890 return( 0 );
891#endif
892
893#if defined(POLARSSL_X509_CRT_PARSE_C)
894 /*
895 * Final check: if ciphersuite requires us to have a
896 * certificate/key of a particular type:
897 * - select the appropriate certificate if we have one, or
898 * - try the next ciphersuite if we don't
899 * This must be done last since we modify the key_cert list.
900 */
901 if( ssl_pick_cert( ssl, suite_info ) != 0 )
902 return( 0 );
903#endif
904
905 *ciphersuite_info = suite_info;
906 return( 0 );
907}
908
Paul Bakker78a8c712013-03-06 17:01:52 +0100909#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
910static int ssl_parse_client_hello_v2( ssl_context *ssl )
911{
912 int ret;
913 unsigned int i, j;
914 size_t n;
915 unsigned int ciph_len, sess_len, chal_len;
916 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200917 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200918 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100919
920 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
921
922 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
923 {
924 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
925
926 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
927 return( ret );
928
929 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
930 }
931
932 buf = ssl->in_hdr;
933
934 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
935
936 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
937 buf[2] ) );
938 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
939 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
940 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
941 buf[3], buf[4] ) );
942
943 /*
944 * SSLv2 Client Hello
945 *
946 * Record layer:
947 * 0 . 1 message length
948 *
949 * SSL layer:
950 * 2 . 2 message type
951 * 3 . 4 protocol version
952 */
953 if( buf[2] != SSL_HS_CLIENT_HELLO ||
954 buf[3] != SSL_MAJOR_VERSION_3 )
955 {
956 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
957 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
958 }
959
960 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
961
962 if( n < 17 || n > 512 )
963 {
964 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
965 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
966 }
967
968 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200969 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
970 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100971
972 if( ssl->minor_ver < ssl->min_minor_ver )
973 {
974 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200975 " [%d:%d] < [%d:%d]",
976 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100977 ssl->min_major_ver, ssl->min_minor_ver ) );
978
979 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
980 SSL_ALERT_MSG_PROTOCOL_VERSION );
981 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
982 }
983
Paul Bakker2fbefde2013-06-29 16:01:15 +0200984 ssl->handshake->max_major_ver = buf[3];
985 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100986
987 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
988 {
989 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
990 return( ret );
991 }
992
993 ssl->handshake->update_checksum( ssl, buf + 2, n );
994
995 buf = ssl->in_msg;
996 n = ssl->in_left - 5;
997
998 /*
999 * 0 . 1 ciphersuitelist length
1000 * 2 . 3 session id length
1001 * 4 . 5 challenge length
1002 * 6 . .. ciphersuitelist
1003 * .. . .. session id
1004 * .. . .. challenge
1005 */
1006 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1007
1008 ciph_len = ( buf[0] << 8 ) | buf[1];
1009 sess_len = ( buf[2] << 8 ) | buf[3];
1010 chal_len = ( buf[4] << 8 ) | buf[5];
1011
1012 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
1013 ciph_len, sess_len, chal_len ) );
1014
1015 /*
1016 * Make sure each parameter length is valid
1017 */
1018 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
1019 {
1020 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1021 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1022 }
1023
1024 if( sess_len > 32 )
1025 {
1026 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1027 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1028 }
1029
1030 if( chal_len < 8 || chal_len > 32 )
1031 {
1032 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1033 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1034 }
1035
1036 if( n != 6 + ciph_len + sess_len + chal_len )
1037 {
1038 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1039 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1040 }
1041
1042 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1043 buf + 6, ciph_len );
1044 SSL_DEBUG_BUF( 3, "client hello, session id",
1045 buf + 6 + ciph_len, sess_len );
1046 SSL_DEBUG_BUF( 3, "client hello, challenge",
1047 buf + 6 + ciph_len + sess_len, chal_len );
1048
1049 p = buf + 6 + ciph_len;
1050 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001051 memset( ssl->session_negotiate->id, 0,
1052 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001053 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1054
1055 p += sess_len;
1056 memset( ssl->handshake->randbytes, 0, 64 );
1057 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1058
1059 /*
1060 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1061 */
1062 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1063 {
1064 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1065 {
1066 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1067 if( ssl->renegotiation == SSL_RENEGOTIATION )
1068 {
1069 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1070
1071 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1072 return( ret );
1073
1074 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1075 }
1076 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1077 break;
1078 }
1079 }
1080
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001081 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001082 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001083#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1084 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1085 {
1086 for( i = 0; ciphersuites[i] != 0; i++ )
1087#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001088 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001089 {
1090 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001091#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001092 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001093 if( p[0] != 0 ||
1094 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1095 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1096 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001097
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001098 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1099 &ciphersuite_info ) ) != 0 )
1100 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001101
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001102 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001103 goto have_ciphersuite_v2;
1104 }
1105 }
1106
1107 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1108
1109 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1110
1111have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001112 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001113 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001114 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001115
1116 /*
1117 * SSLv2 Client Hello relevant renegotiation security checks
1118 */
1119 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1120 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1121 {
1122 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1123
1124 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1125 return( ret );
1126
1127 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1128 }
1129
1130 ssl->in_left = 0;
1131 ssl->state++;
1132
1133 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1134
1135 return( 0 );
1136}
1137#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1138
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001139#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001140
1141/*
1142 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
1143 * available. Try SHA-256 first, 512 wastes resources since we need to stay
1144 * with max 32 bytes of cookie for DTLS 1.0
1145 */
1146#if defined(POLARSSL_SHA256_C)
1147#define HVR_MD POLARSSL_MD_SHA256
1148#define HVR_MD_LEN 32
1149#define HVR_MD_USE 32
1150#elif defined(POLARSSL_SHA512_C)
1151#define HVR_MD POLARSSL_MD_SHA384
1152#define HVR_MD_LEN 48
1153#define HVR_MD_USE 32
1154#elif defined(POLARSSL_SHA1_C)
1155#define HVR_MD POLARSSL_MD_SHA1
1156#define HVR_MD_LEN 20
1157#define HVR_MD_USE 20
1158#else
1159#error "DTLS hello verify needs SHA-1 or SHA-2"
1160#endif
1161
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001162/*
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001163 * Generate server key for HelloVerifyRequest
1164 */
1165int ssl_setup_hvr_key( ssl_context *ssl )
1166{
1167 int ret;
1168 unsigned char key[HVR_MD_LEN];
1169
1170 if( ( ret = ssl->f_rng( ssl->p_rng, key, sizeof( key ) ) ) != 0 )
1171 return( ret );
1172
1173 ret = md_init_ctx( &ssl->hvr_hmac_ctx, md_info_from_type( HVR_MD ) );
1174 if( ret != 0 )
1175 return( ret );
1176
1177 ret = md_hmac_starts( &ssl->hvr_hmac_ctx, key, sizeof( key ) );
1178 if( ret != 0 )
1179 return( ret );
1180
1181 polarssl_zeroize( key, sizeof( key ) );
1182
1183 return( 0 );
1184}
1185
1186/*
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001187 * Generate cookie for DTLS ClientHello verification
1188 */
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001189static int ssl_cookie_generate( ssl_context *ssl )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001190{
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001191 int ret;
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001192 unsigned char *cookie = ssl->handshake->verify_cookie;
1193 unsigned char cookie_len;
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001194 unsigned char hmac_out[HVR_MD_LEN];
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001195
1196 polarssl_free( cookie );
1197
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001198 cookie_len = HVR_MD_LEN;
1199
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001200 if( ( cookie = polarssl_malloc( cookie_len ) ) == NULL )
1201 {
1202 SSL_DEBUG_MSG( 1, ( "malloc (%d bytes) failed\n", cookie_len ) );
1203 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
1204 }
1205
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001206 if( ( ret = md_hmac_reset( &ssl->hvr_hmac_ctx ) ) != 0 ||
1207 ( ret = md_hmac_update( &ssl->hvr_hmac_ctx,
1208 ssl->cli_id, ssl->cli_id_len ) ) != 0 ||
1209 ( ret = md_hmac_finish( &ssl->hvr_hmac_ctx, hmac_out ) ) != 0 )
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001210 {
Manuel Pégourié-Gonnard98545f12014-07-22 22:10:43 +02001211 SSL_DEBUG_RET( 1, "md_hmac", ret );
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001212 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
1213 }
1214
Manuel Pégourié-Gonnarddd3cdb02014-07-22 20:40:40 +02001215 memcpy( cookie, hmac_out, HVR_MD_USE );
1216
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001217 ssl->handshake->verify_cookie = cookie;
1218 ssl->handshake->verify_cookie_len = cookie_len;
1219
1220 return( 0 );
1221}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001222#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001223
Paul Bakker5121ce52009-01-03 21:22:43 +00001224static int ssl_parse_client_hello( ssl_context *ssl )
1225{
Paul Bakker23986e52011-04-24 08:57:21 +00001226 int ret;
1227 unsigned int i, j;
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001228 unsigned int ciph_offset, comp_offset, ext_offset;
1229 unsigned int msg_len, ciph_len, sess_len, comp_len, ext_len;
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001230#if defined(POLARSSL_SSL_PROTO_DTLS)
1231 unsigned int cookie_offset, cookie_len;
1232#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001233 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001234 int renegotiation_info_seen = 0;
1235 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001236 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001237 const ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001238 int major, minor;
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
1240 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1241
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001242 /*
1243 * If renegotiating, then the input was read with ssl_read_record(),
1244 * otherwise read it ourselves manually in order to support SSLv2
1245 * ClientHello, which doesn't use the same record layer format.
1246 */
Paul Bakker48916f92012-09-16 19:57:18 +00001247 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001248 ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 {
1250 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1251 return( ret );
1252 }
1253
1254 buf = ssl->in_hdr;
1255
Paul Bakker78a8c712013-03-06 17:01:52 +01001256#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001257 if( ssl->transport == SSL_TRANSPORT_STREAM && ( buf[0] & 0x80 ) != 0 )
Paul Bakker78a8c712013-03-06 17:01:52 +01001258 return ssl_parse_client_hello_v2( ssl );
1259#endif
1260
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001261 SSL_DEBUG_BUF( 4, "record header", buf, ssl_hdr_len( ssl ) );
Paul Bakkerec636f32012-09-09 19:17:02 +00001262
Paul Bakkerec636f32012-09-09 19:17:02 +00001263 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001264 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001265 *
1266 * Record layer:
1267 * 0 . 0 message type
1268 * 1 . 2 protocol version
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001269 * 3 . 11 DTLS: epoch + record sequence number
Paul Bakkerec636f32012-09-09 19:17:02 +00001270 * 3 . 4 message length
1271 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001272 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1273 buf[0] ) );
1274
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001275 if( buf[0] != SSL_MSG_HANDSHAKE )
1276 {
1277 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1278 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1279 }
1280
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001281 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1282 ( ssl->in_len[0] << 8 ) | ssl->in_len[1] ) );
1283
1284 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol version: [%d:%d]",
1285 buf[1], buf[2] ) );
1286
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001287 ssl_read_version( &major, &minor, ssl->transport, buf + 1 );
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001288
1289 /* According to RFC 5246 Appendix E.1, the version here is typically
1290 * "{03,00}, the lowest version number supported by the client, [or] the
1291 * value of ClientHello.client_version", so the only meaningful check here
1292 * is the major version shouldn't be less than 3 */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001293 if( major < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001294 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001295 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1296 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1297 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001298
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001299 /* For DTLS if this is the initial handshake, remember the client sequence
1300 * number to use it in our next message (RFC 6347 4.2.1) */
1301#if defined(POLARSSL_SSL_PROTO_DTLS)
1302 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
1303 ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1304 {
1305 /* Epoch should be 0 for initial handshakes */
1306 if( ssl->in_ctr[0] != 0 || ssl->in_ctr[1] != 0 )
1307 {
1308 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1309 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1310 }
1311
1312 memcpy( ssl->out_ctr + 2, ssl->in_ctr + 2, 6 );
1313 }
1314#endif /* POLARSSL_SSL_PROTO_DTLS */
1315
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001316 msg_len = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Paul Bakker5121ce52009-01-03 21:22:43 +00001317
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001318 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
Paul Bakkerec636f32012-09-09 19:17:02 +00001319 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001320 if( msg_len > SSL_MAX_CONTENT_LEN )
1321 {
1322 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1323 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1324 }
Paul Bakkerec636f32012-09-09 19:17:02 +00001325
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001326 if( ( ret = ssl_fetch_input( ssl, ssl_hdr_len( ssl ) + msg_len ) ) != 0 )
1327 {
1328 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1329 return( ret );
1330 }
1331 }
1332 else
Paul Bakkerec636f32012-09-09 19:17:02 +00001333 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001334 /* Set by ssl_read_record() */
1335 msg_len = ssl->in_hslen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001336 }
1337
1338 buf = ssl->in_msg;
Paul Bakkerec636f32012-09-09 19:17:02 +00001339
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001340 SSL_DEBUG_BUF( 4, "record contents", buf, msg_len );
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001341
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001342 ssl->handshake->update_checksum( ssl, buf, msg_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001343
1344 /*
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001345 * The DTLS handshake layer contains additional fields. Use them, then
1346 * remove them so that it looks like TLS format to the rest of the code.
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001347 */
1348#if defined(POLARSSL_SSL_PROTO_DTLS)
1349 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1350 {
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001351 /* 1 (type) + 3 (len) + 2 (seq) + 3 (frag_offset) + 3 (frag_len) */
1352 if( msg_len < 12 )
1353 {
1354 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1355 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1356 }
1357
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001358 /*
1359 * Copy the client's handshake message_seq on initial handshakes
1360 */
1361 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
1362 ssl->handshake->msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
1363
1364 // TODO: DTLS: check message_seq on non-initial handshakes?
1365 // (or already done in ssl_read_record?)
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001366
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001367 /*
1368 * For now we don't support fragmentation, so make sure
1369 * fragment_offset == 0 and fragment_length == length
1370 *
1371 * TODO: DTLS: support fragmentation??
1372 * Well, ClientHello is rarely much longer than 512 bytes
1373 * so it will probably never be fragmented anyway...
1374 */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01001375 if( ssl->in_msg[6] != 0 || ssl->in_msg[7] != 0 || ssl->in_msg[8] != 0 ||
1376 memcmp( ssl->in_msg + 1, ssl->in_msg + 9, 3 ) != 0 )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "handshake fragmentation not supported" ) );
1379 return( POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE );
1380 }
1381
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001382 /* Remove the additional fields */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001383 memmove( buf + 4, buf + 12, msg_len - 12 );
1384 msg_len -= 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01001385 }
1386#endif /* POLARSSL_SSL_PROTO_DTLS */
1387
1388 /*
Paul Bakkerec636f32012-09-09 19:17:02 +00001389 * SSL layer:
1390 * 0 . 0 handshake type
1391 * 1 . 3 handshake length
1392 * 4 . 5 protocol version
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001393 * 6 . 37 random bytes (starting with 4 bytes of Unix time)
1394 * 38 . 38 session id length (1 byte)
Paul Bakkerec636f32012-09-09 19:17:02 +00001395 * 39 . 38+x session id
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001396 * 39+x . 39+x DTLS only: cookie length (1 byte)
1397 * 40+x . .. DTSL only: cookie
1398 * .. . .. ciphersuite list length (2 bytes)
1399 * .. . .. ciphersuite list
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001400 * .. . .. compression alg. list length (1 byte)
1401 * .. . .. compression alg. list
1402 * .. . .. extensions length (2 bytes, optional)
1403 * .. . .. extensions (optional)
1404 *
1405 * Minimal length (with everything empty and extensions ommitted) is
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001406 * 4 + 2 + 32 + 1 + 2 + 1 = 42 bytes. Check that first, so that we can
1407 * read at least up to session id length without worrying.
Paul Bakkerec636f32012-09-09 19:17:02 +00001408 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001409
1410 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001411 * Check the handshake type and message length
Paul Bakkerec636f32012-09-09 19:17:02 +00001412 */
Manuel Pégourié-Gonnardd6b721c2014-03-24 12:13:54 +01001413 if( msg_len < 42 )
1414 {
1415 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1416 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1417 }
1418
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001419 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d", buf[0] ) );
1420
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001421 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001422 {
1423 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1424 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1425 }
1426
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001427 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1428 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1429
1430 if( buf[1] != 0 ||
1431 msg_len != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1432 {
1433 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1434 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1435 }
1436
1437 /*
1438 * Check and save the protocol version
1439 */
1440 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1441 buf[4], buf[5] ) );
1442
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01001443 ssl_read_version( &ssl->major_ver, &ssl->minor_ver,
1444 ssl->transport, buf + 4 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001445
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001446 ssl->handshake->max_major_ver = ssl->major_ver;
1447 ssl->handshake->max_minor_ver = ssl->minor_ver;
1448
1449 if( ssl->major_ver < ssl->min_major_ver ||
1450 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001451 {
1452 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001453 " [%d:%d] < [%d:%d]",
1454 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001455 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001456
1457 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1458 SSL_ALERT_MSG_PROTOCOL_VERSION );
1459
1460 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1461 }
1462
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001463 if( ssl->major_ver > ssl->max_major_ver )
1464 {
1465 ssl->major_ver = ssl->max_major_ver;
1466 ssl->minor_ver = ssl->max_minor_ver;
1467 }
1468 else if( ssl->minor_ver > ssl->max_minor_ver )
1469 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001470
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001471 /*
1472 * Save client random (inc. Unix time)
1473 */
1474 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1475 buf + 6, 32 );
1476
Paul Bakker48916f92012-09-16 19:57:18 +00001477 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001478
1479 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001480 * Check the session ID length and save session ID
Paul Bakkerec636f32012-09-09 19:17:02 +00001481 */
1482 sess_len = buf[38];
1483
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001484 if( sess_len > sizeof( ssl->session_negotiate->id ) ||
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001485 sess_len + 38 + 2 > msg_len ) /* 2 for cipherlist length field */
Paul Bakkerec636f32012-09-09 19:17:02 +00001486 {
1487 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1488 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1489 }
1490
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001491 SSL_DEBUG_BUF( 3, "client hello, session id",
1492 buf + 39, sess_len );
1493
Paul Bakker48916f92012-09-16 19:57:18 +00001494 ssl->session_negotiate->length = sess_len;
1495 memset( ssl->session_negotiate->id, 0,
1496 sizeof( ssl->session_negotiate->id ) );
1497 memcpy( ssl->session_negotiate->id, buf + 39,
1498 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001499
1500 /*
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001501 * Check the cookie length and content
1502 */
1503#if defined(POLARSSL_SSL_PROTO_DTLS)
1504 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1505 {
1506 cookie_offset = 39 + sess_len;
1507 cookie_len = buf[cookie_offset];
1508
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001509 if( cookie_offset + 1 + cookie_len + 2 > msg_len )
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001510 {
1511 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1512 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1513 }
1514
1515 SSL_DEBUG_BUF( 3, "client hello, cookie",
1516 buf + cookie_offset + 1, cookie_len );
1517
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001518#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001519 /*
1520 * Generate reference cookie content:
1521 * - used for verification below,
1522 * - stored to be sent if verification fails
1523 */
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001524 if( ( ret = ssl_cookie_generate( ssl ) ) != 0 )
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001525 {
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001526 SSL_DEBUG_RET( 1, "ssl_cookie_generate", ret );
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001527 return( ret );
1528 }
1529
1530 /* If the received cookie is OK, no need to send one */
1531 if( cookie_len == ssl->handshake->verify_cookie_len &&
1532 safer_memcmp( buf + cookie_offset + 1,
1533 ssl->handshake->verify_cookie,
1534 ssl->handshake->verify_cookie_len ) == 0 )
1535 {
1536 polarssl_free( ssl->handshake->verify_cookie );
1537 ssl->handshake->verify_cookie = NULL;
1538 ssl->handshake->verify_cookie_len = 0;
1539 }
1540
1541 SSL_DEBUG_MSG( 2, ( "client hello, cookie verification %s",
1542 ssl->handshake->verify_cookie == NULL ?
1543 "passed" : "failed" ) );
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001544#else
1545 /* We know we didn't send a cookie, so it should be empty */
1546 if( cookie_len != 0 )
1547 {
1548 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1549 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1550 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001551
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001552 SSL_DEBUG_MSG( 2, ( "cookie verification disabled" ) );
1553#endif
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001554 }
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02001555#endif /* POLARSSL_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001556
1557 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001558 * Check the ciphersuitelist length (will be parsed later)
Paul Bakkerec636f32012-09-09 19:17:02 +00001559 */
Manuel Pégourié-Gonnard4128aa72014-03-21 09:40:12 +01001560#if defined(POLARSSL_SSL_PROTO_DTLS)
1561 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1562 ciph_offset = cookie_offset + 1 + cookie_len;
1563 else
1564#endif
1565 ciph_offset = 39 + sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001566
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001567 ciph_len = ( buf[ciph_offset + 0] << 8 )
1568 | ( buf[ciph_offset + 1] );
1569
1570 if( ciph_len < 2 ||
1571 ciph_len + 2 + ciph_offset + 1 > msg_len || /* 1 for comp. alg. len */
1572 ( ciph_len % 2 ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001573 {
1574 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1575 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1576 }
1577
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001578 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1579 buf + ciph_offset + 2, ciph_len );
Paul Bakkerec636f32012-09-09 19:17:02 +00001580
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001581 /*
1582 * Check the compression algorithms length and pick one
1583 */
1584 comp_offset = ciph_offset + 2 + ciph_len;
1585
1586 comp_len = buf[comp_offset];
1587
1588 if( comp_len < 1 ||
1589 comp_len > 16 ||
1590 comp_len + comp_offset + 1 > msg_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001591 {
1592 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1593 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1594 }
1595
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001596 SSL_DEBUG_BUF( 3, "client hello, compression",
1597 buf + comp_offset + 1, comp_len );
Paul Bakker48916f92012-09-16 19:57:18 +00001598
1599 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001600#if defined(POLARSSL_ZLIB_SUPPORT)
1601 for( i = 0; i < comp_len; ++i )
1602 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001603 if( buf[comp_offset + 1 + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 {
Paul Bakker48916f92012-09-16 19:57:18 +00001605 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001606 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001607 }
1608 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609#endif
1610
Manuel Pégourié-Gonnarda0e16322014-07-14 17:38:41 +02001611 /* See comments in ssl_write_client_hello() */
1612#if defined(POLARSSL_SSL_PROTO_DTLS)
1613 if( ssl->transport == SSL_TRANSPORT_DATAGRAM )
1614 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
1615#endif
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02001616
Paul Bakkerec636f32012-09-09 19:17:02 +00001617 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001618 * Check the extension length
Paul Bakker48916f92012-09-16 19:57:18 +00001619 */
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001620 ext_offset = comp_offset + 1 + comp_len;
1621 if( msg_len > ext_offset )
Paul Bakker48916f92012-09-16 19:57:18 +00001622 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001623 if( msg_len < ext_offset + 2 )
Paul Bakker48916f92012-09-16 19:57:18 +00001624 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001625 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1626 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1627 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001628
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001629 ext_len = ( buf[ext_offset + 0] << 8 )
1630 | ( buf[ext_offset + 1] );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001631
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001632 if( ( ext_len > 0 && ext_len < 4 ) ||
1633 msg_len != ext_offset + 2 + ext_len )
1634 {
1635 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1636 SSL_DEBUG_BUF( 3, "client hello extensions",
1637 buf + ext_offset + 2, ext_len );
1638 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker48916f92012-09-16 19:57:18 +00001639 }
1640 }
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001641 else
1642 ext_len = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001643
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001644 ext = buf + ext_offset + 2;
Paul Bakker48916f92012-09-16 19:57:18 +00001645
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001646 while( ext_len != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001647 {
1648 unsigned int ext_id = ( ( ext[0] << 8 )
1649 | ( ext[1] ) );
1650 unsigned int ext_size = ( ( ext[2] << 8 )
1651 | ( ext[3] ) );
1652
1653 if( ext_size + 4 > ext_len )
1654 {
1655 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1656 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1657 }
1658 switch( ext_id )
1659 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001660#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001661 case TLS_EXT_SERVERNAME:
1662 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1663 if( ssl->f_sni == NULL )
1664 break;
1665
1666 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1667 if( ret != 0 )
1668 return( ret );
1669 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001670#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001671
Paul Bakker48916f92012-09-16 19:57:18 +00001672 case TLS_EXT_RENEGOTIATION_INFO:
1673 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1674 renegotiation_info_seen = 1;
1675
Paul Bakker23f36802012-09-28 14:15:14 +00001676 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1677 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001678 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001679 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001680
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001681#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001682 case TLS_EXT_SIG_ALG:
1683 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1684 if( ssl->renegotiation == SSL_RENEGOTIATION )
1685 break;
1686
1687 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1688 if( ret != 0 )
1689 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001690 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001691#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001692
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001693#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001694 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1695 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1696
1697 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1698 if( ret != 0 )
1699 return( ret );
1700 break;
1701
1702 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1703 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001704 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001705
1706 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1707 if( ret != 0 )
1708 return( ret );
1709 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001710#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001711
Paul Bakker05decb22013-08-15 13:33:48 +02001712#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001713 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1714 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1715
1716 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1717 if( ret != 0 )
1718 return( ret );
1719 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001720#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001721
Paul Bakker1f2bc622013-08-15 13:45:55 +02001722#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001723 case TLS_EXT_TRUNCATED_HMAC:
1724 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1725
1726 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1727 if( ret != 0 )
1728 return( ret );
1729 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001730#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001731
Paul Bakkera503a632013-08-14 13:48:06 +02001732#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001733 case TLS_EXT_SESSION_TICKET:
1734 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1735
1736 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1737 if( ret != 0 )
1738 return( ret );
1739 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001740#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001741
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001742#if defined(POLARSSL_SSL_ALPN)
1743 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001744 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001745
1746 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1747 if( ret != 0 )
1748 return( ret );
1749 break;
1750#endif /* POLARSSL_SSL_SESSION_TICKETS */
1751
Paul Bakker48916f92012-09-16 19:57:18 +00001752 default:
1753 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1754 ext_id ) );
1755 }
1756
1757 ext_len -= 4 + ext_size;
1758 ext += 4 + ext_size;
1759
1760 if( ext_len > 0 && ext_len < 4 )
1761 {
1762 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1763 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1764 }
1765 }
1766
1767 /*
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001768 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1769 */
1770 for( i = 0, p = buf + ciph_offset + 2; i < ciph_len; i += 2, p += 2 )
1771 {
1772 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1773 {
1774 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1775 if( ssl->renegotiation == SSL_RENEGOTIATION )
1776 {
1777 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1778
1779 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1780 return( ret );
1781
1782 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1783 }
1784 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1785 break;
1786 }
1787 }
1788
1789 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001790 * Renegotiation security checks
1791 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001792 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1793 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1794 {
1795 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1796 handshake_failure = 1;
1797 }
1798 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1799 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1800 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001801 {
1802 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001803 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001804 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001805 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1806 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1807 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001808 {
1809 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001810 handshake_failure = 1;
1811 }
1812 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1813 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1814 renegotiation_info_seen == 1 )
1815 {
1816 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1817 handshake_failure = 1;
1818 }
1819
1820 if( handshake_failure == 1 )
1821 {
1822 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1823 return( ret );
1824
Paul Bakker48916f92012-09-16 19:57:18 +00001825 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1826 }
Paul Bakker380da532012-04-18 16:10:25 +00001827
Paul Bakker41c83d32013-03-20 14:39:14 +01001828 /*
1829 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001830 * (At the end because we need information from the EC-based extensions
1831 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001832 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001833 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001834 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001835#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001836 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001837 {
1838 for( i = 0; ciphersuites[i] != 0; i++ )
1839#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001840 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001841 {
Manuel Pégourié-Gonnard8933a652014-03-20 17:29:27 +01001842 for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001843#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001844 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001845 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1846 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1847 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001848
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001849 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1850 &ciphersuite_info ) ) != 0 )
1851 return( ret );
1852
1853 if( ciphersuite_info != NULL )
1854 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001855 }
1856 }
1857
1858 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1859
1860 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1861 return( ret );
1862
1863 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1864
1865have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001866 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001867 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1868 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1869
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02001870 /* ClientHello can't be bundled with another record in same datagram */
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 ssl->in_left = 0;
1872 ssl->state++;
1873
1874 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1875
1876 return( 0 );
1877}
1878
Paul Bakker1f2bc622013-08-15 13:45:55 +02001879#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001880static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1881 unsigned char *buf,
1882 size_t *olen )
1883{
1884 unsigned char *p = buf;
1885
1886 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1887 {
1888 *olen = 0;
1889 return;
1890 }
1891
1892 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1893
1894 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1895 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1896
1897 *p++ = 0x00;
1898 *p++ = 0x00;
1899
1900 *olen = 4;
1901}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001902#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001903
Paul Bakkera503a632013-08-14 13:48:06 +02001904#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001905static void ssl_write_session_ticket_ext( ssl_context *ssl,
1906 unsigned char *buf,
1907 size_t *olen )
1908{
1909 unsigned char *p = buf;
1910
1911 if( ssl->handshake->new_session_ticket == 0 )
1912 {
1913 *olen = 0;
1914 return;
1915 }
1916
1917 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1918
1919 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1920 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1921
1922 *p++ = 0x00;
1923 *p++ = 0x00;
1924
1925 *olen = 4;
1926}
Paul Bakkera503a632013-08-14 13:48:06 +02001927#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001928
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001929static void ssl_write_renegotiation_ext( ssl_context *ssl,
1930 unsigned char *buf,
1931 size_t *olen )
1932{
1933 unsigned char *p = buf;
1934
1935 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1936 {
1937 *olen = 0;
1938 return;
1939 }
1940
1941 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1942
1943 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1944 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1945
1946 *p++ = 0x00;
1947 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1948 *p++ = ssl->verify_data_len * 2 & 0xFF;
1949
1950 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1951 p += ssl->verify_data_len;
1952 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1953 p += ssl->verify_data_len;
1954
1955 *olen = 5 + ssl->verify_data_len * 2;
1956}
1957
Paul Bakker05decb22013-08-15 13:33:48 +02001958#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001959static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1960 unsigned char *buf,
1961 size_t *olen )
1962{
1963 unsigned char *p = buf;
1964
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001965 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1966 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001967 *olen = 0;
1968 return;
1969 }
1970
1971 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1972
1973 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1974 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1975
1976 *p++ = 0x00;
1977 *p++ = 1;
1978
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001979 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001980
1981 *olen = 5;
1982}
Paul Bakker05decb22013-08-15 13:33:48 +02001983#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001984
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001985#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001986static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1987 unsigned char *buf,
1988 size_t *olen )
1989{
1990 unsigned char *p = buf;
1991 ((void) ssl);
1992
Paul Bakker677377f2013-10-28 12:54:26 +01001993 if( ( ssl->handshake->cli_exts &
1994 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1995 {
1996 *olen = 0;
1997 return;
1998 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001999
2000 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
2001
2002 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
2003 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
2004
2005 *p++ = 0x00;
2006 *p++ = 2;
2007
2008 *p++ = 1;
2009 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
2010
2011 *olen = 6;
2012}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002013#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002014
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002015#if defined(POLARSSL_SSL_ALPN )
2016static void ssl_write_alpn_ext( ssl_context *ssl,
2017 unsigned char *buf, size_t *olen )
2018{
2019 if( ssl->alpn_chosen == NULL )
2020 {
2021 *olen = 0;
2022 return;
2023 }
2024
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02002025 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002026
2027 /*
2028 * 0 . 1 ext identifier
2029 * 2 . 3 ext length
2030 * 4 . 5 protocol list length
2031 * 6 . 6 protocol name length
2032 * 7 . 7+n protocol name
2033 */
2034 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
2035 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
2036
2037 *olen = 7 + strlen( ssl->alpn_chosen );
2038
2039 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
2040 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
2041
2042 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
2043 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
2044
2045 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
2046
2047 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
2048}
2049#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
2050
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002051#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002052static int ssl_write_hello_verify_request( ssl_context *ssl )
2053{
2054 int ret;
2055 unsigned char *p = ssl->out_msg + 4;
2056
2057 SSL_DEBUG_MSG( 2, ( "=> write hello verify request" ) );
2058
2059 /*
2060 * struct {
2061 * ProtocolVersion server_version;
2062 * opaque cookie<0..2^8-1>;
2063 * } HelloVerifyRequest;
2064 */
2065
2066 /* For now, use fixed version = DTLS 1.0 */
2067 ssl_write_version( SSL_MAJOR_VERSION_3, SSL_MINOR_VERSION_1,
2068 ssl->transport, p );
2069 SSL_DEBUG_BUF( 3, "server version", (unsigned char *) p, 2 );
2070 p += 2;
2071
2072 SSL_DEBUG_BUF( 3, "cookie", ssl->handshake->verify_cookie,
2073 ssl->handshake->verify_cookie_len );
2074 *p++ = ssl->handshake->verify_cookie_len;
2075 memcpy( p, ssl->handshake->verify_cookie,
2076 ssl->handshake->verify_cookie_len );
2077 p += ssl->handshake->verify_cookie_len;
2078
2079 ssl->out_msglen = p - ssl->out_msg;
2080 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2081 ssl->out_msg[0] = SSL_HS_HELLO_VERIFY_REQUEST;
2082
2083 ssl->state = SSL_CLIENT_HELLO;
2084
2085 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2086 {
2087 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2088 return( ret );
2089 }
2090
2091 SSL_DEBUG_MSG( 2, ( "<= write hello verify request" ) );
2092
2093 return( 0 );
2094}
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002095#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002096
Paul Bakker5121ce52009-01-03 21:22:43 +00002097static int ssl_write_server_hello( ssl_context *ssl )
2098{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002099#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002100 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002101#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002102 int ret;
2103 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002104 unsigned char *buf, *p;
2105
2106 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
2107
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002108#if defined(POLARSSL_SSL_DTLS_HELLO_VERIFY)
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002109 if( ssl->transport == SSL_TRANSPORT_DATAGRAM &&
2110 ssl->handshake->verify_cookie != NULL )
2111 {
2112 SSL_DEBUG_MSG( 2, ( "client hello was not authenticated" ) );
2113 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2114
2115 if( ( ret = ssl_write_hello_verify_request( ssl ) ) != 0 )
2116 {
2117 SSL_DEBUG_RET( 1, "ssl_write_hello_verify_request", ret );
2118 return( ret );
2119 }
2120
2121 return( POLARSSL_ERR_SSL_HELLO_VERIFY_REQUIRED );
2122 }
Manuel Pégourié-Gonnard82202f02014-07-23 00:28:58 +02002123#endif /* POLARSSL_SSL_DTLS_HELLO_VERIFY */
Manuel Pégourié-Gonnard2c9ee812014-07-22 11:45:03 +02002124
Paul Bakkera9a028e2013-11-21 17:31:06 +01002125 if( ssl->f_rng == NULL )
2126 {
2127 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
2128 return( POLARSSL_ERR_SSL_NO_RNG );
2129 }
2130
Paul Bakker5121ce52009-01-03 21:22:43 +00002131 /*
2132 * 0 . 0 handshake type
2133 * 1 . 3 handshake length
2134 * 4 . 5 protocol version
2135 * 6 . 9 UNIX time()
2136 * 10 . 37 random bytes
2137 */
2138 buf = ssl->out_msg;
2139 p = buf + 4;
2140
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002141 ssl_write_version( ssl->major_ver, ssl->minor_ver,
2142 ssl->transport, p );
2143 p += 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
2145 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002146 buf[4], buf[5] ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002147
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002148#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00002149 t = time( NULL );
2150 *p++ = (unsigned char)( t >> 24 );
2151 *p++ = (unsigned char)( t >> 16 );
2152 *p++ = (unsigned char)( t >> 8 );
2153 *p++ = (unsigned char)( t );
2154
2155 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02002156#else
2157 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
2158 return( ret );
2159
2160 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02002161#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00002162
Paul Bakkera3d195c2011-11-27 21:07:34 +00002163 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
2164 return( ret );
2165
2166 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00002167
Paul Bakker48916f92012-09-16 19:57:18 +00002168 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
2170 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
2171
2172 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002173 * Resume is 0 by default, see ssl_handshake_init().
2174 * It may be already set to 1 by ssl_parse_session_ticket_ext().
2175 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00002176 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002177 if( ssl->handshake->resume == 0 &&
2178 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02002179 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002180 ssl->f_get_cache != NULL &&
2181 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
2182 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01002183 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002184 ssl->handshake->resume = 1;
2185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002186
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002187 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002188 {
2189 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002190 * New session, create a new session id,
2191 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00002192 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002193 ssl->state++;
2194
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002195#if defined(POLARSSL_HAVE_TIME)
2196 ssl->session_negotiate->start = time( NULL );
2197#endif
2198
Paul Bakkera503a632013-08-14 13:48:06 +02002199#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02002200 if( ssl->handshake->new_session_ticket != 0 )
2201 {
2202 ssl->session_negotiate->length = n = 0;
2203 memset( ssl->session_negotiate->id, 0, 32 );
2204 }
2205 else
2206#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002207 {
2208 ssl->session_negotiate->length = n = 32;
2209 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02002210 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002211 return( ret );
2212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 }
2214 else
2215 {
2216 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002217 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02002219 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00002221
2222 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2223 {
2224 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2225 return( ret );
2226 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002227 }
2228
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02002229 /*
2230 * 38 . 38 session id length
2231 * 39 . 38+n session id
2232 * 39+n . 40+n chosen ciphersuite
2233 * 41+n . 41+n chosen compression alg.
2234 * 42+n . 43+n extensions length
2235 * 44+n . 43+n+m extensions
2236 */
2237 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00002238 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
2239 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00002240
2241 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
2242 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
2243 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00002244 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002245
Paul Bakker48916f92012-09-16 19:57:18 +00002246 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
2247 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
2248 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00002249
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02002250 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
2251 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02002252 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00002253 ssl->session_negotiate->compression ) );
2254
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002255 /*
2256 * First write extensions, then the total length
2257 */
2258 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
2259 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00002260
Paul Bakker05decb22013-08-15 13:33:48 +02002261#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002262 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
2263 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02002264#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02002265
Paul Bakker1f2bc622013-08-15 13:45:55 +02002266#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002267 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
2268 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02002269#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02002270
Paul Bakkera503a632013-08-14 13:48:06 +02002271#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002272 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
2273 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02002274#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02002275
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02002276#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02002277 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
2278 ext_len += olen;
2279#endif
2280
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02002281#if defined(POLARSSL_SSL_ALPN)
2282 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
2283 ext_len += olen;
2284#endif
2285
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02002286 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00002287
Paul Bakkera7036632014-04-30 10:15:38 +02002288 if( ext_len > 0 )
2289 {
2290 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
2291 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
2292 p += ext_len;
2293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
2295 ssl->out_msglen = p - buf;
2296 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2297 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
2298
2299 ret = ssl_write_record( ssl );
2300
2301 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
2302
2303 return( ret );
2304}
2305
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002306#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2307 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002308 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2309 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002310static int ssl_write_certificate_request( ssl_context *ssl )
2311{
Paul Bakkered27a042013-04-18 22:46:23 +02002312 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002313
2314 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2315
2316 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002317 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002318 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2319 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002320 {
2321 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2322 ssl->state++;
2323 return( 0 );
2324 }
2325
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002326 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2327 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002328}
2329#else
2330static int ssl_write_certificate_request( ssl_context *ssl )
2331{
2332 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2333 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002334 size_t dn_size, total_dn_size; /* excluding length bytes */
2335 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002336 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002337 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002338
2339 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2340
2341 ssl->state++;
2342
Paul Bakkerfbb17802013-04-17 19:10:21 +02002343 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002344 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002345 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002346 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002347 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002348 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002349 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002350 return( 0 );
2351 }
2352
2353 /*
2354 * 0 . 0 handshake type
2355 * 1 . 3 handshake length
2356 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002357 * 5 .. m-1 cert types
2358 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002359 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002360 * n .. n+1 length of all DNs
2361 * n+2 .. n+3 length of DN 1
2362 * n+4 .. ... Distinguished Name #1
2363 * ... .. ... length of DN 2, etc.
2364 */
2365 buf = ssl->out_msg;
2366 p = buf + 4;
2367
2368 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002369 * Supported certificate types
2370 *
2371 * ClientCertificateType certificate_types<1..2^8-1>;
2372 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002373 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002374 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002375
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002376#if defined(POLARSSL_RSA_C)
2377 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2378#endif
2379#if defined(POLARSSL_ECDSA_C)
2380 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2381#endif
2382
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002383 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002384 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002385
Paul Bakker577e0062013-08-28 11:57:20 +02002386 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002387#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002388 /*
2389 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002390 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002391 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2392 *
2393 * struct {
2394 * HashAlgorithm hash;
2395 * SignatureAlgorithm signature;
2396 * } SignatureAndHashAlgorithm;
2397 *
2398 * enum { (255) } HashAlgorithm;
2399 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002400 */
Paul Bakker21dca692013-01-03 11:41:08 +01002401 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002402 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002403 /*
2404 * Only use current running hash algorithm that is already required
2405 * for requested ciphersuite.
2406 */
Paul Bakker926af752012-11-23 13:38:07 +01002407 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2408
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002409 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2410 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002411 {
2412 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2413 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002414
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002415 /*
2416 * Supported signature algorithms
2417 */
2418#if defined(POLARSSL_RSA_C)
2419 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2420 p[2 + sa_len++] = SSL_SIG_RSA;
2421#endif
2422#if defined(POLARSSL_ECDSA_C)
2423 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2424 p[2 + sa_len++] = SSL_SIG_ECDSA;
2425#endif
Paul Bakker926af752012-11-23 13:38:07 +01002426
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002427 p[0] = (unsigned char)( sa_len >> 8 );
2428 p[1] = (unsigned char)( sa_len );
2429 sa_len += 2;
2430 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002431 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002432#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002433
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002434 /*
2435 * DistinguishedName certificate_authorities<0..2^16-1>;
2436 * opaque DistinguishedName<1..2^16-1>;
2437 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002438 p += 2;
2439 crt = ssl->ca_chain;
2440
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002441 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002442 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002443 {
2444 if( p - buf > 4096 )
2445 break;
2446
Paul Bakker926af752012-11-23 13:38:07 +01002447 dn_size = crt->subject_raw.len;
2448 *p++ = (unsigned char)( dn_size >> 8 );
2449 *p++ = (unsigned char)( dn_size );
2450 memcpy( p, crt->subject_raw.p, dn_size );
2451 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002452
Paul Bakker926af752012-11-23 13:38:07 +01002453 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2454
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002455 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002456 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002457 }
2458
Paul Bakker926af752012-11-23 13:38:07 +01002459 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002460 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2461 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002462 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2463 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002464
2465 ret = ssl_write_record( ssl );
2466
2467 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2468
2469 return( ret );
2470}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002471#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2472 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002473 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2474 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002475
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002476#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2477 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2478static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2479{
2480 int ret;
2481
2482 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2483 {
2484 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2485 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2486 }
2487
2488 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2489 pk_ec( *ssl_own_key( ssl ) ),
2490 POLARSSL_ECDH_OURS ) ) != 0 )
2491 {
2492 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2493 return( ret );
2494 }
2495
2496 return( 0 );
2497}
2498#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2499 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2500
Paul Bakker41c83d32013-03-20 14:39:14 +01002501static int ssl_write_server_key_exchange( ssl_context *ssl )
2502{
Paul Bakker23986e52011-04-24 08:57:21 +00002503 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002504 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002505 const ssl_ciphersuite_t *ciphersuite_info =
2506 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002507
2508#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2509 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2510 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002511 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002512 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002513 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002514 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002515 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002516 ((void) dig_signed);
2517 ((void) dig_signed_len);
2518#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002519
Paul Bakker5121ce52009-01-03 21:22:43 +00002520 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2521
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002522#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2523 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2524 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002525 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2526 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2527 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 {
2529 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2530 ssl->state++;
2531 return( 0 );
2532 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002533#endif
2534
2535#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2536 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2537 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2538 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2539 {
2540 ssl_get_ecdh_params_from_cert( ssl );
2541
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002542 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002543 ssl->state++;
2544 return( 0 );
2545 }
2546#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002547
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002548#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2549 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2550 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2551 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002552 {
2553 /* TODO: Support identity hints */
2554 *(p++) = 0x00;
2555 *(p++) = 0x00;
2556
2557 n += 2;
2558 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002559#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2560 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002561
2562#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2563 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2564 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2565 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002566 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002567 /*
2568 * Ephemeral DH parameters:
2569 *
2570 * struct {
2571 * opaque dh_p<1..2^16-1>;
2572 * opaque dh_g<1..2^16-1>;
2573 * opaque dh_Ys<1..2^16-1>;
2574 * } ServerDHParams;
2575 */
2576 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2577 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2578 {
2579 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2580 return( ret );
2581 }
Paul Bakker48916f92012-09-16 19:57:18 +00002582
Paul Bakker41c83d32013-03-20 14:39:14 +01002583 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002584 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2585 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002586 {
2587 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2588 return( ret );
2589 }
2590
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002591 dig_signed = p;
2592 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002593
2594 p += len;
2595 n += len;
2596
Paul Bakker41c83d32013-03-20 14:39:14 +01002597 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2598 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2599 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2600 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2601 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002602#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2603 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002604
Gergely Budai987bfb52014-01-19 21:48:42 +01002605#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002606 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002607 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2608 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002610 /*
2611 * Ephemeral ECDH parameters:
2612 *
2613 * struct {
2614 * ECParameters curve_params;
2615 * ECPoint public;
2616 * } ServerECDHParams;
2617 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002618 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002619#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002620 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002621
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002622 /* Match our preference list against the offered curves */
2623 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2624 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2625 if( (*curve)->grp_id == *gid )
2626 goto curve_matching_done;
2627
2628curve_matching_done:
2629#else
2630 curve = ssl->handshake->curves;
2631#endif
2632
2633 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002634 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002635 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2636 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002637 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002638
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002639 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002640
Paul Bakker41c83d32013-03-20 14:39:14 +01002641 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002642 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002643 {
2644 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2645 return( ret );
2646 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002647
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002648 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2649 p, SSL_MAX_CONTENT_LEN - n,
2650 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002651 {
2652 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2653 return( ret );
2654 }
2655
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002656 dig_signed = p;
2657 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002658
2659 p += len;
2660 n += len;
2661
Paul Bakker41c83d32013-03-20 14:39:14 +01002662 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2663 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002664#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002665
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002666#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002667 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2668 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002670 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2671 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002672 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002673 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002674 unsigned int hashlen = 0;
2675 unsigned char hash[64];
2676 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002677
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002678 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002679 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2680 */
Paul Bakker577e0062013-08-28 11:57:20 +02002681#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002682 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2683 {
2684 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2685
2686 if( md_alg == POLARSSL_MD_NONE )
2687 {
2688 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002689 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002690 }
2691 }
Paul Bakker577e0062013-08-28 11:57:20 +02002692 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002693#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002694#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2695 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002696 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002697 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2698 {
2699 md_alg = POLARSSL_MD_SHA1;
2700 }
2701 else
Paul Bakker577e0062013-08-28 11:57:20 +02002702#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002703 {
2704 md_alg = POLARSSL_MD_NONE;
2705 }
2706
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002707 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002708 * Compute the hash to be signed
2709 */
Paul Bakker577e0062013-08-28 11:57:20 +02002710#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2711 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002712 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002713 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002714 md5_context md5;
2715 sha1_context sha1;
2716
Paul Bakker5b4af392014-06-26 12:09:34 +02002717 md5_init( &md5 );
2718 sha1_init( &sha1 );
2719
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002720 /*
2721 * digitally-signed struct {
2722 * opaque md5_hash[16];
2723 * opaque sha_hash[20];
2724 * };
2725 *
2726 * md5_hash
2727 * MD5(ClientHello.random + ServerHello.random
2728 * + ServerParams);
2729 * sha_hash
2730 * SHA(ClientHello.random + ServerHello.random
2731 * + ServerParams);
2732 */
2733 md5_starts( &md5 );
2734 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002735 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002736 md5_finish( &md5, hash );
2737
2738 sha1_starts( &sha1 );
2739 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002740 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002741 sha1_finish( &sha1, hash + 16 );
2742
2743 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002744
2745 md5_free( &md5 );
2746 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002747 }
2748 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002749#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2750 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002751#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2752 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002753 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002754 {
2755 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002756 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002757
Paul Bakker84bbeb52014-07-01 14:53:22 +02002758 md_init( &ctx );
2759
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002760 /* Info from md_alg will be used instead */
2761 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002762
2763 /*
2764 * digitally-signed struct {
2765 * opaque client_random[32];
2766 * opaque server_random[32];
2767 * ServerDHParams params;
2768 * };
2769 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002770 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002771 {
2772 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2773 return( ret );
2774 }
2775
2776 md_starts( &ctx );
2777 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002778 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002779 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002780 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002781 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002782 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002783#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2784 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002785 {
2786 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002787 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002788 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002789
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002790 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2791 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002793 /*
2794 * Make the signature
2795 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002796 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002797 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002798 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2799 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002800 }
Paul Bakker23f36802012-09-28 14:15:14 +00002801
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002802#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002803 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2804 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002805 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002806 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807
2808 n += 2;
2809 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002810#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002811
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002812 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002813 p + 2 , &signature_len,
2814 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002815 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002816 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002817 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002818 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002819
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002820 *(p++) = (unsigned char)( signature_len >> 8 );
2821 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002822 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002823
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002824 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002825
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002826 p += signature_len;
2827 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002828 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002830 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2831 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002832
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002833 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2835 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2836
2837 ssl->state++;
2838
2839 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2840 {
2841 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2842 return( ret );
2843 }
2844
2845 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2846
2847 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002848}
2849
2850static int ssl_write_server_hello_done( ssl_context *ssl )
2851{
2852 int ret;
2853
2854 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2855
2856 ssl->out_msglen = 4;
2857 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2858 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2859
2860 ssl->state++;
2861
2862 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2863 {
2864 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2865 return( ret );
2866 }
2867
2868 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2869
2870 return( 0 );
2871}
2872
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002873#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2874 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2875static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2876 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002877{
2878 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002879 size_t n;
2880
2881 /*
2882 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2883 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002884 if( *p + 2 > end )
2885 {
2886 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2887 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2888 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002889
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002890 n = ( (*p)[0] << 8 ) | (*p)[1];
2891 *p += 2;
2892
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002893 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002894 {
2895 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2896 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2897 }
2898
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002899 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002900 {
2901 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2902 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2903 }
2904
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002905 *p += n;
2906
Paul Bakker70df2fb2013-04-17 17:19:09 +02002907 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2908
Paul Bakker70df2fb2013-04-17 17:19:09 +02002909 return( ret );
2910}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2912 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002913
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002914#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2915 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002916static int ssl_parse_encrypted_pms( ssl_context *ssl,
2917 const unsigned char *p,
2918 const unsigned char *end,
2919 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002920{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002921 int ret;
2922 size_t len = pk_get_len( ssl_own_key( ssl ) );
2923 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002924 unsigned char ver[2];
Paul Bakker70df2fb2013-04-17 17:19:09 +02002925
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002926 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002927 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002928 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002929 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2930 }
2931
2932 /*
2933 * Decrypt the premaster using own private RSA key
2934 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002935#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2936 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002937 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2938 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002939 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2940 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002941 {
2942 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2943 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2944 }
2945 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002946#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002947
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002948 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002949 {
2950 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2951 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2952 }
2953
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002954 ssl_write_version( ssl->handshake->max_major_ver,
2955 ssl->handshake->max_minor_ver,
2956 ssl->transport, ver );
2957
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002958 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2959 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002960 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002961 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002962
2963 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01002964 pms[0] != ver[0] ||
2965 pms[1] != ver[1] )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002966 {
2967 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2968
2969 /*
2970 * Protection against Bleichenbacher's attack:
2971 * invalid PKCS#1 v1.5 padding must not cause
2972 * the connection to end immediately; instead,
2973 * send a bad_record_mac later in the handshake.
2974 */
2975 ssl->handshake->pmslen = 48;
2976
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002977 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002978 if( ret != 0 )
2979 return( ret );
2980 }
2981
2982 return( ret );
2983}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002984#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2985 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002986
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002987#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002988static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2989 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002990{
Paul Bakker6db455e2013-09-18 17:29:31 +02002991 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002992 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002993
Paul Bakker6db455e2013-09-18 17:29:31 +02002994 if( ssl->f_psk == NULL &&
2995 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2996 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002997 {
2998 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2999 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
3000 }
3001
3002 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003003 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02003004 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 if( *p + 2 > end )
3006 {
3007 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3008 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3009 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003010
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003011 n = ( (*p)[0] << 8 ) | (*p)[1];
3012 *p += 2;
3013
3014 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003015 {
3016 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
3017 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3018 }
3019
Paul Bakker6db455e2013-09-18 17:29:31 +02003020 if( ssl->f_psk != NULL )
3021 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003022 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003023 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3024 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003025 else
Paul Bakker6db455e2013-09-18 17:29:31 +02003026 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003027 /* Identity is not a big secret since clients send it in the clear,
3028 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02003029 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01003030 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02003031 {
3032 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
3033 }
3034 }
3035
3036 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003037 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003038 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02003039 if( ( ret = ssl_send_alert_message( ssl,
3040 SSL_ALERT_LEVEL_FATAL,
3041 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
3042 {
3043 return( ret );
3044 }
3045
3046 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003047 }
3048
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003049 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02003050
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02003051 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02003052}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02003053#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02003054
Paul Bakker5121ce52009-01-03 21:22:43 +00003055static int ssl_parse_client_key_exchange( ssl_context *ssl )
3056{
Paul Bakker23986e52011-04-24 08:57:21 +00003057 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01003058 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02003059
Paul Bakker41c83d32013-03-20 14:39:14 +01003060 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003061
3062 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
3063
3064 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3065 {
3066 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3067 return( ret );
3068 }
3069
3070 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3071 {
3072 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003073 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003074 }
3075
3076 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
3077 {
3078 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003079 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00003080 }
3081
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003082#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01003083 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00003084 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003085 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003086 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003087
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003088 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02003090 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3091 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003092 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003093
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003094 if( p != end )
3095 {
3096 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3097 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3098 }
3099
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02003100 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003101
3102 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
3103 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02003104 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02003105 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003106 {
3107 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
3108 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3109 }
3110
3111 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003112 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003113 else
3114#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003115#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003116 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
3117 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
3118 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003119 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003120 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
3121 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
3122 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02003123 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003124 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02003125 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003126 {
3127 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3128 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
3129 }
3130
3131 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3132
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003133 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
3134 &ssl->handshake->pmslen,
3135 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02003136 POLARSSL_MPI_MAX_SIZE,
3137 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003138 {
3139 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
3140 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
3141 }
3142
3143 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003145 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003146#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01003147 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
3148 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
3149 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003150#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
3151 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003152 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003153 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003154 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003155
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003156 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02003157 {
3158 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3159 return( ret );
3160 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003161
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003162 if( p != end )
3163 {
3164 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3165 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3166 }
3167
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003168 if( ( ret = ssl_psk_derive_premaster( ssl,
3169 ciphersuite_info->key_exchange ) ) != 0 )
3170 {
3171 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3172 return( ret );
3173 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02003174 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003176#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003177#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
3178 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
3179 {
3180 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003181 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003182
3183 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3184 {
3185 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3186 return( ret );
3187 }
3188
3189 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
3190 {
3191 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
3192 return( ret );
3193 }
3194
3195 if( ( ret = ssl_psk_derive_premaster( ssl,
3196 ciphersuite_info->key_exchange ) ) != 0 )
3197 {
3198 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3199 return( ret );
3200 }
3201 }
3202 else
3203#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003204#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
3205 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3206 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02003207 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003208 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003209
3210 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3211 {
3212 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3213 return( ret );
3214 }
3215 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
3216 {
3217 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
3218 return( ret );
3219 }
3220
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003221 if( p != end )
3222 {
3223 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
3224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
3225 }
3226
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003227 if( ( ret = ssl_psk_derive_premaster( ssl,
3228 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003229 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003230 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
3231 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003232 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003233 }
3234 else
3235#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003236#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
3237 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
3238 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003239 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003240 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003241
3242 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
3243 {
3244 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
3245 return( ret );
3246 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003247
3248 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
3249 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003250 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003251 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
3252 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003253 }
3254
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02003255 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
3256
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003257 if( ( ret = ssl_psk_derive_premaster( ssl,
3258 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003259 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02003260 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003261 return( ret );
3262 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02003263 }
3264 else
3265#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003266#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
3267 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01003268 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003269 if( ( ret = ssl_parse_encrypted_pms( ssl,
3270 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003271 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02003272 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01003273 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01003274 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02003275 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003278 else
3279#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
3280 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02003281 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003282 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003283 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003284
Paul Bakkerff60ee62010-03-16 21:09:09 +00003285 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
3286 {
3287 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
3288 return( ret );
3289 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003290
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 ssl->state++;
3292
3293 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
3294
3295 return( 0 );
3296}
3297
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003298#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
3299 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02003300 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
3301 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00003302static int ssl_parse_certificate_verify( ssl_context *ssl )
3303{
Paul Bakkerfbb17802013-04-17 19:10:21 +02003304 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00003305
3306 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3307
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003308 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003309 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003310 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003311 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02003312 {
3313 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3314 ssl->state++;
3315 return( 0 );
3316 }
3317
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003318 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3319 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003320}
3321#else
3322static int ssl_parse_certificate_verify( ssl_context *ssl )
3323{
3324 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003325 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003326 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003327 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003328 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003329#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003330 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003331#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003332 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003333 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3334
3335 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3336
3337 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003338 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003339 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003340 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3341 {
3342 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3343 ssl->state++;
3344 return( 0 );
3345 }
3346
Paul Bakkered27a042013-04-18 22:46:23 +02003347 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003348 {
3349 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3350 ssl->state++;
3351 return( 0 );
3352 }
3353
Paul Bakker48916f92012-09-16 19:57:18 +00003354 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003355
3356 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3357 {
3358 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3359 return( ret );
3360 }
3361
3362 ssl->state++;
3363
3364 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3365 {
3366 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003367 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003368 }
3369
3370 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3371 {
3372 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003373 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003374 }
3375
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003376 /*
3377 * 0 . 0 handshake type
3378 * 1 . 3 handshake length
3379 * 4 . 5 sig alg (TLS 1.2 only)
3380 * 4+n . 5+n signature length (n = sa_len)
3381 * 6+n . 6+n+m signature (m = sig_len)
3382 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003383
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003384#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3385 defined(POLARSSL_SSL_PROTO_TLS1_1)
3386 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003387 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003388 sa_len = 0;
3389
Paul Bakkerc70b9822013-04-07 22:00:46 +02003390 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003391 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003392
3393 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3394 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3395 POLARSSL_PK_ECDSA ) )
3396 {
3397 hash_start += 16;
3398 hashlen -= 16;
3399 md_alg = POLARSSL_MD_SHA1;
3400 }
Paul Bakker926af752012-11-23 13:38:07 +01003401 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003402 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003403#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3404 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003405#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3406 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003407 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003408 sa_len = 2;
3409
Paul Bakker5121ce52009-01-03 21:22:43 +00003410 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003411 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003412 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003413 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003414 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003415 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3416 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003417 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3418 }
3419
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003420 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003421
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003422 /* Info from md_alg will be used instead */
3423 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003424
3425 /*
3426 * Signature
3427 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003428 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3429 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003430 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003431 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3432 " for verify message" ) );
3433 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003434 }
3435
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003436 /*
3437 * Check the certificate's key type matches the signature alg
3438 */
3439 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3440 {
3441 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3442 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3443 }
Paul Bakker577e0062013-08-28 11:57:20 +02003444 }
3445 else
3446#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3447 {
3448 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003449 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003450 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003451
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003452 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003453
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003454 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003455 {
3456 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003457 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003458 }
3459
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003460 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003461 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003462 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003463 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003464 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003465 return( ret );
3466 }
3467
3468 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3469
Paul Bakkered27a042013-04-18 22:46:23 +02003470 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003471}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003472#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3473 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3474 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003475
Paul Bakkera503a632013-08-14 13:48:06 +02003476#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003477static int ssl_write_new_session_ticket( ssl_context *ssl )
3478{
3479 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003480 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003481 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003482
3483 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3484
3485 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3486 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3487
3488 /*
3489 * struct {
3490 * uint32 ticket_lifetime_hint;
3491 * opaque ticket<0..2^16-1>;
3492 * } NewSessionTicket;
3493 *
3494 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3495 * 8 . 9 ticket_len (n)
3496 * 10 . 9+n ticket content
3497 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003498
3499 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3500 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3501 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3502 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003503
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003504 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3505 {
3506 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3507 tlen = 0;
3508 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003509
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003510 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3511 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003512
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003513 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003514
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003515 /*
3516 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3517 * ChangeCipherSpec share the same state.
3518 */
3519 ssl->handshake->new_session_ticket = 0;
3520
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003521 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3522 {
3523 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3524 return( ret );
3525 }
3526
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003527 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3528
3529 return( 0 );
3530}
Paul Bakkera503a632013-08-14 13:48:06 +02003531#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003532
Paul Bakker5121ce52009-01-03 21:22:43 +00003533/*
Paul Bakker1961b702013-01-25 14:49:24 +01003534 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003535 */
Paul Bakker1961b702013-01-25 14:49:24 +01003536int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003537{
3538 int ret = 0;
3539
Paul Bakker1961b702013-01-25 14:49:24 +01003540 if( ssl->state == SSL_HANDSHAKE_OVER )
3541 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003542
Paul Bakker1961b702013-01-25 14:49:24 +01003543 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3544
3545 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3546 return( ret );
3547
3548 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 {
Paul Bakker1961b702013-01-25 14:49:24 +01003550 case SSL_HELLO_REQUEST:
3551 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 break;
3553
Paul Bakker1961b702013-01-25 14:49:24 +01003554 /*
3555 * <== ClientHello
3556 */
3557 case SSL_CLIENT_HELLO:
3558 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003559 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003560
3561 /*
3562 * ==> ServerHello
3563 * Certificate
3564 * ( ServerKeyExchange )
3565 * ( CertificateRequest )
3566 * ServerHelloDone
3567 */
3568 case SSL_SERVER_HELLO:
3569 ret = ssl_write_server_hello( ssl );
3570 break;
3571
3572 case SSL_SERVER_CERTIFICATE:
3573 ret = ssl_write_certificate( ssl );
3574 break;
3575
3576 case SSL_SERVER_KEY_EXCHANGE:
3577 ret = ssl_write_server_key_exchange( ssl );
3578 break;
3579
3580 case SSL_CERTIFICATE_REQUEST:
3581 ret = ssl_write_certificate_request( ssl );
3582 break;
3583
3584 case SSL_SERVER_HELLO_DONE:
3585 ret = ssl_write_server_hello_done( ssl );
3586 break;
3587
3588 /*
3589 * <== ( Certificate/Alert )
3590 * ClientKeyExchange
3591 * ( CertificateVerify )
3592 * ChangeCipherSpec
3593 * Finished
3594 */
3595 case SSL_CLIENT_CERTIFICATE:
3596 ret = ssl_parse_certificate( ssl );
3597 break;
3598
3599 case SSL_CLIENT_KEY_EXCHANGE:
3600 ret = ssl_parse_client_key_exchange( ssl );
3601 break;
3602
3603 case SSL_CERTIFICATE_VERIFY:
3604 ret = ssl_parse_certificate_verify( ssl );
3605 break;
3606
3607 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3608 ret = ssl_parse_change_cipher_spec( ssl );
3609 break;
3610
3611 case SSL_CLIENT_FINISHED:
3612 ret = ssl_parse_finished( ssl );
3613 break;
3614
3615 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003616 * ==> ( NewSessionTicket )
3617 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003618 * Finished
3619 */
3620 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003621#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003622 if( ssl->handshake->new_session_ticket != 0 )
3623 ret = ssl_write_new_session_ticket( ssl );
3624 else
Paul Bakkera503a632013-08-14 13:48:06 +02003625#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003626 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003627 break;
3628
3629 case SSL_SERVER_FINISHED:
3630 ret = ssl_write_finished( ssl );
3631 break;
3632
3633 case SSL_FLUSH_BUFFERS:
3634 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3635 ssl->state = SSL_HANDSHAKE_WRAPUP;
3636 break;
3637
3638 case SSL_HANDSHAKE_WRAPUP:
3639 ssl_handshake_wrapup( ssl );
3640 break;
3641
3642 default:
3643 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3644 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003645 }
3646
Paul Bakker5121ce52009-01-03 21:22:43 +00003647 return( ret );
3648}
Paul Bakker9af723c2014-05-01 13:03:14 +02003649#endif /* POLARSSL_SSL_SRV_C */