blob: 57b0ecf3001fe59799e2457fa69f6ca53bde904f [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é-Gonnardc3f6b62c2014-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/*
752 * Return 1 if the given EC key uses the given curve, 0 otherwise
753 */
754#if defined(POLARSSL_ECDSA_C)
755static int ssl_key_matches_curves( pk_context *pk,
756 const ecp_curve_info **curves )
757{
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 )
764 return( 1 );
765 crv++;
766 }
767
768 return( 0 );
769}
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)
812 if( pk_alg == POLARSSL_PK_ECDSA )
813 {
814 if( ssl_key_matches_curves( cur->key, ssl->handshake->curves ) )
815 break;
816 }
817 else
818#endif
819 break;
820 }
821
822 if( cur == NULL )
823 return( -1 );
824
825 ssl->handshake->key_cert = cur;
826 return( 0 );
827}
828#endif /* POLARSSL_X509_CRT_PARSE_C */
829
830/*
831 * Check if a given ciphersuite is suitable for use with our config/keys/etc
832 * Sets ciphersuite_info only if the suite matches.
833 */
834static int ssl_ciphersuite_match( ssl_context *ssl, int suite_id,
835 const ssl_ciphersuite_t **ciphersuite_info )
836{
837 const ssl_ciphersuite_t *suite_info;
838
839 suite_info = ssl_ciphersuite_from_id( suite_id );
840 if( suite_info == NULL )
841 {
842 SSL_DEBUG_MSG( 1, ( "ciphersuite info for %04x not found", suite_id ) );
843 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
844 }
845
846 if( suite_info->min_minor_ver > ssl->minor_ver ||
847 suite_info->max_minor_ver < ssl->minor_ver )
848 return( 0 );
849
850#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
851 if( ssl_ciphersuite_uses_ec( suite_info ) &&
852 ( ssl->handshake->curves == NULL ||
853 ssl->handshake->curves[0] == NULL ) )
854 return( 0 );
855#endif
856
857#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
858 /* If the ciphersuite requires a pre-shared key and we don't
859 * have one, skip it now rather than failing later */
860 if( ssl_ciphersuite_uses_psk( suite_info ) &&
861 ssl->f_psk == NULL &&
862 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
863 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
864 return( 0 );
865#endif
866
867#if defined(POLARSSL_X509_CRT_PARSE_C)
868 /*
869 * Final check: if ciphersuite requires us to have a
870 * certificate/key of a particular type:
871 * - select the appropriate certificate if we have one, or
872 * - try the next ciphersuite if we don't
873 * This must be done last since we modify the key_cert list.
874 */
875 if( ssl_pick_cert( ssl, suite_info ) != 0 )
876 return( 0 );
877#endif
878
879 *ciphersuite_info = suite_info;
880 return( 0 );
881}
882
Paul Bakker78a8c712013-03-06 17:01:52 +0100883#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
884static int ssl_parse_client_hello_v2( ssl_context *ssl )
885{
886 int ret;
887 unsigned int i, j;
888 size_t n;
889 unsigned int ciph_len, sess_len, chal_len;
890 unsigned char *buf, *p;
Paul Bakker8f4ddae2013-04-15 15:09:54 +0200891 const int *ciphersuites;
Paul Bakker59c28a22013-06-29 15:33:42 +0200892 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker78a8c712013-03-06 17:01:52 +0100893
894 SSL_DEBUG_MSG( 2, ( "=> parse client hello v2" ) );
895
896 if( ssl->renegotiation != SSL_INITIAL_HANDSHAKE )
897 {
898 SSL_DEBUG_MSG( 1, ( "client hello v2 illegal for renegotiation" ) );
899
900 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
901 return( ret );
902
903 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
904 }
905
906 buf = ssl->in_hdr;
907
908 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
909
910 SSL_DEBUG_MSG( 3, ( "client hello v2, message type: %d",
911 buf[2] ) );
912 SSL_DEBUG_MSG( 3, ( "client hello v2, message len.: %d",
913 ( ( buf[0] & 0x7F ) << 8 ) | buf[1] ) );
914 SSL_DEBUG_MSG( 3, ( "client hello v2, max. version: [%d:%d]",
915 buf[3], buf[4] ) );
916
917 /*
918 * SSLv2 Client Hello
919 *
920 * Record layer:
921 * 0 . 1 message length
922 *
923 * SSL layer:
924 * 2 . 2 message type
925 * 3 . 4 protocol version
926 */
927 if( buf[2] != SSL_HS_CLIENT_HELLO ||
928 buf[3] != SSL_MAJOR_VERSION_3 )
929 {
930 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
931 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
932 }
933
934 n = ( ( buf[0] << 8 ) | buf[1] ) & 0x7FFF;
935
936 if( n < 17 || n > 512 )
937 {
938 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
939 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
940 }
941
942 ssl->major_ver = SSL_MAJOR_VERSION_3;
Paul Bakker2fbefde2013-06-29 16:01:15 +0200943 ssl->minor_ver = ( buf[4] <= ssl->max_minor_ver )
944 ? buf[4] : ssl->max_minor_ver;
Paul Bakker78a8c712013-03-06 17:01:52 +0100945
946 if( ssl->minor_ver < ssl->min_minor_ver )
947 {
948 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200949 " [%d:%d] < [%d:%d]",
950 ssl->major_ver, ssl->minor_ver,
Paul Bakker78a8c712013-03-06 17:01:52 +0100951 ssl->min_major_ver, ssl->min_minor_ver ) );
952
953 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
954 SSL_ALERT_MSG_PROTOCOL_VERSION );
955 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
956 }
957
Paul Bakker2fbefde2013-06-29 16:01:15 +0200958 ssl->handshake->max_major_ver = buf[3];
959 ssl->handshake->max_minor_ver = buf[4];
Paul Bakker78a8c712013-03-06 17:01:52 +0100960
961 if( ( ret = ssl_fetch_input( ssl, 2 + n ) ) != 0 )
962 {
963 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
964 return( ret );
965 }
966
967 ssl->handshake->update_checksum( ssl, buf + 2, n );
968
969 buf = ssl->in_msg;
970 n = ssl->in_left - 5;
971
972 /*
973 * 0 . 1 ciphersuitelist length
974 * 2 . 3 session id length
975 * 4 . 5 challenge length
976 * 6 . .. ciphersuitelist
977 * .. . .. session id
978 * .. . .. challenge
979 */
980 SSL_DEBUG_BUF( 4, "record contents", buf, n );
981
982 ciph_len = ( buf[0] << 8 ) | buf[1];
983 sess_len = ( buf[2] << 8 ) | buf[3];
984 chal_len = ( buf[4] << 8 ) | buf[5];
985
986 SSL_DEBUG_MSG( 3, ( "ciph_len: %d, sess_len: %d, chal_len: %d",
987 ciph_len, sess_len, chal_len ) );
988
989 /*
990 * Make sure each parameter length is valid
991 */
992 if( ciph_len < 3 || ( ciph_len % 3 ) != 0 )
993 {
994 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
995 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
996 }
997
998 if( sess_len > 32 )
999 {
1000 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1001 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1002 }
1003
1004 if( chal_len < 8 || chal_len > 32 )
1005 {
1006 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1007 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1008 }
1009
1010 if( n != 6 + ciph_len + sess_len + chal_len )
1011 {
1012 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1013 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1014 }
1015
1016 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1017 buf + 6, ciph_len );
1018 SSL_DEBUG_BUF( 3, "client hello, session id",
1019 buf + 6 + ciph_len, sess_len );
1020 SSL_DEBUG_BUF( 3, "client hello, challenge",
1021 buf + 6 + ciph_len + sess_len, chal_len );
1022
1023 p = buf + 6 + ciph_len;
1024 ssl->session_negotiate->length = sess_len;
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001025 memset( ssl->session_negotiate->id, 0,
1026 sizeof( ssl->session_negotiate->id ) );
Paul Bakker78a8c712013-03-06 17:01:52 +01001027 memcpy( ssl->session_negotiate->id, p, ssl->session_negotiate->length );
1028
1029 p += sess_len;
1030 memset( ssl->handshake->randbytes, 0, 64 );
1031 memcpy( ssl->handshake->randbytes + 32 - chal_len, p, chal_len );
1032
1033 /*
1034 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1035 */
1036 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1037 {
1038 if( p[0] == 0 && p[1] == 0 && p[2] == SSL_EMPTY_RENEGOTIATION_INFO )
1039 {
1040 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1041 if( ssl->renegotiation == SSL_RENEGOTIATION )
1042 {
1043 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
1044
1045 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1046 return( ret );
1047
1048 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1049 }
1050 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1051 break;
1052 }
1053 }
1054
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001055#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1056 for( i = 0, p = buf + 6; i < ciph_len; i += 3, p += 3 )
1057 {
1058 if( p[0] == 0 &&
1059 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1060 p[2] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1061 {
1062 SSL_DEBUG_MSG( 3, ( "received FALLBACK_SCSV" ) );
1063
1064 if( ssl->minor_ver < ssl->max_minor_ver )
1065 {
1066 SSL_DEBUG_MSG( 1, ( "inapropriate fallback" ) );
1067
1068 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1069 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1070
1071 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1072 }
1073
1074 break;
1075 }
1076 }
1077#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1078
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001079 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001080 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001081#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1082 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
1083 {
1084 for( i = 0; ciphersuites[i] != 0; i++ )
1085#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001086 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker78a8c712013-03-06 17:01:52 +01001087 {
1088 for( j = 0, p = buf + 6; j < ciph_len; j += 3, p += 3 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001089#endif
Paul Bakker78a8c712013-03-06 17:01:52 +01001090 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001091 if( p[0] != 0 ||
1092 p[1] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1093 p[2] != ( ( ciphersuites[i] ) & 0xFF ) )
1094 continue;
Paul Bakker59c28a22013-06-29 15:33:42 +02001095
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001096 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1097 &ciphersuite_info ) ) != 0 )
1098 return( ret );
Paul Bakker59c28a22013-06-29 15:33:42 +02001099
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001100 if( ciphersuite_info != NULL )
Paul Bakker78a8c712013-03-06 17:01:52 +01001101 goto have_ciphersuite_v2;
1102 }
1103 }
1104
1105 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1106
1107 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1108
1109have_ciphersuite_v2:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001110 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker59c28a22013-06-29 15:33:42 +02001111 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
Paul Bakker41c83d32013-03-20 14:39:14 +01001112 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
Paul Bakker78a8c712013-03-06 17:01:52 +01001113
1114 /*
1115 * SSLv2 Client Hello relevant renegotiation security checks
1116 */
1117 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1118 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1119 {
1120 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1121
1122 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1123 return( ret );
1124
1125 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1126 }
1127
1128 ssl->in_left = 0;
1129 ssl->state++;
1130
1131 SSL_DEBUG_MSG( 2, ( "<= parse client hello v2" ) );
1132
1133 return( 0 );
1134}
1135#endif /* POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO */
1136
Paul Bakker5121ce52009-01-03 21:22:43 +00001137static int ssl_parse_client_hello( ssl_context *ssl )
1138{
Paul Bakker23986e52011-04-24 08:57:21 +00001139 int ret;
1140 unsigned int i, j;
1141 size_t n;
1142 unsigned int ciph_len, sess_len;
Paul Bakkerec636f32012-09-09 19:17:02 +00001143 unsigned int comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001144 unsigned int ext_len = 0;
1145 unsigned char *buf, *p, *ext;
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001146 int renegotiation_info_seen = 0;
1147 int handshake_failure = 0;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001148 const int *ciphersuites;
Paul Bakker41c83d32013-03-20 14:39:14 +01001149 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00001150
1151 SSL_DEBUG_MSG( 2, ( "=> parse client hello" ) );
1152
Paul Bakker48916f92012-09-16 19:57:18 +00001153 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1154 ( ret = ssl_fetch_input( ssl, 5 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 {
1156 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1157 return( ret );
1158 }
1159
1160 buf = ssl->in_hdr;
1161
Paul Bakker78a8c712013-03-06 17:01:52 +01001162#if defined(POLARSSL_SSL_SRV_SUPPORT_SSLV2_CLIENT_HELLO)
1163 if( ( buf[0] & 0x80 ) != 0 )
1164 return ssl_parse_client_hello_v2( ssl );
1165#endif
1166
Paul Bakkerec636f32012-09-09 19:17:02 +00001167 SSL_DEBUG_BUF( 4, "record header", buf, 5 );
1168
1169 SSL_DEBUG_MSG( 3, ( "client hello v3, message type: %d",
1170 buf[0] ) );
1171 SSL_DEBUG_MSG( 3, ( "client hello v3, message len.: %d",
1172 ( buf[3] << 8 ) | buf[4] ) );
1173 SSL_DEBUG_MSG( 3, ( "client hello v3, protocol ver: [%d:%d]",
1174 buf[1], buf[2] ) );
1175
1176 /*
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001177 * SSLv3/TLS Client Hello
Paul Bakkerec636f32012-09-09 19:17:02 +00001178 *
1179 * Record layer:
1180 * 0 . 0 message type
1181 * 1 . 2 protocol version
1182 * 3 . 4 message length
1183 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001184
1185 /* According to RFC 5246 Appendix E.1, the version here is typically
1186 * "{03,00}, the lowest version number supported by the client, [or] the
1187 * value of ClientHello.client_version", so the only meaningful check here
1188 * is the major version shouldn't be less than 3 */
Paul Bakkerec636f32012-09-09 19:17:02 +00001189 if( buf[0] != SSL_MSG_HANDSHAKE ||
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001190 buf[1] < SSL_MAJOR_VERSION_3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001191 {
Paul Bakkerec636f32012-09-09 19:17:02 +00001192 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1193 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1194 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001195
Paul Bakkerec636f32012-09-09 19:17:02 +00001196 n = ( buf[3] << 8 ) | buf[4];
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Paul Bakker4f42c112014-04-17 14:48:23 +02001198 if( n < 45 || n > SSL_MAX_CONTENT_LEN )
Paul Bakkerec636f32012-09-09 19:17:02 +00001199 {
1200 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1201 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1202 }
1203
Paul Bakker48916f92012-09-16 19:57:18 +00001204 if( ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
1205 ( ret = ssl_fetch_input( ssl, 5 + n ) ) != 0 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001206 {
1207 SSL_DEBUG_RET( 1, "ssl_fetch_input", ret );
1208 return( ret );
1209 }
1210
1211 buf = ssl->in_msg;
Paul Bakker48916f92012-09-16 19:57:18 +00001212 if( !ssl->renegotiation )
1213 n = ssl->in_left - 5;
1214 else
1215 n = ssl->in_msglen;
Paul Bakkerec636f32012-09-09 19:17:02 +00001216
Paul Bakker48916f92012-09-16 19:57:18 +00001217 ssl->handshake->update_checksum( ssl, buf, n );
Paul Bakkerec636f32012-09-09 19:17:02 +00001218
1219 /*
1220 * SSL layer:
1221 * 0 . 0 handshake type
1222 * 1 . 3 handshake length
1223 * 4 . 5 protocol version
1224 * 6 . 9 UNIX time()
1225 * 10 . 37 random bytes
1226 * 38 . 38 session id length
1227 * 39 . 38+x session id
1228 * 39+x . 40+x ciphersuitelist length
Paul Bakker0f651c72014-05-22 15:12:19 +02001229 * 41+x . 40+y ciphersuitelist
1230 * 41+y . 41+y compression alg length
1231 * 42+y . 41+z compression algs
Paul Bakkerec636f32012-09-09 19:17:02 +00001232 * .. . .. extensions
1233 */
1234 SSL_DEBUG_BUF( 4, "record contents", buf, n );
1235
1236 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake type: %d",
1237 buf[0] ) );
1238 SSL_DEBUG_MSG( 3, ( "client hello v3, handshake len.: %d",
1239 ( buf[1] << 16 ) | ( buf[2] << 8 ) | buf[3] ) );
1240 SSL_DEBUG_MSG( 3, ( "client hello v3, max. version: [%d:%d]",
1241 buf[4], buf[5] ) );
1242
1243 /*
1244 * Check the handshake type and protocol version
1245 */
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001246 if( buf[0] != SSL_HS_CLIENT_HELLO )
Paul Bakkerec636f32012-09-09 19:17:02 +00001247 {
1248 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1249 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1250 }
1251
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001252 ssl->major_ver = buf[4];
1253 ssl->minor_ver = buf[5];
Paul Bakkerec636f32012-09-09 19:17:02 +00001254
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001255 ssl->handshake->max_major_ver = ssl->major_ver;
1256 ssl->handshake->max_minor_ver = ssl->minor_ver;
1257
1258 if( ssl->major_ver < ssl->min_major_ver ||
1259 ssl->minor_ver < ssl->min_minor_ver )
Paul Bakker1d29fb52012-09-28 13:28:45 +00001260 {
1261 SSL_DEBUG_MSG( 1, ( "client only supports ssl smaller than minimum"
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001262 " [%d:%d] < [%d:%d]",
1263 ssl->major_ver, ssl->minor_ver,
Paul Bakker81420ab2012-10-23 10:31:15 +00001264 ssl->min_major_ver, ssl->min_minor_ver ) );
Paul Bakker1d29fb52012-09-28 13:28:45 +00001265
1266 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1267 SSL_ALERT_MSG_PROTOCOL_VERSION );
1268
1269 return( POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION );
1270 }
1271
Manuel Pégourié-Gonnard6b1e2072014-02-12 10:14:54 +01001272 if( ssl->major_ver > ssl->max_major_ver )
1273 {
1274 ssl->major_ver = ssl->max_major_ver;
1275 ssl->minor_ver = ssl->max_minor_ver;
1276 }
1277 else if( ssl->minor_ver > ssl->max_minor_ver )
1278 ssl->minor_ver = ssl->max_minor_ver;
Paul Bakkerec636f32012-09-09 19:17:02 +00001279
Paul Bakker48916f92012-09-16 19:57:18 +00001280 memcpy( ssl->handshake->randbytes, buf + 6, 32 );
Paul Bakkerec636f32012-09-09 19:17:02 +00001281
1282 /*
1283 * Check the handshake message length
1284 */
1285 if( buf[1] != 0 || n != (unsigned int) 4 + ( ( buf[2] << 8 ) | buf[3] ) )
1286 {
1287 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1288 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1289 }
1290
1291 /*
1292 * Check the session length
1293 */
1294 sess_len = buf[38];
1295
Paul Bakker0f651c72014-05-22 15:12:19 +02001296 if( sess_len > 32 || sess_len > n - 42 )
Paul Bakkerec636f32012-09-09 19:17:02 +00001297 {
1298 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1299 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1300 }
1301
Paul Bakker48916f92012-09-16 19:57:18 +00001302 ssl->session_negotiate->length = sess_len;
1303 memset( ssl->session_negotiate->id, 0,
1304 sizeof( ssl->session_negotiate->id ) );
1305 memcpy( ssl->session_negotiate->id, buf + 39,
1306 ssl->session_negotiate->length );
Paul Bakkerec636f32012-09-09 19:17:02 +00001307
1308 /*
1309 * Check the ciphersuitelist length
1310 */
1311 ciph_len = ( buf[39 + sess_len] << 8 )
1312 | ( buf[40 + sess_len] );
1313
Paul Bakker0f651c72014-05-22 15:12:19 +02001314 if( ciph_len < 2 || ( ciph_len % 2 ) != 0 || ciph_len > n - 42 - sess_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001315 {
1316 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1317 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1318 }
1319
1320 /*
1321 * Check the compression algorithms length
1322 */
1323 comp_len = buf[41 + sess_len + ciph_len];
1324
Paul Bakker0f651c72014-05-22 15:12:19 +02001325 if( comp_len < 1 || comp_len > 16 ||
1326 comp_len > n - 42 - sess_len - ciph_len )
Paul Bakkerec636f32012-09-09 19:17:02 +00001327 {
1328 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1329 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1330 }
1331
Paul Bakker48916f92012-09-16 19:57:18 +00001332 /*
1333 * Check the extension length
1334 */
1335 if( n > 42 + sess_len + ciph_len + comp_len )
1336 {
1337 ext_len = ( buf[42 + sess_len + ciph_len + comp_len] << 8 )
1338 | ( buf[43 + sess_len + ciph_len + comp_len] );
1339
1340 if( ( ext_len > 0 && ext_len < 4 ) ||
1341 n != 44 + sess_len + ciph_len + comp_len + ext_len )
1342 {
1343 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1344 SSL_DEBUG_BUF( 3, "Ext", buf + 44 + sess_len + ciph_len + comp_len, ext_len);
1345 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1346 }
1347 }
1348
1349 ssl->session_negotiate->compression = SSL_COMPRESS_NULL;
Paul Bakkerec636f32012-09-09 19:17:02 +00001350#if defined(POLARSSL_ZLIB_SUPPORT)
1351 for( i = 0; i < comp_len; ++i )
1352 {
Paul Bakker48916f92012-09-16 19:57:18 +00001353 if( buf[42 + sess_len + ciph_len + i] == SSL_COMPRESS_DEFLATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 {
Paul Bakker48916f92012-09-16 19:57:18 +00001355 ssl->session_negotiate->compression = SSL_COMPRESS_DEFLATE;
Paul Bakkerec636f32012-09-09 19:17:02 +00001356 break;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
1358 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001359#endif
1360
Paul Bakkerec636f32012-09-09 19:17:02 +00001361 SSL_DEBUG_BUF( 3, "client hello, random bytes",
1362 buf + 6, 32 );
1363 SSL_DEBUG_BUF( 3, "client hello, session id",
1364 buf + 38, sess_len );
1365 SSL_DEBUG_BUF( 3, "client hello, ciphersuitelist",
1366 buf + 41 + sess_len, ciph_len );
1367 SSL_DEBUG_BUF( 3, "client hello, compression",
1368 buf + 42 + sess_len + ciph_len, comp_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Paul Bakkerec636f32012-09-09 19:17:02 +00001370 /*
Paul Bakker48916f92012-09-16 19:57:18 +00001371 * Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV
1372 */
1373 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1374 {
1375 if( p[0] == 0 && p[1] == SSL_EMPTY_RENEGOTIATION_INFO )
1376 {
1377 SSL_DEBUG_MSG( 3, ( "received TLS_EMPTY_RENEGOTIATION_INFO " ) );
1378 if( ssl->renegotiation == SSL_RENEGOTIATION )
1379 {
1380 SSL_DEBUG_MSG( 1, ( "received RENEGOTIATION SCSV during renegotiation" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001381
1382 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1383 return( ret );
1384
Paul Bakker48916f92012-09-16 19:57:18 +00001385 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1386 }
1387 ssl->secure_renegotiation = SSL_SECURE_RENEGOTIATION;
1388 break;
1389 }
1390 }
1391
Manuel Pégourié-Gonnard01b26992014-10-20 14:05:28 +02001392#if defined(POLARSSL_SSL_FALLBACK_SCSV)
1393 for( i = 0, p = buf + 41 + sess_len; i < ciph_len; i += 2, p += 2 )
1394 {
1395 if( p[0] == (unsigned char)( ( SSL_FALLBACK_SCSV >> 8 ) & 0xff ) &&
1396 p[1] == (unsigned char)( ( SSL_FALLBACK_SCSV ) & 0xff ) )
1397 {
1398 SSL_DEBUG_MSG( 0, ( "received FALLBACK_SCSV" ) );
1399
1400 if( ssl->minor_ver < ssl->max_minor_ver )
1401 {
1402 SSL_DEBUG_MSG( 0, ( "inapropriate fallback" ) );
1403
1404 ssl_send_alert_message( ssl, SSL_ALERT_LEVEL_FATAL,
1405 SSL_ALERT_MSG_INAPROPRIATE_FALLBACK );
1406
1407 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1408 }
1409
1410 break;
1411 }
1412 }
1413#endif /* POLARSSL_SSL_FALLBACK_SCSV */
1414
Paul Bakker48916f92012-09-16 19:57:18 +00001415 ext = buf + 44 + sess_len + ciph_len + comp_len;
Paul Bakker48916f92012-09-16 19:57:18 +00001416
1417 while( ext_len )
1418 {
1419 unsigned int ext_id = ( ( ext[0] << 8 )
1420 | ( ext[1] ) );
1421 unsigned int ext_size = ( ( ext[2] << 8 )
1422 | ( ext[3] ) );
1423
1424 if( ext_size + 4 > ext_len )
1425 {
1426 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1427 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1428 }
1429 switch( ext_id )
1430 {
Paul Bakker0be444a2013-08-27 21:55:01 +02001431#if defined(POLARSSL_SSL_SERVER_NAME_INDICATION)
Paul Bakker5701cdc2012-09-27 21:49:42 +00001432 case TLS_EXT_SERVERNAME:
1433 SSL_DEBUG_MSG( 3, ( "found ServerName extension" ) );
1434 if( ssl->f_sni == NULL )
1435 break;
1436
1437 ret = ssl_parse_servername_ext( ssl, ext + 4, ext_size );
1438 if( ret != 0 )
1439 return( ret );
1440 break;
Paul Bakker0be444a2013-08-27 21:55:01 +02001441#endif /* POLARSSL_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00001442
Paul Bakker48916f92012-09-16 19:57:18 +00001443 case TLS_EXT_RENEGOTIATION_INFO:
1444 SSL_DEBUG_MSG( 3, ( "found renegotiation extension" ) );
1445 renegotiation_info_seen = 1;
1446
Paul Bakker23f36802012-09-28 14:15:14 +00001447 ret = ssl_parse_renegotiation_info( ssl, ext + 4, ext_size );
1448 if( ret != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001449 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00001450 break;
Paul Bakker48916f92012-09-16 19:57:18 +00001451
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001452#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00001453 case TLS_EXT_SIG_ALG:
1454 SSL_DEBUG_MSG( 3, ( "found signature_algorithms extension" ) );
1455 if( ssl->renegotiation == SSL_RENEGOTIATION )
1456 break;
1457
1458 ret = ssl_parse_signature_algorithms_ext( ssl, ext + 4, ext_size );
1459 if( ret != 0 )
1460 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00001461 break;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001462#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001463
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001464#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Paul Bakker41c83d32013-03-20 14:39:14 +01001465 case TLS_EXT_SUPPORTED_ELLIPTIC_CURVES:
1466 SSL_DEBUG_MSG( 3, ( "found supported elliptic curves extension" ) );
1467
1468 ret = ssl_parse_supported_elliptic_curves( ssl, ext + 4, ext_size );
1469 if( ret != 0 )
1470 return( ret );
1471 break;
1472
1473 case TLS_EXT_SUPPORTED_POINT_FORMATS:
1474 SSL_DEBUG_MSG( 3, ( "found supported point formats extension" ) );
Paul Bakker677377f2013-10-28 12:54:26 +01001475 ssl->handshake->cli_exts |= TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT;
Paul Bakker41c83d32013-03-20 14:39:14 +01001476
1477 ret = ssl_parse_supported_point_formats( ssl, ext + 4, ext_size );
1478 if( ret != 0 )
1479 return( ret );
1480 break;
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001481#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Paul Bakker41c83d32013-03-20 14:39:14 +01001482
Paul Bakker05decb22013-08-15 13:33:48 +02001483#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001484 case TLS_EXT_MAX_FRAGMENT_LENGTH:
1485 SSL_DEBUG_MSG( 3, ( "found max fragment length extension" ) );
1486
1487 ret = ssl_parse_max_fragment_length_ext( ssl, ext + 4, ext_size );
1488 if( ret != 0 )
1489 return( ret );
1490 break;
Paul Bakker05decb22013-08-15 13:33:48 +02001491#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard48f8d0d2013-07-17 10:25:37 +02001492
Paul Bakker1f2bc622013-08-15 13:45:55 +02001493#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001494 case TLS_EXT_TRUNCATED_HMAC:
1495 SSL_DEBUG_MSG( 3, ( "found truncated hmac extension" ) );
1496
1497 ret = ssl_parse_truncated_hmac_ext( ssl, ext + 4, ext_size );
1498 if( ret != 0 )
1499 return( ret );
1500 break;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001501#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001502
Paul Bakkera503a632013-08-14 13:48:06 +02001503#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001504 case TLS_EXT_SESSION_TICKET:
1505 SSL_DEBUG_MSG( 3, ( "found session ticket extension" ) );
1506
1507 ret = ssl_parse_session_ticket_ext( ssl, ext + 4, ext_size );
1508 if( ret != 0 )
1509 return( ret );
1510 break;
Paul Bakkera503a632013-08-14 13:48:06 +02001511#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001512
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001513#if defined(POLARSSL_SSL_ALPN)
1514 case TLS_EXT_ALPN:
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001515 SSL_DEBUG_MSG( 3, ( "found alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001516
1517 ret = ssl_parse_alpn_ext( ssl, ext + 4, ext_size );
1518 if( ret != 0 )
1519 return( ret );
1520 break;
1521#endif /* POLARSSL_SSL_SESSION_TICKETS */
1522
Paul Bakker48916f92012-09-16 19:57:18 +00001523 default:
1524 SSL_DEBUG_MSG( 3, ( "unknown extension found: %d (ignoring)",
1525 ext_id ) );
1526 }
1527
1528 ext_len -= 4 + ext_size;
1529 ext += 4 + ext_size;
1530
1531 if( ext_len > 0 && ext_len < 4 )
1532 {
1533 SSL_DEBUG_MSG( 1, ( "bad client hello message" ) );
1534 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1535 }
1536 }
1537
1538 /*
1539 * Renegotiation security checks
1540 */
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001541 if( ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1542 ssl->allow_legacy_renegotiation == SSL_LEGACY_BREAK_HANDSHAKE )
1543 {
1544 SSL_DEBUG_MSG( 1, ( "legacy renegotiation, breaking off handshake" ) );
1545 handshake_failure = 1;
1546 }
1547 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1548 ssl->secure_renegotiation == SSL_SECURE_RENEGOTIATION &&
1549 renegotiation_info_seen == 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00001550 {
1551 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension missing (secure)" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001552 handshake_failure = 1;
Paul Bakker48916f92012-09-16 19:57:18 +00001553 }
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001554 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1555 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1556 ssl->allow_legacy_renegotiation == SSL_LEGACY_NO_RENEGOTIATION )
Paul Bakker48916f92012-09-16 19:57:18 +00001557 {
1558 SSL_DEBUG_MSG( 1, ( "legacy renegotiation not allowed" ) );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00001559 handshake_failure = 1;
1560 }
1561 else if( ssl->renegotiation == SSL_RENEGOTIATION &&
1562 ssl->secure_renegotiation == SSL_LEGACY_RENEGOTIATION &&
1563 renegotiation_info_seen == 1 )
1564 {
1565 SSL_DEBUG_MSG( 1, ( "renegotiation_info extension present (legacy)" ) );
1566 handshake_failure = 1;
1567 }
1568
1569 if( handshake_failure == 1 )
1570 {
1571 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1572 return( ret );
1573
Paul Bakker48916f92012-09-16 19:57:18 +00001574 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO );
1575 }
Paul Bakker380da532012-04-18 16:10:25 +00001576
Paul Bakker41c83d32013-03-20 14:39:14 +01001577 /*
1578 * Search for a matching ciphersuite
Manuel Pégourié-Gonnard3ebb2cd2013-09-23 17:00:18 +02001579 * (At the end because we need information from the EC-based extensions
1580 * and certificate from the SNI callback triggered by the SNI extension.)
Paul Bakker41c83d32013-03-20 14:39:14 +01001581 */
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001582 ciphersuites = ssl->ciphersuite_list[ssl->minor_ver];
Manuel Pégourié-Gonnard59b81d72013-11-30 17:46:04 +01001583 ciphersuite_info = NULL;
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001584#if defined(POLARSSL_SSL_SRV_RESPECT_CLIENT_PREFERENCE)
1585 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
1586 {
1587 for( i = 0; ciphersuites[i] != 0; i++ )
1588#else
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001589 for( i = 0; ciphersuites[i] != 0; i++ )
Paul Bakker41c83d32013-03-20 14:39:14 +01001590 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001591 for( j = 0, p = buf + 41 + sess_len; j < ciph_len; j += 2, p += 2 )
Manuel Pégourié-Gonnard1a9f2c72013-11-30 18:30:06 +01001592#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01001593 {
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001594 if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) ||
1595 p[1] != ( ( ciphersuites[i] ) & 0xFF ) )
1596 continue;
Paul Bakker41c83d32013-03-20 14:39:14 +01001597
Manuel Pégourié-Gonnard011a8db2013-11-30 18:11:07 +01001598 if( ( ret = ssl_ciphersuite_match( ssl, ciphersuites[i],
1599 &ciphersuite_info ) ) != 0 )
1600 return( ret );
1601
1602 if( ciphersuite_info != NULL )
1603 goto have_ciphersuite;
Paul Bakker41c83d32013-03-20 14:39:14 +01001604 }
1605 }
1606
1607 SSL_DEBUG_MSG( 1, ( "got no ciphersuites in common" ) );
1608
1609 if( ( ret = ssl_send_fatal_handshake_failure( ssl ) ) != 0 )
1610 return( ret );
1611
1612 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
1613
1614have_ciphersuite:
Paul Bakker8f4ddae2013-04-15 15:09:54 +02001615 ssl->session_negotiate->ciphersuite = ciphersuites[i];
Paul Bakker41c83d32013-03-20 14:39:14 +01001616 ssl->transform_negotiate->ciphersuite_info = ciphersuite_info;
1617 ssl_optimize_checksum( ssl, ssl->transform_negotiate->ciphersuite_info );
1618
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 ssl->in_left = 0;
1620 ssl->state++;
1621
1622 SSL_DEBUG_MSG( 2, ( "<= parse client hello" ) );
1623
1624 return( 0 );
1625}
1626
Paul Bakker1f2bc622013-08-15 13:45:55 +02001627#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001628static void ssl_write_truncated_hmac_ext( ssl_context *ssl,
1629 unsigned char *buf,
1630 size_t *olen )
1631{
1632 unsigned char *p = buf;
1633
1634 if( ssl->session_negotiate->trunc_hmac == SSL_TRUNC_HMAC_DISABLED )
1635 {
1636 *olen = 0;
1637 return;
1638 }
1639
1640 SSL_DEBUG_MSG( 3, ( "server hello, adding truncated hmac extension" ) );
1641
1642 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC >> 8 ) & 0xFF );
1643 *p++ = (unsigned char)( ( TLS_EXT_TRUNCATED_HMAC ) & 0xFF );
1644
1645 *p++ = 0x00;
1646 *p++ = 0x00;
1647
1648 *olen = 4;
1649}
Paul Bakker1f2bc622013-08-15 13:45:55 +02001650#endif /* POLARSSL_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001651
Paul Bakkera503a632013-08-14 13:48:06 +02001652#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001653static void ssl_write_session_ticket_ext( ssl_context *ssl,
1654 unsigned char *buf,
1655 size_t *olen )
1656{
1657 unsigned char *p = buf;
1658
1659 if( ssl->handshake->new_session_ticket == 0 )
1660 {
1661 *olen = 0;
1662 return;
1663 }
1664
1665 SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) );
1666
1667 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF );
1668 *p++ = (unsigned char)( ( TLS_EXT_SESSION_TICKET ) & 0xFF );
1669
1670 *p++ = 0x00;
1671 *p++ = 0x00;
1672
1673 *olen = 4;
1674}
Paul Bakkera503a632013-08-14 13:48:06 +02001675#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001676
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001677static void ssl_write_renegotiation_ext( ssl_context *ssl,
1678 unsigned char *buf,
1679 size_t *olen )
1680{
1681 unsigned char *p = buf;
1682
1683 if( ssl->secure_renegotiation != SSL_SECURE_RENEGOTIATION )
1684 {
1685 *olen = 0;
1686 return;
1687 }
1688
1689 SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) );
1690
1691 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF );
1692 *p++ = (unsigned char)( ( TLS_EXT_RENEGOTIATION_INFO ) & 0xFF );
1693
1694 *p++ = 0x00;
1695 *p++ = ( ssl->verify_data_len * 2 + 1 ) & 0xFF;
1696 *p++ = ssl->verify_data_len * 2 & 0xFF;
1697
1698 memcpy( p, ssl->peer_verify_data, ssl->verify_data_len );
1699 p += ssl->verify_data_len;
1700 memcpy( p, ssl->own_verify_data, ssl->verify_data_len );
1701 p += ssl->verify_data_len;
1702
1703 *olen = 5 + ssl->verify_data_len * 2;
1704}
1705
Paul Bakker05decb22013-08-15 13:33:48 +02001706#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001707static void ssl_write_max_fragment_length_ext( ssl_context *ssl,
1708 unsigned char *buf,
1709 size_t *olen )
1710{
1711 unsigned char *p = buf;
1712
Manuel Pégourié-Gonnarde048b672013-07-19 12:47:00 +02001713 if( ssl->session_negotiate->mfl_code == SSL_MAX_FRAG_LEN_NONE )
1714 {
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001715 *olen = 0;
1716 return;
1717 }
1718
1719 SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) );
1720
1721 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF );
1722 *p++ = (unsigned char)( ( TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF );
1723
1724 *p++ = 0x00;
1725 *p++ = 1;
1726
Manuel Pégourié-Gonnarded4af8b2013-07-18 14:07:09 +02001727 *p++ = ssl->session_negotiate->mfl_code;
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001728
1729 *olen = 5;
1730}
Paul Bakker05decb22013-08-15 13:33:48 +02001731#endif /* POLARSSL_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001732
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001733#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001734static void ssl_write_supported_point_formats_ext( ssl_context *ssl,
1735 unsigned char *buf,
1736 size_t *olen )
1737{
1738 unsigned char *p = buf;
1739 ((void) ssl);
1740
Paul Bakker677377f2013-10-28 12:54:26 +01001741 if( ( ssl->handshake->cli_exts &
1742 TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT ) == 0 )
1743 {
1744 *olen = 0;
1745 return;
1746 }
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001747
1748 SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) );
1749
1750 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF );
1751 *p++ = (unsigned char)( ( TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF );
1752
1753 *p++ = 0x00;
1754 *p++ = 2;
1755
1756 *p++ = 1;
1757 *p++ = POLARSSL_ECP_PF_UNCOMPRESSED;
1758
1759 *olen = 6;
1760}
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001761#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001762
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001763#if defined(POLARSSL_SSL_ALPN )
1764static void ssl_write_alpn_ext( ssl_context *ssl,
1765 unsigned char *buf, size_t *olen )
1766{
1767 if( ssl->alpn_chosen == NULL )
1768 {
1769 *olen = 0;
1770 return;
1771 }
1772
Manuel Pégourié-Gonnardf6521de2014-04-07 12:42:04 +02001773 SSL_DEBUG_MSG( 3, ( "server hello, adding alpn extension" ) );
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001774
1775 /*
1776 * 0 . 1 ext identifier
1777 * 2 . 3 ext length
1778 * 4 . 5 protocol list length
1779 * 6 . 6 protocol name length
1780 * 7 . 7+n protocol name
1781 */
1782 buf[0] = (unsigned char)( ( TLS_EXT_ALPN >> 8 ) & 0xFF );
1783 buf[1] = (unsigned char)( ( TLS_EXT_ALPN ) & 0xFF );
1784
1785 *olen = 7 + strlen( ssl->alpn_chosen );
1786
1787 buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF );
1788 buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF );
1789
1790 buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF );
1791 buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF );
1792
1793 buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF );
1794
1795 memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 );
1796}
1797#endif /* POLARSSL_ECDH_C || POLARSSL_ECDSA_C */
1798
Paul Bakker5121ce52009-01-03 21:22:43 +00001799static int ssl_write_server_hello( ssl_context *ssl )
1800{
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001801#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001802 time_t t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001803#endif
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001804 int ret;
1805 size_t olen, ext_len = 0, n;
Paul Bakker5121ce52009-01-03 21:22:43 +00001806 unsigned char *buf, *p;
1807
1808 SSL_DEBUG_MSG( 2, ( "=> write server hello" ) );
1809
Paul Bakkera9a028e2013-11-21 17:31:06 +01001810 if( ssl->f_rng == NULL )
1811 {
1812 SSL_DEBUG_MSG( 1, ( "no RNG provided") );
1813 return( POLARSSL_ERR_SSL_NO_RNG );
1814 }
1815
Paul Bakker5121ce52009-01-03 21:22:43 +00001816 /*
1817 * 0 . 0 handshake type
1818 * 1 . 3 handshake length
1819 * 4 . 5 protocol version
1820 * 6 . 9 UNIX time()
1821 * 10 . 37 random bytes
1822 */
1823 buf = ssl->out_msg;
1824 p = buf + 4;
1825
1826 *p++ = (unsigned char) ssl->major_ver;
1827 *p++ = (unsigned char) ssl->minor_ver;
1828
1829 SSL_DEBUG_MSG( 3, ( "server hello, chosen version: [%d:%d]",
1830 buf[4], buf[5] ) );
1831
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001832#if defined(POLARSSL_HAVE_TIME)
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 t = time( NULL );
1834 *p++ = (unsigned char)( t >> 24 );
1835 *p++ = (unsigned char)( t >> 16 );
1836 *p++ = (unsigned char)( t >> 8 );
1837 *p++ = (unsigned char)( t );
1838
1839 SSL_DEBUG_MSG( 3, ( "server hello, current time: %lu", t ) );
Paul Bakkerfa9b1002013-07-03 15:31:03 +02001840#else
1841 if( ( ret = ssl->f_rng( ssl->p_rng, p, 4 ) ) != 0 )
1842 return( ret );
1843
1844 p += 4;
Paul Bakker9af723c2014-05-01 13:03:14 +02001845#endif /* POLARSSL_HAVE_TIME */
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Paul Bakkera3d195c2011-11-27 21:07:34 +00001847 if( ( ret = ssl->f_rng( ssl->p_rng, p, 28 ) ) != 0 )
1848 return( ret );
1849
1850 p += 28;
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
Paul Bakker48916f92012-09-16 19:57:18 +00001852 memcpy( ssl->handshake->randbytes + 32, buf + 6, 32 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
1854 SSL_DEBUG_BUF( 3, "server hello, random bytes", buf + 6, 32 );
1855
1856 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001857 * Resume is 0 by default, see ssl_handshake_init().
1858 * It may be already set to 1 by ssl_parse_session_ticket_ext().
1859 * If not, try looking up session ID in our cache.
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001861 if( ssl->handshake->resume == 0 &&
1862 ssl->renegotiation == SSL_INITIAL_HANDSHAKE &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02001863 ssl->session_negotiate->length != 0 &&
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001864 ssl->f_get_cache != NULL &&
1865 ssl->f_get_cache( ssl->p_get_cache, ssl->session_negotiate ) == 0 )
1866 {
Manuel Pégourié-Gonnardf7c52012014-02-20 11:43:46 +01001867 SSL_DEBUG_MSG( 3, ( "session successfully restored from cache" ) );
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001868 ssl->handshake->resume = 1;
1869 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001871 if( ssl->handshake->resume == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 {
1873 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001874 * New session, create a new session id,
1875 * unless we're about to issue a session ticket
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001877 ssl->state++;
1878
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001879#if defined(POLARSSL_HAVE_TIME)
1880 ssl->session_negotiate->start = time( NULL );
1881#endif
1882
Paul Bakkera503a632013-08-14 13:48:06 +02001883#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02001884 if( ssl->handshake->new_session_ticket != 0 )
1885 {
1886 ssl->session_negotiate->length = n = 0;
1887 memset( ssl->session_negotiate->id, 0, 32 );
1888 }
1889 else
1890#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001891 {
1892 ssl->session_negotiate->length = n = 32;
1893 if( ( ret = ssl->f_rng( ssl->p_rng, ssl->session_negotiate->id,
Paul Bakkera503a632013-08-14 13:48:06 +02001894 n ) ) != 0 )
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001895 return( ret );
1896 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 }
1898 else
1899 {
1900 /*
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001901 * Resuming a session
Paul Bakker5121ce52009-01-03 21:22:43 +00001902 */
Paul Bakkerf0e39ac2013-08-15 11:40:48 +02001903 n = ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 ssl->state = SSL_SERVER_CHANGE_CIPHER_SPEC;
Paul Bakkerff60ee62010-03-16 21:09:09 +00001905
1906 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
1907 {
1908 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
1909 return( ret );
1910 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 }
1912
Manuel Pégourié-Gonnard3ffa3db2013-08-02 11:59:05 +02001913 /*
1914 * 38 . 38 session id length
1915 * 39 . 38+n session id
1916 * 39+n . 40+n chosen ciphersuite
1917 * 41+n . 41+n chosen compression alg.
1918 * 42+n . 43+n extensions length
1919 * 44+n . 43+n+m extensions
1920 */
1921 *p++ = (unsigned char) ssl->session_negotiate->length;
Paul Bakker48916f92012-09-16 19:57:18 +00001922 memcpy( p, ssl->session_negotiate->id, ssl->session_negotiate->length );
1923 p += ssl->session_negotiate->length;
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
1925 SSL_DEBUG_MSG( 3, ( "server hello, session id len.: %d", n ) );
1926 SSL_DEBUG_BUF( 3, "server hello, session id", buf + 39, n );
1927 SSL_DEBUG_MSG( 3, ( "%s session has been resumed",
Paul Bakker0a597072012-09-25 21:55:46 +00001928 ssl->handshake->resume ? "a" : "no" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Paul Bakker48916f92012-09-16 19:57:18 +00001930 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 );
1931 *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite );
1932 *p++ = (unsigned char)( ssl->session_negotiate->compression );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Manuel Pégourié-Gonnard51451f82013-09-17 12:06:25 +02001934 SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s",
1935 ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) );
Manuel Pégourié-Gonnard32ea60a2013-08-17 17:39:04 +02001936 SSL_DEBUG_MSG( 3, ( "server hello, compress alg.: 0x%02X",
Paul Bakker48916f92012-09-16 19:57:18 +00001937 ssl->session_negotiate->compression ) );
1938
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001939 /*
1940 * First write extensions, then the total length
1941 */
1942 ssl_write_renegotiation_ext( ssl, p + 2 + ext_len, &olen );
1943 ext_len += olen;
Paul Bakker48916f92012-09-16 19:57:18 +00001944
Paul Bakker05decb22013-08-15 13:33:48 +02001945#if defined(POLARSSL_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001946 ssl_write_max_fragment_length_ext( ssl, p + 2 + ext_len, &olen );
1947 ext_len += olen;
Paul Bakker05decb22013-08-15 13:33:48 +02001948#endif
Manuel Pégourié-Gonnard7bb78992013-07-17 13:50:08 +02001949
Paul Bakker1f2bc622013-08-15 13:45:55 +02001950#if defined(POLARSSL_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001951 ssl_write_truncated_hmac_ext( ssl, p + 2 + ext_len, &olen );
1952 ext_len += olen;
Paul Bakker1f2bc622013-08-15 13:45:55 +02001953#endif
Manuel Pégourié-Gonnard57c28522013-07-19 11:41:43 +02001954
Paul Bakkera503a632013-08-14 13:48:06 +02001955#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001956 ssl_write_session_ticket_ext( ssl, p + 2 + ext_len, &olen );
1957 ext_len += olen;
Paul Bakkera503a632013-08-14 13:48:06 +02001958#endif
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02001959
Manuel Pégourié-Gonnard0b272672013-08-15 19:38:07 +02001960#if defined(POLARSSL_ECDH_C) || defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnard7b19c162013-08-15 18:01:11 +02001961 ssl_write_supported_point_formats_ext( ssl, p + 2 + ext_len, &olen );
1962 ext_len += olen;
1963#endif
1964
Manuel Pégourié-Gonnard89e35792014-04-07 12:10:30 +02001965#if defined(POLARSSL_SSL_ALPN)
1966 ssl_write_alpn_ext( ssl, p + 2 + ext_len, &olen );
1967 ext_len += olen;
1968#endif
1969
Manuel Pégourié-Gonnardf11a6d72013-07-17 11:17:14 +02001970 SSL_DEBUG_MSG( 3, ( "server hello, total extension length: %d", ext_len ) );
Paul Bakker48916f92012-09-16 19:57:18 +00001971
Paul Bakkera7036632014-04-30 10:15:38 +02001972 if( ext_len > 0 )
1973 {
1974 *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF );
1975 *p++ = (unsigned char)( ( ext_len ) & 0xFF );
1976 p += ext_len;
1977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001978
1979 ssl->out_msglen = p - buf;
1980 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
1981 ssl->out_msg[0] = SSL_HS_SERVER_HELLO;
1982
1983 ret = ssl_write_record( ssl );
1984
1985 SSL_DEBUG_MSG( 2, ( "<= write server hello" ) );
1986
1987 return( ret );
1988}
1989
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001990#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
1991 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02001992 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
1993 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00001994static int ssl_write_certificate_request( ssl_context *ssl )
1995{
Paul Bakkered27a042013-04-18 22:46:23 +02001996 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02001997
1998 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
1999
2000 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002001 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002002 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2003 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002004 {
2005 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
2006 ssl->state++;
2007 return( 0 );
2008 }
2009
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002010 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2011 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002012}
2013#else
2014static int ssl_write_certificate_request( ssl_context *ssl )
2015{
2016 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
2017 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002018 size_t dn_size, total_dn_size; /* excluding length bytes */
2019 size_t ct_len, sa_len; /* including length bytes */
Paul Bakker5121ce52009-01-03 21:22:43 +00002020 unsigned char *buf, *p;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002021 const x509_crt *crt;
Paul Bakker5121ce52009-01-03 21:22:43 +00002022
2023 SSL_DEBUG_MSG( 2, ( "=> write certificate request" ) );
2024
2025 ssl->state++;
2026
Paul Bakkerfbb17802013-04-17 19:10:21 +02002027 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002028 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002029 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002030 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakkerfbb17802013-04-17 19:10:21 +02002031 ssl->authmode == SSL_VERIFY_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00002032 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002033 SSL_DEBUG_MSG( 2, ( "<= skip write certificate request" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002034 return( 0 );
2035 }
2036
2037 /*
2038 * 0 . 0 handshake type
2039 * 1 . 3 handshake length
2040 * 4 . 4 cert type count
Paul Bakker926af752012-11-23 13:38:07 +01002041 * 5 .. m-1 cert types
2042 * m .. m+1 sig alg length (TLS 1.2 only)
Paul Bakker9af723c2014-05-01 13:03:14 +02002043 * m+1 .. n-1 SignatureAndHashAlgorithms (TLS 1.2 only)
Paul Bakker5121ce52009-01-03 21:22:43 +00002044 * n .. n+1 length of all DNs
2045 * n+2 .. n+3 length of DN 1
2046 * n+4 .. ... Distinguished Name #1
2047 * ... .. ... length of DN 2, etc.
2048 */
2049 buf = ssl->out_msg;
2050 p = buf + 4;
2051
2052 /*
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002053 * Supported certificate types
2054 *
2055 * ClientCertificateType certificate_types<1..2^8-1>;
2056 * enum { (255) } ClientCertificateType;
Paul Bakker5121ce52009-01-03 21:22:43 +00002057 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002058 ct_len = 0;
Paul Bakker926af752012-11-23 13:38:07 +01002059
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002060#if defined(POLARSSL_RSA_C)
2061 p[1 + ct_len++] = SSL_CERT_TYPE_RSA_SIGN;
2062#endif
2063#if defined(POLARSSL_ECDSA_C)
2064 p[1 + ct_len++] = SSL_CERT_TYPE_ECDSA_SIGN;
2065#endif
2066
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02002067 p[0] = (unsigned char) ct_len++;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002068 p += ct_len;
Paul Bakker926af752012-11-23 13:38:07 +01002069
Paul Bakker577e0062013-08-28 11:57:20 +02002070 sa_len = 0;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002071#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker926af752012-11-23 13:38:07 +01002072 /*
2073 * Add signature_algorithms for verify (TLS 1.2)
Paul Bakker926af752012-11-23 13:38:07 +01002074 *
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002075 * SignatureAndHashAlgorithm supported_signature_algorithms<2..2^16-2>;
2076 *
2077 * struct {
2078 * HashAlgorithm hash;
2079 * SignatureAlgorithm signature;
2080 * } SignatureAndHashAlgorithm;
2081 *
2082 * enum { (255) } HashAlgorithm;
2083 * enum { (255) } SignatureAlgorithm;
Paul Bakker926af752012-11-23 13:38:07 +01002084 */
Paul Bakker21dca692013-01-03 11:41:08 +01002085 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01002086 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002087 /*
2088 * Only use current running hash algorithm that is already required
2089 * for requested ciphersuite.
2090 */
Paul Bakker926af752012-11-23 13:38:07 +01002091 ssl->handshake->verify_sig_alg = SSL_HASH_SHA256;
2092
Paul Bakkerb7149bc2013-03-20 15:30:09 +01002093 if( ssl->transform_negotiate->ciphersuite_info->mac ==
2094 POLARSSL_MD_SHA384 )
Paul Bakker926af752012-11-23 13:38:07 +01002095 {
2096 ssl->handshake->verify_sig_alg = SSL_HASH_SHA384;
2097 }
Paul Bakkerf7abd422013-04-16 13:15:56 +02002098
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002099 /*
2100 * Supported signature algorithms
2101 */
2102#if defined(POLARSSL_RSA_C)
2103 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2104 p[2 + sa_len++] = SSL_SIG_RSA;
2105#endif
2106#if defined(POLARSSL_ECDSA_C)
2107 p[2 + sa_len++] = ssl->handshake->verify_sig_alg;
2108 p[2 + sa_len++] = SSL_SIG_ECDSA;
2109#endif
Paul Bakker926af752012-11-23 13:38:07 +01002110
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002111 p[0] = (unsigned char)( sa_len >> 8 );
2112 p[1] = (unsigned char)( sa_len );
2113 sa_len += 2;
2114 p += sa_len;
Paul Bakker926af752012-11-23 13:38:07 +01002115 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002116#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002117
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002118 /*
2119 * DistinguishedName certificate_authorities<0..2^16-1>;
2120 * opaque DistinguishedName<1..2^16-1>;
2121 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002122 p += 2;
2123 crt = ssl->ca_chain;
2124
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002125 total_dn_size = 0;
Paul Bakkerc70e4252014-04-18 13:47:38 +02002126 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002127 {
2128 if( p - buf > 4096 )
2129 break;
2130
Paul Bakker926af752012-11-23 13:38:07 +01002131 dn_size = crt->subject_raw.len;
2132 *p++ = (unsigned char)( dn_size >> 8 );
2133 *p++ = (unsigned char)( dn_size );
2134 memcpy( p, crt->subject_raw.p, dn_size );
2135 p += dn_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Paul Bakker926af752012-11-23 13:38:07 +01002137 SSL_DEBUG_BUF( 3, "requested DN", p, dn_size );
2138
Paul Bakkerbc3d9842012-11-26 16:12:02 +01002139 total_dn_size += 2 + dn_size;
Paul Bakker926af752012-11-23 13:38:07 +01002140 crt = crt->next;
Paul Bakker5121ce52009-01-03 21:22:43 +00002141 }
2142
Paul Bakker926af752012-11-23 13:38:07 +01002143 ssl->out_msglen = p - buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00002144 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2145 ssl->out_msg[0] = SSL_HS_CERTIFICATE_REQUEST;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02002146 ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 );
2147 ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size );
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
2149 ret = ssl_write_record( ssl );
2150
2151 SSL_DEBUG_MSG( 2, ( "<= write certificate request" ) );
2152
2153 return( ret );
2154}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002155#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
2156 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
Manuel Pégourié-Gonnardda1ff382013-11-25 17:38:36 +01002157 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED &&
2158 !POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002159
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002160#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2161 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2162static int ssl_get_ecdh_params_from_cert( ssl_context *ssl )
2163{
2164 int ret;
2165
2166 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_ECKEY ) )
2167 {
2168 SSL_DEBUG_MSG( 1, ( "server key not ECDH capable" ) );
2169 return( POLARSSL_ERR_SSL_PK_TYPE_MISMATCH );
2170 }
2171
2172 if( ( ret = ecdh_get_params( &ssl->handshake->ecdh_ctx,
2173 pk_ec( *ssl_own_key( ssl ) ),
2174 POLARSSL_ECDH_OURS ) ) != 0 )
2175 {
2176 SSL_DEBUG_RET( 1, ( "ecdh_get_params" ), ret );
2177 return( ret );
2178 }
2179
2180 return( 0 );
2181}
2182#endif /* POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
2183 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
2184
Paul Bakker41c83d32013-03-20 14:39:14 +01002185static int ssl_write_server_key_exchange( ssl_context *ssl )
2186{
Paul Bakker23986e52011-04-24 08:57:21 +00002187 int ret;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002188 size_t n = 0;
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002189 const ssl_ciphersuite_t *ciphersuite_info =
2190 ssl->transform_negotiate->ciphersuite_info;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002191
2192#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2193 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2194 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002195 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
Paul Bakker2292d1f2013-09-15 17:06:49 +02002196 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002197 unsigned char *p = ssl->out_msg + 4;
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002198 unsigned char *dig_signed = p;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002199 size_t dig_signed_len = 0, len;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002200 ((void) dig_signed);
2201 ((void) dig_signed_len);
2202#endif
Paul Bakker41c83d32013-03-20 14:39:14 +01002203
Paul Bakker5121ce52009-01-03 21:22:43 +00002204 SSL_DEBUG_MSG( 2, ( "=> write server key exchange" ) );
2205
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002206#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2207 defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED) || \
2208 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard09258b92013-10-15 10:43:36 +02002209 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA ||
2210 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
2211 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 {
2213 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
2214 ssl->state++;
2215 return( 0 );
2216 }
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002217#endif
2218
2219#if defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2220 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
2221 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2222 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
2223 {
2224 ssl_get_ecdh_params_from_cert( ssl );
2225
Manuel Pégourié-Gonnard8520dac2014-02-21 12:12:23 +01002226 SSL_DEBUG_MSG( 2, ( "<= skip write server key exchange" ) );
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002227 ssl->state++;
2228 return( 0 );
2229 }
2230#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002231
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002232#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED) || \
2233 defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2234 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK ||
2235 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002236 {
2237 /* TODO: Support identity hints */
2238 *(p++) = 0x00;
2239 *(p++) = 0x00;
2240
2241 n += 2;
2242 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002243#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED ||
2244 POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002245
2246#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2247 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2248 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
2249 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakker48916f92012-09-16 19:57:18 +00002250 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002251 /*
2252 * Ephemeral DH parameters:
2253 *
2254 * struct {
2255 * opaque dh_p<1..2^16-1>;
2256 * opaque dh_g<1..2^16-1>;
2257 * opaque dh_Ys<1..2^16-1>;
2258 * } ServerDHParams;
2259 */
2260 if( ( ret = mpi_copy( &ssl->handshake->dhm_ctx.P, &ssl->dhm_P ) ) != 0 ||
2261 ( ret = mpi_copy( &ssl->handshake->dhm_ctx.G, &ssl->dhm_G ) ) != 0 )
2262 {
2263 SSL_DEBUG_RET( 1, "mpi_copy", ret );
2264 return( ret );
2265 }
Paul Bakker48916f92012-09-16 19:57:18 +00002266
Paul Bakker41c83d32013-03-20 14:39:14 +01002267 if( ( ret = dhm_make_params( &ssl->handshake->dhm_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002268 (int) mpi_size( &ssl->handshake->dhm_ctx.P ),
2269 p, &len, ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002270 {
2271 SSL_DEBUG_RET( 1, "dhm_make_params", ret );
2272 return( ret );
2273 }
2274
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002275 dig_signed = p;
2276 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002277
2278 p += len;
2279 n += len;
2280
Paul Bakker41c83d32013-03-20 14:39:14 +01002281 SSL_DEBUG_MPI( 3, "DHM: X ", &ssl->handshake->dhm_ctx.X );
2282 SSL_DEBUG_MPI( 3, "DHM: P ", &ssl->handshake->dhm_ctx.P );
2283 SSL_DEBUG_MPI( 3, "DHM: G ", &ssl->handshake->dhm_ctx.G );
2284 SSL_DEBUG_MPI( 3, "DHM: GX", &ssl->handshake->dhm_ctx.GX );
2285 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002286#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2287 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker41c83d32013-03-20 14:39:14 +01002288
Gergely Budai987bfb52014-01-19 21:48:42 +01002289#if defined(POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002290 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002291 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2292 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
Paul Bakker5121ce52009-01-03 21:22:43 +00002293 {
Paul Bakker41c83d32013-03-20 14:39:14 +01002294 /*
2295 * Ephemeral ECDH parameters:
2296 *
2297 * struct {
2298 * ECParameters curve_params;
2299 * ECPoint public;
2300 * } ServerECDHParams;
2301 */
Paul Bakkerd893aef2014-04-17 14:45:17 +02002302 const ecp_curve_info **curve = NULL;
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002303#if defined(POLARSSL_SSL_SET_CURVES)
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002304 const ecp_group_id *gid;
Gergely Budai987bfb52014-01-19 21:48:42 +01002305
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002306 /* Match our preference list against the offered curves */
2307 for( gid = ssl->curve_list; *gid != POLARSSL_ECP_DP_NONE; gid++ )
2308 for( curve = ssl->handshake->curves; *curve != NULL; curve++ )
2309 if( (*curve)->grp_id == *gid )
2310 goto curve_matching_done;
2311
2312curve_matching_done:
2313#else
2314 curve = ssl->handshake->curves;
2315#endif
2316
2317 if( *curve == NULL )
Gergely Budai987bfb52014-01-19 21:48:42 +01002318 {
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002319 SSL_DEBUG_MSG( 1, ( "no matching curve for ECDHE" ) );
2320 return( POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN );
Gergely Budai987bfb52014-01-19 21:48:42 +01002321 }
Manuel Pégourié-Gonnardde053902014-02-04 13:58:39 +01002322
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002323 SSL_DEBUG_MSG( 2, ( "ECDHE curve: %s", (*curve)->name ) );
Gergely Budai987bfb52014-01-19 21:48:42 +01002324
Paul Bakker41c83d32013-03-20 14:39:14 +01002325 if( ( ret = ecp_use_known_dp( &ssl->handshake->ecdh_ctx.grp,
Manuel Pégourié-Gonnardc3f6b62c2014-02-06 10:13:09 +01002326 (*curve)->grp_id ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002327 {
2328 SSL_DEBUG_RET( 1, "ecp_use_known_dp", ret );
2329 return( ret );
2330 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002332 if( ( ret = ecdh_make_params( &ssl->handshake->ecdh_ctx, &len,
2333 p, SSL_MAX_CONTENT_LEN - n,
2334 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002335 {
2336 SSL_DEBUG_RET( 1, "ecdh_make_params", ret );
2337 return( ret );
2338 }
2339
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002340 dig_signed = p;
2341 dig_signed_len = len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002342
2343 p += len;
2344 n += len;
2345
Paul Bakker41c83d32013-03-20 14:39:14 +01002346 SSL_DEBUG_ECP( 3, "ECDH: Q ", &ssl->handshake->ecdh_ctx.Q );
2347 }
Gergely Budai987bfb52014-01-19 21:48:42 +01002348#endif /* POLARSSL_KEY_EXCHANGE__SOME__ECDHE_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00002349
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002350#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002351 defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
2352 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002353 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002354 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
2355 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
Paul Bakker1ef83d62012-04-11 12:09:53 +00002356 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002357 size_t signature_len = 0;
Paul Bakker2292d1f2013-09-15 17:06:49 +02002358 unsigned int hashlen = 0;
2359 unsigned char hash[64];
2360 md_type_t md_alg = POLARSSL_MD_NONE;
Paul Bakker23f36802012-09-28 14:15:14 +00002361
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002362 /*
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002363 * Choose hash algorithm. NONE means MD5 + SHA1 here.
2364 */
Paul Bakker577e0062013-08-28 11:57:20 +02002365#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002366 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2367 {
2368 md_alg = ssl_md_alg_from_hash( ssl->handshake->sig_alg );
2369
2370 if( md_alg == POLARSSL_MD_NONE )
2371 {
2372 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002373 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002374 }
2375 }
Paul Bakker577e0062013-08-28 11:57:20 +02002376 else
Paul Bakkerdb20c102014-06-17 14:34:44 +02002377#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002378#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2379 defined(POLARSSL_SSL_PROTO_TLS1_1)
Paul Bakker66d5d072014-06-17 16:39:18 +02002380 if( ciphersuite_info->key_exchange ==
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002381 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA )
2382 {
2383 md_alg = POLARSSL_MD_SHA1;
2384 }
2385 else
Paul Bakker577e0062013-08-28 11:57:20 +02002386#endif
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002387 {
2388 md_alg = POLARSSL_MD_NONE;
2389 }
2390
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002391 /*
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002392 * Compute the hash to be signed
2393 */
Paul Bakker577e0062013-08-28 11:57:20 +02002394#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
2395 defined(POLARSSL_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02002396 if( md_alg == POLARSSL_MD_NONE )
Paul Bakker23f36802012-09-28 14:15:14 +00002397 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002398 md5_context md5;
2399 sha1_context sha1;
2400
Paul Bakker5b4af392014-06-26 12:09:34 +02002401 md5_init( &md5 );
2402 sha1_init( &sha1 );
2403
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002404 /*
2405 * digitally-signed struct {
2406 * opaque md5_hash[16];
2407 * opaque sha_hash[20];
2408 * };
2409 *
2410 * md5_hash
2411 * MD5(ClientHello.random + ServerHello.random
2412 * + ServerParams);
2413 * sha_hash
2414 * SHA(ClientHello.random + ServerHello.random
2415 * + ServerParams);
2416 */
2417 md5_starts( &md5 );
2418 md5_update( &md5, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002419 md5_update( &md5, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002420 md5_finish( &md5, hash );
2421
2422 sha1_starts( &sha1 );
2423 sha1_update( &sha1, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002424 sha1_update( &sha1, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002425 sha1_finish( &sha1, hash + 16 );
2426
2427 hashlen = 36;
Paul Bakker5b4af392014-06-26 12:09:34 +02002428
2429 md5_free( &md5 );
2430 sha1_free( &sha1 );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002431 }
2432 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002433#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 || \
2434 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker9659dae2013-08-28 16:21:34 +02002435#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2436 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker577e0062013-08-28 11:57:20 +02002437 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002438 {
2439 md_context_t ctx;
Paul Bakker66d5d072014-06-17 16:39:18 +02002440 const md_info_t *md_info = md_info_from_type( md_alg );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002441
Paul Bakker84bbeb52014-07-01 14:53:22 +02002442 md_init( &ctx );
2443
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02002444 /* Info from md_alg will be used instead */
2445 hashlen = 0;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002446
2447 /*
2448 * digitally-signed struct {
2449 * opaque client_random[32];
2450 * opaque server_random[32];
2451 * ServerDHParams params;
2452 * };
2453 */
Paul Bakker66d5d072014-06-17 16:39:18 +02002454 if( ( ret = md_init_ctx( &ctx, md_info ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002455 {
2456 SSL_DEBUG_RET( 1, "md_init_ctx", ret );
2457 return( ret );
2458 }
2459
2460 md_starts( &ctx );
2461 md_update( &ctx, ssl->handshake->randbytes, 64 );
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002462 md_update( &ctx, dig_signed, dig_signed_len );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002463 md_finish( &ctx, hash );
Paul Bakker84bbeb52014-07-01 14:53:22 +02002464 md_free( &ctx );
Paul Bakker23f36802012-09-28 14:15:14 +00002465 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002466 else
Paul Bakker9659dae2013-08-28 16:21:34 +02002467#endif /* POLARSSL_SSL_PROTO_TLS1 || POLARSSL_SSL_PROTO_TLS1_1 || \
2468 POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002469 {
2470 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002471 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002472 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002473
Manuel Pégourié-Gonnard9cc6f5c2013-08-27 14:29:44 +02002474 SSL_DEBUG_BUF( 3, "parameters hash", hash, hashlen != 0 ? hashlen :
2475 (unsigned int) ( md_info_from_type( md_alg ) )->size );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002476
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002477 /*
2478 * Make the signature
2479 */
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002480 if( ssl_own_key( ssl ) == NULL )
Paul Bakker23f36802012-09-28 14:15:14 +00002481 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002482 SSL_DEBUG_MSG( 1, ( "got no private key" ) );
2483 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002484 }
Paul Bakker23f36802012-09-28 14:15:14 +00002485
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002486#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker23f36802012-09-28 14:15:14 +00002487 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
2488 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002489 *(p++) = ssl->handshake->sig_alg;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002490 *(p++) = ssl_sig_from_pk( ssl_own_key( ssl ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002491
2492 n += 2;
2493 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002494#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002495
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002496 if( ( ret = pk_sign( ssl_own_key( ssl ), md_alg, hash, hashlen,
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002497 p + 2 , &signature_len,
2498 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002499 {
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02002500 SSL_DEBUG_RET( 1, "pk_sign", ret );
Paul Bakkerc70b9822013-04-07 22:00:46 +02002501 return( ret );
Paul Bakker23f36802012-09-28 14:15:14 +00002502 }
Paul Bakkerc70b9822013-04-07 22:00:46 +02002503
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002504 *(p++) = (unsigned char)( signature_len >> 8 );
2505 *(p++) = (unsigned char)( signature_len );
Paul Bakkerbf63b362012-04-12 20:44:34 +00002506 n += 2;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002507
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002508 SSL_DEBUG_BUF( 3, "my signature", p, signature_len );
Paul Bakker48916f92012-09-16 19:57:18 +00002509
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002510 p += signature_len;
2511 n += signature_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00002512 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002513#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) ||
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002514 POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
2515 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Paul Bakker1ef83d62012-04-11 12:09:53 +00002516
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002517 ssl->out_msglen = 4 + n;
Paul Bakker5121ce52009-01-03 21:22:43 +00002518 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2519 ssl->out_msg[0] = SSL_HS_SERVER_KEY_EXCHANGE;
2520
2521 ssl->state++;
2522
2523 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2524 {
2525 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2526 return( ret );
2527 }
2528
2529 SSL_DEBUG_MSG( 2, ( "<= write server key exchange" ) );
2530
2531 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00002532}
2533
2534static int ssl_write_server_hello_done( ssl_context *ssl )
2535{
2536 int ret;
2537
2538 SSL_DEBUG_MSG( 2, ( "=> write server hello done" ) );
2539
2540 ssl->out_msglen = 4;
2541 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
2542 ssl->out_msg[0] = SSL_HS_SERVER_HELLO_DONE;
2543
2544 ssl->state++;
2545
2546 if( ( ret = ssl_write_record( ssl ) ) != 0 )
2547 {
2548 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
2549 return( ret );
2550 }
2551
2552 SSL_DEBUG_MSG( 2, ( "<= write server hello done" ) );
2553
2554 return( 0 );
2555}
2556
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002557#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
2558 defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2559static int ssl_parse_client_dh_public( ssl_context *ssl, unsigned char **p,
2560 const unsigned char *end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002561{
2562 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002563 size_t n;
2564
2565 /*
2566 * Receive G^Y mod P, premaster = (G^Y)^X mod P
2567 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002568 if( *p + 2 > end )
2569 {
2570 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2571 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2572 }
Paul Bakker70df2fb2013-04-17 17:19:09 +02002573
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002574 n = ( (*p)[0] << 8 ) | (*p)[1];
2575 *p += 2;
2576
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002577 if( *p + n > end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002578 {
2579 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2580 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2581 }
2582
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002583 if( ( ret = dhm_read_public( &ssl->handshake->dhm_ctx, *p, n ) ) != 0 )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002584 {
2585 SSL_DEBUG_RET( 1, "dhm_read_public", ret );
2586 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2587 }
2588
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002589 *p += n;
2590
Paul Bakker70df2fb2013-04-17 17:19:09 +02002591 SSL_DEBUG_MPI( 3, "DHM: GY", &ssl->handshake->dhm_ctx.GY );
2592
Paul Bakker70df2fb2013-04-17 17:19:09 +02002593 return( ret );
2594}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002595#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED ||
2596 POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002597
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002598#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) || \
2599 defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002600static int ssl_parse_encrypted_pms( ssl_context *ssl,
2601 const unsigned char *p,
2602 const unsigned char *end,
2603 size_t pms_offset )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002604{
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002605 int ret;
2606 size_t len = pk_get_len( ssl_own_key( ssl ) );
2607 unsigned char *pms = ssl->handshake->premaster + pms_offset;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002608
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02002609 if( ! pk_can_do( ssl_own_key( ssl ), POLARSSL_PK_RSA ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002610 {
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02002611 SSL_DEBUG_MSG( 1, ( "got no RSA private key" ) );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002612 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2613 }
2614
2615 /*
2616 * Decrypt the premaster using own private RSA key
2617 */
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002618#if defined(POLARSSL_SSL_PROTO_TLS1) || defined(POLARSSL_SSL_PROTO_TLS1_1) || \
2619 defined(POLARSSL_SSL_PROTO_TLS1_2)
Paul Bakker70df2fb2013-04-17 17:19:09 +02002620 if( ssl->minor_ver != SSL_MINOR_VERSION_0 )
2621 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002622 if( *p++ != ( ( len >> 8 ) & 0xFF ) ||
2623 *p++ != ( ( len ) & 0xFF ) )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002624 {
2625 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2626 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2627 }
2628 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002629#endif
Paul Bakker70df2fb2013-04-17 17:19:09 +02002630
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002631 if( p + len != end )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002632 {
2633 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2634 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2635 }
2636
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002637 ret = pk_decrypt( ssl_own_key( ssl ), p, len,
2638 pms, &ssl->handshake->pmslen,
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002639 sizeof( ssl->handshake->premaster ) - pms_offset,
Manuel Pégourié-Gonnard070cc7f2013-08-21 15:09:31 +02002640 ssl->f_rng, ssl->p_rng );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002641
2642 if( ret != 0 || ssl->handshake->pmslen != 48 ||
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002643 pms[0] != ssl->handshake->max_major_ver ||
2644 pms[1] != ssl->handshake->max_minor_ver )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002645 {
2646 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2647
2648 /*
2649 * Protection against Bleichenbacher's attack:
2650 * invalid PKCS#1 v1.5 padding must not cause
2651 * the connection to end immediately; instead,
2652 * send a bad_record_mac later in the handshake.
2653 */
2654 ssl->handshake->pmslen = 48;
2655
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002656 ret = ssl->f_rng( ssl->p_rng, pms, ssl->handshake->pmslen );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002657 if( ret != 0 )
2658 return( ret );
2659 }
2660
2661 return( ret );
2662}
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +02002663#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED ||
2664 POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker70df2fb2013-04-17 17:19:09 +02002665
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002666#if defined(POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED)
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002667static int ssl_parse_client_psk_identity( ssl_context *ssl, unsigned char **p,
2668 const unsigned char *end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002669{
Paul Bakker6db455e2013-09-18 17:29:31 +02002670 int ret = 0;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002671 size_t n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002672
Paul Bakker6db455e2013-09-18 17:29:31 +02002673 if( ssl->f_psk == NULL &&
2674 ( ssl->psk == NULL || ssl->psk_identity == NULL ||
2675 ssl->psk_identity_len == 0 || ssl->psk_len == 0 ) )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002676 {
2677 SSL_DEBUG_MSG( 1, ( "got no pre-shared key" ) );
2678 return( POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED );
2679 }
2680
2681 /*
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002682 * Receive client pre-shared key identity name
Paul Bakkerfbb17802013-04-17 19:10:21 +02002683 */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002684 if( *p + 2 > end )
2685 {
2686 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2687 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2688 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002689
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002690 n = ( (*p)[0] << 8 ) | (*p)[1];
2691 *p += 2;
2692
2693 if( n < 1 || n > 65535 || *p + n > end )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002694 {
2695 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
2696 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2697 }
2698
Paul Bakker6db455e2013-09-18 17:29:31 +02002699 if( ssl->f_psk != NULL )
2700 {
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002701 if( ssl->f_psk( ssl->p_psk, ssl, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002702 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2703 }
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002704 else
Paul Bakker6db455e2013-09-18 17:29:31 +02002705 {
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002706 /* Identity is not a big secret since clients send it in the clear,
2707 * but treat it carefully anyway, just in case */
Paul Bakker6db455e2013-09-18 17:29:31 +02002708 if( n != ssl->psk_identity_len ||
Manuel Pégourié-Gonnard31ff1d22013-10-28 13:46:11 +01002709 safer_memcmp( ssl->psk_identity, *p, n ) != 0 )
Paul Bakker6db455e2013-09-18 17:29:31 +02002710 {
2711 ret = POLARSSL_ERR_SSL_UNKNOWN_IDENTITY;
2712 }
2713 }
2714
2715 if( ret == POLARSSL_ERR_SSL_UNKNOWN_IDENTITY )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002716 {
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002717 SSL_DEBUG_BUF( 3, "Unknown PSK identity", *p, n );
Paul Bakker6db455e2013-09-18 17:29:31 +02002718 if( ( ret = ssl_send_alert_message( ssl,
2719 SSL_ALERT_LEVEL_FATAL,
2720 SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY ) ) != 0 )
2721 {
2722 return( ret );
2723 }
2724
2725 return( POLARSSL_ERR_SSL_UNKNOWN_IDENTITY );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002726 }
2727
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002728 *p += n;
Paul Bakkerfbb17802013-04-17 19:10:21 +02002729
Manuel Pégourié-Gonnardd27680b2014-07-08 14:15:55 +02002730 return( 0 );
Paul Bakkerfbb17802013-04-17 19:10:21 +02002731}
Manuel Pégourié-Gonnard8a3c64d2013-10-14 19:54:10 +02002732#endif /* POLARSSL_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakkerfbb17802013-04-17 19:10:21 +02002733
Paul Bakker5121ce52009-01-03 21:22:43 +00002734static int ssl_parse_client_key_exchange( ssl_context *ssl )
2735{
Paul Bakker23986e52011-04-24 08:57:21 +00002736 int ret;
Paul Bakker41c83d32013-03-20 14:39:14 +01002737 const ssl_ciphersuite_t *ciphersuite_info;
Paul Bakker70df2fb2013-04-17 17:19:09 +02002738
Paul Bakker41c83d32013-03-20 14:39:14 +01002739 ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002740
2741 SSL_DEBUG_MSG( 2, ( "=> parse client key exchange" ) );
2742
2743 if( ( ret = ssl_read_record( ssl ) ) != 0 )
2744 {
2745 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
2746 return( ret );
2747 }
2748
2749 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
2750 {
2751 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002752 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002753 }
2754
2755 if( ssl->in_msg[0] != SSL_HS_CLIENT_KEY_EXCHANGE )
2756 {
2757 SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00002758 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00002759 }
2760
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002761#if defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED)
Paul Bakker41c83d32013-03-20 14:39:14 +01002762 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_RSA )
Paul Bakker5121ce52009-01-03 21:22:43 +00002763 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002764 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002765 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002766
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002767 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002768 {
Paul Bakker70df2fb2013-04-17 17:19:09 +02002769 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2770 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002771 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002772
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002773 if( p != end )
2774 {
2775 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2776 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2777 }
2778
Manuel Pégourié-Gonnarddd0c0f32014-06-23 18:07:11 +02002779 ssl->handshake->pmslen = POLARSSL_PREMASTER_SIZE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002780
2781 if( ( ret = dhm_calc_secret( &ssl->handshake->dhm_ctx,
2782 ssl->handshake->premaster,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +02002783 &ssl->handshake->pmslen,
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +02002784 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002785 {
2786 SSL_DEBUG_RET( 1, "dhm_calc_secret", ret );
2787 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2788 }
2789
2790 SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002791 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002792 else
2793#endif /* POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED */
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002794#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002795 defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
2796 defined(POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
2797 defined(POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002798 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_RSA ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002799 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA ||
2800 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_RSA ||
2801 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDH_ECDSA )
Paul Bakker70df2fb2013-04-17 17:19:09 +02002802 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002803 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002804 ssl->in_msg + 4, ssl->in_hslen - 4 ) ) != 0 )
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002805 {
2806 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2807 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
2808 }
2809
2810 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2811
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002812 if( ( ret = ecdh_calc_secret( &ssl->handshake->ecdh_ctx,
2813 &ssl->handshake->pmslen,
2814 ssl->handshake->premaster,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002815 POLARSSL_MPI_MAX_SIZE,
2816 ssl->f_rng, ssl->p_rng ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002817 {
2818 SSL_DEBUG_RET( 1, "ecdh_calc_secret", ret );
2819 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS );
2820 }
2821
2822 SSL_DEBUG_MPI( 3, "ECDH: z ", &ssl->handshake->ecdh_ctx.z );
Paul Bakker5121ce52009-01-03 21:22:43 +00002823 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002824 else
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002825#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
Manuel Pégourié-Gonnard55389702013-12-12 11:14:16 +01002826 POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
2827 POLARSSL_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
2828 POLARSSL_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002829#if defined(POLARSSL_KEY_EXCHANGE_PSK_ENABLED)
2830 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002831 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002832 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002833 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002834
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002835 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
Paul Bakkerfbb17802013-04-17 19:10:21 +02002836 {
2837 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2838 return( ret );
2839 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002840
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002841 if( p != end )
2842 {
2843 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2844 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2845 }
2846
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002847 if( ( ret = ssl_psk_derive_premaster( ssl,
2848 ciphersuite_info->key_exchange ) ) != 0 )
2849 {
2850 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2851 return( ret );
2852 }
Paul Bakkerfbb17802013-04-17 19:10:21 +02002853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002854 else
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002855#endif /* POLARSSL_KEY_EXCHANGE_PSK_ENABLED */
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002856#if defined(POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED)
2857 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK )
2858 {
2859 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002860 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002861
2862 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2863 {
2864 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2865 return( ret );
2866 }
2867
2868 if( ( ret = ssl_parse_encrypted_pms( ssl, p, end, 2 ) ) != 0 )
2869 {
2870 SSL_DEBUG_RET( 1, ( "ssl_parse_encrypted_pms" ), ret );
2871 return( ret );
2872 }
2873
2874 if( ( ret = ssl_psk_derive_premaster( ssl,
2875 ciphersuite_info->key_exchange ) ) != 0 )
2876 {
2877 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2878 return( ret );
2879 }
2880 }
2881 else
2882#endif /* POLARSSL_KEY_EXCHANGE_RSA_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002883#if defined(POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED)
2884 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
2885 {
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +02002886 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002887 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002888
2889 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2890 {
2891 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2892 return( ret );
2893 }
2894 if( ( ret = ssl_parse_client_dh_public( ssl, &p, end ) ) != 0 )
2895 {
2896 SSL_DEBUG_RET( 1, ( "ssl_parse_client_dh_public" ), ret );
2897 return( ret );
2898 }
2899
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002900 if( p != end )
2901 {
2902 SSL_DEBUG_MSG( 1, ( "bad client key exchange" ) );
2903 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE );
2904 }
2905
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002906 if( ( ret = ssl_psk_derive_premaster( ssl,
2907 ciphersuite_info->key_exchange ) ) != 0 )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002908 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002909 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
2910 return( ret );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002911 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002912 }
2913 else
2914#endif /* POLARSSL_KEY_EXCHANGE_DHE_PSK_ENABLED */
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002915#if defined(POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2916 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK )
2917 {
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002918 unsigned char *p = ssl->in_msg + 4;
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002919 unsigned char *end = ssl->in_msg + ssl->in_hslen;
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002920
2921 if( ( ret = ssl_parse_client_psk_identity( ssl, &p, end ) ) != 0 )
2922 {
2923 SSL_DEBUG_RET( 1, ( "ssl_parse_client_psk_identity" ), ret );
2924 return( ret );
2925 }
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002926
2927 if( ( ret = ecdh_read_public( &ssl->handshake->ecdh_ctx,
2928 p, end - p ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002929 {
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002930 SSL_DEBUG_RET( 1, "ecdh_read_public", ret );
2931 return( POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002932 }
2933
Manuel Pégourié-Gonnardb59d6992013-10-14 12:00:45 +02002934 SSL_DEBUG_ECP( 3, "ECDH: Qp ", &ssl->handshake->ecdh_ctx.Qp );
2935
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002936 if( ( ret = ssl_psk_derive_premaster( ssl,
2937 ciphersuite_info->key_exchange ) ) != 0 )
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002938 {
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002939 SSL_DEBUG_RET( 1, "ssl_psk_derive_premaster", ret );
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002940 return( ret );
2941 }
Manuel Pégourié-Gonnard3ce3bbd2013-10-11 16:53:50 +02002942 }
2943 else
2944#endif /* POLARSSL_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002945#if defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED)
2946 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA )
Paul Bakker41c83d32013-03-20 14:39:14 +01002947 {
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002948 if( ( ret = ssl_parse_encrypted_pms( ssl,
2949 ssl->in_msg + 4,
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002950 ssl->in_msg + ssl->in_hslen,
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002951 0 ) ) != 0 )
Paul Bakker41c83d32013-03-20 14:39:14 +01002952 {
Manuel Pégourié-Gonnard969ccc62014-03-26 19:53:25 +01002953 SSL_DEBUG_RET( 1, ( "ssl_parse_parse_encrypted_pms_secret" ), ret );
Paul Bakker70df2fb2013-04-17 17:19:09 +02002954 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00002955 }
2956 }
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002957 else
2958#endif /* POLARSSL_KEY_EXCHANGE_RSA_ENABLED */
2959 {
Manuel Pégourié-Gonnardabae74c2013-08-20 13:53:44 +02002960 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002961 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002963
Paul Bakkerff60ee62010-03-16 21:09:09 +00002964 if( ( ret = ssl_derive_keys( ssl ) ) != 0 )
2965 {
2966 SSL_DEBUG_RET( 1, "ssl_derive_keys", ret );
2967 return( ret );
2968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Paul Bakker5121ce52009-01-03 21:22:43 +00002970 ssl->state++;
2971
2972 SSL_DEBUG_MSG( 2, ( "<= parse client key exchange" ) );
2973
2974 return( 0 );
2975}
2976
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002977#if !defined(POLARSSL_KEY_EXCHANGE_RSA_ENABLED) && \
2978 !defined(POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED) && \
Manuel Pégourié-Gonnarda3104592013-09-17 21:17:44 +02002979 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
2980 !defined(POLARSSL_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
Paul Bakker5121ce52009-01-03 21:22:43 +00002981static int ssl_parse_certificate_verify( ssl_context *ssl )
2982{
Paul Bakkerfbb17802013-04-17 19:10:21 +02002983 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
2985 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
2986
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002987 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01002988 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002989 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002990 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
Paul Bakkered27a042013-04-18 22:46:23 +02002991 {
2992 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
2993 ssl->state++;
2994 return( 0 );
2995 }
2996
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02002997 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2998 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02002999}
3000#else
3001static int ssl_parse_certificate_verify( ssl_context *ssl )
3002{
3003 int ret = POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003004 size_t sa_len, sig_len;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003005 unsigned char hash[48];
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003006 unsigned char *hash_start = hash;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003007 size_t hashlen;
Paul Bakker577e0062013-08-28 11:57:20 +02003008#if defined(POLARSSL_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003009 pk_type_t pk_alg;
Paul Bakker577e0062013-08-28 11:57:20 +02003010#endif
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003011 md_type_t md_alg;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003012 const ssl_ciphersuite_t *ciphersuite_info = ssl->transform_negotiate->ciphersuite_info;
3013
3014 SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
3015
3016 if( ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_PSK ||
Manuel Pégourié-Gonnarddc953e82013-11-25 17:27:39 +01003017 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_RSA_PSK ||
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02003018 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_ECDHE_PSK ||
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003019 ciphersuite_info->key_exchange == POLARSSL_KEY_EXCHANGE_DHE_PSK )
3020 {
3021 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3022 ssl->state++;
3023 return( 0 );
3024 }
3025
Paul Bakkered27a042013-04-18 22:46:23 +02003026 if( ssl->session_negotiate->peer_cert == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00003027 {
3028 SSL_DEBUG_MSG( 2, ( "<= skip parse certificate verify" ) );
3029 ssl->state++;
3030 return( 0 );
3031 }
3032
Paul Bakker48916f92012-09-16 19:57:18 +00003033 ssl->handshake->calc_verify( ssl, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00003034
3035 if( ( ret = ssl_read_record( ssl ) ) != 0 )
3036 {
3037 SSL_DEBUG_RET( 1, "ssl_read_record", ret );
3038 return( ret );
3039 }
3040
3041 ssl->state++;
3042
3043 if( ssl->in_msgtype != SSL_MSG_HANDSHAKE )
3044 {
3045 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003046 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003047 }
3048
3049 if( ssl->in_msg[0] != SSL_HS_CERTIFICATE_VERIFY )
3050 {
3051 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003052 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003053 }
3054
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003055 /*
3056 * 0 . 0 handshake type
3057 * 1 . 3 handshake length
3058 * 4 . 5 sig alg (TLS 1.2 only)
3059 * 4+n . 5+n signature length (n = sa_len)
3060 * 6+n . 6+n+m signature (m = sig_len)
3061 */
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003063#if defined(POLARSSL_SSL_PROTO_SSL3) || defined(POLARSSL_SSL_PROTO_TLS1) || \
3064 defined(POLARSSL_SSL_PROTO_TLS1_1)
3065 if( ssl->minor_ver != SSL_MINOR_VERSION_3 )
Paul Bakker926af752012-11-23 13:38:07 +01003066 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003067 sa_len = 0;
3068
Paul Bakkerc70b9822013-04-07 22:00:46 +02003069 md_alg = POLARSSL_MD_NONE;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003070 hashlen = 36;
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003071
3072 /* For ECDSA, use SHA-1, not MD-5 + SHA-1 */
3073 if( pk_can_do( &ssl->session_negotiate->peer_cert->pk,
3074 POLARSSL_PK_ECDSA ) )
3075 {
3076 hash_start += 16;
3077 hashlen -= 16;
3078 md_alg = POLARSSL_MD_SHA1;
3079 }
Paul Bakker926af752012-11-23 13:38:07 +01003080 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003081 else
Paul Bakker9af723c2014-05-01 13:03:14 +02003082#endif /* POLARSSL_SSL_PROTO_SSL3 || POLARSSL_SSL_PROTO_TLS1 ||
3083 POLARSSL_SSL_PROTO_TLS1_1 */
Paul Bakker577e0062013-08-28 11:57:20 +02003084#if defined(POLARSSL_SSL_PROTO_TLS1_2)
3085 if( ssl->minor_ver == SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003086 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003087 sa_len = 2;
3088
Paul Bakker5121ce52009-01-03 21:22:43 +00003089 /*
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003090 * Hash
Paul Bakker5121ce52009-01-03 21:22:43 +00003091 */
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003092 if( ssl->in_msg[4] != ssl->handshake->verify_sig_alg )
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003094 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3095 " for verify message" ) );
Paul Bakker926af752012-11-23 13:38:07 +01003096 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3097 }
3098
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003099 md_alg = ssl_md_alg_from_hash( ssl->handshake->verify_sig_alg );
Paul Bakker926af752012-11-23 13:38:07 +01003100
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02003101 /* Info from md_alg will be used instead */
3102 hashlen = 0;
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003103
3104 /*
3105 * Signature
3106 */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003107 if( ( pk_alg = ssl_pk_alg_from_sig( ssl->in_msg[5] ) )
3108 == POLARSSL_PK_NONE )
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003109 {
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003110 SSL_DEBUG_MSG( 1, ( "peer not adhering to requested sig_alg"
3111 " for verify message" ) );
3112 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003113 }
3114
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003115 /*
3116 * Check the certificate's key type matches the signature alg
3117 */
3118 if( ! pk_can_do( &ssl->session_negotiate->peer_cert->pk, pk_alg ) )
3119 {
3120 SSL_DEBUG_MSG( 1, ( "sig_alg doesn't match cert key" ) );
3121 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
3122 }
Paul Bakker577e0062013-08-28 11:57:20 +02003123 }
3124 else
3125#endif /* POLARSSL_SSL_PROTO_TLS1_2 */
3126 {
3127 SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02003128 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02003129 }
Manuel Pégourié-Gonnardff56da32013-07-11 10:46:21 +02003130
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003131 sig_len = ( ssl->in_msg[4 + sa_len] << 8 ) | ssl->in_msg[5 + sa_len];
Paul Bakker926af752012-11-23 13:38:07 +01003132
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003133 if( sa_len + sig_len + 6 != ssl->in_hslen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003134 {
3135 SSL_DEBUG_MSG( 1, ( "bad certificate verify message" ) );
Paul Bakker40e46942009-01-03 21:51:57 +00003136 return( POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137 }
3138
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003139 if( ( ret = pk_verify( &ssl->session_negotiate->peer_cert->pk,
Manuel Pégourié-Gonnard4bd12842013-08-27 13:31:28 +02003140 md_alg, hash_start, hashlen,
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02003141 ssl->in_msg + 6 + sa_len, sig_len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003142 {
Manuel Pégourié-Gonnard0b032002013-08-17 13:01:41 +02003143 SSL_DEBUG_RET( 1, "pk_verify", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003144 return( ret );
3145 }
3146
3147 SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
3148
Paul Bakkered27a042013-04-18 22:46:23 +02003149 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150}
Paul Bakker48f7a5d2013-04-19 14:30:58 +02003151#endif /* !POLARSSL_KEY_EXCHANGE_RSA_ENABLED &&
3152 !POLARSSL_KEY_EXCHANGE_DHE_RSA_ENABLED &&
3153 !POLARSSL_KEY_EXCHANGE_ECDHE_RSA_ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00003154
Paul Bakkera503a632013-08-14 13:48:06 +02003155#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003156static int ssl_write_new_session_ticket( ssl_context *ssl )
3157{
3158 int ret;
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003159 size_t tlen;
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003160 uint32_t lifetime = (uint32_t) ssl->ticket_lifetime;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003161
3162 SSL_DEBUG_MSG( 2, ( "=> write new session ticket" ) );
3163
3164 ssl->out_msgtype = SSL_MSG_HANDSHAKE;
3165 ssl->out_msg[0] = SSL_HS_NEW_SESSION_TICKET;
3166
3167 /*
3168 * struct {
3169 * uint32 ticket_lifetime_hint;
3170 * opaque ticket<0..2^16-1>;
3171 * } NewSessionTicket;
3172 *
3173 * 4 . 7 ticket_lifetime_hint (0 = unspecified)
3174 * 8 . 9 ticket_len (n)
3175 * 10 . 9+n ticket content
3176 */
Manuel Pégourié-Gonnard164d8942013-09-23 22:01:39 +02003177
3178 ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF;
3179 ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF;
3180 ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF;
3181 ssl->out_msg[7] = ( lifetime ) & 0xFF;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003182
Manuel Pégourié-Gonnard990c51a2013-08-03 15:37:58 +02003183 if( ( ret = ssl_write_ticket( ssl, &tlen ) ) != 0 )
3184 {
3185 SSL_DEBUG_RET( 1, "ssl_write_ticket", ret );
3186 tlen = 0;
3187 }
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003188
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003189 ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF );
3190 ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF );
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003191
Manuel Pégourié-Gonnard609bc812013-08-01 15:08:40 +02003192 ssl->out_msglen = 10 + tlen;
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003193
Manuel Pégourié-Gonnard145dfcb2014-02-26 14:23:33 +01003194 /*
3195 * Morally equivalent to updating ssl->state, but NewSessionTicket and
3196 * ChangeCipherSpec share the same state.
3197 */
3198 ssl->handshake->new_session_ticket = 0;
3199
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003200 if( ( ret = ssl_write_record( ssl ) ) != 0 )
3201 {
3202 SSL_DEBUG_RET( 1, "ssl_write_record", ret );
3203 return( ret );
3204 }
3205
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003206 SSL_DEBUG_MSG( 2, ( "<= write new session ticket" ) );
3207
3208 return( 0 );
3209}
Paul Bakkera503a632013-08-14 13:48:06 +02003210#endif /* POLARSSL_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003211
Paul Bakker5121ce52009-01-03 21:22:43 +00003212/*
Paul Bakker1961b702013-01-25 14:49:24 +01003213 * SSL handshake -- server side -- single step
Paul Bakker5121ce52009-01-03 21:22:43 +00003214 */
Paul Bakker1961b702013-01-25 14:49:24 +01003215int ssl_handshake_server_step( ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003216{
3217 int ret = 0;
3218
Paul Bakker1961b702013-01-25 14:49:24 +01003219 if( ssl->state == SSL_HANDSHAKE_OVER )
3220 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003221
Paul Bakker1961b702013-01-25 14:49:24 +01003222 SSL_DEBUG_MSG( 2, ( "server state: %d", ssl->state ) );
3223
3224 if( ( ret = ssl_flush_output( ssl ) ) != 0 )
3225 return( ret );
3226
3227 switch( ssl->state )
Paul Bakker5121ce52009-01-03 21:22:43 +00003228 {
Paul Bakker1961b702013-01-25 14:49:24 +01003229 case SSL_HELLO_REQUEST:
3230 ssl->state = SSL_CLIENT_HELLO;
Paul Bakker5121ce52009-01-03 21:22:43 +00003231 break;
3232
Paul Bakker1961b702013-01-25 14:49:24 +01003233 /*
3234 * <== ClientHello
3235 */
3236 case SSL_CLIENT_HELLO:
3237 ret = ssl_parse_client_hello( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00003238 break;
Paul Bakker1961b702013-01-25 14:49:24 +01003239
3240 /*
3241 * ==> ServerHello
3242 * Certificate
3243 * ( ServerKeyExchange )
3244 * ( CertificateRequest )
3245 * ServerHelloDone
3246 */
3247 case SSL_SERVER_HELLO:
3248 ret = ssl_write_server_hello( ssl );
3249 break;
3250
3251 case SSL_SERVER_CERTIFICATE:
3252 ret = ssl_write_certificate( ssl );
3253 break;
3254
3255 case SSL_SERVER_KEY_EXCHANGE:
3256 ret = ssl_write_server_key_exchange( ssl );
3257 break;
3258
3259 case SSL_CERTIFICATE_REQUEST:
3260 ret = ssl_write_certificate_request( ssl );
3261 break;
3262
3263 case SSL_SERVER_HELLO_DONE:
3264 ret = ssl_write_server_hello_done( ssl );
3265 break;
3266
3267 /*
3268 * <== ( Certificate/Alert )
3269 * ClientKeyExchange
3270 * ( CertificateVerify )
3271 * ChangeCipherSpec
3272 * Finished
3273 */
3274 case SSL_CLIENT_CERTIFICATE:
3275 ret = ssl_parse_certificate( ssl );
3276 break;
3277
3278 case SSL_CLIENT_KEY_EXCHANGE:
3279 ret = ssl_parse_client_key_exchange( ssl );
3280 break;
3281
3282 case SSL_CERTIFICATE_VERIFY:
3283 ret = ssl_parse_certificate_verify( ssl );
3284 break;
3285
3286 case SSL_CLIENT_CHANGE_CIPHER_SPEC:
3287 ret = ssl_parse_change_cipher_spec( ssl );
3288 break;
3289
3290 case SSL_CLIENT_FINISHED:
3291 ret = ssl_parse_finished( ssl );
3292 break;
3293
3294 /*
Manuel Pégourié-Gonnard7a358b82013-08-01 11:47:56 +02003295 * ==> ( NewSessionTicket )
3296 * ChangeCipherSpec
Paul Bakker1961b702013-01-25 14:49:24 +01003297 * Finished
3298 */
3299 case SSL_SERVER_CHANGE_CIPHER_SPEC:
Paul Bakkera503a632013-08-14 13:48:06 +02003300#if defined(POLARSSL_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003301 if( ssl->handshake->new_session_ticket != 0 )
3302 ret = ssl_write_new_session_ticket( ssl );
3303 else
Paul Bakkera503a632013-08-14 13:48:06 +02003304#endif
Manuel Pégourié-Gonnard7cd59242013-08-02 13:24:41 +02003305 ret = ssl_write_change_cipher_spec( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01003306 break;
3307
3308 case SSL_SERVER_FINISHED:
3309 ret = ssl_write_finished( ssl );
3310 break;
3311
3312 case SSL_FLUSH_BUFFERS:
3313 SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
3314 ssl->state = SSL_HANDSHAKE_WRAPUP;
3315 break;
3316
3317 case SSL_HANDSHAKE_WRAPUP:
3318 ssl_handshake_wrapup( ssl );
3319 break;
3320
3321 default:
3322 SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
3323 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00003324 }
3325
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 return( ret );
3327}
Paul Bakker9af723c2014-05-01 13:03:14 +02003328#endif /* POLARSSL_SSL_SRV_C */