blob: 211ab5d4785d9ecd21b2ac69f7fca108f9a73b06 [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,
Paul Bakker66d5d072014-06-17 16:39:18 +0200215 SSL_MAX_CONTENT_LEN - ( state - ssl->out_ctr ) - 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
Paul Bakker0be444a2013-08-27 21:55:01 +0200354#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200355/*
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200356 * Wrapper around f_sni, allowing use of ssl_set_own_cert() but
357 * making it act on ssl->hanshake->sni_key_cert instead.
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200358 */
359static int ssl_sni_wrapper( ssl_context *ssl,
360 const unsigned char* name, size_t len )
361{
362 int ret;
363 ssl_key_cert *key_cert_ori = ssl->key_cert;
364
365 ssl->key_cert = NULL;
366 ret = ssl->f_sni( ssl->p_sni, ssl, name, len );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +0200367 ssl->handshake->sni_key_cert = ssl->key_cert;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200368
369 ssl->key_cert = key_cert_ori;
370
371 return( ret );
372}
373
Paul Bakker5701cdc2012-09-27 21:49:42 +0000374static int ssl_parse_servername_ext( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000375 const unsigned char *buf,
Paul Bakker5701cdc2012-09-27 21:49:42 +0000376 size_t len )
377{
378 int ret;
379 size_t servername_list_size, hostname_len;
Paul Bakker23f36802012-09-28 14:15:14 +0000380 const unsigned char *p;
Paul Bakker5701cdc2012-09-27 21:49:42 +0000381
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100382 SSL_DEBUG_MSG( 3, ( "parse ServerName extension" ) );
383
Paul Bakker5701cdc2012-09-27 21:49:42 +0000384 servername_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
385 if( servername_list_size + 2 != len )
386 {
387 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
388 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
389 }
390
391 p = buf + 2;
392 while( servername_list_size > 0 )
393 {
394 hostname_len = ( ( p[1] << 8 ) | p[2] );
395 if( hostname_len + 3 > servername_list_size )
396 {
397 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
398 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
399 }
400
401 if( p[0] == TLS_EXT_SERVERNAME_HOSTNAME )
402 {
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +0200403 ret = ssl_sni_wrapper( ssl, p + 3, hostname_len );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000404 if( ret != 0 )
405 {
Manuel Pégourié-Gonnard96ea2f22014-02-25 12:26:29 +0100406 SSL_DEBUG_RET( 1, "ssl_sni_wrapper", ret );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000407 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
408 SSL_ALERT_MSG_UNRECOGNIZED_NAME );
409 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
410 }
Paul Bakker81420ab2012-10-23 10:31:15 +0000411 return( 0 );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000412 }
413
414 servername_list_size -= hostname_len + 3;
Paul Bakker23f36802012-09-28 14:15:14 +0000415 p += hostname_len + 3;
416 }
417
418 if( servername_list_size != 0 )
419 {
420 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
421 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
Paul Bakker5701cdc2012-09-27 21:49:42 +0000422 }
423
424 return( 0 );
425}
Paul Bakker0be444a2013-08-27 21:55:01 +0200426#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +0000427
Paul Bakker48916f92012-09-16 19:57:18 +0000428static int ssl_parse_renegotiation_info( ssl_context *ssl,
Paul Bakker23f36802012-09-28 14:15:14 +0000429 const unsigned char *buf,
Paul Bakker48916f92012-09-16 19:57:18 +0000430 size_t len )
431{
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000432 int ret;
433
Paul Bakker48916f92012-09-16 19:57:18 +0000434 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE )
435 {
436 if( len != 1 || buf[0] != 0x0 )
437 {
438 SSL_DEBUG_MSG( 1, ( "non-zero length renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000439
440 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
441 return( ret );
442
Paul Bakker48916f92012-09-16 19:57:18 +0000443 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
444 }
445
446 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
447 }
448 else
449 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100450 /* Check verify-data in constant-time. The length OTOH is no secret */
Paul Bakker48916f92012-09-16 19:57:18 +0000451 if( len != 1 + ssl->verify_data_len ||
452 buf[0] != ssl->verify_data_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +0100453 safer_memcmp( buf + 1, ssl->peer_verify_data,
454 ssl->verify_data_len ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +0000455 {
456 SSL_DEBUG_MSG( 1, ( "non-matching renegotiated connection field" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +0000457
458 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
459 return( ret );
460
Paul Bakker48916f92012-09-16 19:57:18 +0000461 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
462 }
463 }
464
465 return( 0 );
466}
467
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200468#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +0000469static int ssl_parse_signature_algorithms_ext( ssl_context *ssl,
470 const unsigned char *buf,
471 size_t len )
472{
473 size_t sig_alg_list_size;
474 const unsigned char *p;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200475 const unsigned char *end = buf + len;
476 const int *md_cur;
477
Paul Bakker23f36802012-09-28 14:15:14 +0000478
479 sig_alg_list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
480 if( sig_alg_list_size + 2 != len ||
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200481 sig_alg_list_size % 2 != 0 )
Paul Bakker23f36802012-09-28 14:15:14 +0000482 {
483 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
484 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
485 }
486
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200487 /*
488 * For now, ignore the SignatureAlgorithm part and rely on offered
489 * ciphersuites only for that part. To be fixed later.
490 *
491 * So, just look at the HashAlgorithm part.
492 */
493 for( md_cur = md_list(); *md_cur != POLARSSL_MD_NONE; md_cur++ ) {
494 for( p = buf + 2; p < end; p += 2 ) {
495 if( *md_cur == (int) ssl_md_alg_from_hash( p[0] ) ) {
496 ssl->handshake->sig_alg = p[0];
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200497 goto have_sig_alg;
Manuel Pégourié-Gonnard08e81e02014-07-08 12:56:25 +0200498 }
Paul Bakker23f36802012-09-28 14:15:14 +0000499 }
Paul Bakker23f36802012-09-28 14:15:14 +0000500 }
501
Manuel Pégourié-Gonnard480905d2014-08-21 19:38:32 +0200502 /* Some key echanges do not need signatures at all */
503 SSL_DEBUG_MSG( 3, ( "no signature_algorithm in common" ) );
504 return( 0 );
505
506have_sig_alg:
Paul Bakker23f36802012-09-28 14:15:14 +0000507 SSL_DEBUG_MSG( 3, ( "client hello v3, signature_algorithm ext: %d",
508 ssl->handshake->sig_alg ) );
509
510 return( 0 );
511}
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200512#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker23f36802012-09-28 14:15:14 +0000513
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200514#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515static int ssl_parse_supported_elliptic_curves( ssl_context *ssl,
516 const unsigned char *buf,
517 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100518{
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200519 size_t list_size, our_size;
Paul Bakker41c83d32013-03-20 14:39:14 +0100520 const unsigned char *p;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200521 const ecp_curve_info *curve_info, **curves;
Paul Bakker41c83d32013-03-20 14:39:14 +0100522
523 list_size = ( ( buf[0] << 8 ) | ( buf[1] ) );
524 if( list_size + 2 != len ||
525 list_size % 2 != 0 )
526 {
527 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
528 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
529 }
530
Manuel Pégourié-Gonnard43c3b282014-10-17 12:42:11 +0200531 /* Should never happen unless client duplicates the extension */
532 if( ssl->handshake->curves != NULL )
533 {
534 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
535 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
536 }
537
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +0100538 /* Don't allow our peer to make us allocate too much memory,
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200539 * and leave room for a final 0 */
540 our_size = list_size / 2 + 1;
541 if( our_size > POLARSSL_ECP_DP_MAX )
542 our_size = POLARSSL_ECP_DP_MAX;
543
544 if( ( curves = polarssl_malloc( our_size * sizeof( *curves ) ) ) == NULL )
545 return( POLARSSL_ERR_SSL_MALLOC_FAILED );
546
Paul Bakker9af723c2014-05-01 13:03:14 +0200547 /* explicit void pointer cast for buggy MS compiler */
Paul Bakkerbeccd9f2013-10-11 15:20:27 +0200548 memset( (void *) curves, 0, our_size * sizeof( *curves ) );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200549 ssl->handshake->curves = curves;
550
Paul Bakker41c83d32013-03-20 14:39:14 +0100551 p = buf + 2;
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200552 while( list_size > 0 && our_size > 1 )
Paul Bakker41c83d32013-03-20 14:39:14 +0100553 {
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200554 curve_info = ecp_curve_info_from_tls_id( ( p[0] << 8 ) | p[1] );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200555
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200556 if( curve_info != NULL )
Paul Bakker41c83d32013-03-20 14:39:14 +0100557 {
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +0200558 *curves++ = curve_info;
559 our_size--;
Paul Bakker41c83d32013-03-20 14:39:14 +0100560 }
561
562 list_size -= 2;
563 p += 2;
564 }
565
566 return( 0 );
567}
568
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200569static int ssl_parse_supported_point_formats( ssl_context *ssl,
570 const unsigned char *buf,
571 size_t len )
Paul Bakker41c83d32013-03-20 14:39:14 +0100572{
573 size_t list_size;
574 const unsigned char *p;
575
576 list_size = buf[0];
577 if( list_size + 1 != len )
578 {
579 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
581 }
582
583 p = buf + 2;
584 while( list_size > 0 )
585 {
586 if( p[0] == POLARSSL_ECP_PF_UNCOMPRESSED ||
587 p[0] == POLARSSL_ECP_PF_COMPRESSED )
588 {
Manuel Pégourié-Gonnard5734b2d2013-08-15 19:04:02 +0200589 ssl->handshake->ecdh_ctx.point_format = p[0];
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +0200590 SSL_DEBUG_MSG( 4, ( "point format selected: %d", p[0] ) );
Paul Bakker41c83d32013-03-20 14:39:14 +0100591 return( 0 );
592 }
593
594 list_size--;
595 p++;
596 }
597
598 return( 0 );
599}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +0200600#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100601
Paul Bakker05decb22013-08-15 13:33:48 +0200602#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200603static int ssl_parse_max_fragment_length_ext( ssl_context *ssl,
604 const unsigned char *buf,
605 size_t len )
606{
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200607 if( len != 1 || buf[0] >= SSL_MAX_FRAG_LEN_INVALID )
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200608 {
609 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
610 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
611 }
612
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +0200613 ssl->session_negotiate->mfl_code = buf[0];
614
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200615 return( 0 );
616}
Paul Bakker05decb22013-08-15 13:33:48 +0200617#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +0200618
Paul Bakker1f2bc622013-08-15 13:45:55 +0200619#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200620static int ssl_parse_truncated_hmac_ext( ssl_context *ssl,
621 const unsigned char *buf,
622 size_t len )
623{
624 if( len != 0 )
625 {
626 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
627 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
628 }
629
630 ((void) buf);
631
632 ssl->session_negotiate->trunc_hmac = SSL_TRUNC_HMAC_ENABLED;
633
634 return( 0 );
635}
Paul Bakker1f2bc622013-08-15 13:45:55 +0200636#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +0200637
Paul Bakkera503a632013-08-14 13:48:06 +0200638#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200639static int ssl_parse_session_ticket_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200640 unsigned char *buf,
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200641 size_t len )
642{
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200643 int ret;
644
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +0200645 if( ssl->session_tickets == SSL_SESSION_TICKETS_DISABLED )
646 return( 0 );
647
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200648 /* Remember the client asked us to send a new ticket */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200649 ssl->handshake->new_session_ticket = 1;
650
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200651 SSL_DEBUG_MSG( 3, ( "ticket length: %d", len ) );
652
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200653 if( len == 0 )
654 return( 0 );
655
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +0200656 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
657 {
658 SSL_DEBUG_MSG( 3, ( "ticket rejected: renegotiating" ) );
659 return( 0 );
660 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200661
662 /*
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200663 * Failures are ok: just ignore the ticket and proceed.
664 */
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200665 if( ( ret = ssl_parse_ticket( ssl, buf, len ) ) != 0 )
666 {
667 SSL_DEBUG_RET( 1, "ssl_parse_ticket", ret );
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200668 return( 0 );
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +0200669 }
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200670
671 SSL_DEBUG_MSG( 3, ( "session successfully restored from ticket" ) );
672
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +0200673 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200674
Manuel Pégourié-Gonnard306827e2013-08-02 18:05:14 +0200675 /* Don't send a new ticket after all, this one is OK */
676 ssl->handshake->new_session_ticket = 0;
677
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200678 return( 0 );
679}
Paul Bakkera503a632013-08-14 13:48:06 +0200680#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +0200681
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200682#if defined(POLARSSL_SSL_ALPN)
683static int ssl_parse_alpn_ext( ssl_context *ssl,
Manuel Pégourié-Gonnard14beb082014-07-08 13:50:35 +0200684 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200685{
Paul Bakker14b16c62014-05-28 11:33:54 +0200686 size_t list_len, cur_len, ours_len;
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200687 const unsigned char *theirs, *start, *end;
688 const char **ours;
689
690 /* If ALPN not configured, just ignore the extension */
691 if( ssl->alpn_list == NULL )
692 return( 0 );
693
694 /*
695 * opaque ProtocolName<1..2^8-1>;
696 *
697 * struct {
698 * ProtocolName protocol_name_list<2..2^16-1>
699 * } ProtocolNameList;
700 */
701
702 /* Min length is 2 (list_len) + 1 (name_len) + 1 (name) */
703 if( len < 4 )
704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
705
706 list_len = ( buf[0] << 8 ) | buf[1];
707 if( list_len != len - 2 )
708 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
709
710 /*
711 * Use our order of preference
712 */
713 start = buf + 2;
714 end = buf + len;
715 for( ours = ssl->alpn_list; *ours != NULL; ours++ )
716 {
Paul Bakker14b16c62014-05-28 11:33:54 +0200717 ours_len = strlen( *ours );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200718 for( theirs = start; theirs != end; theirs += cur_len )
719 {
720 /* If the list is well formed, we should get equality first */
721 if( theirs > end )
722 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
723
724 cur_len = *theirs++;
725
726 /* Empty strings MUST NOT be included */
727 if( cur_len == 0 )
728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
729
Paul Bakker14b16c62014-05-28 11:33:54 +0200730 if( cur_len == ours_len &&
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +0200731 memcmp( theirs, *ours, cur_len ) == 0 )
732 {
733 ssl->alpn_chosen = *ours;
734 return( 0 );
735 }
736 }
737 }
738
739 /* If we get there, no match was found */
740 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
741 SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL );
742 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
743}
744#endif /* POLARSSL_SSL_ALPN */
745
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100746/*
747 * Auxiliary functions for ServerHello parsing and related actions
748 */
749
750#if defined(POLARSSL_X509_CRT_PARSE_C)
751/*
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100752 * Return 0 if the given key uses one of the acceptable curves, -1 otherwise
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100753 */
754#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100755static int ssl_check_key_curve( pk_context *pk,
756 const ecp_curve_info **curves )
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100757{
758 const ecp_curve_info **crv = curves;
759 ecp_group_id grp_id = pk_ec( *pk )->grp.id;
760
761 while( *crv != NULL )
762 {
763 if( (*crv)->grp_id == grp_id )
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100764 return( 0 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100765 crv++;
766 }
767
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100768 return( -1 );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100769}
770#endif /* POLARSSL_ECDSA_C */
771
772/*
773 * Try picking a certificate for this ciphersuite,
774 * return 0 on success and -1 on failure.
775 */
776static int ssl_pick_cert( ssl_context *ssl,
777 const ssl_ciphersuite_t * ciphersuite_info )
778{
779 ssl_key_cert *cur, *list;
780 pk_type_t pk_alg = ssl_get_ciphersuite_sig_pk_alg( ciphersuite_info );
781
782#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
783 if( ssl->handshake->sni_key_cert != NULL )
784 list = ssl->handshake->sni_key_cert;
785 else
786#endif
787 list = ssl->handshake->key_cert;
788
789 if( pk_alg == POLARSSL_PK_NONE )
790 return( 0 );
791
792 for( cur = list; cur != NULL; cur = cur->next )
793 {
794 if( ! pk_can_do( cur->key, pk_alg ) )
795 continue;
796
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +0200797 /*
798 * This avoids sending the client a cert it'll reject based on
799 * keyUsage or other extensions.
800 *
801 * It also allows the user to provision different certificates for
802 * different uses based on keyUsage, eg if they want to avoid signing
803 * and decrypting with the same RSA key.
804 */
805 if( ssl_check_cert_usage( cur->cert, ciphersuite_info,
806 SSL_IS_SERVER ) != 0 )
807 {
808 continue;
809 }
810
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100811#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100812 if( pk_alg == POLARSSL_PK_ECDSA &&
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100813 ssl_check_key_curve( cur->key, ssl->handshake->curves ) != 0 )
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100814 continue;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100815#endif
Manuel Pégourié-Gonnard846ba472015-01-08 13:54:38 +0100816
817 /* If we get there, we got a winner */
818 break;
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100819 }
820
821 if( cur == NULL )
822 return( -1 );
823
824 ssl->handshake->key_cert = cur;
825 return( 0 );
826}
827#endif /* POLARSSL_X509_CRT_PARSE_C */
828
829/*
830 * Check if a given ciphersuite is suitable for use with our config/keys/etc
831 * Sets ciphersuite_info only if the suite matches.
832 */
833static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
834 const ssl_ciphersuite_t **ciphersuite_info )
835{
836 const ssl_ciphersuite_t *suite_info;
837
838 suite_info = ssl_ciphersuite_from_id( suite_id );
839 if( suite_info == NULL )
840 {
Manuel Pégourié-Gonnard6458e3b2015-01-08 14:16:56 +0100841 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
842 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard32525602013-11-30 17:50:32 +0100843 }
844
845 if( suite_info->min_minor_ver > ssl->minor_ver ||
846 suite_info->max_minor_ver < ssl->minor_ver )
847 return( 0 );
848
849#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
850 if( ssl_ciphersuite_uses_ec( suite_info ) &&
851 ( ssl->handshake->curves == NULL ||
852 ssl->handshake->curves[0] == NULL ) )
853 return( 0 );
854#endif
855
856#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
857 /* If the ciphersuite requires a pre-shared key and we don't
858 * have one, skip it now rather than failing later */
859 if( ssl_ciphersuite_uses_psk( suite_info ) &&
860 ssl->f_psk == NULL &&
861 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
862 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
863 return( 0 );
864#endif
865
866#if defined(POLARSSL_X509_CRT_PARSE_C)
867 /*
868 * Final check: if ciphersuite requires us to have a
869 * certificate/key of a particular type:
870 * - select the appropriate certificate if we have one, or
871 * - try the next ciphersuite if we don't
872 * This must be done last since we modify the key_cert list.
873 */
874 if( ssl_pick_cert( ssl, suite_info ) != 0 )
875 return( 0 );
876#endif
877
878 *ciphersuite_info = suite_info;
879 return( 0 );
880}
881
Paul Bakker78a8c712013-03-06 17:01:52 +0100882#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
883static int ssl_parse_client_hello_v2( ssl_context *ssl )
884{
885 int ret;
886 unsigned int i, j;
887 size_t n;
888 unsigned int ciph_len, sess_len, chal_len;
889 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200890 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200891 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100892
893 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
894
895 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
896 {
897 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
898
899 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
900 return( ret );
901
902 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
903 }
904
905 buf = ssl->in_hdr;
906
907 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
908
909 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
910 buf[2] ) );
911 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
912 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
913 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
914 buf[3], buf[4] ) );
915
916 /*
917 * SSLv2 Client Hello
918 *
919 * Record layer:
920 * 0 . 1 message length
921 *
922 * SSL layer:
923 * 2 . 2 message type
924 * 3 . 4 protocol version
925 */
926 if( buf[2] != SSL_HS_CLIENT_HELLO ||
927 buf[3] != SSL_MAJOR_VERSION_3 )
928 {
929 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
930 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
931 }
932
933 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
934
935 if( n < 17 || n > 512 )
936 {
937 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
938 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
939 }
940
941 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200942 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
943 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100944
945 if( ssl->minor_ver < ssl->min_minor_ver )
946 {
947 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200948 " [%d:%d] < [%d:%d]",
949 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100950 ssl->min_major_ver, ssl->min_minor_ver ) );
951
952 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
953 SSL_ALERT_MSG_PROTOCOL_VERSION );
954 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
955 }
956
Paul Bakker2fbefde2013-06-29 16:01:15 +0200957 ssl->handshake->max_major_ver = buf[3];
958 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100959
960 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
961 {
962 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
963 return( ret );
964 }
965
966 ssl->handshake->update_checksum( ssl, buf + 2, n );
967
968 buf = ssl->in_msg;
969 n = ssl->in_left - 5;
970
971 /*
972 * 0 . 1 ciphersuitelist length
973 * 2 . 3 session id length
974 * 4 . 5 challenge length
975 * 6 . .. ciphersuitelist
976 * .. . .. session id
977 * .. . .. challenge
978 */
979 SSL_DEBUG_BUF( 4, "record contents", buf, n );
980
981 ciph_len = ( buf[0] << 8 ) | buf[1];
982 sess_len = ( buf[2] << 8 ) | buf[3];
983 chal_len = ( buf[4] << 8 ) | buf[5];
984
985 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
986 ciph_len, sess_len, chal_len ) );
987
988 /*
989 * Make sure each parameter length is valid
990 */
991 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
992 {
993 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
994 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
995 }
996
997 if( sess_len > 32 )
998 {
999 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1000 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1001 }
1002
1003 if( chal_len < 8 || chal_len > 32 )
1004 {
1005 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1006 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1007 }
1008
1009 if( n != 6 + ciph_len + sess_len + chal_len )
1010 {
1011 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1012 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1013 }
1014
1015 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1016 buf + 6, ciph_len );
1017 SSL_DEBUG_BUF( 3, "client hello, session id",
1018 buf + 6 + ciph_len, sess_len );
1019 SSL_DEBUG_BUF( 3, "client hello, challenge",
1020 buf + 6 + ciph_len + sess_len, chal_len );
1021
1022 p = buf + 6 + ciph_len;
1023 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001024 memset( ssl->session_negotiate->id, 0,
1025 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001026 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1027
1028 p += sess_len;
1029 memset( ssl->handshake->randbytes, 0, 64 );
1030 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1031
1032 /*
1033 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1034 */
1035 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1036 {
1037 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1038 {
1039 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1040 if( ssl->renegotiation == SSL_RENEGOTIATION )
1041 {
1042 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1043
1044 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1045 return( ret );
1046
1047 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1048 }
1049 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1050 break;
1051 }
1052 }
1053
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001054 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001055 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001056#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1057 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1058 {
1059 for( i = 0; ciphersuites[i] != 0; i++ )
1060#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001061 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001062 {
1063 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001064#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001065 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001066 if( p[0] != 0 ||
1067 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1068 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1069 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001070
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001071 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1072 &ciphersuite_info ) ) != 0 )
1073 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001074
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001075 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001076 goto have_ciphersuite_v2;
1077 }
1078 }
1079
1080 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1081
1082 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1083
1084have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001085 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001086 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001087 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001088
1089 /*
1090 * SSLv2 Client Hello relevant renegotiation security checks
1091 */
1092 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1093 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1094 {
1095 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1096
1097 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1098 return( ret );
1099
1100 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1101 }
1102
1103 ssl->in_left = 0;
1104 ssl->state++;
1105
1106 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1107
1108 return( 0 );
1109}
1110#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1111
Paul Bakker5121ce52009-01-03 21:22:43 +00001112static int ssl_parse_client_hello( ssl_context *ssl )
1113{
Paul Bakker23986e52011-04-24 08:57:21 +00001114 int ret;
1115 unsigned int i, j;
1116 size_t n;
1117 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001118 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001119 unsigned int ext_len = 0;
1120 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001121 int renegotiation_info_seen = 0;
1122 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001123 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001124 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001125
1126 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1127
Paul Bakker48916f92012-09-16 19:57:18 +00001128 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1129 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
1131 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1132 return( ret );
1133 }
1134
1135 buf = ssl->in_hdr;
1136
Paul Bakker78a8c712013-03-06 17:01:52 +01001137#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1138 if( ( buf[0] & 0x80 ) != 0 )
1139 return ssl_parse_client_hello_v2( ssl );
1140#endif
1141
Paul Bakkerec636f32012-09-09 19:17:02 +00001142 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1143
1144 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1145 buf[0] ) );
1146 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1147 ( buf[3] << 8 ) | buf[4] ) );
1148 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1149 buf[1], buf[2] ) );
1150
1151 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001152 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001153 *
1154 * Record layer:
1155 * 0 . 0 message type
1156 * 1 . 2 protocol version
1157 * 3 . 4 message length
1158 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001159
1160 /* According to RFC 5246 Appendix E.1, the version here is typically
1161 * "{03,00}, the lowest version number supported by the client, [or] the
1162 * value of ClientHello.client_version", so the only meaningful check here
1163 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001164 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001165 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001167 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1168 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
Paul Bakkerec636f32012-09-09 19:17:02 +00001171 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
Paul Bakker4f42c112014-04-17 14:48:23 +02001173 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001174 {
1175 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1176 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1177 }
1178
Paul Bakker48916f92012-09-16 19:57:18 +00001179 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1180 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001181 {
1182 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1183 return( ret );
1184 }
1185
1186 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001187 if( !ssl->renegotiation )
1188 n = ssl->in_left - 5;
1189 else
1190 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001191
Paul Bakker48916f92012-09-16 19:57:18 +00001192 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001193
1194 /*
1195 * SSL layer:
1196 * 0 . 0 handshake type
1197 * 1 . 3 handshake length
1198 * 4 . 5 protocol version
1199 * 6 . 9 UNIX time()
1200 * 10 . 37 random bytes
1201 * 38 . 38 session id length
1202 * 39 . 38+x session id
1203 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001204 * 41+x . 40+y ciphersuitelist
1205 * 41+y . 41+y compression alg length
1206 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001207 * .. . .. extensions
1208 */
1209 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1210
1211 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1212 buf[0] ) );
1213 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1214 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1215 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1216 buf[4], buf[5] ) );
1217
1218 /*
1219 * Check the handshake type and protocol version
1220 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001221 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001222 {
1223 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1224 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1225 }
1226
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001227 ssl->major_ver = buf[4];
1228 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001229
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001230 ssl->handshake->max_major_ver = ssl->major_ver;
1231 ssl->handshake->max_minor_ver = ssl->minor_ver;
1232
1233 if( ssl->major_ver < ssl->min_major_ver ||
1234 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001235 {
1236 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001237 " [%d:%d] < [%d:%d]",
1238 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001239 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001240
1241 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1242 SSL_ALERT_MSG_PROTOCOL_VERSION );
1243
1244 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1245 }
1246
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001247 if( ssl->major_ver > ssl->max_major_ver )
1248 {
1249 ssl->major_ver = ssl->max_major_ver;
1250 ssl->minor_ver = ssl->max_minor_ver;
1251 }
1252 else if( ssl->minor_ver > ssl->max_minor_ver )
1253 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001254
Paul Bakker48916f92012-09-16 19:57:18 +00001255 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001256
1257 /*
1258 * Check the handshake message length
1259 */
1260 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1261 {
1262 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1263 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1264 }
1265
1266 /*
1267 * Check the session length
1268 */
1269 sess_len = buf[38];
1270
Paul Bakker0f651c72014-05-22 15:12:19 +02001271 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001272 {
1273 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1274 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1275 }
1276
Paul Bakker48916f92012-09-16 19:57:18 +00001277 ssl->session_negotiate->length = sess_len;
1278 memset( ssl->session_negotiate->id, 0,
1279 sizeof( ssl->session_negotiate->id ) );
1280 memcpy( ssl->session_negotiate->id, buf + 39,
1281 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001282
1283 /*
1284 * Check the ciphersuitelist length
1285 */
1286 ciph_len = ( buf[39 + sess_len] << 8 )
1287 | ( buf[40 + sess_len] );
1288
Paul Bakker0f651c72014-05-22 15:12:19 +02001289 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001290 {
1291 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1292 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1293 }
1294
1295 /*
1296 * Check the compression algorithms length
1297 */
1298 comp_len = buf[41 + sess_len + ciph_len];
1299
Paul Bakker0f651c72014-05-22 15:12:19 +02001300 if( comp_len < 1 || comp_len > 16 ||
1301 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001302 {
1303 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1304 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1305 }
1306
Paul Bakker48916f92012-09-16 19:57:18 +00001307 /*
1308 * Check the extension length
1309 */
1310 if( n > 42 + sess_len + ciph_len + comp_len )
1311 {
1312 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1313 | ( buf[43 + sess_len + ciph_len + comp_len] );
1314
1315 if( ( ext_len > 0 && ext_len < 4 ) ||
1316 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1317 {
1318 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1319 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1320 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1321 }
1322 }
1323
1324 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001325#if defined(POLARSSL_ZLIB_SUPPORT)
1326 for( i = 0; i < comp_len; ++i )
1327 {
Paul Bakker48916f92012-09-16 19:57:18 +00001328 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001329 {
Paul Bakker48916f92012-09-16 19:57:18 +00001330 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001331 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
1333 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001334#endif
1335
Paul Bakkerec636f32012-09-09 19:17:02 +00001336 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1337 buf + 6, 32 );
1338 SSL_DEBUG_BUF( 3, "client hello, session id",
1339 buf + 38, sess_len );
1340 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1341 buf + 41 + sess_len, ciph_len );
1342 SSL_DEBUG_BUF( 3, "client hello, compression",
1343 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Paul Bakkerec636f32012-09-09 19:17:02 +00001345 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001346 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1347 */
1348 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1349 {
1350 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1351 {
1352 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1353 if( ssl->renegotiation == SSL_RENEGOTIATION )
1354 {
1355 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001356
1357 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1358 return( ret );
1359
Paul Bakker48916f92012-09-16 19:57:18 +00001360 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1361 }
1362 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1363 break;
1364 }
1365 }
1366
Paul Bakker48916f92012-09-16 19:57:18 +00001367 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001368
1369 while( ext_len )
1370 {
1371 unsigned int ext_id = ( ( ext[0] << 8 )
1372 | ( ext[1] ) );
1373 unsigned int ext_size = ( ( ext[2] << 8 )
1374 | ( ext[3] ) );
1375
1376 if( ext_size + 4 > ext_len )
1377 {
1378 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1379 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1380 }
1381 switch( ext_id )
1382 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001383#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001384 case TLS_EXT_SERVERNAME:
1385 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1386 if( ssl->f_sni == NULL )
1387 break;
1388
1389 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1390 if( ret != 0 )
1391 return( ret );
1392 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001393#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001394
Paul Bakker48916f92012-09-16 19:57:18 +00001395 case TLS_EXT_RENEGOTIATION_INFO:
1396 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1397 renegotiation_info_seen = 1;
1398
Paul Bakker23f36802012-09-28 14:15:14 +00001399 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1400 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001401 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001402 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001403
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001404#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001405 case TLS_EXT_SIG_ALG:
1406 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1407 if( ssl->renegotiation == SSL_RENEGOTIATION )
1408 break;
1409
1410 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1411 if( ret != 0 )
1412 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001413 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001414#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001415
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001416#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001417 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1418 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1419
1420 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1421 if( ret != 0 )
1422 return( ret );
1423 break;
1424
1425 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1426 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001427 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001428
1429 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1430 if( ret != 0 )
1431 return( ret );
1432 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001433#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001434
Paul Bakker05decb22013-08-15 13:33:48 +02001435#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001436 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1437 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1438
1439 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1440 if( ret != 0 )
1441 return( ret );
1442 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001443#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001444
Paul Bakker1f2bc622013-08-15 13:45:55 +02001445#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001446 case TLS_EXT_TRUNCATED_HMAC:
1447 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1448
1449 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1450 if( ret != 0 )
1451 return( ret );
1452 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001453#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001454
Paul Bakkera503a632013-08-14 13:48:06 +02001455#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001456 case TLS_EXT_SESSION_TICKET:
1457 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1458
1459 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1460 if( ret != 0 )
1461 return( ret );
1462 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001463#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001464
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001465#if defined(POLARSSL_SSL_ALPN)
1466 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001467 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001468
1469 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1470 if( ret != 0 )
1471 return( ret );
1472 break;
1473#endif /* POLARSSL_SSL_SESSION_TICKETS */
1474
Paul Bakker48916f92012-09-16 19:57:18 +00001475 default:
1476 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1477 ext_id ) );
1478 }
1479
1480 ext_len -= 4 + ext_size;
1481 ext += 4 + ext_size;
1482
1483 if( ext_len > 0 && ext_len < 4 )
1484 {
1485 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1486 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1487 }
1488 }
1489
1490 /*
1491 * Renegotiation security checks
1492 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001493 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1494 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1495 {
1496 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1497 handshake_failure = 1;
1498 }
1499 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1500 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1501 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001502 {
1503 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001504 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001505 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001506 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1507 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1508 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001509 {
1510 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001511 handshake_failure = 1;
1512 }
1513 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1514 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1515 renegotiation_info_seen == 1 )
1516 {
1517 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1518 handshake_failure = 1;
1519 }
1520
1521 if( handshake_failure == 1 )
1522 {
1523 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1524 return( ret );
1525
Paul Bakker48916f92012-09-16 19:57:18 +00001526 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1527 }
Paul Bakker380da532012-04-18 16:10:25 +00001528
Paul Bakker41c83d32013-03-20 14:39:14 +01001529 /*
1530 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001531 * (At the end because we need information from the EC-based extensions
1532 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001533 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001534 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001535 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001536#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1537 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1538 {
1539 for( i = 0; ciphersuites[i] != 0; i++ )
1540#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001541 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001542 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001543 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001544#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001545 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001546 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1547 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1548 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001549
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001550 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1551 &ciphersuite_info ) ) != 0 )
1552 return( ret );
1553
1554 if( ciphersuite_info != NULL )
1555 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001556 }
1557 }
1558
1559 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1560
1561 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1562 return( ret );
1563
1564 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1565
1566have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001567 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001568 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1569 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1570
Paul Bakker5121ce52009-01-03 21:22:43 +00001571 ssl->in_left = 0;
1572 ssl->state++;
1573
1574 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1575
1576 return( 0 );
1577}
1578
Paul Bakker1f2bc622013-08-15 13:45:55 +02001579#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001580static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1581 unsigned char *buf,
1582 size_t *olen )
1583{
1584 unsigned char *p = buf;
1585
1586 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1587 {
1588 *olen = 0;
1589 return;
1590 }
1591
1592 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1593
1594 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1595 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1596
1597 *p++ = 0x00;
1598 *p++ = 0x00;
1599
1600 *olen = 4;
1601}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001602#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001603
Paul Bakkera503a632013-08-14 13:48:06 +02001604#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001605static void ssl_write_session_ticket_ext( ssl_context *ssl,
1606 unsigned char *buf,
1607 size_t *olen )
1608{
1609 unsigned char *p = buf;
1610
1611 if( ssl->handshake->new_session_ticket == 0 )
1612 {
1613 *olen = 0;
1614 return;
1615 }
1616
1617 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1618
1619 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1620 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1621
1622 *p++ = 0x00;
1623 *p++ = 0x00;
1624
1625 *olen = 4;
1626}
Paul Bakkera503a632013-08-14 13:48:06 +02001627#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001628
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001629static void ssl_write_renegotiation_ext( ssl_context *ssl,
1630 unsigned char *buf,
1631 size_t *olen )
1632{
1633 unsigned char *p = buf;
1634
1635 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1636 {
1637 *olen = 0;
1638 return;
1639 }
1640
1641 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1642
1643 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1644 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1645
1646 *p++ = 0x00;
1647 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1648 *p++ = ssl->verify_data_len * 2 & 0xFF;
1649
1650 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1651 p += ssl->verify_data_len;
1652 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1653 p += ssl->verify_data_len;
1654
1655 *olen = 5 + ssl->verify_data_len * 2;
1656}
1657
Paul Bakker05decb22013-08-15 13:33:48 +02001658#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001659static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1660 unsigned char *buf,
1661 size_t *olen )
1662{
1663 unsigned char *p = buf;
1664
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001665 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1666 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001667 *olen = 0;
1668 return;
1669 }
1670
1671 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1672
1673 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1674 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1675
1676 *p++ = 0x00;
1677 *p++ = 1;
1678
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001679 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001680
1681 *olen = 5;
1682}
Paul Bakker05decb22013-08-15 13:33:48 +02001683#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001684
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001685#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001686static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1687 unsigned char *buf,
1688 size_t *olen )
1689{
1690 unsigned char *p = buf;
1691 ((void) ssl);
1692
Paul Bakker677377f2013-10-28 12:54:26 +01001693 if( ( ssl->handshake->cli_exts &
1694 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1695 {
1696 *olen = 0;
1697 return;
1698 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001699
1700 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1701
1702 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1703 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1704
1705 *p++ = 0x00;
1706 *p++ = 2;
1707
1708 *p++ = 1;
1709 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1710
1711 *olen = 6;
1712}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001713#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001714
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001715#if defined(POLARSSL_SSL_ALPN )
1716static void ssl_write_alpn_ext( ssl_context *ssl,
1717 unsigned char *buf, size_t *olen )
1718{
1719 if( ssl->alpn_chosen == NULL )
1720 {
1721 *olen = 0;
1722 return;
1723 }
1724
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001725 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001726
1727 /*
1728 * 0 . 1 ext identifier
1729 * 2 . 3 ext length
1730 * 4 . 5 protocol list length
1731 * 6 . 6 protocol name length
1732 * 7 . 7+n protocol name
1733 */
1734 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1735 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1736
1737 *olen = 7 + strlen( ssl->alpn_chosen );
1738
1739 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1740 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1741
1742 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1743 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1744
1745 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1746
1747 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1748}
1749#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1750
Paul Bakker5121ce52009-01-03 21:22:43 +00001751static int ssl_write_server_hello( ssl_context *ssl )
1752{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001753#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001755#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001756 int ret;
1757 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001758 unsigned char *buf, *p;
1759
1760 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1761
Paul Bakkera9a028e2013-11-21 17:31:06 +01001762 if( ssl->f_rng == NULL )
1763 {
1764 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1765 return( POLARSSL_ERR_SSL_NO_RNG );
1766 }
1767
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 /*
1769 * 0 . 0 handshake type
1770 * 1 . 3 handshake length
1771 * 4 . 5 protocol version
1772 * 6 . 9 UNIX time()
1773 * 10 . 37 random bytes
1774 */
1775 buf = ssl->out_msg;
1776 p = buf + 4;
1777
1778 *p++ = (unsigned char) ssl->major_ver;
1779 *p++ = (unsigned char) ssl->minor_ver;
1780
1781 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1782 buf[4], buf[5] ) );
1783
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001784#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001785 t = time( NULL );
1786 *p++ = (unsigned char)( t >> 24 );
1787 *p++ = (unsigned char)( t >> 16 );
1788 *p++ = (unsigned char)( t >> 8 );
1789 *p++ = (unsigned char)( t );
1790
1791 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001792#else
1793 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1794 return( ret );
1795
1796 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001797#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Paul Bakkera3d195c2011-11-27 21:07:34 +00001799 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1800 return( ret );
1801
1802 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Paul Bakker48916f92012-09-16 19:57:18 +00001804 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
1806 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1807
1808 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001809 * Resume is 0 by default, see ssl_handshake_init().
1810 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1811 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001813 if( ssl->handshake->resume == 0 &&
1814 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001815 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001816 ssl->f_get_cache != NULL &&
1817 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1818 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001819 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001820 ssl->handshake->resume = 1;
1821 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001823 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001824 {
1825 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001826 * New session, create a new session id,
1827 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 ssl->state++;
1830
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001831#if defined(POLARSSL_HAVE_TIME)
1832 ssl->session_negotiate->start = time( NULL );
1833#endif
1834
Paul Bakkera503a632013-08-14 13:48:06 +02001835#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001836 if( ssl->handshake->new_session_ticket != 0 )
1837 {
1838 ssl->session_negotiate->length = n = 0;
1839 memset( ssl->session_negotiate->id, 0, 32 );
1840 }
1841 else
1842#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001843 {
1844 ssl->session_negotiate->length = n = 32;
1845 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001846 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001847 return( ret );
1848 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
1850 else
1851 {
1852 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001853 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001854 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001855 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001856 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001857
1858 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1859 {
1860 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1861 return( ret );
1862 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 }
1864
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001865 /*
1866 * 38 . 38 session id length
1867 * 39 . 38+n session id
1868 * 39+n . 40+n chosen ciphersuite
1869 * 41+n . 41+n chosen compression alg.
1870 * 42+n . 43+n extensions length
1871 * 44+n . 43+n+m extensions
1872 */
1873 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001874 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1875 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
1877 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1878 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1879 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001880 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Paul Bakker48916f92012-09-16 19:57:18 +00001882 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1883 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1884 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001886 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1887 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001888 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001889 ssl->session_negotiate->compression ) );
1890
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001891 /*
1892 * First write extensions, then the total length
1893 */
1894 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1895 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001896
Paul Bakker05decb22013-08-15 13:33:48 +02001897#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001898 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1899 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001900#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001901
Paul Bakker1f2bc622013-08-15 13:45:55 +02001902#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001903 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1904 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001905#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001906
Paul Bakkera503a632013-08-14 13:48:06 +02001907#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001908 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1909 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001910#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001911
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001912#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001913 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1914 ext_len += olen;
1915#endif
1916
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001917#if defined(POLARSSL_SSL_ALPN)
1918 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1919 ext_len += olen;
1920#endif
1921
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001922 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001923
Paul Bakkera7036632014-04-30 10:15:38 +02001924 if( ext_len > 0 )
1925 {
1926 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1927 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1928 p += ext_len;
1929 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
1931 ssl->out_msglen = p - buf;
1932 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1933 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1934
1935 ret = ssl_write_record( ssl );
1936
1937 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1938
1939 return( ret );
1940}
1941
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001942#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1943 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001944 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1945 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001946static int ssl_write_certificate_request( ssl_context *ssl )
1947{
Paul Bakkered27a042013-04-18 22:46:23 +02001948 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001949
1950 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1951
1952 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001953 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001954 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
1955 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001956 {
1957 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
1958 ssl->state++;
1959 return( 0 );
1960 }
1961
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02001962 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1963 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001964}
1965#else
1966static int ssl_write_certificate_request( ssl_context *ssl )
1967{
1968 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
1969 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02001970 size_t dn_size, total_dn_size; /* excluding length bytes */
1971 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00001972 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001973 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
1975 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1976
1977 ssl->state++;
1978
Paul Bakkerfbb17802013-04-17 19:10:21 +02001979 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01001980 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001981 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02001982 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02001983 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001985 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001986 return( 0 );
1987 }
1988
1989 /*
1990 * 0 . 0 handshake type
1991 * 1 . 3 handshake length
1992 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01001993 * 5 .. m-1 cert types
1994 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02001995 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 * n .. n+1 length of all DNs
1997 * n+2 .. n+3 length of DN 1
1998 * n+4 .. ... Distinguished Name #1
1999 * ... .. ... length of DN 2, etc.
2000 */
2001 buf = ssl->out_msg;
2002 p = buf + 4;
2003
2004 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002005 * Supported certificate types
2006 *
2007 * ClientCertificateType certificate_types<1..2^8-1>;
2008 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002009 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002010 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002011
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002012#if defined(POLARSSL_RSA_C)
2013 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2014#endif
2015#if defined(POLARSSL_ECDSA_C)
2016 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2017#endif
2018
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002019 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002020 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002021
Paul Bakker577e0062013-08-28 11:57:20 +02002022 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002023#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002024 /*
2025 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002026 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002027 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2028 *
2029 * struct {
2030 * HashAlgorithm hash;
2031 * SignatureAlgorithm signature;
2032 * } SignatureAndHashAlgorithm;
2033 *
2034 * enum { (255) } HashAlgorithm;
2035 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002036 */
Paul Bakker21dca692013-01-03 11:41:08 +01002037 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002038 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002039 /*
2040 * Only use current running hash algorithm that is already required
2041 * for requested ciphersuite.
2042 */
Paul Bakker926af752012-11-23 13:38:07 +01002043 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2044
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002045 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2046 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002047 {
2048 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2049 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002050
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002051 /*
2052 * Supported signature algorithms
2053 */
2054#if defined(POLARSSL_RSA_C)
2055 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2056 p[2 + sa_len++] = SSL_SIG_RSA;
2057#endif
2058#if defined(POLARSSL_ECDSA_C)
2059 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2060 p[2 + sa_len++] = SSL_SIG_ECDSA;
2061#endif
Paul Bakker926af752012-11-23 13:38:07 +01002062
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002063 p[0] = (unsigned char)( sa_len >> 8 );
2064 p[1] = (unsigned char)( sa_len );
2065 sa_len += 2;
2066 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002067 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002068#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002069
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002070 /*
2071 * DistinguishedName certificate_authorities<0..2^16-1>;
2072 * opaque DistinguishedName<1..2^16-1>;
2073 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002074 p += 2;
2075 crt = ssl->ca_chain;
2076
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002077 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002078 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002079 {
2080 if( p - buf > 4096 )
2081 break;
2082
Paul Bakker926af752012-11-23 13:38:07 +01002083 dn_size = crt->subject_raw.len;
2084 *p++ = (unsigned char)( dn_size >> 8 );
2085 *p++ = (unsigned char)( dn_size );
2086 memcpy( p, crt->subject_raw.p, dn_size );
2087 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002088
Paul Bakker926af752012-11-23 13:38:07 +01002089 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2090
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002091 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002092 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002093 }
2094
Paul Bakker926af752012-11-23 13:38:07 +01002095 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002096 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2097 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002098 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2099 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002100
2101 ret = ssl_write_record( ssl );
2102
2103 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2104
2105 return( ret );
2106}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002107#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2108 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002109 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2110 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002111
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002112#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2113 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2114static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2115{
2116 int ret;
2117
2118 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2119 {
2120 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2121 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2122 }
2123
2124 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2125 pk_ec( *ssl_own_key( ssl ) ),
2126 POLARSSL_ECDH_OURS ) ) != 0 )
2127 {
2128 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2129 return( ret );
2130 }
2131
2132 return( 0 );
2133}
2134#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2135 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2136
Paul Bakker41c83d32013-03-20 14:39:14 +01002137static int ssl_write_server_key_exchange( ssl_context *ssl )
2138{
Paul Bakker23986e52011-04-24 08:57:21 +00002139 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002140 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002141 const ssl_ciphersuite_t *ciphersuite_info =
2142 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002143
2144#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2145 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2146 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002147 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002148 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002149 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002150 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002151 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002152 ((void) dig_signed);
2153 ((void) dig_signed_len);
2154#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002155
Paul Bakker5121ce52009-01-03 21:22:43 +00002156 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2157
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002158#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2159 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2160 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002161 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2162 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2163 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002164 {
2165 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2166 ssl->state++;
2167 return( 0 );
2168 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002169#endif
2170
2171#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2172 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2173 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2174 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2175 {
2176 ssl_get_ecdh_params_from_cert( ssl );
2177
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002178 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002179 ssl->state++;
2180 return( 0 );
2181 }
2182#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002183
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002184#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2185 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2186 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2187 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002188 {
2189 /* TODO: Support identity hints */
2190 *(p++) = 0x00;
2191 *(p++) = 0x00;
2192
2193 n += 2;
2194 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002195#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2196 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002197
2198#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2199 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2200 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2201 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002202 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002203 /*
2204 * Ephemeral DH parameters:
2205 *
2206 * struct {
2207 * opaque dh_p<1..2^16-1>;
2208 * opaque dh_g<1..2^16-1>;
2209 * opaque dh_Ys<1..2^16-1>;
2210 * } ServerDHParams;
2211 */
2212 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2213 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2214 {
2215 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2216 return( ret );
2217 }
Paul Bakker48916f92012-09-16 19:57:18 +00002218
Paul Bakker41c83d32013-03-20 14:39:14 +01002219 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002220 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2221 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002222 {
2223 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2224 return( ret );
2225 }
2226
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002227 dig_signed = p;
2228 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002229
2230 p += len;
2231 n += len;
2232
Paul Bakker41c83d32013-03-20 14:39:14 +01002233 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2234 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2235 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2236 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2237 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002238#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2239 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002240
Gergely Budai987bfb52014-01-19 21:48:42 +01002241#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002242 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002243 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2244 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002245 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002246 /*
2247 * Ephemeral ECDH parameters:
2248 *
2249 * struct {
2250 * ECParameters curve_params;
2251 * ECPoint public;
2252 * } ServerECDHParams;
2253 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002254 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002255#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002256 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002257
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002258 /* Match our preference list against the offered curves */
2259 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2260 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2261 if( (*curve)->grp_id == *gid )
2262 goto curve_matching_done;
2263
2264curve_matching_done:
2265#else
2266 curve = ssl->handshake->curves;
2267#endif
2268
2269 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002270 {
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002271 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2272 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002273 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002274
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002275 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002276
Paul Bakker41c83d32013-03-20 14:39:14 +01002277 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b622014-02-06 10:13:09 +01002278 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002279 {
2280 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2281 return( ret );
2282 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002283
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002284 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2285 p, SSL_MAX_CONTENT_LEN - n,
2286 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002287 {
2288 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2289 return( ret );
2290 }
2291
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002292 dig_signed = p;
2293 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002294
2295 p += len;
2296 n += len;
2297
Paul Bakker41c83d32013-03-20 14:39:14 +01002298 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2299 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002300#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002301
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002302#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002303 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2304 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002305 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002306 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2307 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002308 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002309 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002310 unsigned int hashlen = 0;
2311 unsigned char hash[64];
2312 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002313
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002314 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002315 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2316 */
Paul Bakker577e0062013-08-28 11:57:20 +02002317#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002318 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2319 {
2320 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2321
2322 if( md_alg == POLARSSL_MD_NONE )
2323 {
2324 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002325 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002326 }
2327 }
Paul Bakker577e0062013-08-28 11:57:20 +02002328 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002329#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002330#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2331 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002332 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002333 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2334 {
2335 md_alg = POLARSSL_MD_SHA1;
2336 }
2337 else
Paul Bakker577e0062013-08-28 11:57:20 +02002338#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002339 {
2340 md_alg = POLARSSL_MD_NONE;
2341 }
2342
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002343 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002344 * Compute the hash to be signed
2345 */
Paul Bakker577e0062013-08-28 11:57:20 +02002346#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2347 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002348 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002349 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350 md5_context md5;
2351 sha1_context sha1;
2352
Paul Bakker5b4af392014-06-26 12:09:34 +02002353 md5_init( &md5 );
2354 sha1_init( &sha1 );
2355
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002356 /*
2357 * digitally-signed struct {
2358 * opaque md5_hash[16];
2359 * opaque sha_hash[20];
2360 * };
2361 *
2362 * md5_hash
2363 * MD5(ClientHello.random + ServerHello.random
2364 * + ServerParams);
2365 * sha_hash
2366 * SHA(ClientHello.random + ServerHello.random
2367 * + ServerParams);
2368 */
2369 md5_starts( &md5 );
2370 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002371 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002372 md5_finish( &md5, hash );
2373
2374 sha1_starts( &sha1 );
2375 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002376 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002377 sha1_finish( &sha1, hash + 16 );
2378
2379 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002380
2381 md5_free( &md5 );
2382 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002383 }
2384 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002385#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2386 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002387#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2388 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002389 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002390 {
2391 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002392 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002393
Paul Bakker84bbeb52014-07-01 14:53:22 +02002394 md_init( &ctx );
2395
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002396 /* Info from md_alg will be used instead */
2397 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398
2399 /*
2400 * digitally-signed struct {
2401 * opaque client_random[32];
2402 * opaque server_random[32];
2403 * ServerDHParams params;
2404 * };
2405 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002406 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002407 {
2408 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2409 return( ret );
2410 }
2411
2412 md_starts( &ctx );
2413 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002414 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002415 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002416 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002418 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002419#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2420 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002421 {
2422 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002423 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002424 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002425
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002426 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2427 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002428
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002429 /*
2430 * Make the signature
2431 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002432 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002433 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002434 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2435 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002436 }
Paul Bakker23f36802012-09-28 14:15:14 +00002437
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002438#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002439 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2440 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002442 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002443
2444 n += 2;
2445 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002446#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002447
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002448 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002449 p + 2 , &signature_len,
2450 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002451 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002452 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002453 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002454 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002455
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002456 *(p++) = (unsigned char)( signature_len >> 8 );
2457 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002458 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002459
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002460 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002461
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002462 p += signature_len;
2463 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002464 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002465#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002466 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2467 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002468
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002469 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2471 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2472
2473 ssl->state++;
2474
2475 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2476 {
2477 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2478 return( ret );
2479 }
2480
2481 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2482
2483 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002484}
2485
2486static int ssl_write_server_hello_done( ssl_context *ssl )
2487{
2488 int ret;
2489
2490 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2491
2492 ssl->out_msglen = 4;
2493 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2494 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2495
2496 ssl->state++;
2497
2498 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2499 {
2500 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2501 return( ret );
2502 }
2503
2504 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2505
2506 return( 0 );
2507}
2508
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002509#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2510 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2511static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2512 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002513{
2514 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002515 size_t n;
2516
2517 /*
2518 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2519 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002520 if( *p + 2 > end )
2521 {
2522 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2523 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2524 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002525
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002526 n = ( (*p)[0] << 8 ) | (*p)[1];
2527 *p += 2;
2528
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002529 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002530 {
2531 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2532 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2533 }
2534
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002535 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002536 {
2537 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2538 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2539 }
2540
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002541 *p += n;
2542
Paul Bakker70df2fb2013-04-17 17:19:09 +02002543 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2544
Paul Bakker70df2fb2013-04-17 17:19:09 +02002545 return( ret );
2546}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002547#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2548 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002549
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002550#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2551 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002552static int ssl_parse_encrypted_pms( ssl_context *ssl,
2553 const unsigned char *p,
2554 const unsigned char *end,
2555 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002556{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002557 int ret;
2558 size_t len = pk_get_len( ssl_own_key( ssl ) );
2559 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002560
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002561 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002562 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002563 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002564 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2565 }
2566
2567 /*
2568 * Decrypt the premaster using own private RSA key
2569 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002570#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2571 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002572 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2573 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002574 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2575 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002576 {
2577 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2578 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2579 }
2580 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002581#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002582
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002583 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002584 {
2585 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2586 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2587 }
2588
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002589 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2590 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002591 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002592 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002593
2594 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002595 pms[0] != ssl->handshake->max_major_ver ||
2596 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002597 {
2598 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2599
2600 /*
2601 * Protection against Bleichenbacher's attack:
2602 * invalid PKCS#1 v1.5 padding must not cause
2603 * the connection to end immediately; instead,
2604 * send a bad_record_mac later in the handshake.
2605 */
2606 ssl->handshake->pmslen = 48;
2607
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002608 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002609 if( ret != 0 )
2610 return( ret );
2611 }
2612
2613 return( ret );
2614}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002615#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2616 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002617
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002618#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002619static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2620 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002621{
Paul Bakker6db455e2013-09-18 17:29:31 +02002622 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002623 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002624
Paul Bakker6db455e2013-09-18 17:29:31 +02002625 if( ssl->f_psk == NULL &&
2626 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2627 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002628 {
2629 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2630 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2631 }
2632
2633 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002634 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002635 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002636 if( *p + 2 > end )
2637 {
2638 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2639 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2640 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002641
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002642 n = ( (*p)[0] << 8 ) | (*p)[1];
2643 *p += 2;
2644
2645 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002646 {
2647 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2648 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2649 }
2650
Paul Bakker6db455e2013-09-18 17:29:31 +02002651 if( ssl->f_psk != NULL )
2652 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002653 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002654 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2655 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002656 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002657 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002658 /* Identity is not a big secret since clients send it in the clear,
2659 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002660 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002661 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002662 {
2663 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2664 }
2665 }
2666
2667 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002668 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002669 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002670 if( ( ret = ssl_send_alert_message( ssl,
2671 SSL_ALERT_LEVEL_FATAL,
2672 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2673 {
2674 return( ret );
2675 }
2676
2677 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002678 }
2679
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002680 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002681
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002682 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002683}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002684#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002685
Paul Bakker5121ce52009-01-03 21:22:43 +00002686static int ssl_parse_client_key_exchange( ssl_context *ssl )
2687{
Paul Bakker23986e52011-04-24 08:57:21 +00002688 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002689 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002690
Paul Bakker41c83d32013-03-20 14:39:14 +01002691 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002692
2693 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2694
2695 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2696 {
2697 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2698 return( ret );
2699 }
2700
2701 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2702 {
2703 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002704 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002705 }
2706
2707 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2708 {
2709 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002710 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
2712
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002713#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002714 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002716 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002717 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002718
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002719 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002721 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2722 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002724
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002725 if( p != end )
2726 {
2727 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2728 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2729 }
2730
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002731 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002732
2733 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2734 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002735 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002736 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002737 {
2738 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2739 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2740 }
2741
2742 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002743 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002744 else
2745#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002746#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002747 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2748 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2749 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002750 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002751 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2752 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2753 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002754 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002755 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002756 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002757 {
2758 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2759 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2760 }
2761
2762 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2763
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002764 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2765 &ssl->handshake->pmslen,
2766 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002767 POLARSSL_MPI_MAX_SIZE,
2768 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002769 {
2770 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2771 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2772 }
2773
2774 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002775 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002776 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002777#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002778 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2779 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2780 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002781#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2782 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002783 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002784 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002785 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002786
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002787 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002788 {
2789 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2790 return( ret );
2791 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002793 if( p != end )
2794 {
2795 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2796 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2797 }
2798
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002799 if( ( ret = ssl_psk_derive_premaster( ssl,
2800 ciphersuite_info->key_exchange ) ) != 0 )
2801 {
2802 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2803 return( ret );
2804 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002805 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002806 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002807#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002808#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2809 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2810 {
2811 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002812 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002813
2814 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2815 {
2816 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2817 return( ret );
2818 }
2819
2820 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2821 {
2822 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2823 return( ret );
2824 }
2825
2826 if( ( ret = ssl_psk_derive_premaster( ssl,
2827 ciphersuite_info->key_exchange ) ) != 0 )
2828 {
2829 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2830 return( ret );
2831 }
2832 }
2833 else
2834#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2836 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2837 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002838 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002839 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002840
2841 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2842 {
2843 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2844 return( ret );
2845 }
2846 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2847 {
2848 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2849 return( ret );
2850 }
2851
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002852 if( p != end )
2853 {
2854 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2855 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2856 }
2857
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002858 if( ( ret = ssl_psk_derive_premaster( ssl,
2859 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002860 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002861 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2862 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002863 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002864 }
2865 else
2866#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002867#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2868 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2869 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002870 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002871 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002872
2873 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2874 {
2875 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2876 return( ret );
2877 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002878
2879 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2880 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002881 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002882 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2883 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002884 }
2885
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002886 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2887
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002888 if( ( ret = ssl_psk_derive_premaster( ssl,
2889 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002890 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002891 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002892 return( ret );
2893 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002894 }
2895 else
2896#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002897#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2898 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002899 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002900 if( ( ret = ssl_parse_encrypted_pms( ssl,
2901 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002902 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002903 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002904 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002905 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002906 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002907 }
2908 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002909 else
2910#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2911 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002912 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002913 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002914 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002915
Paul Bakkerff60ee62010-03-16 21:09:09 +00002916 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2917 {
2918 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2919 return( ret );
2920 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002921
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 ssl->state++;
2923
2924 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2925
2926 return( 0 );
2927}
2928
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002929#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2930 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002931 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2932 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002933static int ssl_parse_certificate_verify( ssl_context *ssl )
2934{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002935 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936
2937 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2938
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002939 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002940 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002941 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002942 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002943 {
2944 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2945 ssl->state++;
2946 return( 0 );
2947 }
2948
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002949 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2950 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002951}
2952#else
2953static int ssl_parse_certificate_verify( ssl_context *ssl )
2954{
2955 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002956 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002958 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002959 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02002960#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002961 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02002962#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002963 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002964 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
2965
2966 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2967
2968 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002969 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002970 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002971 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2972 {
2973 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2974 ssl->state++;
2975 return( 0 );
2976 }
2977
Paul Bakkered27a042013-04-18 22:46:23 +02002978 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00002979 {
2980 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2981 ssl->state++;
2982 return( 0 );
2983 }
2984
Paul Bakker48916f92012-09-16 19:57:18 +00002985 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
2987 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2988 {
2989 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2990 return( ret );
2991 }
2992
2993 ssl->state++;
2994
2995 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2996 {
2997 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002998 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00002999 }
3000
3001 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3002 {
3003 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003004 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003005 }
3006
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003007 /*
3008 * 0 . 0 handshake type
3009 * 1 . 3 handshake length
3010 * 4 . 5 sig alg (TLS 1.2 only)
3011 * 4+n . 5+n signature length (n = sa_len)
3012 * 6+n . 6+n+m signature (m = sig_len)
3013 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003014
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003015#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3016 defined(POLARSSL_SSL_PROTO_TLS1_1)
3017 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003018 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003019 sa_len = 0;
3020
Paul Bakkerc70b9822013-04-07 22:00:46 +02003021 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003022 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003023
3024 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3025 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3026 POLARSSL_PK_ECDSA ) )
3027 {
3028 hash_start += 16;
3029 hashlen -= 16;
3030 md_alg = POLARSSL_MD_SHA1;
3031 }
Paul Bakker926af752012-11-23 13:38:07 +01003032 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003033 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003034#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3035 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003036#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3037 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003038 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003039 sa_len = 2;
3040
Paul Bakker5121ce52009-01-03 21:22:43 +00003041 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003042 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003043 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003044 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003045 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003046 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3047 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003048 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3049 }
3050
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003051 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003052
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003053 /* Info from md_alg will be used instead */
3054 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003055
3056 /*
3057 * Signature
3058 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003059 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3060 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003061 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003062 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3063 " for verify message" ) );
3064 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003065 }
3066
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003067 /*
3068 * Check the certificate's key type matches the signature alg
3069 */
3070 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3071 {
3072 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3073 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3074 }
Paul Bakker577e0062013-08-28 11:57:20 +02003075 }
3076 else
3077#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3078 {
3079 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003080 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003081 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003082
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003083 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003084
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003085 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003086 {
3087 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003088 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 }
3090
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003091 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003092 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003093 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003094 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003095 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003096 return( ret );
3097 }
3098
3099 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3100
Paul Bakkered27a042013-04-18 22:46:23 +02003101 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003102}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003103#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3104 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3105 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Paul Bakkera503a632013-08-14 13:48:06 +02003107#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003108static int ssl_write_new_session_ticket( ssl_context *ssl )
3109{
3110 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003111 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003112 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003113
3114 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3115
3116 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3117 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3118
3119 /*
3120 * struct {
3121 * uint32 ticket_lifetime_hint;
3122 * opaque ticket<0..2^16-1>;
3123 * } NewSessionTicket;
3124 *
3125 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3126 * 8 . 9 ticket_len (n)
3127 * 10 . 9+n ticket content
3128 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003129
3130 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3131 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3132 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3133 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003134
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003135 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3136 {
3137 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3138 tlen = 0;
3139 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003140
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003141 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3142 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003143
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003144 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003145
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003146 /*
3147 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3148 * ChangeCipherSpec share the same state.
3149 */
3150 ssl->handshake->new_session_ticket = 0;
3151
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003152 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3153 {
3154 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3155 return( ret );
3156 }
3157
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003158 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3159
3160 return( 0 );
3161}
Paul Bakkera503a632013-08-14 13:48:06 +02003162#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003163
Paul Bakker5121ce52009-01-03 21:22:43 +00003164/*
Paul Bakker1961b702013-01-25 14:49:24 +01003165 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003166 */
Paul Bakker1961b702013-01-25 14:49:24 +01003167int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003168{
3169 int ret = 0;
3170
Paul Bakker1961b702013-01-25 14:49:24 +01003171 if( ssl->state == SSL_HANDSHAKE_OVER )
3172 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003173
Paul Bakker1961b702013-01-25 14:49:24 +01003174 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3175
3176 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3177 return( ret );
3178
3179 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003180 {
Paul Bakker1961b702013-01-25 14:49:24 +01003181 case SSL_HELLO_REQUEST:
3182 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003183 break;
3184
Paul Bakker1961b702013-01-25 14:49:24 +01003185 /*
3186 * <== ClientHello
3187 */
3188 case SSL_CLIENT_HELLO:
3189 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003190 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003191
3192 /*
3193 * ==> ServerHello
3194 * Certificate
3195 * ( ServerKeyExchange )
3196 * ( CertificateRequest )
3197 * ServerHelloDone
3198 */
3199 case SSL_SERVER_HELLO:
3200 ret = ssl_write_server_hello( ssl );
3201 break;
3202
3203 case SSL_SERVER_CERTIFICATE:
3204 ret = ssl_write_certificate( ssl );
3205 break;
3206
3207 case SSL_SERVER_KEY_EXCHANGE:
3208 ret = ssl_write_server_key_exchange( ssl );
3209 break;
3210
3211 case SSL_CERTIFICATE_REQUEST:
3212 ret = ssl_write_certificate_request( ssl );
3213 break;
3214
3215 case SSL_SERVER_HELLO_DONE:
3216 ret = ssl_write_server_hello_done( ssl );
3217 break;
3218
3219 /*
3220 * <== ( Certificate/Alert )
3221 * ClientKeyExchange
3222 * ( CertificateVerify )
3223 * ChangeCipherSpec
3224 * Finished
3225 */
3226 case SSL_CLIENT_CERTIFICATE:
3227 ret = ssl_parse_certificate( ssl );
3228 break;
3229
3230 case SSL_CLIENT_KEY_EXCHANGE:
3231 ret = ssl_parse_client_key_exchange( ssl );
3232 break;
3233
3234 case SSL_CERTIFICATE_VERIFY:
3235 ret = ssl_parse_certificate_verify( ssl );
3236 break;
3237
3238 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3239 ret = ssl_parse_change_cipher_spec( ssl );
3240 break;
3241
3242 case SSL_CLIENT_FINISHED:
3243 ret = ssl_parse_finished( ssl );
3244 break;
3245
3246 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003247 * ==> ( NewSessionTicket )
3248 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003249 * Finished
3250 */
3251 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003252#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003253 if( ssl->handshake->new_session_ticket != 0 )
3254 ret = ssl_write_new_session_ticket( ssl );
3255 else
Paul Bakkera503a632013-08-14 13:48:06 +02003256#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003257 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003258 break;
3259
3260 case SSL_SERVER_FINISHED:
3261 ret = ssl_write_finished( ssl );
3262 break;
3263
3264 case SSL_FLUSH_BUFFERS:
3265 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3266 ssl->state = SSL_HANDSHAKE_WRAPUP;
3267 break;
3268
3269 case SSL_HANDSHAKE_WRAPUP:
3270 ssl_handshake_wrapup( ssl );
3271 break;
3272
3273 default:
3274 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3275 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003276 }
3277
Paul Bakker5121ce52009-01-03 21:22:43 +00003278 return( ret );
3279}
Paul Bakker9af723c2014-05-01 13:03:14 +02003280#endif /* POLARSSL_SSL_SRV_C */